package routes import ( "context" "encoding/binary" rest_api_stuff "enshi/REST_API_stuff" db_repo "enshi/db/go_queries" "enshi/db_connection" "enshi/middleware/getters" "github.com/gin-gonic/gin" "github.com/google/uuid" ) func CreateComment(c *gin.Context) { var CommentParams db_repo.CreateCommentParams if err := c.BindJSON(&CommentParams); err != nil { rest_api_stuff.BadRequestAnswer(c, err) return } userId, err := getters.GetUserIdFromContext(c) if err != nil { rest_api_stuff.BadRequestAnswer(c, err) return } if commentId, err := uuid.NewV7(); err != nil { rest_api_stuff.InternalErrorAnswer(c, err) return } else { CommentParams.CommentID = -int64(binary.BigEndian.Uint64(commentId[8:])) } CommentParams.UserID = userId postId, err := getters.GetInt64Param(c, "post-id") if err != nil { rest_api_stuff.BadRequestAnswer(c, err) return } CommentParams.PostID = postId transaction, err := db_connection.Dbx.Begin(context.Background()) if err != nil { rest_api_stuff.InternalErrorAnswer(c, err) return } defer transaction.Rollback(context.Background()) _, err = db_repo.New(transaction).CreateComment( context.Background(), CommentParams, ) if err != nil { rest_api_stuff.InternalErrorAnswer(c, err) return } transaction.Commit(context.Background()) rest_api_stuff.OkAnswer(c, "comment has been created") }