create_user_command.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package command
  2. import (
  3. "api-dockerization/internal/models"
  4. "errors"
  5. )
  6. type CreateUserCommand struct {
  7. FirstName string `json:"first_name"`
  8. LastName string `json:"last_name"`
  9. Sex string `json:"sex"`
  10. BirthDate string `json:"birth_date"`
  11. Flags int `json:"flags"`
  12. }
  13. func NewCreateUserCommand(firstName, lastName, sex, birthDate string, flags int) *CreateUserCommand {
  14. return &CreateUserCommand{
  15. FirstName: firstName,
  16. LastName: lastName,
  17. Sex: sex,
  18. BirthDate: birthDate,
  19. Flags: flags,
  20. }
  21. }
  22. // Validate valida los campos del comando
  23. func (c *CreateUserCommand) Validate() error {
  24. if c.FirstName == "" {
  25. return errors.New("your firstname is required")
  26. }
  27. if c.LastName == "" {
  28. return errors.New("your lastname is required")
  29. }
  30. if c.BirthDate == "" {
  31. return errors.New("your birthdate is required")
  32. }
  33. if c.Sex == "" {
  34. return errors.New("your sex is required")
  35. }
  36. if c.Flags < 0 {
  37. return errors.New("flags must be a non-negative value")
  38. }
  39. // Crear una instancia de User para utilizar la validacion del modelo
  40. user := models.User{
  41. FirstName: c.FirstName,
  42. LastName: c.LastName,
  43. Sex: c.Sex,
  44. BirthDate: c.BirthDate,
  45. Flags: c.Flags,
  46. }
  47. // Validar el modelo User
  48. if err := user.Validate(); err != nil {
  49. return err
  50. }
  51. return nil
  52. }