2fas-server/internal/api/mobile/app/command/update_mobile_device.go
2022-12-31 10:22:38 +01:00

38 lines
842 B
Go

package command
import (
"github.com/2fas/api/internal/api/mobile/domain"
"github.com/google/uuid"
)
type UpdateMobileDevice struct {
Id string `uri:"device_id" validate:"required,uuid4"`
Name string `json:"name" validate:"max=128"`
Platform string `json:"platform" validate:"omitempty,oneof=ios android huawei"`
FcmToken string `json:"fcm_token" validate:"max=256"`
}
type UpdateMobileDeviceHandler struct {
Repository domain.MobileDeviceRepository
}
func (h *UpdateMobileDeviceHandler) Handle(cmd *UpdateMobileDevice) error {
deviceId, _ := uuid.Parse(cmd.Id)
mobileDevice, err := h.Repository.FindById(deviceId)
if err != nil {
return err
}
if cmd.Name != "" {
mobileDevice.Name = cmd.Name
}
if cmd.FcmToken != "" {
mobileDevice.FcmToken = cmd.FcmToken
}
return h.Repository.Update(mobileDevice)
}