Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
269 changes: 206 additions & 63 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package api

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/samber/lo"
"github.com/scarlettmiss/bestPal/application"
"github.com/scarlettmiss/bestPal/application/domain/pet"
"github.com/scarlettmiss/bestPal/application/domain/user"
"github.com/scarlettmiss/bestPal/cmd/server/types"
"github.com/scarlettmiss/bestPal/converters"
typesPet "github.com/scarlettmiss/bestPal/cmd/server/types/pet"
typesUser "github.com/scarlettmiss/bestPal/cmd/server/types/user"
"github.com/scarlettmiss/bestPal/converters/petConverter"
"github.com/scarlettmiss/bestPal/converters/userConverter"
"github.com/scarlettmiss/bestPal/middlewares"
"github.com/scarlettmiss/bestPal/utils"
"net/http"
Expand All @@ -30,26 +32,33 @@ func New(application *application.Application) *API {
api.POST("/api/auth/register", api.register)
api.POST("/api/auth/login", api.login)

protected := api.Group("/").Use(middlewares.Auth())
protected.PATCH("/api/user", api.updateUser)
protected.DELETE("/api/user", api.deleteUser)
protected.GET("/api/user/:id", api.user)
protected.GET("/api/user", api.user)
protected.GET("/api/users", api.users)
userApi := api.Group("/").Use(middlewares.Auth())
userApi.GET("/api/users", api.users)
userApi.GET("/api/user", api.user)
userApi.GET("/api/user/:id", api.user)
userApi.PATCH("/api/user", api.updateUser)
userApi.DELETE("/api/user", api.deleteUser)

petApi := api.Group("/").Use(middlewares.Auth())
petApi.POST("/api/pet", api.createPet)
petApi.GET("/api/pets", api.pets)
petApi.GET("/api/pet/:id", api.pet)
petApi.PATCH("/api/pet/:id", api.updatePet)
petApi.DELETE("/api/pet/:id", api.deleteUser)

return api
}

func (api *API) register(c *gin.Context) {
var requestBody types.UserCreateRequest
var requestBody typesUser.UserCreateRequest

err := c.ShouldBindJSON(&requestBody)
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

u, err := converters.UserCreateRequestToUser(requestBody)
u, err := userConverter.UserCreateRequestToUser(requestBody)
if err != nil {
c.JSON(http.StatusBadRequest, utils.ErrorResponse(err))
return
Expand All @@ -71,13 +80,95 @@ func (api *API) register(c *gin.Context) {
return
}

c.JSON(http.StatusCreated, gin.H{"user": converters.UserToResponse(u), "token": token})
fmt.Println(u)
c.JSON(http.StatusCreated, gin.H{"user": userConverter.UserToResponse(u), "token": token})
}

func (api *API) users(c *gin.Context) {
users := api.app.Users()

usersResp := lo.MapValues(users, func(u user.User, _ uuid.UUID) typesUser.UserResponse {
return userConverter.UserToResponse(u)
})

c.JSON(http.StatusOK, usersResp)
}

func (api *API) user(c *gin.Context) {
id := c.Param("id")

if id == "" {
id = c.GetString("UserId")
}

//parse
uId, err := uuid.Parse(id)
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

u, err := api.app.User(uId)
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

c.JSON(http.StatusOK, userConverter.UserToResponse(u))
}

func (api *API) updateUser(c *gin.Context) {
uId, err := uuid.Parse(c.GetString("UserId"))
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

var requestBody typesUser.UserUpdateRequest
err = c.ShouldBindJSON(&requestBody)
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

u, err := api.app.User(uId)
if err != nil {
c.JSON(http.StatusNotFound, utils.ErrorResponse(err))
return
}

u = userConverter.UserUpdateRequestToUser(requestBody, u)

u, err = api.app.UpdateUser(u)
if err != nil {
if err == user.ErrMailExists {
c.JSON(http.StatusBadRequest, utils.ErrorResponse(err))
} else {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
}
return
}

c.JSON(http.StatusOK, userConverter.UserToResponse(u))
}

func (api *API) deleteUser(c *gin.Context) {
uId, err := uuid.Parse(c.GetString("UserId"))
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

err = api.app.DeleteUser(uId)
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

c.JSON(http.StatusOK, gin.H{"message": "user deleted"})
}

func (api *API) login(c *gin.Context) {
var requestBody types.LoginRequest
var requestBody typesUser.LoginRequest

err := c.ShouldBindJSON(&requestBody)
if err != nil {
Expand All @@ -96,114 +187,166 @@ func (api *API) login(c *gin.Context) {
return
}

c.JSON(http.StatusCreated, gin.H{"user": converters.UserToResponse(u), "token": token})
c.JSON(http.StatusCreated, gin.H{"user": userConverter.UserToResponse(u), "token": token})

}

func (api *API) users(c *gin.Context) {
users := api.app.Users()
func (api *API) createPet(c *gin.Context) {
var requestBody typesPet.PetRequest

err := c.ShouldBindJSON(&requestBody)
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

usersResp := lo.MapValues(users, func(u user.User, _ uuid.UUID) types.UserResponse {
return converters.UserToResponse(u)
uId, err := uuid.Parse(c.GetString("UserId"))
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

p, err := petConverter.PetCreateRequestToPet(requestBody, uId)
if err != nil {
c.JSON(http.StatusBadRequest, utils.ErrorResponse(err))
return
}

p, err = api.app.CreatePet(p)
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

c.JSON(http.StatusCreated, gin.H{"pet": petConverter.PetToResponse(p)})
}

func (api *API) pets(c *gin.Context) {
uId, err := uuid.Parse(c.GetString("UserId"))
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

pets := api.app.PetsByUser(uId)

petsResp := lo.MapValues(pets, func(p pet.Pet, _ uuid.UUID) typesPet.PetResponse {
return petConverter.PetToResponse(p)
})

c.JSON(http.StatusOK, usersResp)
c.JSON(http.StatusOK, petsResp)
}

func (api *API) user(c *gin.Context) {
func (api *API) pet(c *gin.Context) {
uId, err := uuid.Parse(c.GetString("UserId"))
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

id := c.Param("id")

var ok bool
if id == "" {
idParam, _ := c.Get("UserId")
// Convert UserId to string if it's not already.
id, ok = idParam.(string)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
return
}
c.JSON(http.StatusBadRequest, gin.H{"error": "Bad Request"})
return
}

//parse
uId, err := uuid.Parse(id)
pId, err := uuid.Parse(id)
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}
p, err := api.app.PetByUser(uId, pId)
if err != nil {
c.JSON(http.StatusNotFound, utils.ErrorResponse(err))
return
}

u, err := api.app.User(uId)
c.JSON(http.StatusOK, petConverter.PetToResponse(p))
}

func (api *API) updatePet(c *gin.Context) {
uId, err := uuid.Parse(c.GetString("UserId"))
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

c.JSON(http.StatusOK, converters.UserToResponse(u))
}
id := c.Param("id")

func (api *API) deleteUser(c *gin.Context) {
idParam, _ := c.Get("UserId")
// Convert UserId to string if it's not already.
id, ok := idParam.(string)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Bad Request"})
return
}

//parse
uId, err := uuid.Parse(id)
var requestBody typesPet.PetRequest

err = c.ShouldBindJSON(&requestBody)
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

err = api.app.DeleteUser(uId)
pId, err := uuid.Parse(id)
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

c.JSON(http.StatusOK, gin.H{"message": "user deleted"})
}
p, err := api.app.PetByUser(uId, pId)
if err != nil {
c.JSON(http.StatusNotFound, utils.ErrorResponse(err))
return
}

func (api *API) updateUser(c *gin.Context) {
id, _ := c.Get("UserId")
// Convert UserId to string if it's not already.
idString, ok := id.(string)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
p, err = petConverter.PetUpdateRequestToPet(requestBody, p)

if err != nil {
c.JSON(http.StatusBadRequest, utils.ErrorResponse(err))
return
}

uId, err := uuid.Parse(idString)
p, err = api.app.UpdatePet(p)
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

var requestBody types.UserUpdateRequest
err = c.ShouldBindJSON(&requestBody)
c.JSON(http.StatusCreated, gin.H{"pet": petConverter.PetToResponse(p)})
}

func (api *API) deletePet(c *gin.Context) {
uId, err := uuid.Parse(c.GetString("UserId"))
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

u, err := api.app.User(uId)
id := c.Param("id")

if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Bad Request"})
return
}

//parse
pId, err := uuid.Parse(id)
if err != nil {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}
_, err = api.app.PetByUser(uId, pId)
if err != nil {
c.JSON(http.StatusNotFound, utils.ErrorResponse(err))
return
}

u = converters.UserUpdateRequestToUser(requestBody, u)

u, err = api.app.UpdateUser(u)
err = api.app.DeletePet(pId)
if err != nil {
if err == user.ErrMailExists {
c.JSON(http.StatusBadRequest, utils.ErrorResponse(err))
} else {
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
}
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
return
}

c.JSON(http.StatusOK, converters.UserToResponse(u))
c.JSON(http.StatusOK, gin.H{"message": "pet deleted"})
}
Loading