package api import ( "net/http" db "git.nochill.in/nochill/hiling_go/db/sqlc" "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"` } 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"` } 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) }