[FEATURE] adding update and fixing on get by id

This commit is contained in:
areeqakbr 2025-02-14 15:27:01 +07:00
parent 0727b724ca
commit 54d191dcb4
20 changed files with 408 additions and 51 deletions

View File

@ -7,6 +7,7 @@ import (
"users_management/m/utils/common"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type BackboneController struct {
@ -18,7 +19,8 @@ func (bc *BackboneController) Route() {
rg := bc.rg.Group("/backbone")
rg.GET("", bc.GetBackbone())
rg.POST("", bc.CreateBackbone())
rg.GET("/:id", bc.GetBackboneByID())
rg.GET("/:uuid", bc.GetBackboneByID())
rg.PUT("/:uuid", bc.UpdateBackbone())
}
func NewBackboneController(bu usecase.BackboneUseCase, rg *gin.RouterGroup) *BackboneController {
@ -64,8 +66,13 @@ func (bc *BackboneController) CreateBackbone() gin.HandlerFunc {
func (bc *BackboneController) GetBackboneByID() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
backbone, err := bc.bu.GetByID(id)
id := c.Param("uuid")
uuid, err := uuid.Parse(id)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
backbone, err := bc.bu.GetByID(uuid)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
@ -74,4 +81,32 @@ func (bc *BackboneController) GetBackboneByID() gin.HandlerFunc {
common.SingleResponses(c, "Success", backbone)
}
}
func (bc *BackboneController) UpdateBackbone() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("uuid")
uuid, err := uuid.Parse(id)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
var backboneDTO req.UpdateBackboneDTO
err = c.ShouldBindJSON(&backboneDTO)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
err = bc.bu.UpdateBackbone(uuid, backboneDTO)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
common.SingleResponses(c, "Backbone has been updated", nil)
}
}

View File

@ -7,6 +7,7 @@ import (
"users_management/m/utils/common"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type DevicePortController struct {
@ -18,7 +19,8 @@ func (dc *DevicePortController) Route() {
rg := dc.rg.Group("/device-port")
rg.GET("", dc.GetDevicePort())
rg.POST("", dc.CreateDevicePort())
rg.GET("/:id", dc.GetDevicePortByID())
rg.GET("/:uuid", dc.GetDevicePortByID())
rg.PUT("/:uuid", dc.UpdateDevicePort())
}
func NewDevicePortController(du usecase.DevicePortUseCase, rg *gin.RouterGroup) *DevicePortController {
@ -64,8 +66,13 @@ func (dc *DevicePortController) CreateDevicePort() gin.HandlerFunc {
func (dc *DevicePortController) GetDevicePortByID() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
devicePort, err := dc.du.GetByID(id)
id := c.Param("uuid")
uuid, err := uuid.Parse(id)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
devicePort, err := dc.du.GetByID(uuid)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
@ -74,4 +81,32 @@ func (dc *DevicePortController) GetDevicePortByID() gin.HandlerFunc {
common.SingleResponses(c, "Success", devicePort)
}
}
func (dc *DevicePortController) UpdateDevicePort() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("uuid")
uuid, err := uuid.Parse(id)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
var devicePortDTO req.UpdateDevicePort
err = c.ShouldBindJSON(&devicePortDTO)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
err = dc.du.UpdateDevicePort(uuid, devicePortDTO)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
common.SingleResponses(c, "Device Port has been updated", nil)
}
}

View File

@ -7,6 +7,7 @@ import (
"users_management/m/utils/common"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type DeviceController struct {
@ -18,7 +19,8 @@ func (dc *DeviceController) Route() {
rg := dc.rg.Group("/devices")
rg.POST("", dc.CreateDevice())
rg.GET("", dc.GetAllDevices())
rg.GET("/:id", dc.GetDeviceByID())
rg.GET("/:uuid", dc.GetDeviceByID())
rg.PUT("/:uuid", dc.UpdateDevice())
}
func NewDeviceController(du usecase.DeviceUseCase, rg *gin.RouterGroup) *DeviceController {
@ -65,8 +67,12 @@ func (dc *DeviceController) GetAllDevices() gin.HandlerFunc {
func (dc *DeviceController) GetDeviceByID() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
device, err := dc.du.GetByID(id)
uuid, err := uuid.Parse(id)
if err != nil{
common.ErrorResponses(c, http.StatusBadGateway, err.Error())
return
}
device, err := dc.du.GetByID(uuid)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
@ -74,4 +80,32 @@ func (dc *DeviceController) GetDeviceByID() gin.HandlerFunc {
common.SingleResponses(c, "Success", device)
}
}
func (dc *DeviceController) UpdateDevice() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
uuid, err := uuid.Parse(id)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
var deviceDTO req.UpdateDeviceDTO
err = c.ShouldBindJSON(&deviceDTO)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
err = dc.du.UpdateDevice(uuid, deviceDTO)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
common.SingleResponses(c, "Device has been updated", nil)
}
}

View File

@ -7,6 +7,7 @@ import (
"users_management/m/utils/common"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type FishboneController struct {
@ -18,7 +19,8 @@ func (fc *FishboneController) Route() {
rg := fc.rg.Group("/fishbone")
rg.GET("", fc.GetFishbone())
rg.POST("", fc.CreateFishbone())
rg.GET("/:id", fc.GetFishboneByID())
rg.GET("/:uuid", fc.GetFishboneByID())
rg.PUT("/:uuid", fc.UpdateFishbone())
}
func NewFishboneController(fu usecase.FishboneUseCase, rg *gin.RouterGroup) *FishboneController {
@ -64,8 +66,13 @@ func (fc *FishboneController) CreateFishbone() gin.HandlerFunc {
func (fc *FishboneController) GetFishboneByID() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
fishbone, err := fc.fu.GetByID(id)
id := c.Param("uuid")
uuid, err := uuid.Parse(id)
if err != nil{
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
fishbone, err := fc.fu.GetByID(uuid)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
@ -74,4 +81,32 @@ func (fc *FishboneController) GetFishboneByID() gin.HandlerFunc {
common.SingleResponses(c, "Success", fishbone)
}
}
func (fc *FishboneController) UpdateFishbone() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("uuid")
uuid, err := uuid.Parse(id)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
var fishboneDTO req.UpdateFishboneDTO
err = c.ShouldBindJSON(&fishboneDTO)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
err = fc.fu.UpdateFishbone(uuid, fishboneDTO)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
common.SingleResponses(c, "Fishbone has been updated", nil)
}
}

View File

@ -7,6 +7,7 @@ import (
"users_management/m/utils/common"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type TowerController struct {
@ -18,7 +19,8 @@ func (tc *TowerController) Route() {
rg := tc.rg.Group("/tower")
rg.GET("", tc.GetTower())
rg.POST("", tc.CreateTower())
rg.GET("/:id", tc.GetTowerByID())
rg.GET("/:uuid", tc.GetTowerByID())
rg.PUT("/:uuid", tc.UpdateTower())
}
func NewTowerController(tu usecase.TowerUseCase, rg *gin.RouterGroup) *TowerController {
@ -64,8 +66,13 @@ func (tc *TowerController) CreateTower() gin.HandlerFunc {
func (tc *TowerController) GetTowerByID() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
tower, err := tc.tu.GetByID(id)
id := c.Param("uuid")
uuid, err := uuid.Parse(id)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
tower, err := tc.tu.GetByID(uuid)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
@ -74,4 +81,30 @@ func (tc *TowerController) GetTowerByID() gin.HandlerFunc {
common.SingleResponses(c, "Success", tower)
}
}
func (tc *TowerController) UpdateTower() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("uuid")
uuid, err := uuid.Parse(id)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
}
var towerUpdateDTO req.UpdateTowerDTO
err = c.ShouldBindJSON(&towerUpdateDTO)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
err = tc.tu.UpdateTower(uuid, towerUpdateDTO)
if err != nil {
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
return
}
common.SingleResponses(c, "Tower has been updated", nil)
}
}

View File

@ -6,4 +6,10 @@ type BackboneDTO struct {
DeviceStartID uuid.UUID `json:"dev_start_id"`
DeviceEndID uuid.UUID `json:"dev_end_id"`
CoreAmount int `json:"core_amount"`
}
}
type UpdateBackboneDTO struct {
DeviceStartID *uuid.UUID `json:"dev_start_id,omitempty" validate:"omitempty,min=3"`
DeviceEndID *uuid.UUID `json:"dev_end_id,omitempty" validate:"omitempty,min=3"`
CoreAmount *int `json:"core_amount,omitempty" validate:"omitempty,min=1"`
}

View File

@ -5,4 +5,9 @@ import "github.com/google/uuid"
type DevicePort struct {
DeviceID uuid.UUID `json:"device_id"`
PortNumber int `json:"port_number"`
}
type UpdateDevicePort struct {
DeviceID *uuid.UUID `json:"device_id,omitempty" validate:"omitempty,min=3"`
PortNumber *int `json:"port_number,omitempty" validate:"omitempty,min=1"`
}

View File

@ -8,4 +8,13 @@ type DeviceDTO struct {
Latitude float64 `json:"latitude" validate:"required"`
PortAmount int `json:"port_amount" validate:"required"`
Status string `json:"status" validate:"required,oneof=active inactive maintenance"`
}
type UpdateDeviceDTO struct {
DeviceCode *string `json:"device_code,omitempty" validate:"omitempty,min=3"`
DeviceType *string `json:"device_type,omitempty" validate:"omitempty,min=3"`
Longitude *float64 `json:"longitude,omitempty" validate:"omitempty,longitude"`
Latitude *float64 `json:"latitude,omitempty" validate:"omitempty,latitude"`
PortAmount *int `json:"port_amount,omitempty" validate:"omitempty,min=1"`
Status *string `json:"status,omitempty" validate:"omitempty,oneof=active inactive maintenance"`
}

View File

@ -7,4 +7,11 @@ type FishboneDTO struct {
DeviceStartID uuid.UUID `json:"dev_start_id"`
DeviceEndID uuid.UUID `json:"dev_end_id"`
CoreAmount int `json:"core_amount"`
}
type UpdateFishboneDTO struct {
BackboneID *uuid.UUID `json:"bb_id,omitempty" validate:"omitempty,min=3"`
DeviceStartID *uuid.UUID `json:"dev_start_id,omitempty" validate:"omitempty,min=3"`
DeviceEndID *uuid.UUID `json:"dev_end_id,omitempty" validate:"omitempty,min=3"`
CoreAmount *int `json:"core_amount,omitempty" validate:"omitempty,min=1"`
}

View File

@ -7,4 +7,11 @@ type TowerDTO struct {
TowerCode string `json:"tower_code"`
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
}
type UpdateTowerDTO struct {
DeviceID *string `json:"device_id,omitempty" validate:"omitempty,min=3"`
TowerCode *string `json:"tower_code,omitempty" validate:"omitempty,alphanum"`
Longitude *float64 `json:"longitude,omitempty" validate:"omitempty,longitude"`
Latitude *float64 `json:"latitude,omitempty" validate:"omitempty,latitude"`
}

View File

@ -3,15 +3,16 @@ package repository
import (
"users_management/m/model/entity"
"github.com/google/uuid"
"gorm.io/gorm"
)
type BackboneRepo interface {
Post(backbone entity.Backbone) error
GetAll() ([]entity.Backbone, error)
Update(backbone entity.Backbone) error
Update(id uuid.UUID, updates map[string]interface{}) error
GetByID(id string) (entity.Backbone, error)
GetByID(id uuid.UUID) (entity.Backbone, error)
}
type backboneRepo struct {
@ -41,15 +42,15 @@ func (r *backboneRepo) GetAll() ([]entity.Backbone, error) {
return backbones, nil
}
func (r *backboneRepo) Update(backbone entity.Backbone) error {
err := r.db.Save(&backbone).Error
func (r *backboneRepo) Update(id uuid.UUID, updates map[string]interface{}) error {
err := r.db.Model(&entity.Backbone{}).Where("id = ?", id).Updates(updates).Error
if err != nil {
return err
}
return nil
}
func (r *backboneRepo) GetByID(id string) (entity.Backbone, error) {
func (r *backboneRepo) GetByID(id uuid.UUID) (entity.Backbone, error) {
var backbone entity.Backbone
err := r.db.Where("id = ?", id).First(&backbone).Error
if err != nil {

View File

@ -3,15 +3,16 @@ package repository
import (
"users_management/m/model/entity"
"github.com/google/uuid"
"gorm.io/gorm"
)
type DevicePortRepo interface {
Post(devicePort entity.DevicePort) error
GetAll() ([]entity.DevicePort, error)
Update(devicePort entity.DevicePort) error
Update(id uuid.UUID, updates map[string]interface{}) error
GetByID(id string) (entity.DevicePort, error)
GetByID(id uuid.UUID) (entity.DevicePort, error)
}
type devicePortRepo struct {
@ -41,15 +42,15 @@ func (r *devicePortRepo) GetAll() ([]entity.DevicePort, error) {
return devicePorts, nil
}
func (r *devicePortRepo) Update(devicePort entity.DevicePort) error {
err := r.db.Save(&devicePort).Error
func (r *devicePortRepo) Update(id uuid.UUID,updates map[string]interface{}) error {
err := r.db.Model(&entity.DevicePort{}).Where("id = ?", id).Updates(updates).Error
if err != nil {
return err
}
return nil
}
func (r *devicePortRepo) GetByID(id string) (entity.DevicePort, error) {
func (r *devicePortRepo) GetByID(id uuid.UUID) (entity.DevicePort, error) {
var devicePort entity.DevicePort
err := r.db.Where("id = ?", id).First(&devicePort).Error
if err != nil {

View File

@ -3,15 +3,16 @@ package repository
import (
"users_management/m/model/entity"
"github.com/google/uuid"
"gorm.io/gorm"
)
type DevicesRepo interface {
Post(device entity.Device) error
GetAll() ([]entity.Device, error)
Update(device entity.Device) error
Update(id uuid.UUID,updates map[string]interface{}) error
GetByID(id string) (entity.Device, error)
GetByID(id uuid.UUID) (entity.Device, error)
}
type devicesRepo struct {
@ -40,15 +41,15 @@ func (r *devicesRepo) GetAll() ([]entity.Device, error) {
}
return devices, nil
}
func (r *devicesRepo) Update(device entity.Device) error {
err := r.db.Save(&device).Error
func (r *devicesRepo) Update(id uuid.UUID,updates map[string]interface{}) error {
err := r.db.Model(&entity.Device{}).Where("id = ?", id).Updates(updates).Error
if err != nil {
return err
}
return nil
}
func (r *devicesRepo) GetByID(id string) (entity.Device, error) {
func (r *devicesRepo) GetByID(id uuid.UUID) (entity.Device, error) {
var device entity.Device
err := r.db.Where("id = ?", id).First(&device).Error
if err != nil {

View File

@ -3,15 +3,16 @@ package repository
import (
"users_management/m/model/entity"
"github.com/google/uuid"
"gorm.io/gorm"
)
type FishboneRepo interface {
Post(fishbone entity.Fishbone) error
GetAll() ([]entity.Fishbone, error)
Update(fishbone entity.Fishbone) error
Update(id uuid.UUID,updates map[string]interface{}) error
GetByID(id string) (entity.Fishbone, error)
GetByID(id uuid.UUID) (entity.Fishbone, error)
}
type fishboneRepo struct {
@ -41,15 +42,15 @@ func (r *fishboneRepo) GetAll() ([]entity.Fishbone, error) {
return fishbones, nil
}
func (r *fishboneRepo) Update(fishbone entity.Fishbone) error {
err := r.db.Save(&fishbone).Error
func (r *fishboneRepo) Update(id uuid.UUID,updates map[string]interface{}) error {
err := r.db.Model(&entity.Fishbone{}).Where("id = ?", id).Updates(updates).Error
if err != nil {
return err
}
return nil
}
func (r *fishboneRepo) GetByID(id string) (entity.Fishbone, error) {
func (r *fishboneRepo) GetByID(id uuid.UUID) (entity.Fishbone, error) {
var fishbone entity.Fishbone
err := r.db.Where("id = ?", id).First(&fishbone).Error
if err != nil {

View File

@ -3,14 +3,15 @@ package repository
import (
"users_management/m/model/entity"
"github.com/google/uuid"
"gorm.io/gorm"
)
type TowerRepo interface {
Post(tower entity.Tower) error
GetAll() ([]entity.Tower, error)
Update(tower entity.Tower) error
GetByID(id string) (entity.Tower, error)
Update(id uuid.UUID,updates map[string]interface{}) error
GetByID(id uuid.UUID) (entity.Tower, error)
}
type towerRepo struct {
@ -40,15 +41,15 @@ func (r *towerRepo) GetAll() ([]entity.Tower, error) {
return towers, nil
}
func (r *towerRepo) Update(tower entity.Tower) error {
err := r.db.Save(&tower).Error
func (r *towerRepo) Update(id uuid.UUID,updates map[string]interface{}) error {
err := r.db.Model(&entity.Tower{}).Where("id = ?", id).Updates(updates).Error
if err != nil {
return err
}
return nil
}
func (r *towerRepo) GetByID(id string) (entity.Tower, error) {
func (r *towerRepo) GetByID(id uuid.UUID) (entity.Tower, error) {
var tower entity.Tower
err := r.db.Where("id = ?", id).First(&tower).Error
if err != nil {

View File

@ -15,7 +15,8 @@ type BackboneUseCase interface {
CreateBackbone(backbone req.BackboneDTO) error
GetAllBackbone() ([]entity.Backbone, error)
GetByID(id string) (entity.Backbone, error)
GetByID(id uuid.UUID) (entity.Backbone, error)
UpdateBackbone(id uuid.UUID, backbone req.UpdateBackboneDTO) error
}
type backboneUseCase struct {
@ -57,10 +58,35 @@ func (u *backboneUseCase) GetAllBackbone() ([]entity.Backbone, error) {
return backbones, nil
}
func (u *backboneUseCase) GetByID(id string) (entity.Backbone, error) {
func (u *backboneUseCase) GetByID(id uuid.UUID) (entity.Backbone, error) {
backbone, err := u.backboneRepo.GetByID(id)
if err != nil {
return backbone, err
}
return backbone, nil
}
func (u *backboneUseCase) UpdateBackbone(id uuid.UUID, backbone req.UpdateBackboneDTO) error {
err := u.validate.Struct(backbone)
if err != nil {
return fmt.Errorf("validation error: %w", err)
}
updates := make(map[string]interface{})
if backbone.DeviceStartID != nil {
updates["DeviceStartID"] = backbone.DeviceStartID
}
if backbone.DeviceEndID != nil {
updates["DeviceEndID"] = backbone.DeviceEndID
}
if backbone.CoreAmount != nil {
updates["CoreAmount"] = backbone.CoreAmount
}
updates["UpdatedAt"] = time.Now()
return u.backboneRepo.Update(id, updates)
}

View File

@ -14,7 +14,8 @@ type DevicePortUseCase interface {
CreateDevicePort(devicePort req.DevicePort) error
GetAllDevicePort() ([]entity.DevicePort, error)
GetByID(id string) (entity.DevicePort, error)
GetByID(id uuid.UUID) (entity.DevicePort, error)
UpdateDevicePort(id uuid.UUID, devicePort req.UpdateDevicePort) error
}
type devicePortUseCase struct {
@ -54,11 +55,32 @@ func (u *devicePortUseCase) GetAllDevicePort() ([]entity.DevicePort, error) {
return devicePorts, nil
}
func (u *devicePortUseCase) GetByID(id string) (entity.DevicePort, error) {
func (u *devicePortUseCase) GetByID(id uuid.UUID) (entity.DevicePort, error) {
devicePort, err := u.devicePortRepo.GetByID(id)
if err != nil {
return devicePort, err
}
return devicePort, nil
}
func (u *devicePortUseCase) UpdateDevicePort(id uuid.UUID, devicePort req.UpdateDevicePort) error {
err := u.validate.Struct(devicePort)
if err != nil {
return err
}
updates := make(map[string]interface{})
if devicePort.DeviceID != nil {
updates["DeviceID"] = *devicePort.DeviceID
}
if devicePort.PortNumber != nil {
updates["PortNumber"] = *devicePort.PortNumber
}
updates["UpdatedAt"] = time.Now()
return u.devicePortRepo.Update(id, updates)
}

View File

@ -15,7 +15,8 @@ type DeviceUseCase interface {
CreateDevice(device req.DeviceDTO) error
GetAllDevices() ([]entity.Device, error)
GetByID(id string) (entity.Device, error)
GetByID(id uuid.UUID) (entity.Device, error)
UpdateDevice(id uuid.UUID, device req.UpdateDeviceDTO) error
}
type deviceUseCase struct {
@ -59,10 +60,43 @@ func (u *deviceUseCase) GetAllDevices() ([]entity.Device, error) {
return devices, nil
}
func (u *deviceUseCase) GetByID(id string) (entity.Device, error) {
func (u *deviceUseCase) GetByID(id uuid.UUID) (entity.Device, error) {
device, err := u.deviceRepo.GetByID(id)
if err != nil {
return device, err
}
return device, 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)
}

View File

@ -1,6 +1,7 @@
package usecase
import (
"errors"
"time"
"users_management/m/model/dto/req"
"users_management/m/model/entity"
@ -13,7 +14,9 @@ import (
type FishboneUseCase interface {
CreateFishbone(fishbone req.FishboneDTO) error
GetAllFishbone() ([]entity.Fishbone, error)
GetByID(id string) (entity.Fishbone, error)
GetByID(id uuid.UUID) (entity.Fishbone, error)
UpdateFishbone(id uuid.UUID, fishbone req.UpdateFishboneDTO) error
}
type fishboneUsecase struct {
@ -55,10 +58,39 @@ func (u *fishboneUsecase) GetAllFishbone() ([]entity.Fishbone, error) {
return fishbones, nil
}
func (u *fishboneUsecase) GetByID(id string) (entity.Fishbone, error) {
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)
}

View File

@ -1,6 +1,7 @@
package usecase
import (
"errors"
"time"
"users_management/m/model/dto/req"
"users_management/m/model/entity"
@ -13,7 +14,8 @@ import (
type TowerUseCase interface {
Post(tower req.TowerDTO) error
GetAll() ([]entity.Tower, error)
GetByID(id string) (entity.Tower, error)
GetByID(id uuid.UUID) (entity.Tower, error)
UpdateTower(id uuid.UUID, tower req.UpdateTowerDTO) error
}
type towerUsecase struct {
@ -56,10 +58,40 @@ func (u *towerUsecase) GetAll() ([]entity.Tower, error) {
}
func (u *towerUsecase) GetByID(id string) (entity.Tower, error) {
func (u *towerUsecase) GetByID(id uuid.UUID) (entity.Tower, error) {
tower, err := u.towerRepo.GetByID(id)
if err != nil {
return tower, err
}
return tower, nil
}
func (u *towerUsecase) UpdateTower(id uuid.UUID, tower req.UpdateTowerDTO) error {
err := u.validate.Struct(tower)
if err != nil {
return err // Return validation error
}
updates := make(map[string]interface{})
if tower.DeviceID != nil {
updates["DeviceID"] = *tower.DeviceID
}
if tower.TowerCode != nil {
updates["TowerCode"] = *tower.TowerCode
}
if tower.Longitude != nil {
updates["Longitude"] = *tower.Longitude
}
if tower.Latitude != nil {
updates["Latitude"] = *tower.Latitude
}
// If no fields are updated, return an error
if len(updates) == 0 {
return errors.New("no fields to update")
}
updates["UpdatedAt"] = time.Now()
return u.towerRepo.Update(id, updates)
}