public async Task <Status> AddEntry(AddEntryDto addEntryDto, int userId) { var user = _mainDbContext.Users.FirstOrDefault(u => u.UserId == userId); if (user == null) { return(new Status(false, "User not exist")); } var passwordE = SymmetricEncryptor.EncryptString(addEntryDto.PasswordDecrypted, user.PasswordHash); //todo: add auto mapper var newEntry = new Entry { UserOwnerUsername = user.Username, }; var newEntryState = new EntryState { Username = addEntryDto.Username, PasswordE = passwordE, Description = addEntryDto.Description, Email = addEntryDto.Email, WebAddress = addEntryDto.WebAddress, IsDeleted = false }; newEntry.CurrentEntryState = newEntryState; var newUserEntry = new UsersEntries { IsUserOwner = true, Entry = newEntry, User = user }; var entryAction = CreateEntryAction(user, newEntryState, newEntry, ActionTypesEnum.Create); try { _mainDbContext.Update(user); _mainDbContext.Update(entryAction); _mainDbContext.Update(newUserEntry); await _mainDbContext.SaveChangesAsync(); return(new Status { Success = true, Message = "Added new password" }); } catch (Exception e) { Console.WriteLine(e); return(new Status { Success = false, Message = "Something went wrong" }); } }
public async Task <Status> ShareEntry(int givingUserId, string receivingUsername, int entryId) { var userEntry = await _mainDbContext.UsersEntries .Include(x => x.Entry.CurrentEntryState) .Include(x => x.User) .FirstOrDefaultAsync(ue => ue.UserId == givingUserId && ue.EntryId == entryId); if (userEntry == null) { return(new Status(false, "Entry not found")); } if (!userEntry.IsUserOwner) { return(new Status(false, "You are not entry owner")); } var receivingUser = await _mainDbContext.Users. FirstOrDefaultAsync(ue => ue.Username == receivingUsername); if (receivingUser == null) { return(new Status(false, $"User with username {receivingUsername} not found")); } if (receivingUser.UserId == givingUserId) { return(new Status(false, $"You cannot share password for yourself")); } UsersEntries usersEntries = new UsersEntries { CreationDateTime = DateTime.Now, EntryId = entryId, IsUserOwner = false, User = receivingUser }; var newEntryAction = CreateEntryAction(userEntry.User, userEntry.Entry.CurrentEntryState, userEntry.Entry, ActionTypesEnum.Share); try { await _mainDbContext.AddAsync(usersEntries); await _mainDbContext.AddAsync(newEntryAction); await _mainDbContext.SaveChangesAsync(); return(new Status(true, $"The entry successfully shared for user {receivingUsername}")); } catch (Exception e) { Console.WriteLine(e); return(new Status(false, $"Something went wrong")); } }