Ejemplo n.º 1
0
        public async Task <ActionResult <ParticipantDto> > PostParticipant(ParticipantDto participantDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!participantDTO.ConfirmPassword.Equals(participantDTO.Password))
            {
                return(StatusCode(412));
            }

            string passwordHash = BCrypt.Net.BCrypt.HashPassword(participantDTO.Password);

            var participant = new Participant
            {
                Login            = participantDTO.Login,
                Password         = passwordHash,
                Participant_Type = (ParticipantType)participantDTO.Participant_Type
            };

            try
            {
                await _participant.CreateParticipantsAsync(participant);
            }
            catch (DbUpdateException)
            {
                return(Conflict(new { message = "The login already exist" }));
            }

            var tokenJWT = GenerateJWToken(participant);

            return(Ok(new JwtSecurityTokenHandler().WriteToken(tokenJWT)));
        }