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
|
||||
}
|
||||
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 client, nil
|
||||
|
|
|
|||
|
|
@ -65,7 +65,19 @@ type (
|
|||
}
|
||||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import (
|
|||
authDto "github.com/Caknoooo/go-gin-clean-starter/modules/auth/dto"
|
||||
authRepo "github.com/Caknoooo/go-gin-clean-starter/modules/auth/repository"
|
||||
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"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/dto"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/repository"
|
||||
|
|
@ -45,6 +46,7 @@ type UserService interface {
|
|||
}
|
||||
|
||||
type userService struct {
|
||||
clientRepository clientrepository.ClientRepository
|
||||
userRepository repository.UserRepository
|
||||
roleRepository rolerepository.RoleRepository
|
||||
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))
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
users[i] = entities.M_User{
|
||||
Username: r.Username,
|
||||
Email: r.Email,
|
||||
Password: hashedPassword,
|
||||
Name: r.Name,
|
||||
Gender: r.Gender,
|
||||
Address: r.Address,
|
||||
Phone: r.Phone,
|
||||
PhotoUrl: r.PhotoUrl,
|
||||
ClientID: r.ClientID,
|
||||
MaintenanceGroupUserID: r.MaintenanceGroupUserID,
|
||||
LocationID: r.LocationID,
|
||||
Username: r.Username,
|
||||
Email: r.Email,
|
||||
Password: hashedPassword,
|
||||
Name: r.Name,
|
||||
Gender: r.Gender,
|
||||
Address: r.Address,
|
||||
Phone: r.Phone,
|
||||
PhotoUrl: r.PhotoUrl,
|
||||
ClientID: client.ID,
|
||||
// MaintenanceGroupUserID: r.MaintenanceGroupUserID,
|
||||
// LocationID: r.LocationID,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -352,6 +359,7 @@ func NewUserService(
|
|||
userRepo repository.UserRepository,
|
||||
roleRepo rolerepository.RoleRepository,
|
||||
warehouserepository warehouserepository.WarehouseRepository,
|
||||
clientRepository clientrepository.ClientRepository,
|
||||
refreshTokenRepo authRepo.RefreshTokenRepository,
|
||||
jwtService authService.JWTService,
|
||||
db *gorm.DB,
|
||||
|
|
@ -360,6 +368,7 @@ func NewUserService(
|
|||
userRepository: userRepo,
|
||||
roleRepository: roleRepo,
|
||||
warehouserepository: warehouserepository,
|
||||
clientRepository: clientRepository,
|
||||
refreshTokenRepository: refreshTokenRepo,
|
||||
jwtService: jwtService,
|
||||
db: db,
|
||||
|
|
|
|||
|
|
@ -161,7 +161,7 @@ func RegisterDependencies(injector *do.Injector) {
|
|||
inventoryTransactionRepository := inventoryTransactionRepo.NewInventoryTransactionRepository(db)
|
||||
|
||||
// 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)
|
||||
roleServ := roleService.NewRoleService(roleRepository, refreshTokenRepository, jwtService, userServ, db)
|
||||
menuSvc := menuService.NewMenuService(menuRepository, jwtService, db)
|
||||
|
|
|
|||
Loading…
Reference in New Issue