90 lines
2.6 KiB
Go
Executable File
90 lines
2.6 KiB
Go
Executable File
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
db "git.nochill.in/nochill/hiling_go/db/repository"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
type CreateNewsEventsReq struct {
|
|
Url string `json:"url" binding:"required,url"`
|
|
Title string `json:"title" binding:"required"`
|
|
Source string `json:"source"`
|
|
Description string `json:"description"`
|
|
SubmittedBy int32 `json:"submitted_by" binding:"required,numeric"`
|
|
}
|
|
|
|
// @Summary Create a news/event
|
|
// @Tags news
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security CookieAuth
|
|
// @Param body body CreateNewsEventsReq true "News/event payload"
|
|
// @Success 201
|
|
// @Failure 400 {object} map[string]any
|
|
// @Router /news-events [post]
|
|
func (server *Server) createNews(ctx *gin.Context) {
|
|
var req CreateNewsEventsReq
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
ctx.JSON(http.StatusBadRequest, ValidationErrorResponse(err))
|
|
return
|
|
}
|
|
|
|
err := server.Store.CreateNewsEvents(ctx, db.CreateNewsEventsParams{
|
|
Title: req.Title,
|
|
Url: req.Url,
|
|
Source: req.Source,
|
|
Description: pgtype.Text{Valid: len(req.Description) > 0, String: req.Description},
|
|
SubmittedBy: req.SubmittedBy,
|
|
})
|
|
|
|
if err != nil {
|
|
ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong while try to save news/evnts"))
|
|
return
|
|
}
|
|
|
|
ctx.Writer.WriteHeader(http.StatusCreated)
|
|
}
|
|
|
|
type GetNewsEventsListReq struct {
|
|
BaseGetListRequest
|
|
Approved int8 `form:"is_with_approval"`
|
|
}
|
|
|
|
// @Summary List news and events
|
|
// @Tags news
|
|
// @Produce json
|
|
// @Param page query int true "Page (min 1)"
|
|
// @Param page_size query int true "Page size (min 5)"
|
|
// @Param is_with_approval query int false "1 = only approved items"
|
|
// @Success 200 {array} map[string]any
|
|
// @Failure 400 {object} map[string]any
|
|
// @Router /news-events [get]
|
|
func (server *Server) GetNewsEventsList(ctx *gin.Context) {
|
|
var req GetNewsEventsListReq
|
|
isWithApproval := ""
|
|
|
|
if err := ctx.ShouldBindQuery(&req); err != nil {
|
|
ctx.JSON(http.StatusBadRequest, ValidationErrorResponse(err))
|
|
return
|
|
}
|
|
if req.Approved > 0 {
|
|
isWithApproval = "WHERE approved_by IS NOT NULL"
|
|
}
|
|
|
|
news, err := server.Store.GetNewsEventsList(ctx, db.GetNewsEventsListParams{
|
|
Limit: req.PageSize,
|
|
Offset: (req.Page - 1) * req.PageSize,
|
|
IsWithApproved: isWithApproval,
|
|
})
|
|
|
|
if err != nil {
|
|
ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong while try to get news / events"))
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, news)
|
|
}
|