457 lines
15 KiB
Go
457 lines
15 KiB
Go
package handlers
|
|
|
|
import (
|
|
"wish-list-api/api/presenter"
|
|
"wish-list-api/pkg/auth"
|
|
"wish-list-api/pkg/entities"
|
|
wishlist "wish-list-api/pkg/wish-list"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type WishListHandler struct {
|
|
wishListService wishlist.Service
|
|
authService auth.Service
|
|
}
|
|
|
|
func NewWishListHandler(wishListService wishlist.Service, authService auth.Service) *WishListHandler {
|
|
return &WishListHandler{
|
|
wishListService: wishListService,
|
|
authService: authService,
|
|
}
|
|
}
|
|
|
|
// @Summary Create a new wishlist
|
|
// @Description Create a new wishlist for the authenticated user
|
|
// @Tags wishlist
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer token"
|
|
// @Param wishlist body entities.WishList true "Wishlist data"
|
|
// @Success 201 {object} presenter.WishListResponse
|
|
// @Failure 400 {object} presenter.WishListResponse
|
|
// @Failure 401 {object} presenter.WishListResponse
|
|
// @Security BearerAuth
|
|
// @Router /wishlist [post]
|
|
func (h *WishListHandler) CreateWishList(c *fiber.Ctx) error {
|
|
token := c.Get("Authorization")
|
|
if token == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
userID, err := h.authService.GetUserIDFromToken(token)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
wishList := new(entities.WishList)
|
|
if err := c.BodyParser(wishList); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
wishList.UserID = userID
|
|
|
|
result, err := h.wishListService.CreateWishList(wishList)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
return c.Status(fiber.StatusCreated).JSON(presenter.WishListSuccessResponse(result))
|
|
}
|
|
|
|
// @Summary Get a wishlist
|
|
// @Description Get a wishlist by its ID
|
|
// @Tags wishlist
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "Wishlist ID"
|
|
// @Success 200 {object} presenter.WishListResponse
|
|
// @Failure 404 {object} presenter.WishListResponse
|
|
// @Security BearerAuth
|
|
// @Router /wishlist/{id} [get]
|
|
func (h *WishListHandler) GetWishList(c *fiber.Ctx) error {
|
|
id := c.Params("id")
|
|
result, err := h.wishListService.GetWishList(id)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
if !result.IsPublic {
|
|
token := c.Get("Authorization")
|
|
if token == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
userID, err := h.authService.GetUserIDFromToken(token)
|
|
if err != nil || userID != result.UserID {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(presenter.WishListSuccessResponse(result))
|
|
}
|
|
|
|
// @Summary Get user wishlists
|
|
// @Description Get all wishlists for a specific user
|
|
// @Tags wishlist
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param userId path string true "User ID"
|
|
// @Success 200 {object} presenter.WishListsResponse
|
|
// @Failure 404 {object} presenter.WishListResponse
|
|
// @Security BearerAuth
|
|
// @Router /wishlist/user/{userId} [get]
|
|
func (h *WishListHandler) GetUserWishLists(c *fiber.Ctx) error {
|
|
userID := c.Params("userId")
|
|
|
|
isOwner := false
|
|
token := c.Get("Authorization")
|
|
if token != "" {
|
|
requestorID, err := h.authService.GetUserIDFromToken(token)
|
|
if err == nil && requestorID == userID {
|
|
isOwner = true
|
|
}
|
|
}
|
|
|
|
var result *[]presenter.WishList
|
|
var err error
|
|
|
|
result, err = h.wishListService.GetAllWishLists(userID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
if !isOwner {
|
|
publicLists := []presenter.WishList{}
|
|
for _, list := range *result {
|
|
if list.IsPublic {
|
|
publicLists = append(publicLists, list)
|
|
}
|
|
}
|
|
filteredResult := publicLists
|
|
result = &filteredResult
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(presenter.WishListsSuccessResponse(result))
|
|
}
|
|
|
|
// @Summary Update a wishlist
|
|
// @Description Update an existing wishlist
|
|
// @Tags wishlist
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer token"
|
|
// @Param id path string true "Wishlist ID"
|
|
// @Param wishlist body entities.WishList true "Updated wishlist data"
|
|
// @Success 200 {object} presenter.WishListResponse
|
|
// @Failure 400 {object} presenter.WishListResponse
|
|
// @Failure 401 {object} presenter.WishListResponse
|
|
// @Failure 404 {object} presenter.WishListResponse
|
|
// @Security BearerAuth
|
|
// @Router /wishlist/{id} [put]
|
|
func (h *WishListHandler) UpdateWishList(c *fiber.Ctx) error {
|
|
token := c.Get("Authorization")
|
|
if token == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
userID, err := h.authService.GetUserIDFromToken(token)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
id := c.Params("id")
|
|
currentWishList, err := h.wishListService.GetWishList(id)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
if currentWishList.UserID != userID {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
wishList := new(entities.WishList)
|
|
if err := c.BodyParser(wishList); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
wishList.ID = id
|
|
wishList.UserID = userID
|
|
|
|
result, err := h.wishListService.UpdateWishList(wishList)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(presenter.WishListSuccessResponse(result))
|
|
}
|
|
|
|
// @Summary Delete a wishlist
|
|
// @Description Delete a wishlist and all its items
|
|
// @Tags wishlist
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer token"
|
|
// @Param id path string true "Wishlist ID"
|
|
// @Success 200 {object} presenter.WishListResponse
|
|
// @Failure 401 {object} presenter.WishListResponse
|
|
// @Failure 404 {object} presenter.WishListResponse
|
|
// @Security BearerAuth
|
|
// @Router /wishlist/{id} [delete]
|
|
func (h *WishListHandler) DeleteWishList(c *fiber.Ctx) error {
|
|
token := c.Get("Authorization")
|
|
if token == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
userID, err := h.authService.GetUserIDFromToken(token)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
id := c.Params("id")
|
|
currentWishList, err := h.wishListService.GetWishList(id)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
if currentWishList.UserID != userID {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
if err := h.wishListService.DeleteWishList(id); err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"status": true,
|
|
"data": "Wishlist deleted successfully",
|
|
"error": nil,
|
|
})
|
|
}
|
|
|
|
// @Summary Create a wishlist item
|
|
// @Description Create a new item for a wishlist
|
|
// @Tags wishlist-items
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer token"
|
|
// @Param item body entities.WishListItem true "Wishlist item data"
|
|
// @Success 201 {object} presenter.WishListItemResponse
|
|
// @Failure 400 {object} presenter.WishListItemResponse
|
|
// @Failure 401 {object} presenter.WishListItemResponse
|
|
// @Security BearerAuth
|
|
// @Router /wishlist/item [post]
|
|
func (h *WishListHandler) CreateWishListItem(c *fiber.Ctx) error {
|
|
token := c.Get("Authorization")
|
|
if token == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
userID, err := h.authService.GetUserIDFromToken(token)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
item := new(entities.WishListItem)
|
|
if err := c.BodyParser(item); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
wishList, err := h.wishListService.GetWishList(item.WishListID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
if wishList.UserID != userID {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
result, err := h.wishListService.CreateWishListItem(item)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
return c.Status(fiber.StatusCreated).JSON(presenter.WishListItemSuccessResponse(result))
|
|
}
|
|
|
|
// @Summary Get a wishlist item
|
|
// @Description Get a wishlist item by its ID
|
|
// @Tags wishlist-items
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "Item ID"
|
|
// @Success 200 {object} presenter.WishListItemResponse
|
|
// @Failure 404 {object} presenter.WishListItemResponse
|
|
// @Security BearerAuth
|
|
// @Router /wishlist/item/{id} [get]
|
|
func (h *WishListHandler) GetWishListItem(c *fiber.Ctx) error {
|
|
id := c.Params("id")
|
|
result, err := h.wishListService.GetWishListItem(id)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
wishList, err := h.wishListService.GetWishList(result.WishListID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
if !wishList.IsPublic {
|
|
token := c.Get("Authorization")
|
|
if token == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
userID, err := h.authService.GetUserIDFromToken(token)
|
|
if err != nil || userID != wishList.UserID {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(presenter.WishListItemSuccessResponse(result))
|
|
}
|
|
|
|
// @Summary Get wishlist items
|
|
// @Description Get all items in a wishlist
|
|
// @Tags wishlist-items
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param wishlistId path string true "Wishlist ID"
|
|
// @Success 200 {object} presenter.WishListItemsResponse
|
|
// @Failure 404 {object} presenter.WishListItemsResponse
|
|
// @Security BearerAuth
|
|
// @Router /wishlist/{wishlistId}/items [get]
|
|
func (h *WishListHandler) GetWishListItems(c *fiber.Ctx) error {
|
|
wishListID := c.Params("wishlistId")
|
|
|
|
wishList, err := h.wishListService.GetWishList(wishListID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
if !wishList.IsPublic {
|
|
token := c.Get("Authorization")
|
|
if token == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
userID, err := h.authService.GetUserIDFromToken(token)
|
|
if err != nil || userID != wishList.UserID {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
}
|
|
|
|
result, err := h.wishListService.GetAllWishListItems(wishListID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(presenter.WishListItemsSuccessResponse(result))
|
|
}
|
|
|
|
// @Summary Update a wishlist item
|
|
// @Description Update an existing wishlist item
|
|
// @Tags wishlist-items
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer token"
|
|
// @Param id path string true "Item ID"
|
|
// @Param item body entities.WishListItem true "Updated item data"
|
|
// @Success 200 {object} presenter.WishListItemResponse
|
|
// @Failure 400 {object} presenter.WishListItemResponse
|
|
// @Failure 401 {object} presenter.WishListItemResponse
|
|
// @Failure 404 {object} presenter.WishListItemResponse
|
|
// @Security BearerAuth
|
|
// @Router /wishlist/item/{id} [put]
|
|
func (h *WishListHandler) UpdateWishListItem(c *fiber.Ctx) error {
|
|
token := c.Get("Authorization")
|
|
if token == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
userID, err := h.authService.GetUserIDFromToken(token)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
id := c.Params("id")
|
|
currentItem, err := h.wishListService.GetWishListItem(id)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
wishList, err := h.wishListService.GetWishList(currentItem.WishListID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
if wishList.UserID != userID {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
item := new(entities.WishListItem)
|
|
if err := c.BodyParser(item); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
item.ID = id
|
|
item.WishListID = currentItem.WishListID
|
|
|
|
result, err := h.wishListService.UpdateWishListItem(item)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(presenter.WishListItemSuccessResponse(result))
|
|
}
|
|
|
|
// @Summary Delete a wishlist item
|
|
// @Description Delete a wishlist item
|
|
// @Tags wishlist-items
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string true "Bearer token"
|
|
// @Param id path string true "Item ID"
|
|
// @Success 200 {object} presenter.WishListItemResponse
|
|
// @Failure 401 {object} presenter.WishListItemResponse
|
|
// @Failure 404 {object} presenter.WishListItemResponse
|
|
// @Security BearerAuth
|
|
// @Router /wishlist/item/{id} [delete]
|
|
func (h *WishListHandler) DeleteWishListItem(c *fiber.Ctx) error {
|
|
token := c.Get("Authorization")
|
|
if token == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
userID, err := h.authService.GetUserIDFromToken(token)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
id := c.Params("id")
|
|
currentItem, err := h.wishListService.GetWishListItem(id)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
wishList, err := h.wishListService.GetWishList(currentItem.WishListID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
if wishList.UserID != userID {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(presenter.WishListErrorResponse(fiber.ErrUnauthorized))
|
|
}
|
|
|
|
if err := h.wishListService.DeleteWishListItem(id); err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(presenter.WishListErrorResponse(err))
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"status": true,
|
|
"data": "Wishlist item deleted successfully",
|
|
"error": nil,
|
|
})
|
|
}
|