public async Task <Unit> Handle(RemoveVaultCommand request, CancellationToken cancellationToken) { if (!VaultService.ValidateVaultPassword(request.VaultId, request.MasterPassword)) { throw new Exception("Podano nie poprawne hasło"); } var vault = await(from v in PmContext.Vaults where v.Username == UserResolverService.GetUsername() && v.Id == request.VaultId select v).FirstOrDefaultAsync(); if (vault == null) { throw new Exception(); } var entries = await(from en in PmContext.Entries where en.VaultId == vault.Id select en ).ToListAsync(); using var trans = PmContext.Database.BeginTransaction(); PmContext.Entries.RemoveRange(entries); PmContext.Vaults.Remove(vault); PmContext.SaveChanges(); await trans.CommitAsync(); return(Unit.Value); }
public async Task <Unit> Handle(CreateEntryCommand request, CancellationToken cancellationToken) { if (!VaultService.ValidateVaultPassword(request.VaultId, request.MasterPassword)) { throw new Exception("Podano nie poprawne hasło"); } var vault = await(from v in PmContext.Vaults where v.Id == request.VaultId select new { v.Id } ).FirstOrDefaultAsync(); if (vault == null) { throw new Exception(); } Entry entry = new Entry(); if (String.IsNullOrEmpty(request.Login) || String.IsNullOrEmpty(request.Password) || String.IsNullOrEmpty(request.Portal)) { throw new Exception("Wrong data"); } entry.Login = request.Login; entry.Password = request.Password; entry.Portal = request.Portal; entry.Email = request.Email; entry.VaultId = vault.Id; PmContext.Add(entry); await PmContext.SaveChangesAsync(); return(Unit.Value); }
public async Task <string> Handle(CreateUserCommand request, CancellationToken cancellationToken) { using var erpTrans = PmContext.Database.BeginTransaction(); var a = new User() { Username = request.Username }; a.Password = Encryptor.Encode(request.Password); PmContext.Users.Add(a); var userRole = new UserRole() { RoleName = "SystemUser", Username = request.Username }; PmContext.UserRoles.Add(userRole); await PmContext.SaveChangesAsync(); erpTrans.Commit(); return(a.Username); }
private async Task <User> GetUserAsync(string login, string password) { using (var context = new PmContext()) { return(await context.Users.FirstOrDefaultAsync(x => x.Login == login && x.Password == password)); } }
private async Task <bool> SaveProductsAsync() { try { using (var context = new PmContext()) { foreach (var product in products) { var productDb = await context.Products.FirstOrDefaultAsync(x => x.Id == product.Id); if (productDb is null) { context.Products.Add(product); } else { productDb.Name = product.Name; productDb.Price = product.Price; productDb.CreationDate = product.CreationDate; } } await context.SaveChangesAsync(); } return(true); } catch (Exception ex) { MessageBox.Show(ex.Message); return(false); } }
private async Task <List <Product> > GetProductsAsync() { using (var context = new PmContext()) { return(await context.Products.ToListAsync()); } }
public async Task <Unit> Handle(DeleteEntryCommand request, CancellationToken cancellationToken) { var entry = await PmContext.Entries.FirstOrDefaultAsync(x => x.Id == request.EntryId); if (entry == null) { throw new Exception(); } if (!VaultService.ValidateVaultPassword(entry.VaultId, request.MasterPassword)) { throw new Exception("Podano nie poprawne hasło"); } PmContext.Remove(entry); PmContext.SaveChanges(); return(Unit.Value); }
public async Task <Unit> Handle(CreateVaultCommand request, CancellationToken cancellationToken) { var vault = new Vault() { MasterSalt = Encryptor.GenerateSalt(), MasterPassword = "", Name = request.Name, Username = UserResolverService.GetUsername() }; vault.MasterPassword = CryptoService.HashString(request.MasterPassword, vault.MasterSalt); PmContext.Add(vault); await PmContext.SaveChangesAsync(); return(Unit.Value); }
public async Task <string> Handle(AddUserToRoleCommand request, CancellationToken cancellationToken) { using var erpTrans = PmContext.Database.BeginTransaction(); UserRole userRole = new UserRole() { RoleName = request.Rolename, Username = request.Username }; PmContext.UserRoles.Add(userRole); await PmContext.SaveChangesAsync(cancellationToken); erpTrans.Commit(); return(request.Username); }
public async Task <Unit> Handle(RemoveUserCommand request, CancellationToken cancellationToken) { using var erpTrans = PmContext.Database.BeginTransaction(); var a = await PmContext.Users.FirstOrDefaultAsync(item => item.Username == request.Username); if (a == null) { //TODO throw new Exception(); } //TODO add validation PmContext.Users.Remove(a); await PmContext.SaveChangesAsync(); erpTrans.Commit(); return(Unit.Value); }
public async Task <string> Handle(RemoveUserFromRoleCommand request, CancellationToken cancellationToken) { using var erpTrans = PmContext.Database.BeginTransaction(); UserRole userRole = await PmContext.UserRoles.FirstOrDefaultAsync(item => item.Username == request.Username && item.RoleName == request.Rolename); if (userRole != null) { PmContext.UserRoles.Remove(userRole); await PmContext.SaveChangesAsync(cancellationToken); } else { throw new Exception(request.Rolename + " " + request.Username); } erpTrans.Commit(); return(request.Username); }
private async Task <bool> DeleteProductAsync(Product product) { try { using (var context = new PmContext()) { var productDb = await context.Products.FirstOrDefaultAsync(x => x.Id == product.Id); context.Products.Remove(productDb); await context.SaveChangesAsync(); } return(true); } catch (Exception ex) { MessageBox.Show(ex.Message); return(false); } }
public async Task <string> Handle(UpdateUserCommand request, CancellationToken cancellationToken) { using var erpTrans = PmContext.Database.BeginTransaction(); var user = await PmContext.Users.FirstOrDefaultAsync(item => item.Username == request.Username); if (user == null) { throw new Exception("Nie znaleziono użytkownika"); } user.Username = request.Username; user.Password = Encryptor.Encode(request.Password); PmContext.Users.Update(user); await PmContext.SaveChangesAsync(); erpTrans.Commit(); return(user.Username); }
public async Task <Unit> Handle(UpdateEntryCommand request, CancellationToken cancellationToken) { var entry = await PmContext.Entries.FirstOrDefaultAsync(item => item.Id == request.Id); if (entry == null) { throw new Exception(); } if (!VaultService.ValidateVaultPassword(entry.VaultId, request.MasterPassword)) { throw new Exception("Podano nie poprawne hasło"); } entry.Email = request.Email; entry.Login = request.Login; entry.Password = request.Password; entry.Portal = request.Portal; PmContext.Update(entry); PmContext.SaveChanges(); return(Unit.Value); }