You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.2 KiB
45 lines
1.2 KiB
package users
|
|
|
|
import (
|
|
"git.devices.local/mawas/golang-api-skeleton/lib/cache"
|
|
"git.devices.local/mawas/golang-api-skeleton/lib/response"
|
|
"git.devices.local/mawas/golang-api-skeleton/repositories"
|
|
"git.devices.local/mawas/golang-api-skeleton/services"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func prepare(c *gin.Context) (response.Envelope, *services.UserService) {
|
|
resp := response.Envelope{
|
|
RequestID: c.MustGet("requestID").(string),
|
|
}
|
|
userRepo := repositories.NewUserRepository(
|
|
c.MustGet("db").(*gorm.DB),
|
|
c.MustGet("userID").(string),
|
|
c.MustGet("username").(string),
|
|
c.MustGet("cache").(cache.Cache),
|
|
)
|
|
userService := services.NewUserService(userRepo)
|
|
return resp, userService
|
|
}
|
|
|
|
func ReadByID(c *gin.Context) {
|
|
resp, userService := prepare(c)
|
|
username := c.Param("username")
|
|
user, err := userService.ReadByID(username)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(500, resp.AppendError(err))
|
|
return
|
|
}
|
|
c.JSON(200, resp.SetSuccess(user))
|
|
}
|
|
|
|
func ReadAll(c *gin.Context) {
|
|
resp, userService := prepare(c)
|
|
users, err := userService.ReadAll()
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(500, resp.AppendError(err))
|
|
return
|
|
}
|
|
c.JSON(200, resp.SetSuccess(users))
|
|
}
|