feat: enhance user service with client name handling in bulk user creation
Deploy Application / deploy (push) Successful in 26s
Details
Deploy Application / deploy (push) Successful in 26s
Details
This commit is contained in:
parent
d1beb610a7
commit
8812a294d8
|
|
@ -86,7 +86,9 @@ func (r *clientRepository) GetByName(ctx context.Context, tx *gorm.DB, name stri
|
||||||
tx = r.db
|
tx = r.db
|
||||||
}
|
}
|
||||||
var client entities.M_Client
|
var client entities.M_Client
|
||||||
if err := tx.WithContext(ctx).Where("name = ?", name).First(&client).Error; err != nil {
|
if err := tx.WithContext(ctx).
|
||||||
|
Where("LOWER(name) = ?", name).
|
||||||
|
First(&client).Error; err != nil {
|
||||||
return entities.M_Client{}, err
|
return entities.M_Client{}, err
|
||||||
}
|
}
|
||||||
return client, nil
|
return client, nil
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,19 @@ type (
|
||||||
}
|
}
|
||||||
|
|
||||||
BulkCreateUserRequest struct {
|
BulkCreateUserRequest struct {
|
||||||
Users []UserCreateRequest `json:"users" binding:"required,dive"`
|
Users []UserBulkCreateRequest `json:"users" binding:"required,dive"`
|
||||||
|
}
|
||||||
|
|
||||||
|
UserBulkCreateRequest struct {
|
||||||
|
Name string `json:"name" form:"name" binding:"required,min=2,max=100"`
|
||||||
|
Username string `json:"username" form:"username" binding:"required,min=2,max=100"`
|
||||||
|
Password string `json:"password" form:"password" binding:"required,min=8"`
|
||||||
|
Gender string `json:"gender" form:"gender" binding:"omitempty,max=10"`
|
||||||
|
Address string `json:"address" form:"address" binding:"omitempty"`
|
||||||
|
Phone string `json:"phone" form:"phone" binding:"omitempty,min=8,max=20"`
|
||||||
|
Email string `json:"email" form:"email" binding:"required,email"`
|
||||||
|
PhotoUrl string `json:"photo_url" form:"photo_url" binding:"omitempty"`
|
||||||
|
ClientName string `json:"client_name" form:"client_name" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
UserResponse struct {
|
UserResponse struct {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
authDto "github.com/Caknoooo/go-gin-clean-starter/modules/auth/dto"
|
authDto "github.com/Caknoooo/go-gin-clean-starter/modules/auth/dto"
|
||||||
authRepo "github.com/Caknoooo/go-gin-clean-starter/modules/auth/repository"
|
authRepo "github.com/Caknoooo/go-gin-clean-starter/modules/auth/repository"
|
||||||
authService "github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
|
authService "github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
|
||||||
|
clientrepository "github.com/Caknoooo/go-gin-clean-starter/modules/client/repository"
|
||||||
rolerepository "github.com/Caknoooo/go-gin-clean-starter/modules/role/repository"
|
rolerepository "github.com/Caknoooo/go-gin-clean-starter/modules/role/repository"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/dto"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/user/dto"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/repository"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/user/repository"
|
||||||
|
|
@ -45,6 +46,7 @@ type UserService interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type userService struct {
|
type userService struct {
|
||||||
|
clientRepository clientrepository.ClientRepository
|
||||||
userRepository repository.UserRepository
|
userRepository repository.UserRepository
|
||||||
roleRepository rolerepository.RoleRepository
|
roleRepository rolerepository.RoleRepository
|
||||||
warehouserepository warehouserepository.WarehouseRepository
|
warehouserepository warehouserepository.WarehouseRepository
|
||||||
|
|
@ -58,22 +60,27 @@ func (s *userService) BulkCreate(ctx context.Context, req dto.BulkCreateUserRequ
|
||||||
users := make([]entities.M_User, len(req.Users))
|
users := make([]entities.M_User, len(req.Users))
|
||||||
|
|
||||||
for i, r := range req.Users {
|
for i, r := range req.Users {
|
||||||
|
clientName := strings.ToLower(strings.TrimSpace(r.ClientName))
|
||||||
|
client, err := s.clientRepository.GetByName(ctx, s.db, clientName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
hashedPassword, err := utils.HashPassword(r.Password)
|
hashedPassword, err := utils.HashPassword(r.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
users[i] = entities.M_User{
|
users[i] = entities.M_User{
|
||||||
Username: r.Username,
|
Username: r.Username,
|
||||||
Email: r.Email,
|
Email: r.Email,
|
||||||
Password: hashedPassword,
|
Password: hashedPassword,
|
||||||
Name: r.Name,
|
Name: r.Name,
|
||||||
Gender: r.Gender,
|
Gender: r.Gender,
|
||||||
Address: r.Address,
|
Address: r.Address,
|
||||||
Phone: r.Phone,
|
Phone: r.Phone,
|
||||||
PhotoUrl: r.PhotoUrl,
|
PhotoUrl: r.PhotoUrl,
|
||||||
ClientID: r.ClientID,
|
ClientID: client.ID,
|
||||||
MaintenanceGroupUserID: r.MaintenanceGroupUserID,
|
// MaintenanceGroupUserID: r.MaintenanceGroupUserID,
|
||||||
LocationID: r.LocationID,
|
// LocationID: r.LocationID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -352,6 +359,7 @@ func NewUserService(
|
||||||
userRepo repository.UserRepository,
|
userRepo repository.UserRepository,
|
||||||
roleRepo rolerepository.RoleRepository,
|
roleRepo rolerepository.RoleRepository,
|
||||||
warehouserepository warehouserepository.WarehouseRepository,
|
warehouserepository warehouserepository.WarehouseRepository,
|
||||||
|
clientRepository clientrepository.ClientRepository,
|
||||||
refreshTokenRepo authRepo.RefreshTokenRepository,
|
refreshTokenRepo authRepo.RefreshTokenRepository,
|
||||||
jwtService authService.JWTService,
|
jwtService authService.JWTService,
|
||||||
db *gorm.DB,
|
db *gorm.DB,
|
||||||
|
|
@ -360,6 +368,7 @@ func NewUserService(
|
||||||
userRepository: userRepo,
|
userRepository: userRepo,
|
||||||
roleRepository: roleRepo,
|
roleRepository: roleRepo,
|
||||||
warehouserepository: warehouserepository,
|
warehouserepository: warehouserepository,
|
||||||
|
clientRepository: clientRepository,
|
||||||
refreshTokenRepository: refreshTokenRepo,
|
refreshTokenRepository: refreshTokenRepo,
|
||||||
jwtService: jwtService,
|
jwtService: jwtService,
|
||||||
db: db,
|
db: db,
|
||||||
|
|
|
||||||
|
|
@ -161,7 +161,7 @@ func RegisterDependencies(injector *do.Injector) {
|
||||||
inventoryTransactionRepository := inventoryTransactionRepo.NewInventoryTransactionRepository(db)
|
inventoryTransactionRepository := inventoryTransactionRepo.NewInventoryTransactionRepository(db)
|
||||||
|
|
||||||
// Service
|
// Service
|
||||||
userServ := userService.NewUserService(userRepository, roleRepository, warehouseRepository, refreshTokenRepository, jwtService, db)
|
userServ := userService.NewUserService(userRepository, roleRepository, warehouseRepository, clientRepository, refreshTokenRepository, jwtService, db)
|
||||||
productService := productService.NewProductService(productRepository, db)
|
productService := productService.NewProductService(productRepository, db)
|
||||||
roleServ := roleService.NewRoleService(roleRepository, refreshTokenRepository, jwtService, userServ, db)
|
roleServ := roleService.NewRoleService(roleRepository, refreshTokenRepository, jwtService, userServ, db)
|
||||||
menuSvc := menuService.NewMenuService(menuRepository, jwtService, db)
|
menuSvc := menuService.NewMenuService(menuRepository, jwtService, db)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue