diff --git a/internal/domain/post.go b/internal/domain/post.go index 3bc8b85..5863db2 100644 --- a/internal/domain/post.go +++ b/internal/domain/post.go @@ -6,15 +6,15 @@ import ( ) type Post struct { - ID string - Club EventClub - Title string - Description string - Tags []string - CoverImages []CoverImage - AttachedFiles []EventFile - CreatedAt time.Time - UpdatedAt time.Time + ID string `json:"id,omitempty"` + Club EventClub `json:"club"` + Title string `json:"title,omitempty"` + Description string `json:"description,omitempty"` + Tags []string `json:"tags,omitempty"` + CoverImages []CoverImage `json:"cover_images,omitempty"` + AttachedFiles []EventFile `json:"attached_files,omitempty"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } func PostFromPb(post *postv1.PostObject) *Post { diff --git a/internal/handler/post/management.go b/internal/handler/post/management.go index 38f7b8c..5ea392e 100644 --- a/internal/handler/post/management.go +++ b/internal/handler/post/management.go @@ -78,9 +78,101 @@ func (h *Handler) CreatePostHandler(c *gin.Context) { } func (h *Handler) UpdatePostHandler(c *gin.Context) { + const op = "handler.post.updatePostHandler" + log := h.log.With(slog.String("op", op)) + + postID := c.Params.ByName("id") + if postID == "" { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "post id parameter must be provided"}) + return + } + + userIDFromCtx, ok := c.Get("userID") + if !ok { + c.AbortWithStatus(http.StatusUnauthorized) + return + } + userID := userIDFromCtx.(int64) + + var input struct { + Title string `json:"title" binding:"required"` + Description string `json:"description,omitempty"` + Tags []string `json:"tags,omitempty"` + CoverImages []domain.CoverImage `json:"cover_images,omitempty"` + AttachedFiles []domain.EventFile `json:"attached_files,omitempty"` + } + if err := c.ShouldBindJSON(&input); err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + var paths []string + request := &postv1.UpdatePostRequest{ + Id: postID, + UserId: userID, + } + + if input.Title == "" { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "title is required"}) + return + } else { + paths = append(paths, "title") + request.Title = input.Title + } + if input.Description != "" { + paths = append(paths, "description") + request.Description = input.Description + } + if len(input.Tags) > 0 { + paths = append(paths, "tags") + request.Tags = input.Tags + } + if len(input.CoverImages) > 0 { + paths = append(paths, "cover_images") + request.CoverImages = domain.CoverImageToProtoArr(input.CoverImages) + } + if len(input.AttachedFiles) > 0 { + paths = append(paths, "attached_files") + request.AttachedFiles = domain.EventFileToProtoArr(input.AttachedFiles) + } + + request.UpdateMask = &field_mask.FieldMask{Paths: paths} + + post, err := h.postsClient.UpdatePost(c, request) + if err != nil { + handleErrors(c, log, "failed to update post", err) + return + } + + c.JSON(http.StatusOK, gin.H{"post": domain.PostFromPb(post)}) } func (h *Handler) DeletePostHandler(c *gin.Context) { + const op = "handler.post.deletePostHandler" + log := h.log.With(slog.String("op", op)) + + postID := c.Params.ByName("id") + if postID == "" { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "post id parameter must be provided"}) + return + } + + userIDFromCtx, ok := c.Get("userID") + if !ok { + c.AbortWithStatus(http.StatusUnauthorized) + return + } + userID := userIDFromCtx.(int64) + + post, err := h.postsClient.DeletePost(c, &postv1.ActionRequest{ + Id: postID, + UserId: userID, + }) + if err != nil { + handleErrors(c, log, "failed to delete post", err) + return + } + c.JSON(http.StatusOK, gin.H{"post": domain.PostFromPb(post)}) }