| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package command
- import (
- "api-dockerization/internal/models"
- "errors"
- )
- type CreateUserCommand struct {
- FirstName string `json:"first_name"`
- LastName string `json:"last_name"`
- Sex string `json:"sex"`
- BirthDate string `json:"birth_date"`
- Flags int `json:"flags"`
- }
- func NewCreateUserCommand(firstName, lastName, sex, birthDate string, flags int) *CreateUserCommand {
- return &CreateUserCommand{
- FirstName: firstName,
- LastName: lastName,
- Sex: sex,
- BirthDate: birthDate,
- Flags: flags,
- }
- }
- // Validate valida los campos del comando
- func (c *CreateUserCommand) Validate() error {
- if c.FirstName == "" {
- return errors.New("your firstname is required")
- }
- if c.LastName == "" {
- return errors.New("your lastname is required")
- }
- if c.BirthDate == "" {
- return errors.New("your birthdate is required")
- }
- if c.Sex == "" {
- return errors.New("your sex is required")
- }
- if c.Flags < 0 {
- return errors.New("flags must be a non-negative value")
- }
- // Crear una instancia de User para utilizar la validacion del modelo
- user := models.User{
- FirstName: c.FirstName,
- LastName: c.LastName,
- Sex: c.Sex,
- BirthDate: c.BirthDate,
- Flags: c.Flags,
- }
- // Validar el modelo User
- if err := user.Validate(); err != nil {
- return err
- }
- return nil
- }
|