From 9f4082a832a2b27533f95794bdeb9a6dced60b70 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Sun, 5 Sep 2021 13:50:22 +0300 Subject: [PATCH] create thread wip --- controllers/threads_controller.go | 32 +++++++++++++++++++++++++++---- repositories/posts_repository.go | 10 +++++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index 741d16d..6eb4819 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -13,7 +13,7 @@ import ( func GetThreads(c *gin.Context) { threads, err := Repositories.Posts.Get(10, 10) if err != nil { - // TODO: наверное, тут 500 будет + // TODO: рендерить шаблон 500 log.Println("error:", err) c.JSON(http.StatusOK, gin.H{"error": true}) return @@ -25,23 +25,47 @@ func GetThread(c *gin.Context) { threadIDString := c.Param("threadID") threadID, err := strconv.Atoi(threadIDString) if err != nil { - // TODO: тут 400 будет + // TODO: рендерить шаблон 404 log.Println("error:", err) c.JSON(http.StatusOK, gin.H{"error": true}) return } thread, err := Repositories.Posts.GetThreadByPostID(threadID) if err != nil { - // TODO: наверное, тут 500 будет + // TODO: рендерить шаблон 500 log.Println("error:", err) c.JSON(http.StatusOK, gin.H{"error": true}) return } + if thread == nil { + // TODO: рендерить шаблон 404 + return + } c.HTML(http.StatusOK, "thread.html", thread) } func CreateThread(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{"route": "create thread"}) + form, err := c.MultipartForm() + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "error": "Problem uploading file!", + }) + return + } + + // TODO: dat shit crashes if no fields in request + // text := form.Value["text"][0] + // title := form.Value["title"][0] + // isSageString := form.Value["isSage"][0] + // isSage, err := strconv.ParseBool(isSageString) + // if err != nil { + // // TODO: validation error + // response := Dto.GetInternalServerErrorResponse() + // c.JSON(http.StatusInternalServerError, response) + // return + // } + + c.JSON(http.StatusOK, gin.H{"route": form}) } func UpdateThread(c *gin.Context) { diff --git a/repositories/posts_repository.go b/repositories/posts_repository.go index e795969..e11c27b 100644 --- a/repositories/posts_repository.go +++ b/repositories/posts_repository.go @@ -165,5 +165,13 @@ func (r *PostsRepository) GetThreadByPostID(ID int) ([]Post, error) { return posts, nil } -// func (r *PostsRepository) GetByID() int { + +// func (r *PostsRepository) IsThreadExists(ID int) ([]Post, error) { +// conn, err := Db.Pool.Acquire(context.TODO()) +// if err != nil { +// return nil, err +// } +// defer conn.Release() + +// sql // }