Beispiel #1
0
        public async Task CreateSale(CreateSaleCommand command, CancellationToken token)
        {
            var products = _db.Products.Include(p => p.Inventory);
            var sale     = _mapper.Map <Sale>(command);
            var items    = _db.ItemsSold;

            //make deductions from the quantity of every product that has been sold
            foreach (var p in command.ProductsSold)
            {
                var product = await products.SingleOrDefaultAsync(x => x.Id == p.Id, token).ConfigureAwait(false);

                product.Quantity -= p.Quantity;
                if (product.Inventory != null)
                {
                    product.Inventory.Quantity -= p.Quantity;
                }
                _db.Entry(product).State = EntityState.Modified;
                var item = new ItemSold
                {
                    ItemName  = p.ItemName,
                    Quantity  = p.Quantity,
                    Price     = p.Price,
                    ProductId = product.Id
                };
                await items.AddAsync(item, token).ConfigureAwait(false);
            }
            //if there are any discounts then you can calculate before persisting.
            sale.ItemsSold.AddRange(items !);
            await _set.AddAsync(sale, token).ConfigureAwait(false);
        }
        public async Task <IActionResult> RegisterNewSale([FromBody] SaleDTO sale)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var createSaleCommand = new CreateSaleCommand {
                Model = sale
            };

            int id;

            try
            {
                id = await _mediator.Send(createSaleCommand);
            }
            catch
            {
                _logger.LogInformation($"{sale.Id} {SalesConstants.REGISTER_SALE_CONFLICT}");
                return(Conflict());
            }

            sale.Id = id;

            _logger.LogInformation($"{sale.Id} {SalesConstants.REGISTER_SALE_SUCCESS}");
            return(CreatedAtAction(nameof(RegisterNewSale), sale));
        }
Beispiel #3
0
        public async Task Handler_ShouldAddSale()
        {
            // Arrange
            var sale = new SaleDTO
            {
                Id     = 5,
                Date   = new DateTime(2020, 03, 01),
                Amount = 1000,
            };

            var command = new CreateSaleCommand {
                Model = sale
            };

            // Act
            var handler = new CreateSaleCommand.CreateSaleCommandHandler(Context, Mapper);
            await handler.Handle(command, CancellationToken.None);

            var entity = Context.Sales.Find(sale.Id);

            // Assert
            entity.ShouldNotBeNull();

            entity.Id.ShouldBe(command.Model.Id);
            entity.Date.ShouldBe(command.Model.Date);
            entity.Amount.ShouldBe(command.Model.Amount);
        }
Beispiel #4
0
        public async void CreateSaleTest()
        {
            var genre = new Genre(null, "Pop");

            new DefaultCashback().GetDefaultCashback(genre.Id, genre.Name);
            var album  = new Album(null, "spotifyrandomid", "new album", genre.Id);
            var album2 = new Album(null, "spotifyrandomid2", "new album 2", genre.Id);

            await CommandsHandler.DbContext.Genres.AddAsync(genre);

            await CommandsHandler.DbContext.Cashbacks.AddRangeAsync(genre.Cashbacks);

            await CommandsHandler.DbContext.Albums.AddAsync(album);

            await CommandsHandler.DbContext.Albums.AddAsync(album2);

            await CommandsHandler.DbContext.SaveChangesAsync();

            var items = new List <SaleItemCommand>();

            items.Add(new SaleItemCommand()
            {
                AlbumId = album.Id
            });
            items.Add(new SaleItemCommand()
            {
                AlbumId = album2.Id
            });

            var cmd = new CreateSaleCommand()
            {
                CustomerName = "new customer name",
                Items        = items.ToArray()
            };

            var result = await CommandsHandler.Handle(cmd);

            var obj = await DbContext.Sales.SingleOrDefaultAsync();

            Assert.True(result.Rows > 0);
            Assert.NotNull(obj);
            Assert.Equal(items.Count(), obj.Items.Count());
            Assert.Equal(obj.Items.Sum(s => s.Album.Value), obj.TotalValue);
            Assert.Equal(obj.Items.Sum(s => s.CashbackValue), obj.TotalCashback);
        }
        public void CanInsertSalesIntoDatabase()
        {
            Mock <IDatabaseService>  mockDatabaseService = new Mock <IDatabaseService>();
            Mock <IDateService>      mockDateService     = new Mock <IDateService>();
            Mock <IInventoryService> mockInvServ         = new Mock <IInventoryService>();
            Mock <ISaleFactory>      mockSaleFactory     = new Mock <ISaleFactory>();

            CreateSaleCommand createSaleCommand =
                new CreateSaleCommand(
                    mockDatabaseService.Object,
                    mockDateService.Object,
                    mockInvServ.Object,
                    mockSaleFactory.Object
                    );

            createSaleCommand.Execute(new CreateSaleModel()
            {
                CustomerId = 1,
                EmployeeId = 1,
                ProductId  = 1,
                Quantity   = 3
            });
        }
Beispiel #6
0
        //POST : /api/Users/Create
        public async Task <ActionResult <IEnumerable <object> > > Create([FromBody] SearchSaleRequestModel request)
        {
            var create = new CreateSaleCommand(_context);

            return(Ok(await Task.Run(() => create.CreateSale(request))));
        }
Beispiel #7
0
        public async Task <ActionResult <CommandResult> > Post([FromBody] CreateSaleCommand cmd)
        {
            var result = await _commandsHandler.Handle(cmd);

            return(Ok(result));
        }
        public async Task <ActionResult <Response <SaleApiModel> > > Post([FromBody] CreateSaleCommand command)
        {
            var result = await _mediator.Send(command).ConfigureAwait(false);

            return(CreatedAtRoute("GetSaleById", new { result.Data.Id }, result));
        }