gin and user service (WIP)
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
mappers "actatempus_backend/internal/application/services/mapper"
|
||||
"actatempus_backend/internal/domain/entities"
|
||||
"actatempus_backend/internal/domain/repository"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
E "github.com/IBM/fp-go/either"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
)
|
||||
|
||||
// UserService handles user-related HTTP requests.
|
||||
type UserService struct {
|
||||
repository repository.UserRepository
|
||||
}
|
||||
|
||||
// NewUserService creates a new instance of UserService.
|
||||
func NewUserService(repo repository.UserRepository) *UserService {
|
||||
return &UserService{repository: repo}
|
||||
}
|
||||
|
||||
// RegisterRoutes registers the user-related routes with Gin.
|
||||
func (s *UserService) RegisterRoutes(router *gin.RouterGroup) {
|
||||
router.POST("/", s.CreateUser)
|
||||
router.GET("/:id", s.GetUserByID)
|
||||
router.GET("/", s.GetAllUsers)
|
||||
router.PUT("/:id", s.UpdateUser)
|
||||
router.DELETE("/:id", s.DeleteUser)
|
||||
}
|
||||
|
||||
// CreateUser handles the creation of a new user.
|
||||
func (s *UserService) CreateUser(c *gin.Context) {
|
||||
var userCreate entities.UserCreate
|
||||
if err := c.ShouldBindJSON(&userCreate); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
s.repository.Create(c.Request.Context(), userCreate).Fold(
|
||||
func(err error) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
},
|
||||
func(user entities.User) {
|
||||
c.JSON(http.StatusCreated, user)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// GetUserByID handles fetching a user by their ID.
|
||||
func (s *UserService) GetUserByID(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
mappers.MapCreateDTOToProject(id)
|
||||
res := F.Pipe2(
|
||||
s.repository.FindByID(c.Request.Context(), id),
|
||||
E.Map(mappers.MapUserToDTO),
|
||||
)
|
||||
|
||||
s.repository.FindByID(c.Request.Context(), id).Fold(
|
||||
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.
|
||||
func (s *UserService) GetAllUsers(c *gin.Context) {
|
||||
s.repository.FindAll(c.Request.Context()).Fold(
|
||||
func(err error) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
},
|
||||
func(users []entities.User) {
|
||||
c.JSON(http.StatusOK, users)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// UpdateUser handles updating an existing user.
|
||||
func (s *UserService) UpdateUser(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
var userUpdate entities.UserUpdate
|
||||
|
||||
if err := c.ShouldBindJSON(&userUpdate); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
userUpdate.ID = id
|
||||
s.repository.Update(c.Request.Context(), userUpdate).Fold(
|
||||
func(err error) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
},
|
||||
func(user entities.User) {
|
||||
c.JSON(http.StatusOK, user)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// DeleteUser handles deleting a user by their ID.
|
||||
func (s *UserService) DeleteUser(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
s.repository.Delete(c.Request.Context(), id).Fold(
|
||||
func(err error) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
},
|
||||
func(_ entities.User) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "User deleted successfully"})
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"actatempus_backend/internal/infrastructure/config"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
@@ -18,5 +20,13 @@ func (s *Server) Start() error {
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Welcome to ActaTempus!")
|
||||
})
|
||||
return http.ListenAndServe(fmt.Sprintf(":%s", s.cfg.Port), nil)
|
||||
|
||||
r := gin.Default()
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "pong",
|
||||
})
|
||||
})
|
||||
|
||||
return r.Run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user