package api import ( "log" "net/http" db "git.nochill.in/nochill/hiling_go/db/sqlc" "git.nochill.in/nochill/hiling_go/util/token" "github.com/gin-gonic/gin" "github.com/jackc/pgx/v5" ) type createReviewReq struct { SubmittedBy int8 `json:"submitted_by" binding:"required"` Comments string `json:"comments" binding:"required"` Score int8 `json:"score" binding:"numeric,max=100"` IsFromCritic *bool `json:"is_from_critic"` IsHided *bool `json:"is_hided"` LocationId int32 `json:"location_id" binding:"required"` } func (server *Server) createReview(ctx *gin.Context) { var req createReviewReq if err := ctx.ShouldBindJSON(&req); err != nil { ctx.JSON(http.StatusBadRequest, ValidationErrorResponse(err)) return } checkArg := db.CheckIfReviewExistsParams{ LocationID: req.LocationId, SubmittedBy: int32(req.SubmittedBy), } reviewExist, err := server.Store.CheckIfReviewExists(ctx, checkArg) if err != nil { ctx.JSON(http.StatusInternalServerError, "Something went wrong while try to check review") return } if reviewExist != 0 { ctx.JSON(http.StatusConflict, "User review already exist") return } arg := db.CreateReviewParams{ SubmittedBy: int32(req.SubmittedBy), Comments: req.Comments, Score: int16(req.Score), IsFromCritic: *req.IsFromCritic, IsHided: *req.IsHided, LocationID: req.LocationId, } review, err := server.Store.CreateReview(ctx, arg) if err != nil { ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong while try to save review")) return } ctx.JSON(http.StatusOK, review) } type getUserReviewByLocationReq struct { LocationID int32 `uri:"location_id" binding:"required"` } func (server *Server) getUserReviewByLocation(ctx *gin.Context) { var req getUserReviewByLocationReq if err := ctx.ShouldBindUri(&req); err != nil { ctx.JSON(http.StatusBadRequest, ValidationErrorResponse(err)) return } authPayload := ctx.MustGet(authorizationPayloadKey).(*token.Payload) log.Println(authPayload) arg := db.GetUserReviewByLocationParams{ SubmittedBy: int32(authPayload.UserID), LocationID: req.LocationID, } review, err := server.Store.GetUserReviewByLocation(ctx, arg) if err != nil { if err == pgx.ErrNoRows { ctx.JSON(http.StatusNotFound, ErrorResponse(err, "Review not found")) return } ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong while try to recevie current user review")) return } ctx.JSON(http.StatusOK, review) }