Ejemplo n.º 1
0
        public async Task <User> Create(Dto.RegistrationData registrationData)
        {
            // Create user entity from registration data.
            var dbUser = Mapper.Map <User>(registrationData);

            // Validation.
            if (string.IsNullOrWhiteSpace(registrationData.Password))
            {
                throw new Models.AppException("Password is required");
            }

            if (DbContext.Users.Any(x => x.Username == registrationData.Username))
            {
                throw new Models.AppException("Username " + dbUser.Username + " is already taken");
            }

            // If the area requested by the customer is in the database then replace the suggestion with a reference to that area.
            var matchingDbArea = DbContext.Areas
                                 .FirstOrDefault(a => a.Value
                                                 .Contains(registrationData.FirstZip));

            if (matchingDbArea != null)
            {
                dbUser.UserSuggestions = null;
                dbUser.UserAreas       = new List <UserArea>()
                {
                    new UserArea()
                    {
                        AreaId = matchingDbArea.AreaId
                    }
                };
            }

            // Generate hash and salt from provided password.
            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(registrationData.Password, out passwordHash, out passwordSalt);

            dbUser.Hash = passwordHash;
            dbUser.Salt = passwordSalt;

            // Add user entity and save.
            DbContext.Users.Add(dbUser);
            await DbContext.SaveChangesAsync();

            // Create user payment method entity and associate it to the freshly created user (and visa versa).
            var dbUserPaymentMethod = Mapper.Map <UserPaymentMethod>(registrationData);

            dbUserPaymentMethod.UserId = dbUser.UserId;
            dbUser.UserPaymentMethod   = dbUserPaymentMethod;

            // Add user payment method entity and save.
            DbContext.UserPaymentMethods.Add(dbUserPaymentMethod);
            await DbContext.SaveChangesAsync();

            return(dbUser);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Register([FromBody] Dto.RegistrationData registrationDto)
        {
            // map dto to entity
            //var user = _mapper.Map<DAL.User>(registrationDto);

            try
            {
                // save
                await UserSvc.Create(registrationDto);

                return(Ok());
            }
            catch (Exception ex) //(AppException ex)
            {
                // return error message if there was an exception
                return(BadRequest(ex.Message));
            }
        }