micrach/controllers/threads_controller.go

92 lines
2.0 KiB
Go
Raw Normal View History

2021-08-26 20:23:55 +03:00
package controlers
import (
2021-09-05 00:36:55 +03:00
"log"
2021-09-10 02:17:45 +03:00
"math"
2021-08-26 20:23:55 +03:00
"net/http"
2021-09-05 00:36:55 +03:00
"strconv"
2021-08-26 20:23:55 +03:00
"github.com/gin-gonic/gin"
Repositories "micrach/repositories"
2021-08-26 20:23:55 +03:00
)
func GetThreads(c *gin.Context) {
2021-09-10 02:17:45 +03:00
pageString := c.Query("page")
page, err := strconv.Atoi(pageString)
2021-08-30 12:09:27 +03:00
if err != nil {
2021-09-05 00:36:55 +03:00
log.Println("error:", err)
2021-09-05 16:05:14 +03:00
c.HTML(http.StatusOK, "500.html", nil)
2021-08-30 12:09:27 +03:00
return
}
2021-09-10 02:17:45 +03:00
limit := 10
offset := limit * (page - 1)
threads, err := Repositories.Posts.Get(limit, offset)
if err != nil {
log.Println("error:", err)
c.HTML(http.StatusOK, "500.html", nil)
return
}
count, err := Repositories.Posts.GetCount()
if err != nil {
log.Println("error:", err)
c.HTML(http.StatusOK, "500.html", nil)
return
}
data := Repositories.IndexPageData{
Threads: threads,
PagesCount: int(math.Ceil(float64(count) / 10)),
Page: page,
}
c.HTML(http.StatusOK, "index.html", data)
2021-08-26 20:23:55 +03:00
}
func GetThread(c *gin.Context) {
2021-09-05 00:36:55 +03:00
threadIDString := c.Param("threadID")
threadID, err := strconv.Atoi(threadIDString)
if err != nil {
2021-09-05 15:35:20 +03:00
c.HTML(http.StatusOK, "404.html", nil)
2021-09-05 00:36:55 +03:00
return
}
thread, err := Repositories.Posts.GetThreadByPostID(threadID)
if err != nil {
log.Println("error:", err)
2021-09-05 16:05:14 +03:00
c.HTML(http.StatusOK, "500.html", nil)
2021-09-05 00:36:55 +03:00
return
}
2021-09-05 13:50:22 +03:00
if thread == nil {
2021-09-05 15:35:20 +03:00
c.HTML(http.StatusOK, "404.html", nil)
2021-09-05 13:50:22 +03:00
return
}
2021-09-05 00:36:55 +03:00
c.HTML(http.StatusOK, "thread.html", thread)
2021-08-26 20:23:55 +03:00
}
func CreateThread(c *gin.Context) {
2021-09-05 13:50:22 +03:00
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})
2021-08-26 20:23:55 +03:00
}
func UpdateThread(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"route": "update thread"})
}