Implemented User Repository in a functional manner by making use of IBMs fp-go package
This commit is contained in:
parent
aac9c8af4f
commit
b5c851c018
21
backend-go/internal/application/services/helpers.go
Normal file
21
backend-go/internal/application/services/helpers.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HandleError(c *gin.Context) func(error) any {
|
||||||
|
return func(err error) any {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleSuccess[T any](c *gin.Context, statusCode int) func(T) any {
|
||||||
|
return func(data T) any {
|
||||||
|
c.JSON(statusCode, data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
@ -17,6 +17,15 @@ func MapUserToDTO(user entities.User) dto.UserDTO {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MapUsersToDTOs converts a slice of User domain objects to a slice of UserDTOs.
|
||||||
|
func MapUsersToDTOs(users []entities.User) []dto.UserDTO {
|
||||||
|
var userDTOs []dto.UserDTO
|
||||||
|
for _, user := range users {
|
||||||
|
userDTOs = append(userDTOs, MapUserToDTO(user))
|
||||||
|
}
|
||||||
|
return userDTOs
|
||||||
|
}
|
||||||
|
|
||||||
// MapCreateDTOToUser converts a UserCreateDTO to a User domain object.
|
// MapCreateDTOToUser converts a UserCreateDTO to a User domain object.
|
||||||
func MapCreateDTOToUser(dto dto.UserCreateDTO) entities.UserCreate {
|
func MapCreateDTOToUser(dto dto.UserCreateDTO) entities.UserCreate {
|
||||||
return entities.UserCreate{
|
return entities.UserCreate{
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
package service
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"actatempus_backend/internal/application/services/dto"
|
||||||
mappers "actatempus_backend/internal/application/services/mapper"
|
mappers "actatempus_backend/internal/application/services/mapper"
|
||||||
"actatempus_backend/internal/domain/entities"
|
"actatempus_backend/internal/domain/entities"
|
||||||
"actatempus_backend/internal/domain/repository"
|
"actatempus_backend/internal/domain/repository"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
A "github.com/IBM/fp-go/array"
|
||||||
|
|
||||||
E "github.com/IBM/fp-go/either"
|
E "github.com/IBM/fp-go/either"
|
||||||
F "github.com/IBM/fp-go/function"
|
F "github.com/IBM/fp-go/function"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UserService handles user-related HTTP requests.
|
// UserService handles user-related HTTP requests.
|
||||||
@ -33,71 +34,68 @@ func (s *UserService) RegisterRoutes(router *gin.RouterGroup) {
|
|||||||
|
|
||||||
// CreateUser handles the creation of a new user.
|
// CreateUser handles the creation of a new user.
|
||||||
func (s *UserService) CreateUser(c *gin.Context) {
|
func (s *UserService) CreateUser(c *gin.Context) {
|
||||||
var userCreate entities.UserCreate
|
var userCreateDTO dto.UserCreateDTO
|
||||||
if err := c.ShouldBindJSON(&userCreate); err != nil {
|
if err := c.ShouldBindJSON(&userCreateDTO); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
userCreate := mappers.MapCreateDTOToUser(userCreateDTO)
|
||||||
s.repository.Create(c.Request.Context(), userCreate).Fold(
|
F.Pipe2(
|
||||||
func(err error) {
|
s.repository.Create(c.Request.Context(), userCreate),
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
E.Map[error](mappers.MapUserToDTO),
|
||||||
},
|
E.Fold(
|
||||||
func(user entities.User) {
|
HandleError(c),
|
||||||
c.JSON(http.StatusCreated, user)
|
HandleSuccess[dto.UserDTO](c, http.StatusCreated),
|
||||||
},
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserByID handles fetching a user by their ID.
|
// GetUserByID handles fetching a user by their ID.
|
||||||
func (s *UserService) GetUserByID(c *gin.Context) {
|
func (s *UserService) GetUserByID(c *gin.Context) {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
mappers.MapCreateDTOToProject(id)
|
F.Pipe2(
|
||||||
res := F.Pipe2(
|
|
||||||
s.repository.FindByID(c.Request.Context(), id),
|
s.repository.FindByID(c.Request.Context(), id),
|
||||||
E.Map(mappers.MapUserToDTO),
|
E.Map[error](mappers.MapUserToDTO),
|
||||||
)
|
E.Fold(
|
||||||
|
HandleError(c),
|
||||||
s.repository.FindByID(c.Request.Context(), id).Fold(
|
HandleSuccess[dto.UserDTO](c, http.StatusOK),
|
||||||
func(err error) {
|
),
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
|
||||||
},
|
|
||||||
func(user entities.User) {
|
|
||||||
c.JSON(http.StatusOK, user)
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllUsers handles fetching all users.
|
// GetAllUsers handles fetching all users.
|
||||||
func (s *UserService) GetAllUsers(c *gin.Context) {
|
func (s *UserService) GetAllUsers(c *gin.Context) {
|
||||||
s.repository.FindAll(c.Request.Context()).Fold(
|
F.Pipe2(
|
||||||
func(err error) {
|
s.repository.FindAll(c.Request.Context()),
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
E.Map[error](func(users []entities.User) []dto.UserDTO {
|
||||||
},
|
return A.Map(mappers.MapUserToDTO)(users) // Anwenden des Mappings auf jedes Element
|
||||||
func(users []entities.User) {
|
}),
|
||||||
c.JSON(http.StatusOK, users)
|
E.Fold(
|
||||||
},
|
HandleError(c),
|
||||||
|
HandleSuccess[[]dto.UserDTO](c, http.StatusOK),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateUser handles updating an existing user.
|
// UpdateUser handles updating an existing user.
|
||||||
func (s *UserService) UpdateUser(c *gin.Context) {
|
func (s *UserService) UpdateUser(c *gin.Context) {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
var userUpdate entities.UserUpdate
|
var userUpdateDTO dto.UserUpdateDTO
|
||||||
|
|
||||||
if err := c.ShouldBindJSON(&userUpdate); err != nil {
|
if err := c.ShouldBindJSON(&userUpdateDTO); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userUpdate.ID = id
|
userUpdate := mappers.MapUpdateDTOToUser(userUpdateDTO, id)
|
||||||
s.repository.Update(c.Request.Context(), userUpdate).Fold(
|
|
||||||
func(err error) {
|
F.Pipe2(
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
s.repository.Update(c.Request.Context(), userUpdate),
|
||||||
},
|
E.Map[error](mappers.MapUserToDTO),
|
||||||
func(user entities.User) {
|
E.Fold(
|
||||||
c.JSON(http.StatusOK, user)
|
HandleError(c),
|
||||||
},
|
HandleSuccess[dto.UserDTO](c, http.StatusOK),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,12 +103,13 @@ func (s *UserService) UpdateUser(c *gin.Context) {
|
|||||||
func (s *UserService) DeleteUser(c *gin.Context) {
|
func (s *UserService) DeleteUser(c *gin.Context) {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
|
|
||||||
s.repository.Delete(c.Request.Context(), id).Fold(
|
F.Pipe2(
|
||||||
func(err error) {
|
s.repository.Delete(c.Request.Context(), id),
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
E.Map[error](mappers.MapUserToDTO),
|
||||||
},
|
E.Fold(
|
||||||
func(_ entities.User) {
|
HandleError(c),
|
||||||
c.JSON(http.StatusOK, gin.H{"message": "User deleted successfully"})
|
HandleSuccess[dto.UserDTO](c, http.StatusOK),
|
||||||
},
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user