create_user_command.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. }
  12. func NewCreateUserCommand(firstName, lastName, sex, birthDate string) *CreateUserCommand {
  13. return &CreateUserCommand{
  14. FirstName: firstName,
  15. LastName: lastName,
  16. Sex: sex,
  17. BirthDate: birthDate,
  18. }
  19. }
  20. // Validate valida los campos del comando
  21. func (c *CreateUserCommand) Validate() error {
  22. if c.FirstName == "" {
  23. return errors.New("your firstname is required")
  24. }
  25. if c.LastName == "" {
  26. return errors.New("your lastname is required")
  27. }
  28. if c.BirthDate == "" {
  29. return errors.New("your birthdate is required")
  30. }
  31. if c.Sex == "" {
  32. return errors.New("your sex is required")
  33. }
  34. // Crear una instancia de User para utilizar la validacion del modelo
  35. user := models.User{
  36. FirstName: c.FirstName,
  37. LastName: c.LastName,
  38. Sex: c.Sex,
  39. BirthDate: c.BirthDate,
  40. }
  41. // Validar el modelo User
  42. if err := user.Validate(); err != nil {
  43. return err
  44. }
  45. return nil
  46. }