126 lines
3.1 KiB
Go
126 lines
3.1 KiB
Go
package usecase
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
"users_management/m/model/dto/req"
|
|
"users_management/m/model/dto/res"
|
|
"users_management/m/model/entity"
|
|
"users_management/m/repository"
|
|
"users_management/m/utils/helper"
|
|
"users_management/m/utils/service"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type DeviceUseCase interface {
|
|
CreateDevice(device req.DeviceDTO) error
|
|
GetAllDevices() ([]res.DeviceResponse, error)
|
|
|
|
GetByID(id uuid.UUID) (res.DeviceResponse, error)
|
|
UpdateDevice(id uuid.UUID, device req.UpdateDeviceDTO) error
|
|
GetByType(deviceType string) ([]res.DeviceTypeResponse, error)
|
|
}
|
|
|
|
type deviceUseCase struct {
|
|
deviceRepo repository.DevicesRepo
|
|
validate *validator.Validate
|
|
geocoder service.GeocodingService
|
|
}
|
|
|
|
func NewDeviceUseCase(deviceRepo repository.DevicesRepo, geocoder service.GeocodingService) DeviceUseCase {
|
|
return &deviceUseCase{
|
|
deviceRepo: deviceRepo,
|
|
geocoder: geocoder,
|
|
validate: validator.New(),
|
|
}
|
|
}
|
|
|
|
func (u *deviceUseCase) CreateDevice(device req.DeviceDTO) error {
|
|
err := u.validate.Struct(device)
|
|
if err != nil {
|
|
return fmt.Errorf("validation error: %w", err)
|
|
}
|
|
|
|
newDevice := entity.Device{
|
|
ID: uuid.New(),
|
|
DeviceCode: device.DeviceCode,
|
|
DeviceType: entity.DeviceType(device.DeviceType),
|
|
Longitude: device.Longitude,
|
|
Latitude: device.Latitude,
|
|
PortAmount: device.PortAmount,
|
|
Status: entity.DeviceStatus(device.Status),
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
|
|
return u.deviceRepo.Post(newDevice)
|
|
}
|
|
|
|
func (u *deviceUseCase) GetAllDevices() ([]res.DeviceResponse, error) {
|
|
devices, err := u.deviceRepo.GetAll()
|
|
if err != nil {
|
|
return []res.DeviceResponse{}, err
|
|
}
|
|
devicesResponse,err := helper.ConvertToDeviceResponse(devices, u.geocoder)
|
|
if err != nil {
|
|
return []res.DeviceResponse{}, err
|
|
}
|
|
return devicesResponse, nil
|
|
}
|
|
|
|
func (u *deviceUseCase) GetByID(id uuid.UUID) (res.DeviceResponse, error) {
|
|
device, err := u.deviceRepo.GetByID(id)
|
|
if err != nil {
|
|
return res.DeviceResponse{}, err
|
|
}
|
|
|
|
deviceResp, err := helper.ConvertToDeviceResponseId(device, u.geocoder)
|
|
if err != nil {
|
|
return res.DeviceResponse{}, err
|
|
}
|
|
return deviceResp, nil
|
|
}
|
|
|
|
func (u *deviceUseCase) UpdateDevice(id uuid.UUID, device req.UpdateDeviceDTO) error {
|
|
err := u.validate.Struct(device)
|
|
if err != nil {
|
|
return fmt.Errorf("validation error: %w", err)
|
|
}
|
|
|
|
updates := map[string]interface{}{}
|
|
|
|
if device.DeviceCode != nil {
|
|
updates["DeviceCode"] = *device.DeviceCode
|
|
}
|
|
if device.DeviceType != nil {
|
|
updates["DeviceType"] = *device.DeviceType
|
|
}
|
|
if device.Longitude != nil {
|
|
updates["Longitude"] = *device.Longitude
|
|
}
|
|
if device.Latitude != nil {
|
|
updates["Latitude"] = *device.Latitude
|
|
}
|
|
if device.PortAmount != nil {
|
|
updates["PortAmount"] = *device.PortAmount
|
|
}
|
|
|
|
if len(updates) == 0 {
|
|
return fmt.Errorf("no update data")
|
|
}
|
|
|
|
updates["UpdatedAt"] = time.Now()
|
|
|
|
return u.deviceRepo.Update(id, updates)
|
|
}
|
|
|
|
func (u *deviceUseCase) GetByType(deviceType string) ([]res.DeviceTypeResponse, error) {
|
|
devices, err := u.deviceRepo.GetByType(deviceType)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
deviceTypeResponses := helper.ConvertToDeviceTypeResponse(devices)
|
|
return deviceTypeResponses, nil
|
|
} |