98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
package usecase
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
"users_management/m/model/dto/req"
|
|
"users_management/m/model/entity"
|
|
"users_management/m/repository"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type FishboneUseCase interface {
|
|
CreateFishbone(fishbone req.FishboneDTO) error
|
|
GetAllFishbone() ([]entity.Fishbone, error)
|
|
GetByID(id uuid.UUID) (entity.Fishbone, error)
|
|
|
|
UpdateFishbone(id uuid.UUID, fishbone req.UpdateFishboneDTO) error
|
|
}
|
|
|
|
type fishboneUsecase struct {
|
|
fishboneRepo repository.FishboneRepo
|
|
validate *validator.Validate
|
|
}
|
|
|
|
func NewFishboneUseCase(fishboneRepo repository.FishboneRepo) FishboneUseCase {
|
|
return &fishboneUsecase{
|
|
fishboneRepo: fishboneRepo,
|
|
validate: validator.New(),
|
|
}
|
|
}
|
|
|
|
func (u *fishboneUsecase) CreateFishbone(fishbone req.FishboneDTO) error {
|
|
err := u.validate.Struct(fishbone)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
newFishbone := entity.Fishbone{
|
|
ID: uuid.New(),
|
|
BackboneID: fishbone.BackboneID,
|
|
DeviceStartID: fishbone.DeviceStartID,
|
|
DeviceEndID: fishbone.DeviceEndID,
|
|
CoreAmount: fishbone.CoreAmount,
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
|
|
return u.fishboneRepo.Post(newFishbone)
|
|
}
|
|
|
|
func (u *fishboneUsecase) GetAllFishbone() ([]entity.Fishbone, error) {
|
|
fishbones, err := u.fishboneRepo.GetAll()
|
|
if err != nil {
|
|
return fishbones, err
|
|
}
|
|
return fishbones, nil
|
|
}
|
|
|
|
func (u *fishboneUsecase) GetByID(id uuid.UUID) (entity.Fishbone, error) {
|
|
fishbone, err := u.fishboneRepo.GetByID(id)
|
|
if err != nil {
|
|
return fishbone, err
|
|
}
|
|
return fishbone, nil
|
|
}
|
|
|
|
func (u *fishboneUsecase) UpdateFishbone(id uuid.UUID, fishbone req.UpdateFishboneDTO) error {
|
|
err := u.validate.Struct(fishbone)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
updates := make(map[string]interface{})
|
|
|
|
if fishbone.BackboneID != nil {
|
|
updates["BackboneID"] = *fishbone.BackboneID
|
|
}
|
|
if fishbone.DeviceStartID != nil {
|
|
updates["DeviceStartID"] = *fishbone.DeviceStartID
|
|
}
|
|
if fishbone.DeviceEndID != nil {
|
|
updates["DeviceEndID"] = *fishbone.DeviceEndID
|
|
}
|
|
if fishbone.CoreAmount != nil {
|
|
updates["CoreAmount"] = *fishbone.CoreAmount
|
|
}
|
|
if len(updates) == 0 {
|
|
return errors.New("no fields to update")
|
|
}
|
|
|
|
updates["UpdatedAt"] = time.Now()
|
|
|
|
return u.fishboneRepo.Update(id, updates)
|
|
}
|
|
|