Skip to content

Commit

Permalink
Fixed lastUpdated logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Dec 12, 2022
1 parent e76bb1e commit fb63143
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions api/v1/bookmarks/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/go-playground/validator/v10"
"github.com/mrusme/xbsapi/ent/bookmark"

"github.com/gofiber/fiber/v2"
)
Expand Down Expand Up @@ -71,29 +72,26 @@ func (h *handler) Update(ctx *fiber.Ctx) error {
})
}

dbBookmarkTmp := h.entClient.Bookmark.
UpdateOneID(id)

if updateBookmark.Bookmarks != "" {
dbBookmarkTmp = dbBookmarkTmp.
SetBookmarks(updateBookmark.Bookmarks)
}

if updateBookmark.LastUpdated != "" {
t, err := time.Parse(LAST_UPDATED_FORMAT, updateBookmark.LastUpdated)
if err != nil {
return ctx.
Status(fiber.StatusInternalServerError).
JSON(fiber.Map{
"success": false,
"message": err.Error(),
})
}
dbBookmarkTmp = dbBookmarkTmp.
SetLastUpdated(t)
lastUpdated, err := time.Parse(LAST_UPDATED_FORMAT, updateBookmark.LastUpdated)
if err != nil {
return ctx.
Status(fiber.StatusInternalServerError).
JSON(fiber.Map{
"success": false,
"message": err.Error(),
})
}

dbBookmark, err := dbBookmarkTmp.Save(context.Background())
now := time.Now()
dbBookmarkN, err := h.entClient.Bookmark.
Update().
Where(bookmark.And(
bookmark.ID(id),
bookmark.LastUpdated(lastUpdated),
)).
SetBookmarks(updateBookmark.Bookmarks).
SetLastUpdated(now).
Save(context.Background())

if err != nil {
return ctx.
Expand All @@ -104,8 +102,17 @@ func (h *handler) Update(ctx *fiber.Ctx) error {
})
}

if dbBookmarkN != 0 {
return ctx.
Status(fiber.StatusConflict).
JSON(fiber.Map{
"success": false,
"message": "Internal sync error",
})
}

showBookmark := BookmarkShowModel{
LastUpdated: dbBookmark.LastUpdated.Format(LAST_UPDATED_FORMAT),
LastUpdated: now.Format(LAST_UPDATED_FORMAT),
}

return ctx.
Expand Down

0 comments on commit fb63143

Please sign in to comment.