64 lines
1.4 KiB
Go
64 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 DevicePortUseCase interface {
|
|
CreateDevicePort(devicePort req.DevicePort) error
|
|
GetAllDevicePort() ([]entity.DevicePort, error)
|
|
|
|
GetByID(id string) (entity.DevicePort, error)
|
|
}
|
|
|
|
type devicePortUseCase struct {
|
|
devicePortRepo repository.DevicePortRepo
|
|
validate *validator.Validate
|
|
}
|
|
|
|
func NewDevicePortUseCase(devicePortRepo repository.DevicePortRepo) DevicePortUseCase {
|
|
return &devicePortUseCase{
|
|
devicePortRepo: devicePortRepo,
|
|
validate: validator.New(),
|
|
}
|
|
}
|
|
|
|
func (u *devicePortUseCase) CreateDevicePort(devicePort req.DevicePort) error {
|
|
err := u.validate.Struct(devicePort)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
newDevicePort := entity.DevicePort{
|
|
ID: uuid.New(),
|
|
DeviceID: devicePort.DeviceID,
|
|
PortNumber: devicePort.PortNumber,
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
|
|
return u.devicePortRepo.Post(newDevicePort)
|
|
}
|
|
|
|
func (u *devicePortUseCase) GetAllDevicePort() ([]entity.DevicePort, error) {
|
|
devicePorts, err := u.devicePortRepo.GetAll()
|
|
if err != nil {
|
|
return devicePorts, err
|
|
}
|
|
return devicePorts, nil
|
|
}
|
|
|
|
func (u *devicePortUseCase) GetByID(id string) (entity.DevicePort, error) {
|
|
devicePort, err := u.devicePortRepo.GetByID(id)
|
|
if err != nil {
|
|
return devicePort, err
|
|
}
|
|
|
|
return devicePort, nil
|
|
} |