Ejemplo n.º 1
0
        public GenericCommandResult Put(
            [FromBody] UpdateResidentCommand command,
            [FromServices] ResidentHandler handler
            )
        {
            command.User = User.Claims.FirstOrDefault(x => x.Type == "user_id")?.Value;
            var result = (GenericCommandResult)handler.Handle(command);

            return(result);
        }
Ejemplo n.º 2
0
        public ICommandResult Handle(UpdateResidentCommand command)
        {
            // Fail Fast Validation
            command.Validate();
            if (command.Invalid)
            {
                return(new GenericCommandResult(false, "Ops, erro ao atualizar morador.", command.Notifications));
            }

            // Cria morador
            var resident = _repository.GetById(command.Id, command.User);

            if (resident == null)
            {
                return(new GenericCommandResult(false, "Ops, erro ao atualizar morador.", command.Id));
            }

            resident.Update(command.Name, command.BirthDate, command.Phone, command.Email);

            try
            {
                // Salva morador
                _repository.Update(resident);
            }
            catch (Exception ex)
            {
                return(new GenericCommandResult(false, "Erro inesperado!", ex.Message));
            }

            var residentResult = new ResidentCommandResult
            {
                Name      = resident.Name,
                BirthDate = resident.BirthDate,
                Cpf       = resident.Cpf,
                Email     = resident.Email,
                Phone     = resident.Phone,
                User      = resident.User
            };

            return(new GenericCommandResult(true, "Morador salvo com sucesso!", residentResult));
        }