public User Create(User user, string password)
        {
            // validation
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }

            if (_context.Users.Any(x => x.Username == user.Username))
            {
                throw new AppException("Username \"" + user.Username + "\" is already taken");
            }

            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            user.Power = 0;

            _context.Users.Add(user);
            _context.SaveChanges();

            var notification = _userNotificationService.Create(user.Id);

            user.UserNotification = notification;

            _context.Users.Update(user);
            _context.SaveChanges();

            return(user);
        }
Exemple #2
0
        public ActionResult <UserNotification> PostUserNotification(UserNotificationCM model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                UserNotification userNotification = new UserNotification();
                userNotification = _mapper.Map <UserNotification>(model);
                _userNotificationService.Create(userNotification);
                return(StatusCode(201, userNotification.ID));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }