NAM-APJATEL-BACKEND/usecase/fishbone_usecase.go

65 lines
1.4 KiB
Go

package usecase
import (
"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 string) (entity.Fishbone, 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 string) (entity.Fishbone, error) {
fishbone, err := u.fishboneRepo.GetByID(id)
if err != nil {
return fishbone, err
}
return fishbone, nil
}