public async Task <CommandResult> ExecuteAsync(CashbackCommandsHandler handler) { var obj = new Sale(Id, CustomerName); foreach (var item in Items) { var album = await handler.DbContext .Albums .Include(i => i.Genre) .ThenInclude(t => t.Cashbacks) .Where(w => w.Id == item.AlbumId) .FirstOrDefaultAsync(); if (album == null) { return(await Task.FromResult(new CommandResult(ErrorCode.NotFound, $"Album with id {item.AlbumId} was not found"))); } var saleItem = new SaleItem(obj.Id, album.Id); saleItem.Sale = obj; saleItem.Album = album; saleItem.CalcCashback(); obj.AddItem(saleItem); } obj.TotalValue = obj.Items.Sum(s => s.Album.Value); obj.TotalCashback = obj.Items.Sum(s => s.CashbackValue); await handler.DbContext.Sales.AddAsync(obj); await handler.DbContext.SaleItems.AddRangeAsync(obj.Items); var rows = await handler.DbContext.SaveChangesAsync(); return(await Task.FromResult(new CommandResult(rows, ErrorCode.None))); }
public void SaleTest() { var album1 = new Album(null, null, "Test Album 1", genres[0].Id); album1.Genre = genres[0]; var album2 = new Album(null, null, "Test Album 2", genres[1].Id); album2.Genre = genres[1]; var album3 = new Album(null, null, "Test Album 2", genres[2].Id); album3.Genre = genres[2]; var album4 = new Album(null, null, "Test Album 2", genres[3].Id); album4.Genre = genres[3]; var sale = new Sale(null, "Test Customer"); var saleItem1 = new SaleItem(sale.Id, album1.Id); saleItem1.Album = album1; saleItem1.Sale = sale; var saleItem2 = new SaleItem(sale.Id, album2.Id); saleItem2.Album = album2; saleItem2.Sale = sale; var saleItem3 = new SaleItem(sale.Id, album3.Id); saleItem3.Album = album3; saleItem3.Sale = sale; var saleItem4 = new SaleItem(sale.Id, album4.Id); saleItem4.Album = album4; saleItem4.Sale = sale; saleItem1.CalcCashback(); saleItem2.CalcCashback(); saleItem3.CalcCashback(); saleItem4.CalcCashback(); sale.AddItem(saleItem1); sale.AddItem(saleItem2); sale.AddItem(saleItem3); sale.AddItem(saleItem4); sale.TotalCashback = sale.Items.Sum(s => s.CashbackValue); sale.TotalValue = sale.Items.Sum(s => s.Album.Value); Assert.NotNull(sale); Assert.Equal((album1.Value + album2.Value + album3.Value + album4.Value), sale.TotalValue); Assert.Equal((saleItem1.CashbackValue + saleItem2.CashbackValue + saleItem3.CashbackValue + saleItem4.CashbackValue), sale.TotalCashback); }
public void CashbackValueTest() { var album1 = new Album(null, null, "Test Album 1", genres[0].Id); album1.Genre = genres[0]; var sale = new Sale(null, "Test Customer"); var obj = new SaleItem(sale.Id, album1.Id); obj.Album = album1; foreach (var genre in genres) { Assert.Equal(7, genre.Cashbacks.Count()); } obj.CalcCashback(); Assert.NotNull(obj); Assert.Equal(obj.CashbackValue, (obj.Album.Value * (obj.Album.Genre.GetCashback(sale.Date.DayOfWeek) / 100))); }