Refactor user domain to utilize string IDs and integrate MongoDB ORM

- Updated User entity to embed MongoDB model and replace uuid.UUID with string for ID fields.
- Modified repository methods to accept string IDs instead of uuid.UUID, enhancing compatibility with MongoDB.
- Refactored service and handler methods to align with the new ID type, ensuring consistent usage across the user management functionality.
- Improved response handling in UserListResponse to directly use string IDs.
- Streamlined index creation in the user repository using the MongoDB ORM for better maintainability.
This commit is contained in:
n.nakhostin
2025-08-11 13:45:46 +03:30
parent 8c1e593686
commit 08cf927294
6 changed files with 252 additions and 355 deletions
+6 -31
View File
@@ -7,7 +7,6 @@ import (
"tm/pkg/response"
"github.com/asaskevich/govalidator"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
)
@@ -366,12 +365,8 @@ func (h *Handler) ListUsers(c echo.Context) error {
// @Router /admin/v1/users/{id} [get]
func (h *Handler) GetUserByID(c echo.Context) error {
idStr := c.Param("id")
userID, err := uuid.Parse(idStr)
if err != nil {
return response.BadRequest(c, "Invalid user ID", "")
}
user, err := h.service.GetUserByID(c.Request().Context(), userID)
user, err := h.service.GetUserByID(c.Request().Context(), idStr)
if err != nil {
return response.NotFound(c, "User not found")
}
@@ -403,10 +398,6 @@ func (h *Handler) UpdateUser(c echo.Context) error {
}
idStr := c.Param("id")
userID, err := uuid.Parse(idStr)
if err != nil {
return response.BadRequest(c, "Invalid user ID", "")
}
var form UpdateUserForm
if err := c.Bind(&form); err != nil {
@@ -419,7 +410,7 @@ func (h *Handler) UpdateUser(c echo.Context) error {
}
// Call service
user, err := h.service.UpdateUser(c.Request().Context(), userID, &form, &currentUserID)
user, err := h.service.UpdateUser(c.Request().Context(), idStr, &form, &currentUserID)
if err != nil {
return response.BadRequest(c, err.Error(), "")
}
@@ -450,12 +441,8 @@ func (h *Handler) DeleteUser(c echo.Context) error {
}
idStr := c.Param("id")
userID, err := uuid.Parse(idStr)
if err != nil {
return response.BadRequest(c, "Invalid user ID", "")
}
err = h.service.DeleteUser(c.Request().Context(), userID, &currentUserID)
err = h.service.DeleteUser(c.Request().Context(), idStr, &currentUserID)
if err != nil {
return response.BadRequest(c, err.Error(), "")
}
@@ -488,10 +475,6 @@ func (h *Handler) UpdateUserStatus(c echo.Context) error {
}
idStr := c.Param("id")
userID, err := uuid.Parse(idStr)
if err != nil {
return response.BadRequest(c, "Invalid user ID", "")
}
var form UpdateUserStatusForm
if err := c.Bind(&form); err != nil {
@@ -504,7 +487,7 @@ func (h *Handler) UpdateUserStatus(c echo.Context) error {
}
// Call service
err = h.service.UpdateUserStatus(c.Request().Context(), userID, &form, &currentUserID)
err = h.service.UpdateUserStatus(c.Request().Context(), idStr, &form, &currentUserID)
if err != nil {
return response.BadRequest(c, err.Error(), "")
}
@@ -538,10 +521,6 @@ func (h *Handler) UpdateUserRole(c echo.Context) error {
}
idStr := c.Param("id")
userID, err := uuid.Parse(idStr)
if err != nil {
return response.BadRequest(c, "Invalid user ID", "")
}
var form UpdateUserRoleForm
if err := c.Bind(&form); err != nil {
@@ -554,7 +533,7 @@ func (h *Handler) UpdateUserRole(c echo.Context) error {
}
// Call service
err = h.service.UpdateUserRole(c.Request().Context(), userID, &form, &currentUserID)
err = h.service.UpdateUserRole(c.Request().Context(), idStr, &form, &currentUserID)
if err != nil {
return response.BadRequest(c, err.Error(), "")
}
@@ -582,10 +561,6 @@ func (h *Handler) UpdateUserRole(c echo.Context) error {
// @Router /admin/v1/users/company/{company_id} [get]
func (h *Handler) GetUsersByCompanyID(c echo.Context) error {
companyIDStr := c.Param("company_id")
companyID, err := uuid.Parse(companyIDStr)
if err != nil {
return response.BadRequest(c, "Invalid company ID", "")
}
// Get query parameters
limitStr := c.QueryParam("limit")
@@ -608,7 +583,7 @@ func (h *Handler) GetUsersByCompanyID(c echo.Context) error {
}
// Call service
users, total, err := h.service.GetUsersByCompanyID(c.Request().Context(), companyID, limit, offset)
users, total, err := h.service.GetUsersByCompanyID(c.Request().Context(), companyIDStr, limit, offset)
if err != nil {
return response.InternalServerError(c, "Failed to get users by company")
}