Skip to content

Commit

Permalink
update/delete post
Browse files Browse the repository at this point in the history
  • Loading branch information
ARUMANDESU committed Jun 9, 2024
1 parent 88f474e commit 6903574
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 9 deletions.
18 changes: 9 additions & 9 deletions internal/domain/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
92 changes: 92 additions & 0 deletions internal/handler/post/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
}

0 comments on commit 6903574

Please sign in to comment.