Skip to content

Commit

Permalink
Merge pull request #125 from ppolariss/main
Browse files Browse the repository at this point in the history
feat: favorite group
  • Loading branch information
JingYiJun authored Mar 2, 2024
2 parents eb979fa + 2dcb353 commit 5262c74
Show file tree
Hide file tree
Showing 12 changed files with 1,286 additions and 49 deletions.
239 changes: 225 additions & 14 deletions apis/favourite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"github.com/opentreehole/go-common"
"gorm.io/gorm"
"gorm.io/plugin/dbresolver"

. "treehole_next/models"
. "treehole_next/utils"
"treehole_next/utils"

"github.com/gofiber/fiber/v2"
. "treehole_next/models"
)

// ListFavorites
Expand All @@ -17,8 +16,7 @@ import (
// @Tags Favorite
// @Produce application/json
// @Router /user/favorites [get]
// @Param object query ListModel false "query"
// @Success 200 {object} models.Map
// @Param object query ListFavoriteModel false "query"
// @Success 200 {array} models.Hole
func ListFavorites(c *fiber.Ctx) error {
// get userID
Expand All @@ -27,15 +25,25 @@ func ListFavorites(c *fiber.Ctx) error {
return err
}

var query ListModel
var query ListFavoriteModel
err = common.ValidateQuery(c, &query)
if err != nil {
return err
}
if query.FavoriteGroupID != nil {
if !IsFavoriteGroupExist(DB, userID, *query.FavoriteGroupID) {
return common.Forbidden("收藏夹不存在")
}
}

if query.Plain {
// get favorite ids
data, err := UserGetFavoriteData(DB, userID)
var data []int
if query.FavoriteGroupID == nil {
data, err = UserGetFavoriteData(DB, userID)
} else {
data, err = UserGetFavoriteDataByFavoriteGroup(DB, userID, *query.FavoriteGroupID)
}
if err != nil {
return err
}
Expand All @@ -54,13 +62,20 @@ func ListFavorites(c *fiber.Ctx) error {

// get favorites
holes := make(Holes, 0)
err = DB.
Joins("JOIN user_favorites ON user_favorites.hole_id = hole.id AND user_favorites.user_id = ?", userID).
Order(order).Find(&holes).Error
if query.FavoriteGroupID == nil {
err = DB.
Joins("JOIN user_favorites ON user_favorites.hole_id = hole.id AND user_favorites.user_id = ?", userID).
Order(order).Find(&holes).Error
} else {
err = DB.
Joins("JOIN user_favorites ON user_favorites.hole_id = hole.id AND user_favorites.user_id = ? AND user_favorites.favorite_group_id = ?", userID, *query.FavoriteGroupID).
Order(order).Find(&holes).Error
}

if err != nil {
return err
}
return Serialize(c, &holes)
return utils.Serialize(c, &holes)
}
}

Expand Down Expand Up @@ -92,7 +107,7 @@ func AddFavorite(c *fiber.Ctx) error {

err = DB.Clauses(dbresolver.Write).Transaction(func(tx *gorm.DB) error {
// add favorite
err = AddUserFavourite(tx, userID, body.HoleID)
err = AddUserFavorite(tx, userID, body.HoleID, body.FavoriteGroupID)
if err != nil {
return err
}
Expand Down Expand Up @@ -135,7 +150,7 @@ func ModifyFavorite(c *fiber.Ctx) error {
}

// modify favorite
err = ModifyUserFavourite(DB, userID, body.HoleIDs)
err = ModifyUserFavorite(DB, userID, body.HoleIDs, body.FavoriteGroupID)
if err != nil {
return err
}
Expand Down Expand Up @@ -176,7 +191,7 @@ func DeleteFavorite(c *fiber.Ctx) error {
}

// delete favorite
err = DB.Delete(UserFavorite{UserID: userID, HoleID: body.HoleID}).Error
err = DeleteUserFavorite(DB, userID, body.HoleID, body.FavoriteGroupID)
if err != nil {
return err
}
Expand All @@ -192,3 +207,199 @@ func DeleteFavorite(c *fiber.Ctx) error {
Data: data,
})
}

// ListFavoriteGroups
//
// @Summary List User's Favorite Groups
// @Tags Favorite
// @Produce application/json
// @Router /user/favorite_groups [get]
// @Param object query ListFavoriteGroupModel false "query"
// @Success 200 {array} models.FavoriteGroup
func ListFavoriteGroups(c *fiber.Ctx) error {
// get userID
userID, err := common.GetUserID(c)
if err != nil {
return err
}

var query ListFavoriteGroupModel
err = common.ValidateQuery(c, &query)
if err != nil {
return err
}

if query.Plain {
// get favoriteGroups
data, err := UserGetFavoriteGroups(DB, userID)
if err != nil {
return err
}
return c.JSON(Map{"data": data})
} else {
// get order
var order string
switch query.Order {
case "id":
order = "id desc"
case "time_created":
order = "created_at desc, id desc"
case "time_updated":
order = "updated_at desc, id desc"
}

// get favoriteGroups
var data FavoriteGroups
err = DB.Where("user_id = ? AND deleted = false", userID).Order(order).Find(&data).Error
if err != nil {
return err
}
return c.JSON(&data)
}
}

// AddFavoriteGroup
//
// @Summary Add A Favorite Group
// @Tags Favorite
// @Accept application/json
// @Produce application/json
// @Router /user/favorite_groups [post]
// @Param json body AddFavoriteGroupModel true "json"
// @Success 201 {array} models.FavoriteGroup
func AddFavoriteGroup(c *fiber.Ctx) error {
// validate body
var body AddFavoriteGroupModel
err := common.ValidateBody(c, &body)
if err != nil {
return err
}

// get userID
userID, err := common.GetUserID(c)
if err != nil {
return err
}

// add favorite group
err = AddUserFavoriteGroup(DB, userID, body.Name)
if err != nil {
return err
}

// create response
data, err := UserGetFavoriteGroups(DB, userID)
if err != nil {
return err
}

return c.Status(201).JSON(&data)
}

// ModifyFavoriteGroup
//
// @Summary Modify User's Favorite Group
// @Tags Favorite
// @Produce application/json
// @Router /user/favorite_groups [put]
// @Param json body ModifyFavoriteGroupModel true "json"
// @Success 200 {array} models.FavoriteGroup
// @Failure 404 {object} common.HttpError
func ModifyFavoriteGroup(c *fiber.Ctx) error {
// validate body
var body ModifyFavoriteGroupModel
err := common.ValidateBody(c, &body)
if err != nil {
return err
}

// get userID
userID, err := common.GetUserID(c)
if err != nil {
return err
}

// modify favorite group
err = ModifyUserFavoriteGroup(DB, userID, *body.FavoriteGroupID, body.Name)
if err != nil {
return err
}

// create response
data, err := UserGetFavoriteGroups(DB, userID)
if err != nil {
return err
}

return c.JSON(&data)
}

// DeleteFavoriteGroup
//
// @Summary Delete A Favorite Group
// @Tags Favorite
// @Produce application/json
// @Router /user/favorite_groups [delete]
// @Param json body DeleteModel true "json"
// @Success 204
// @Failure 404 {object} common.HttpError
func DeleteFavoriteGroup(c *fiber.Ctx) error {
// validate body
var body DeleteFavoriteGroupModel
err := common.ValidateBody(c, &body)
if err != nil {
return err
}

// get userID
userID, err := common.GetUserID(c)
if err != nil {
return err
}

// delete favorite group
err = DeleteUserFavoriteGroup(DB, userID, *body.FavoriteGroupID)
if err != nil {
return err
}

return c.Status(204).JSON(nil)
}

// MoveFavorite
//
// @Summary Move User's Favorite
// @Tags Favorite
// @Produce application/json
// @Router /user/favorites/move [put]
// @Param json body MoveModel true "json"
// @Success 200 {array} models.Hole
// @Failure 404 {object} Response
func MoveFavorite(c *fiber.Ctx) error {
// validate body
var body MoveModel
err := common.ValidateBody(c, &body)
if err != nil {
return err
}

// get userID
userID, err := common.GetUserID(c)
if err != nil {
return err
}

// move favorite
err = MoveUserFavorite(DB, userID, body.HoleIDs, *body.FromFavoriteGroupID, *body.ToFavoriteGroupID)
if err != nil {
return err
}

// create response
data, err := UserGetFavoriteData(DB, userID)
if err != nil {
return err
}

return c.JSON(&data)
}
5 changes: 5 additions & 0 deletions apis/favourite/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ func RegisterRoutes(app fiber.Router) {
app.Post("/user/favorites", AddFavorite)
app.Put("/user/favorites", ModifyFavorite)
app.Delete("/user/favorites", DeleteFavorite)
app.Get("/user/favorite_groups", ListFavoriteGroups)
app.Post("/user/favorite_groups", AddFavoriteGroup)
app.Put("/user/favorite_groups", ModifyFavoriteGroup)
app.Delete("/user/favorite_groups", DeleteFavoriteGroup)
app.Put("/user/favorites/move", MoveFavorite)
}
40 changes: 34 additions & 6 deletions apis/favourite/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,47 @@ type Response struct {
Data []int `json:"data"`
}

type ListModel struct {
Order string `json:"order" query:"order" validate:"omitempty,oneof=id time_created hole_time_updated" default:"time_created"`
Plain bool `json:"plain" default:"false" query:"plain"`
type ListFavoriteModel struct {
Order string `json:"order" query:"order" validate:"omitempty,oneof=id time_created hole_time_updated" default:"time_created"`
Plain bool `json:"plain" default:"false" query:"plain"`
FavoriteGroupID *int `json:"favorite_group_id" query:"favorite_group_id"`
}

type AddModel struct {
HoleID int `json:"hole_id"`
HoleID int `json:"hole_id"`
FavoriteGroupID int `json:"favorite_group_id" default:"0"`
}

type ModifyModel struct {
HoleIDs []int `json:"hole_ids"`
HoleIDs []int `json:"hole_ids"`
FavoriteGroupID int `json:"favorite_group_id" default:"0"`
}

type DeleteModel struct {
HoleID int `json:"hole_id"`
HoleID int `json:"hole_id"`
FavoriteGroupID int `json:"favorite_group_id" default:"0"`
}

type AddFavoriteGroupModel struct {
Name string `json:"name" validate:"required,max=64"`
}

type ModifyFavoriteGroupModel struct {
Name string `json:"name" validate:"required,max=64"`
FavoriteGroupID *int `json:"favorite_group_id" validate:"required"`
}

type DeleteFavoriteGroupModel struct {
FavoriteGroupID *int `json:"favorite_group_id" validate:"required"`
}

type MoveModel struct {
HoleIDs []int `json:"hole_ids"`
FromFavoriteGroupID *int `json:"from_favorite_group_id" default:"0" validate:"required"`
ToFavoriteGroupID *int `json:"to_favorite_group_id" validate:"required"`
}

type ListFavoriteGroupModel struct {
Order string `json:"order" query:"order" validate:"omitempty,oneof=id time_created time_updated" default:"time_created"`
Plain bool `json:"plain" default:"false" query:"plain"`
}
Loading

0 comments on commit 5262c74

Please sign in to comment.