public Task <HttpResponseMessage> Post([FromBody] dynamic body)
        {
            var cpf = _servicePerson.GetOneByCPF((string)body.cpf);

            if (cpf != null)
            {
                return(CreateResponse(HttpStatusCode.Ambiguous, cpf));
            }

            var email = _service.GetOneByEmail((string)body.email);

            if (email != null)
            {
                return(CreateResponse(HttpStatusCode.Ambiguous, cpf));
            }

            var listAddress = _serviceAddress.AddToPerson(body.address);

            var listPhone = _servicePhone.AddToPerson(body.phone);

            var commandPerson = new CreatePersonCommand(
                name: (string)body.name,
                cpf: (string)body.cpf,
                birthDate: (DateTime)body.birthDate,
                genre: (EGenre)body.genre,
                address: listAddress,
                phone: listPhone,
                phototgraph: (string)body.photograph
                );

            var person = _servicePerson.Create(commandPerson);

            var commandUser = new CreateUserCommand(
                email: (string)body.email,
                password: (string)body.password,
                userName: (string)body.email,
                idPerson: person.Id,
                type: (ETypeUser)body.type
                );

            var user = _service.Create(commandUser);

            if (commandUser.Type == ETypeUser.Administrator)
            {
                _service.AddRole(user.Id, "Administrator");
            }
            else if (commandUser.Type == ETypeUser.SessionManager)
            {
                _service.AddRole(user.Id, "SessionManager");
            }

            var token = _service.GenerateTokenRecoveryPassword(user.Email);

            _service.SendEmailRecoveryPassword(user.Id, token);

            return(CreateResponse(HttpStatusCode.Created, user));
        }
Esempio n. 2
0
        public IActionResult CreateUser([FromBody] UserCreationDto user)
        {
            _logger.Info($"Event:{LoggingEvents.UserCreate},Method:{nameof(CreateUser)}, Message: trying create a new user. Payload: {JsonConvert.SerializeObject(user)}");

            if (user == null)
            {
                _logger.Warn($"Event:{LoggingEvents.UserCreate},Method:{nameof(CreateUser)}, Error: model is null.");
                return(BadRequest());
            }

            var userNameFound = _userApplication.UserNameExists(user.UserName);

            if (userNameFound.IsSuccessful)
            {
                _logger.Warn($"Event:{LoggingEvents.UserCreate},Method:{nameof(CreateUser)}, Error: username {user.UserName} exists, try using another one.");
                return(BadRequest());
            }

            var emailFound = _userApplication.EmailExists(user.Email);

            if (emailFound.IsSuccessful)
            {
                _logger.Warn($"Event:{LoggingEvents.UserCreate},Method:{nameof(CreateUser)}, Error: email {user.Email} exists, try using another one.");
                return(BadRequest());
            }

            var userCreated = _userApplication.Create(user);

            if (userCreated.IsFailure)
            {
                _logger.Warn($"Event:{LoggingEvents.UserCreate},Method:{nameof(CreateUser)}, Error: the following validations rules were broken: {JsonConvert.SerializeObject(userCreated.Errors)}");
            }

            _logger.Info($"Event:{LoggingEvents.UserCreate},Method:{nameof(CreateUser)}, Message: user was created successfully.");

            return(CreatedAtRoute("GetUser", new { userId = userCreated.Data.Id }, CreateLink(userCreated.Data)));
        }