private async Task DeleteUserData(int userId) { var user = await context.Users.FirstOrDefaultAsync(u => u.Id == userId); if (user == null) { throw new NotFoundException($"The user with id {userId} does not exist"); } var transactionsToDelete = await context.Transactions.Where(t => t.UserId == userId).ToListAsync(); foreach (var trans in transactionsToDelete) { await mediator.Send(new DeleteTransaction.Command(trans.Id, userId)).ConfigureAwait(false); } var keywords = await context.Keywords.Where(k => k.UserId == userId).ToArrayAsync(); context.Keywords.RemoveRange(keywords); var cashWithdrawals = await context.CashWithdrawals.Where(c => c.UserId == userId).ToArrayAsync(); context.CashWithdrawals.RemoveRange(cashWithdrawals); var locations = await context.Locations.Where(l => l.UserId == userId).ToArrayAsync(); context.Locations.RemoveRange(locations); var categories = await context.Categories.Where(c => c.UserId == userId).ToArrayAsync(); context.Categories.RemoveRange(categories); await context.SaveChangesAsync(); }
public async Task <CategoryOut[]> Handle(Query request, CancellationToken cancellationToken) { var category = await context.Categories.Where(k => k.UserId == request.Category.UserId && k.Id == request.Category.Id) .SingleOrDefaultAsync() .ConfigureAwait(false); if (category != null) { category.CategoryName = request.Category.CategoryName; category.Icon = request.Category.Icon; category.Color = request.Category.Color; await context.SaveChangesAsync().ConfigureAwait(false); } else { var msg = $"Category {request.Category.Id} not found for user {request.Category.UserId}"; throw new NotFoundException(msg); } var categories = await context.Categories .Where(c => c.UserId == request.Category.UserId) .Include(c => c.User) .ToListAsync() .ConfigureAwait(false); return(categories.Select(c => mapper.Map <CategoryOut>(c)).ToArray()); }
public async Task <KeywordOut[]> Handle(Query request, CancellationToken response) { var keyword = await context.Keywords.Where(k => k.UserId == request.Keyword.UserId && k.Id == request.Keyword.Id) .SingleOrDefaultAsync() .ConfigureAwait(false); if (keyword != null) { keyword.KeywordName = request.Keyword.KeywordName; await context.SaveChangesAsync().ConfigureAwait(false); } else { var msg = $"Keyword {request.Keyword.Id} not found for user {request.Keyword.UserId}"; throw new NotFoundException(msg); } var keywords = await context.Keywords .Where(k => k.UserId == request.Keyword.UserId) .Include(k => k.User) .ToListAsync() .ConfigureAwait(false); return(keywords.Select(k => mapper.Map <KeywordOut>(k)).ToArray()); }
protected override Task Handle(Command request, CancellationToken response) { var transaction = mapper.Map <Transaction>(request.TransactionIn); context.Transactions.Add(transaction); return(context.SaveChangesAsync()); }
protected override Task Handle(Command request, CancellationToken response) { var cashWithdrawal = mapper.Map <CashWithdrawal>(request.CashWithdrawalIn); context.CashWithdrawals.Add(cashWithdrawal); return(context.SaveChangesAsync()); }
public async Task <LocationOut[]> Handle(Query request, CancellationToken response) { var location = await context.Locations.Where(l => l.UserId == request.Location.UserId && l.Id == request.Location.Id) .SingleOrDefaultAsync() .ConfigureAwait(false); if (location != null) { location.LocationName = request.Location.LocationName; location.CountryId = request.Location.CountryId; await context.SaveChangesAsync().ConfigureAwait(false); } else { var msg = $"Location {request.Location.Id} not found for user {request.Location.UserId}"; throw new NotFoundException(msg); } var locations = await context.Locations .Where(l => l.UserId == request.Location.UserId) .Include(l => l.Country) .Include(l => l.User) .ToListAsync() .ConfigureAwait(false); return(locations.Select(l => mapper.Map <LocationOut>(l)).ToArray()); }
protected override async Task Handle(Command request, CancellationToken response) { if (request.TransactionIn.UserId != request.TokenUserId) { throw new NotFoundException($"User {request.TokenUserId} is trying to update a transaction for user {request.TransactionIn.UserId}"); } var dbTransaction = await context.Transactions .Include(t => t.TransactionKeywords) .AsNoTracking() .SingleOrDefaultAsync(t => t.Id == request.TransactionIn.Id && t.UserId == request.TokenUserId) .ConfigureAwait(false); if (dbTransaction == null) { throw new NotFoundException($"Trying to update transaction {request.TransactionIn.Id} for user {request.TokenUserId} and it does not exist"); } var transaction = mapper.Map <Transaction>(request.TransactionIn); var updateKeywords = transaction.TransactionKeywords .Select(tk => tk.KeywordId) .ToArray(); var dbKeywords = dbTransaction.TransactionKeywords .Select(tk => tk.KeywordId) .ToArray(); var keywordsToAdd = updateKeywords.Except(dbKeywords); var keywordsToDelete = dbKeywords.Except(updateKeywords); transaction.TransactionKeywords.Clear(); if (keywordsToAdd.Any()) { foreach (var id in keywordsToAdd) { transaction.TransactionKeywords.Add(new TransactionKeyword { KeywordId = id }); } } context.Transactions.Update(transaction); await context.SaveChangesAsync().ConfigureAwait(false); if (keywordsToDelete.Any()) { await context.Database.ExecuteSqlCommandAsync( $"DELETE FROM [app].[TransactionKeyword] WHERE TransactionId = {transaction.Id} and KeywordId in ({string.Join(',', keywordsToDelete)})" ).ConfigureAwait(false); } }
protected override Task Handle(Command request, CancellationToken response) { if (request.CashWithdrawal.UserId != request.TokenUserId) { throw new NotFoundException($"User {request.TokenUserId} is trying to update a cash withdrawal for user {request.CashWithdrawal.UserId}"); } var cashWithdrawal = mapper.Map <CashWithdrawal>(request.CashWithdrawal); context.CashWithdrawals.Update(cashWithdrawal); return(context.SaveChangesAsync()); }
public async Task <LocationOut[]> Handle(Query request, CancellationToken response) { context.Locations.Add(request.Location); await context.SaveChangesAsync().ConfigureAwait(false); var locations = await context.Locations .Where(l => l.UserId == request.Location.UserId) .Include(l => l.Country) .Include(l => l.User) .ToListAsync() .ConfigureAwait(false); return(locations.Select(l => mapper.Map <LocationOut>(l)).ToArray()); }
public async Task <CategoryOut[]> Handle(Query request, CancellationToken cancellationToken) { var categories = request.Categories.Select(c => mapper.Map <Category>(c)); context.Categories.AddRange(categories); await context.SaveChangesAsync().ConfigureAwait(false); var userId = request.Categories.First().UserId; var allCategories = await context.Categories .Where(c => c.UserId == userId) .Include(c => c.User) .ToListAsync() .ConfigureAwait(false); return(allCategories.Select(c => mapper.Map <CategoryOut>(c)).ToArray()); }
public async Task <UserOut> Handle(Query request, CancellationToken cancellationToken) { var user = await context.Users .SingleOrDefaultAsync(u => u.Id == request.UserId) .ConfigureAwait(false); if (user == null) { throw new NotFoundException($"User {request.UserId} not found"); } var json = JsonConvert.SerializeObject(request.Preferences); user.Preferences = json; await context.SaveChangesAsync().ConfigureAwait(false); return(mapper.Map <UserOut>(user)); }
protected override async Task Handle(Command request, CancellationToken response) { var cashWithdrawal = await context.CashWithdrawals .Include(t => t.User) .SingleOrDefaultAsync(t => t.Id == request.CashWithdrawalId && t.UserId == request.TokenUserId) .ConfigureAwait(false); if (cashWithdrawal != null) { context.CashWithdrawals.Remove(cashWithdrawal); await context.SaveChangesAsync(); } else { throw new NotFoundException($"Cash Withdrawal {request.CashWithdrawalId} not found"); } }
protected override async Task Handle(Command request, CancellationToken response) { var transaction = await context.Transactions .Include(t => t.User) .Include(t => t.TransactionKeywords) .SingleOrDefaultAsync(t => t.Id == request.TransactionId && t.UserId == request.TokenUserId) .ConfigureAwait(false); if (transaction != null) { context.Transactions.Remove(transaction); await context.SaveChangesAsync(); } else { throw new NotFoundException($"Transaction {request.TransactionId} not found"); } }
public async Task <KeywordOut[]> Handle(Query request, CancellationToken cancellationToken) { var keywordsIn = request.Keywords .Select(k => new Keyword() { KeywordName = k, UserId = request.UserId }) .ToArray(); context.Keywords.AddRange(keywordsIn); await context.SaveChangesAsync().ConfigureAwait(false); var keywordsOut = await context.Keywords .Where(k => k.UserId == request.UserId) .Include(k => k.User) .ToListAsync() .ConfigureAwait(false); return(keywordsOut.Select(k => mapper.Map <KeywordOut>(k)).ToArray()); }
public async Task <AuthenticatedUserOut> Handle(Command request, CancellationToken cancellationToken) { var userExists = await context.Users.AnyAsync(u => u.Email == request.LoginDetails.Email).ConfigureAwait(false); if (userExists) { throw new UserAlreadyExistsException(); } var user = mapper.Map <User>(request.LoginDetails); user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(request.LoginDetails.Password); context.Users.Add(user); try { await context.SaveChangesAsync().ConfigureAwait(false); var userOut = mapper.Map <AuthenticatedUserOut>(user); userOut.Token = tokenGenerator.CreateTokenString(user); return(userOut); } catch (DbUpdateException ex) { Exception resultEx = ex; var sex = ex.InnerException as SqlException; if (sex != null) { switch (sex.Number) { case 2601: resultEx = new UserAlreadyExistsException(); break; } } throw resultEx; } }