Base Project
This commit is contained in:
128
internal/services/auth_service.go
Normal file
128
internal/services/auth_service.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"wm-backend/global"
|
||||
"wm-backend/internal/models"
|
||||
"wm-backend/internal/models/requests"
|
||||
"wm-backend/internal/models/responses"
|
||||
"wm-backend/internal/repositories"
|
||||
"wm-backend/pkg/helper"
|
||||
"wm-backend/response"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// Register handles user registration.
|
||||
// It validates the request body, checks for duplicate email,
|
||||
// hashes the password, and creates the user in the database.
|
||||
//
|
||||
// @Summary Register a new user
|
||||
// @Description Register with email, username and password
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body requests.BodyRegisterRequest true "Register request"
|
||||
// @Success 201 {object} response.SuccessResponse{data=responses.BodyRegisterResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 409 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /auth/register [post]
|
||||
func Register(c *gin.Context) error {
|
||||
// 1. Bind & validate request body
|
||||
requestBody := requests.BodyRegisterRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 2. Check if user already exists by email
|
||||
existingUser, err := repositories.GetUserByEmail(c.Request.Context(), global.Queries, requestBody.Email)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError)
|
||||
log.Error().Err(err).Msg("Error checking existing user")
|
||||
return nil
|
||||
}
|
||||
if existingUser != nil {
|
||||
response.ConflictError(c, http.StatusConflict, "Email already registered")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 3. Hash the password
|
||||
hashedPassword, err := helper.HashPassword(requestBody.Password)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError)
|
||||
log.Error().Err(err).Msg("Password hashing error")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 4. Create user in database
|
||||
userID, err := repositories.CreateUser(c.Request.Context(), global.Queries, db.CreateUserParams{
|
||||
Username: requestBody.Username,
|
||||
Email: requestBody.Email,
|
||||
PasswordHash: hashedPassword,
|
||||
FullName: pgtype.Text{String: requestBody.FullName, Valid: requestBody.FullName != ""},
|
||||
CreatedBy: pgtype.Text{String: requestBody.Username, Valid: true},
|
||||
})
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError)
|
||||
log.Error().Err(err).Msg("Error creating user")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 5. Return success response
|
||||
response.Created(c, "User registered successfully", responses.BodyRegisterResponse{
|
||||
ID: userID,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func Login(c *gin.Context) error {
|
||||
loginRequestBody := requests.BodyLoginRequest{}
|
||||
if helper.IsShouldBindJSON(c, &loginRequestBody) {
|
||||
return nil
|
||||
}
|
||||
var user *models.User
|
||||
var err error
|
||||
if helper.IsEmail(loginRequestBody.Username) {
|
||||
user, err = repositories.GetUserByEmail(c.Request.Context(), global.Queries, loginRequestBody.Username)
|
||||
} else {
|
||||
user, err = repositories.GetUserByUsername(c.Request.Context(), global.Queries, loginRequestBody.Username)
|
||||
}
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError)
|
||||
log.Error().Err(err).Msg("Error finding user")
|
||||
return nil
|
||||
}
|
||||
if user == nil {
|
||||
response.UnauthorizedError(c, http.StatusUnauthorized, "Invalid credentials")
|
||||
return nil
|
||||
}
|
||||
// 2. Check if user is active
|
||||
if !user.IsActive {
|
||||
response.UnauthorizedError(c, http.StatusUnauthorized, "Account is disabled")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 3. Compare password
|
||||
if err := helper.ComparePassword(loginRequestBody.Password, user.PasswordHash); err != nil {
|
||||
response.UnauthorizedError(c, http.StatusUnauthorized, "Invalid credentials")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 4. Generate JWT token
|
||||
token, err := helper.GenerateToken(user.ID)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError)
|
||||
log.Error().Err(err).Msg("Error generating token")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 5. Return token
|
||||
response.Ok(c, "Login successful", responses.BodyLoginResponse{
|
||||
Token: token,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
18
internal/services/check_service.go
Normal file
18
internal/services/check_service.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"wm-backend/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// @Summary Health check
|
||||
// @Description Check server is running
|
||||
// @Tags health
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} map[string]string
|
||||
// @Router /ping [get]
|
||||
func PingHandler(c *gin.Context) {
|
||||
response.Ok(c, "Ponggg", nil)
|
||||
}
|
||||
58
internal/services/role_service.go
Normal file
58
internal/services/role_service.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"wm-backend/global"
|
||||
"wm-backend/internal/models"
|
||||
"wm-backend/internal/models/requests"
|
||||
"wm-backend/internal/models/responses"
|
||||
"wm-backend/internal/repositories"
|
||||
"wm-backend/pkg/helper"
|
||||
"wm-backend/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RoleCreate(c *gin.Context) error {
|
||||
requestBody := requests.CreateRoleRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
roleModel := &models.Role{
|
||||
Name: requestBody.Name,
|
||||
Description: requestBody.Description,
|
||||
CreatedBy: "",
|
||||
}
|
||||
role, err := repositories.CreateRole(c.Request.Context(), global.Queries, *roleModel)
|
||||
if err != nil {
|
||||
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to create role")
|
||||
return nil
|
||||
}
|
||||
// if isUniqueViolation(err) {
|
||||
// response.BadRequest(c, "Role name already exists")
|
||||
// } else {
|
||||
// response.InternalServerError(c, "Failed to create role")
|
||||
// }
|
||||
// Return success response with the created role
|
||||
response.Created(c, "Role created successfully", &responses.BodyRoleResponse{
|
||||
ID: role.ID,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func RoleList(c *gin.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func RoleGetByID(c *gin.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func RoleUpdate(c *gin.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func RoleDelete(c *gin.Context) error {
|
||||
return nil
|
||||
}
|
||||
171
internal/services/warehouse_service.go
Normal file
171
internal/services/warehouse_service.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"wm-backend/global"
|
||||
"wm-backend/internal/models"
|
||||
"wm-backend/internal/models/requests"
|
||||
"wm-backend/internal/models/responses"
|
||||
"wm-backend/internal/repositories"
|
||||
"wm-backend/pkg/helper"
|
||||
"wm-backend/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// WareHouseCreate creates a new warehouse.
|
||||
// It validates the request body and creates the warehouse in the database.
|
||||
//
|
||||
// @Summary Create a new warehouse
|
||||
// @Description Create a new warehouse with the provided details
|
||||
// @Tags warehouse
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body requests.CreateWarehouseRequest true "Warehouse request body"
|
||||
// @Success 201 {object} response.SuccessResponse{data=responses.CreateWarehouseResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/warehouses [post]
|
||||
func WareHouseCreate(c *gin.Context) error {
|
||||
requestBody := requests.CreateWarehouseRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
warehouseModel := &models.Warehouse{
|
||||
Name: requestBody.Name,
|
||||
Description: requestBody.Description,
|
||||
Address: requestBody.Address,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
warehouse, err := repositories.CreateWareHouse(c.Request.Context(), global.Queries, *warehouseModel)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to create warehouse")
|
||||
return nil
|
||||
}
|
||||
response.Created(c, "Warehouse created successfully", &responses.CreateWarehouseResponse{
|
||||
ID: warehouse.ID,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// WareHouseGetByID retrieves a single warehouse by its ID.
|
||||
//
|
||||
// @Summary Get warehouse by ID
|
||||
// @Description Retrieve a single warehouse using its unique identifier
|
||||
// @Tags warehouse
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Warehouse ID"
|
||||
// @Success 200 {object} response.SuccessResponse{data=models.Warehouse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 404 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/warehouses/{id} [get]
|
||||
func WareHouseGetByID(c *gin.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.BadRequestError(c, http.StatusBadRequest, "Invalid ID")
|
||||
return nil
|
||||
}
|
||||
warehouse, err := repositories.GetWarehouseByID(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
response.NotFoundError(c, http.StatusNotFound, "Warehouse not found")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", warehouse)
|
||||
return nil
|
||||
}
|
||||
|
||||
// WareHouseList retrieves all warehouses.
|
||||
//
|
||||
// @Summary List all warehouses
|
||||
// @Description Retrieve a list of all warehouses ordered by creation date
|
||||
// @Tags warehouse
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.SuccessResponse{data=[]models.Warehouse}
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/warehouses [get]
|
||||
func WareHouseList(c *gin.Context) error {
|
||||
warehouses, err := repositories.ListWarehouses(c.Request.Context(), global.Queries)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to list warehouses")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", warehouses)
|
||||
return nil
|
||||
}
|
||||
|
||||
// WareHouseUpdate updates an existing warehouse by its ID.
|
||||
// It validates the request body and updates the warehouse in the database.
|
||||
//
|
||||
// @Summary Update warehouse
|
||||
// @Description Update an existing warehouse by its ID with the provided details
|
||||
// @Tags warehouse
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Warehouse ID"
|
||||
// @Param body body requests.UpdateWarehouseRequest true "Warehouse request body"
|
||||
// @Success 200 {object} response.SuccessResponse{data=responses.UpdateWarehouseResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/warehouses/{id} [put]
|
||||
func WareHouseUpdate(c *gin.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.BadRequestError(c, http.StatusBadRequest, "Invalid ID")
|
||||
return nil
|
||||
}
|
||||
requestBody := requests.UpdateWarehouseRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
warehouseModel := &models.Warehouse{
|
||||
ID: id,
|
||||
Name: requestBody.Name,
|
||||
Description: requestBody.Description,
|
||||
Address: requestBody.Address,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
warehouse, err := repositories.UpdateWarehouse(c.Request.Context(), global.Queries, *warehouseModel)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to update warehouse")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Warehouse updated successfully", &responses.UpdateWarehouseResponse{
|
||||
ID: warehouse.ID,
|
||||
Name: warehouse.Name,
|
||||
Description: warehouse.Description,
|
||||
Address: warehouse.Address,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// WareHouseDelete deletes a warehouse by its ID.
|
||||
//
|
||||
// @Summary Delete warehouse
|
||||
// @Description Delete a warehouse by its unique identifier
|
||||
// @Tags warehouse
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Warehouse ID"
|
||||
// @Success 200 {object} response.SuccessResponse
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/warehouses/{id} [delete]
|
||||
func WareHouseDelete(c *gin.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.BadRequestError(c, http.StatusBadRequest, "Invalid ID")
|
||||
return nil
|
||||
}
|
||||
err = repositories.DeleteWarehouse(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete warehouse")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Đã xóa thành công", nil)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user