public async Task <IActionResult> EditEntry([FromBody] EditEntryDto editEntryDto) { var userId = GetUserId(); var result = await _entryService.EditEntry(editEntryDto, userId); if (result.Success) { return(Ok(result)); } return(BadRequest(result)); }
public async Task <Status> EditEntry(EditEntryDto editEntryDto, int userId) { var owner = await _mainDbContext.Users.FirstOrDefaultAsync(user => user.UserId == userId); if (owner == null) { return(new Status(false, "User owner not found")); } var userEntry = await _mainDbContext.UsersEntries.Include(p => p.Entry.CurrentEntryState).FirstOrDefaultAsync(x => x.EntryId == editEntryDto.EntryId && x.UserId == userId); if (userEntry == null) { return(new Status(false, "Entry not found")); } if (!userEntry.IsUserOwner) { return(new Status(false, "You cannot edit shared for you entry. You have to be an owner for edit.")); } try { var newEntryState = new EntryState { PasswordE = SymmetricEncryptor.EncryptString(editEntryDto.PasswordDecrypted, owner.PasswordHash), Username = editEntryDto.Username, Description = editEntryDto.Description, Email = editEntryDto.Email, WebAddress = editEntryDto.WebAddress, IsDeleted = false }; userEntry.Entry.CurrentEntryState = newEntryState; var entryAction = CreateEntryAction(owner, newEntryState, userEntry.Entry, ActionTypesEnum.Edit); await _mainDbContext.AddAsync(entryAction); await _mainDbContext.SaveChangesAsync(); return(new Status(true, "Password has been successfully edited")); } catch (Exception e) { Console.WriteLine(e); return(new Status { Success = false, Message = "Something went wrong" }); } }