Exemple #1
0
        public async Task <NoteViewModel> Create(ApplicationUser applicationUser, NoteCreateModel noteCreateModel)
        {
            var noteToCreate = noteCreateModel.ToNote(applicationUser);

            try
            {
                var createdEntity = _dbContext.Notes.Add(noteToCreate);

                await _dbContext.SaveChangesAsync().ConfigureAwait(false);

                var note = (await _dbContext.Notes.Include(n => n.User).FirstAsync(n => n.Id == createdEntity.Entity.Id));
                return(_noteConverter.ToNoteViewModel(applicationUser, note, null));
            }
            catch (DbUpdateException updateException)
            {
                throw new ConflictException(updateException.Message, updateException);
            }
        }
Exemple #2
0
        public async Task UpdatePassword(ApplicationUser applicationUser, UpdatePasswordModel updatePasswordModel)
        {
            if (updatePasswordModel == null)
            {
                throw new ValidationException(nameof(updatePasswordModel));
            }

            var userDto = await _dbContext.Users.FindAsync(applicationUser.Id);

            if (userDto.PasswordHash != _cryptoService.Hash(updatePasswordModel.CurrentPassword))
            {
                throw new ValidationException("Current password is incorrect");             // TODO: somehow handle in controller and return BadRequest()
            }

            userDto.PasswordHash = _cryptoService.Hash(updatePasswordModel.NewPassword);

            await _dbContext.SaveChangesAsync();
        }