Base Project
This commit is contained in:
47
internal/initialization/postgresql.go
Normal file
47
internal/initialization/postgresql.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package initialization
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"wm-backend/internal/models"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func ConnectPostgreSQL(config *models.Config) (*pgxpool.Pool, error) {
|
||||
connStr := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
|
||||
config.Database.Username, config.Database.Password, config.Database.Host, config.Database.Port, config.Database.Name)
|
||||
|
||||
var pool *pgxpool.Pool
|
||||
var err error
|
||||
|
||||
maxRetries := 5
|
||||
for i := range maxRetries {
|
||||
poolConfig, parseErr := pgxpool.ParseConfig(connStr)
|
||||
if parseErr != nil {
|
||||
log.Error().Err(parseErr).Msgf("Failed to parse DB config (attempt %d/%d)", i+1, maxRetries)
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
pool, err = pgxpool.NewWithConfig(context.Background(), poolConfig)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Failed to connect to PostgreSQL (attempt %d/%d)", i+1, maxRetries)
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
err = pool.Ping(context.Background())
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Failed to ping PostgreSQL (attempt %d/%d)", i+1, maxRetries)
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Info().Msg("Successfully connected to PostgreSQL")
|
||||
return pool, nil
|
||||
}
|
||||
return nil, fmt.Errorf("failed to connect to PostgreSQL after %d attempts: %v", maxRetries, err)
|
||||
}
|
||||
39
internal/initialization/redis.go
Normal file
39
internal/initialization/redis.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package initialization
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"wm-backend/internal/models"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func ConnectRedis(cfg models.Config) (*redis.Client, error) {
|
||||
var rdb *redis.Client
|
||||
var pong string
|
||||
var err error
|
||||
|
||||
maxRetries := 10
|
||||
for range maxRetries {
|
||||
rdb = redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%s", cfg.Cache.Host, cfg.Cache.Port),
|
||||
Password: cfg.Cache.Password,
|
||||
DB: 0,
|
||||
})
|
||||
|
||||
pong, err = rdb.Ping(context.Background()).Result()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error connecting to Redis")
|
||||
log.Error().Msg("Retrying in 5 seconds...")
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
} else {
|
||||
log.Info().Msg("CONNECTED TO REDIS:" + pong + "🥩")
|
||||
return rdb, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("failed to connect to Redis after %d attempts: %v", maxRetries, err)
|
||||
}
|
||||
28
internal/mapper/role_mapper.go
Normal file
28
internal/mapper/role_mapper.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package mapper
|
||||
|
||||
import (
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
// ToDomainRole maps a SQLC-generated Role to the domain Role model.
|
||||
func ToDomainRole(r db.Role) *models.Role {
|
||||
return &models.Role{
|
||||
ID: r.ID.String(),
|
||||
Name: r.Name,
|
||||
Description: r.Description.String,
|
||||
CreatedAt: r.CreatedAt.Time,
|
||||
CreatedBy: r.CreatedBy.String,
|
||||
}
|
||||
}
|
||||
|
||||
// ToModelRole maps a domain Role model to the parameters needed for creating a Role in the database.
|
||||
func ToModelRole(r *models.Role) *db.CreateRoleParams {
|
||||
return &db.CreateRoleParams{
|
||||
Name: r.Name,
|
||||
Description: pgtype.Text{String: r.Description, Valid: r.Description != ""},
|
||||
CreatedBy: pgtype.Text{String: r.CreatedBy, Valid: r.CreatedBy != ""},
|
||||
}
|
||||
}
|
||||
21
internal/mapper/user_mapper.go
Normal file
21
internal/mapper/user_mapper.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package mapper
|
||||
|
||||
import (
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
)
|
||||
|
||||
// toDomainUser maps a SQLC-generated User to the domain User model.
|
||||
func ToDomainUser(u db.User) *models.User {
|
||||
return &models.User{
|
||||
ID: u.ID.String(),
|
||||
Username: u.Username,
|
||||
Email: u.Email,
|
||||
FullName: u.FullName.String,
|
||||
PasswordHash: u.PasswordHash,
|
||||
IsActive: u.IsActive.Bool,
|
||||
CreatedAt: u.CreatedAt.Time,
|
||||
UpdatedAt: u.UpdatedAt.Time,
|
||||
CreatedBy: u.CreatedBy.String,
|
||||
}
|
||||
}
|
||||
51
internal/mapper/warehouse_mapper.go
Normal file
51
internal/mapper/warehouse_mapper.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package mapper
|
||||
|
||||
import (
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
// ToDomainWarehouse maps a SQLC-generated Warehouse to the domain Warehouse model.
|
||||
func ToDomainWarehouse(r db.Warehouse) *models.Warehouse {
|
||||
return &models.Warehouse{
|
||||
ID: r.ID,
|
||||
Name: r.Name,
|
||||
Description: r.Description.String,
|
||||
Address: r.Address.String,
|
||||
CreatedAt: r.CreatedAt,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ToModelWarehouse(r *models.Warehouse) *db.CreateWarehouseParams {
|
||||
return &db.CreateWarehouseParams{
|
||||
Name: r.Name,
|
||||
Description: pgtype.Text{
|
||||
String: r.Description,
|
||||
Valid: r.Description != "",
|
||||
},
|
||||
Address: pgtype.Text{
|
||||
String: r.Address,
|
||||
Valid: r.Address != "",
|
||||
},
|
||||
CreatedAt: r.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ToUpdateModelWarehouse(r *models.Warehouse) *db.UpdateWarehouseParams {
|
||||
return &db.UpdateWarehouseParams{
|
||||
Name: r.Name,
|
||||
Description: pgtype.Text{
|
||||
String: r.Description,
|
||||
Valid: r.Description != "",
|
||||
},
|
||||
Address: pgtype.Text{
|
||||
String: r.Address,
|
||||
Valid: r.Address != "",
|
||||
},
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
ID: r.ID,
|
||||
}
|
||||
}
|
||||
10
internal/middlewares/auth_middleware.go
Normal file
10
internal/middlewares/auth_middleware.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package middlewares
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func AuthMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 1. Get the Authorization header
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
34
internal/middlewares/logging_middleware.go
Normal file
34
internal/middlewares/logging_middleware.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func LoggingMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
|
||||
log.Info().
|
||||
Str("type", "request").
|
||||
Str("method", c.Request.Method).
|
||||
Str("path", c.Request.URL.Path).
|
||||
Str("ip", c.ClientIP()).
|
||||
Str("query", c.Request.URL.RawQuery).
|
||||
Time("time", start).
|
||||
Msg("incoming request")
|
||||
|
||||
c.Next()
|
||||
|
||||
log.Info().
|
||||
Str("type", "response").
|
||||
Str("method", c.Request.Method).
|
||||
Str("path", c.Request.URL.Path).
|
||||
Int("status", c.Writer.Status()).
|
||||
Dur("latency", time.Since(start)).
|
||||
Time("time", time.Now()).
|
||||
Msg("outgoing response")
|
||||
}
|
||||
}
|
||||
43
internal/models/config_model.go
Normal file
43
internal/models/config_model.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package models
|
||||
|
||||
type Config struct {
|
||||
Server ServerConfig
|
||||
Database DatabaseConfig
|
||||
Cache CacheConfig
|
||||
Admin AdminConfig
|
||||
JWT JWTConfig
|
||||
}
|
||||
|
||||
type AdminConfig struct {
|
||||
Username string
|
||||
Email string
|
||||
Password string
|
||||
FullName string
|
||||
}
|
||||
|
||||
type JWTConfig struct {
|
||||
SecretKey string
|
||||
ExpirationHours int
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Host string
|
||||
Port string
|
||||
Username string
|
||||
Password string
|
||||
Name string
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Host string
|
||||
Port string
|
||||
PortFrontend string
|
||||
KeyPassword string
|
||||
}
|
||||
|
||||
type CacheConfig struct {
|
||||
Username string
|
||||
Password string
|
||||
Host string
|
||||
Port string
|
||||
}
|
||||
1
internal/models/jwt_model.go
Normal file
1
internal/models/jwt_model.go
Normal file
@@ -0,0 +1 @@
|
||||
package models
|
||||
13
internal/models/requests/auth_request.go
Normal file
13
internal/models/requests/auth_request.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package requests
|
||||
|
||||
type BodyRegisterRequest struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Email string `json:"email" binding:"required,email"`
|
||||
FullName string `json:"fullName"`
|
||||
Password string `json:"password" binding:"required,min=8"`
|
||||
}
|
||||
|
||||
type BodyLoginRequest struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
6
internal/models/requests/role_request.go
Normal file
6
internal/models/requests/role_request.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package requests
|
||||
|
||||
type CreateRoleRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
13
internal/models/requests/warehouse_request.go
Normal file
13
internal/models/requests/warehouse_request.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package requests
|
||||
|
||||
type CreateWarehouseRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
Address string `json:"address" binding:"required"`
|
||||
}
|
||||
|
||||
type UpdateWarehouseRequest struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
8
internal/models/responses/auth_response.go
Normal file
8
internal/models/responses/auth_response.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package responses
|
||||
|
||||
type BodyRegisterResponse struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
type BodyLoginResponse struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
5
internal/models/responses/role_response.go
Normal file
5
internal/models/responses/role_response.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package responses
|
||||
|
||||
type BodyRoleResponse struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
12
internal/models/responses/warehouse_response.go
Normal file
12
internal/models/responses/warehouse_response.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package responses
|
||||
|
||||
type CreateWarehouseResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
type UpdateWarehouseResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
11
internal/models/role_model.go
Normal file
11
internal/models/role_model.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type Role struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
}
|
||||
17
internal/models/user_model.go
Normal file
17
internal/models/user_model.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
FullName string `json:"fullName"`
|
||||
PasswordHash string `json:"-"`
|
||||
IsActive bool `json:"isActive"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
}
|
||||
12
internal/models/warehouse_model.go
Normal file
12
internal/models/warehouse_model.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type Warehouse struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Address string `json:"address"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
45
internal/repositories/auth_repository.go
Normal file
45
internal/repositories/auth_repository.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"wm-backend/internal/mapper"
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// GetUserByEmail retrieves a user by their email address using SQLC-generated queries.
|
||||
// Returns nil, nil if no user is found.
|
||||
func GetUserByEmail(ctx context.Context, queries *db.Queries, email string) (*models.User, error) {
|
||||
user, err := queries.GetUserByEmail(ctx, email)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return mapper.ToDomainUser(user), nil
|
||||
}
|
||||
|
||||
func GetUserByUsername(ctx context.Context, queries *db.Queries, username string) (*models.User, error) {
|
||||
user, err := queries.GetUserByUsername(ctx, username)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return mapper.ToDomainUser(user), nil
|
||||
}
|
||||
|
||||
// CreateUser inserts a new user using SQLC-generated queries.
|
||||
// Returns the created user's ID as a string.
|
||||
func CreateUser(ctx context.Context, queries *db.Queries, params db.CreateUserParams) (string, error) {
|
||||
id, err := queries.CreateUser(ctx, params)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id.String(), nil
|
||||
}
|
||||
1
internal/repositories/redis/permission_redis.go
Normal file
1
internal/repositories/redis/permission_redis.go
Normal file
@@ -0,0 +1 @@
|
||||
package redis
|
||||
16
internal/repositories/role_repository.go
Normal file
16
internal/repositories/role_repository.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"wm-backend/internal/mapper"
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
)
|
||||
|
||||
func CreateRole(ctx context.Context, queries *db.Queries, body models.Role) (models.Role, error) {
|
||||
role, err := queries.CreateRole(ctx, *mapper.ToModelRole(&body))
|
||||
if err != nil {
|
||||
return models.Role{}, err
|
||||
}
|
||||
return *mapper.ToDomainRole(role), nil
|
||||
}
|
||||
48
internal/repositories/warehouse_repository.go
Normal file
48
internal/repositories/warehouse_repository.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"wm-backend/internal/mapper"
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
)
|
||||
|
||||
func CreateWareHouse(ctx context.Context, queries *db.Queries, body models.Warehouse) (models.Warehouse, error) {
|
||||
warehouse, err := queries.CreateWarehouse(ctx, *mapper.ToModelWarehouse(&body))
|
||||
if err != nil {
|
||||
return models.Warehouse{}, err
|
||||
}
|
||||
return *mapper.ToDomainWarehouse(warehouse), nil
|
||||
}
|
||||
|
||||
func GetWarehouseByID(ctx context.Context, queries *db.Queries, id int64) (models.Warehouse, error) {
|
||||
result, err := queries.GetWarehouseByID(ctx, id)
|
||||
if err != nil {
|
||||
return models.Warehouse{}, err
|
||||
}
|
||||
return *mapper.ToDomainWarehouse(result), nil
|
||||
}
|
||||
|
||||
func ListWarehouses(ctx context.Context, queries *db.Queries) ([]models.Warehouse, error) {
|
||||
results, err := queries.ListWarehouses(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var items []models.Warehouse
|
||||
for _, r := range results {
|
||||
items = append(items, *mapper.ToDomainWarehouse(r))
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func UpdateWarehouse(ctx context.Context, queries *db.Queries, body models.Warehouse) (models.Warehouse, error) {
|
||||
result, err := queries.UpdateWarehouse(ctx, *mapper.ToUpdateModelWarehouse(&body))
|
||||
if err != nil {
|
||||
return models.Warehouse{}, err
|
||||
}
|
||||
return *mapper.ToDomainWarehouse(result), nil
|
||||
}
|
||||
|
||||
func DeleteWarehouse(ctx context.Context, queries *db.Queries, id int64) error {
|
||||
return queries.DeleteWarehouse(ctx, id)
|
||||
}
|
||||
45
internal/routers/router.go
Normal file
45
internal/routers/router.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"os"
|
||||
"wm-backend/configs/constants"
|
||||
_ "wm-backend/docs/swagger"
|
||||
"wm-backend/internal/middlewares"
|
||||
"wm-backend/internal/services"
|
||||
"wm-backend/pkg/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
)
|
||||
|
||||
func NewRouter() *gin.Engine {
|
||||
nodeEnv := os.Getenv("ENV")
|
||||
|
||||
if nodeEnv != constants.DevEnvironment {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
r := gin.Default()
|
||||
r.Use(middlewares.LoggingMiddleware())
|
||||
v1 := r.Group(constants.API_VERSION_1)
|
||||
{
|
||||
auth := v1.Group(constants.API_GROUP_AUTH)
|
||||
{
|
||||
auth.POST(constants.API_PATH_AUTH_REGISTER, utils.AsyncHandler(services.Register))
|
||||
auth.POST(constants.API_PATH_AUTH_LOGIN, utils.AsyncHandler(services.Login))
|
||||
}
|
||||
|
||||
warehouse := v1.Group(constants.API_GROUP_WAREHOUSE)
|
||||
{
|
||||
warehouse.GET("", utils.AsyncHandler(services.WareHouseList))
|
||||
warehouse.GET("/:id", utils.AsyncHandler(services.WareHouseGetByID))
|
||||
warehouse.POST("", utils.AsyncHandler(services.WareHouseCreate))
|
||||
warehouse.PUT("/:id", utils.AsyncHandler(services.WareHouseUpdate))
|
||||
warehouse.DELETE("/:id", utils.AsyncHandler(services.WareHouseDelete))
|
||||
}
|
||||
}
|
||||
|
||||
r.GET(constants.API_PATH_PING, services.PingHandler)
|
||||
r.GET(constants.API_PATH_DOCS, ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
return r
|
||||
}
|
||||
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