Esempio n. 1
0
        public async Task List_Only_Current_Month_Orders()
        {
            var options = new DbContextOptionsBuilder <CashbackContext>()
                          .UseInMemoryDatabase(databaseName: $"Teste_OrderService{Guid.NewGuid()}")
                          .Options;

            var retailerCpf        = new Cpf("408.477.340-99");
            var orderReferenceDate = DateTime.Now;


            using (var context = new CashbackContext(options))
            {
                var retailerRepository = new RetailerRepository(context);

                await retailerRepository.Add(new Domain.Retailers.Retailer(new Domain.Dtos.Retailers.CreateRetailerDto()
                {
                    CPF      = retailerCpf.Value,
                    Name     = "Test name",
                    Email    = "*****@*****.**",
                    Password = "******"
                }));

                var retailer = await retailerRepository.Find(retailerCpf);


                var orderRepository = new OrderRepository(context);

                await orderRepository.Add(new Order("ABC", 20, orderReferenceDate.AddMonths(-5), retailer));

                await orderRepository.Add(new Order("DEF", 30, orderReferenceDate, retailer));

                await orderRepository.Add(new Order("GHI", 40, orderReferenceDate.AddMonths(5), retailer));
            }
            using (var context = new CashbackContext(options))
            {
                var retailerRepository = new RetailerRepository(context);
                var orderRepository    = new OrderRepository(context);


                var orderService = new OrderService(
                    retailerRepository,
                    orderRepository,
                    new CashbackService(),
                    new Mock <ILogger <OrderService> >().Object);

                var orders = await orderService.ListCurrentMonth(retailerCpf.Value);

                Assert.Single(orders);
                Assert.Equal("DEF", orders.First().Code);
                Assert.Equal(30, orders.First().Value);
                Assert.Equal(OrderStatus.Validating, orders.First().Status);
                Assert.Equal(orderReferenceDate, orders.First().ReferenceDate);
                Assert.Equal(10, orders.First().CashbackPercent);
                Assert.Equal(3, orders.First().CashbackValue);
            }
        }
Esempio n. 2
0
        public async Task Create_Order_With_Approoved_Status_Success()
        {
            var options = new DbContextOptionsBuilder <CashbackContext>()
                          .UseInMemoryDatabase(databaseName: $"Teste_OrderService{Guid.NewGuid()}")
                          .Options;

            var retailerCpf        = new Cpf("15350946056");
            var orderReferenceDate = DateTime.Now;


            using (var context = new CashbackContext(options))
            {
                context.Database.EnsureCreated();

                var retailerRepository = new RetailerRepository(context);

                await retailerRepository.Add(new Domain.Retailers.Retailer(new Domain.Dtos.Retailers.CreateRetailerDto()
                {
                    CPF      = retailerCpf.Value,
                    Name     = "Test name",
                    Email    = "*****@*****.**",
                    Password = "******"
                }));

                var orderRepository = new OrderRepository(context);

                var orderService = new OrderService(
                    retailerRepository,
                    orderRepository,
                    new CashbackService(),
                    new Mock <ILogger <OrderService> >().Object);

                await orderService.Create(new Domain.Dtos.Orders.CreateOrderDto()
                {
                    Code          = "ABC",
                    ReferenceDate = orderReferenceDate,
                    Value         = 200
                }, retailerCpf.Value);
            }
            using (var context = new CashbackContext(options))
            {
                var orders = await context.Set <OrderDbModel>().ToListAsync();

                Assert.Single(orders);
                Assert.Equal("ABC", orders.First().Code);
                Assert.Equal(200, orders.First().Value);
                Assert.Equal(orderReferenceDate, orders.First().ReferenceDate);
                Assert.Equal(OrderStatus.Approved, orders.First().Status);
            }
        }
        public ActionResult SaveOrEdit(Retailer retailer)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (retailer.Id == 0)
                    {
                        RetailerRepository.Add(retailer);
                    }
                    else
                    {
                        RetailerRepository.Edit(retailer);
                    }
                    RetailerRepository.Save();

                    if (IsSuperAdmin)
                    {
                        return(RedirectToAction("Index", new { storeId = retailer.StoreId }));
                    }
                    else
                    {
                        return(RedirectToAction("Index"));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Unable to save:" + ex.StackTrace, retailer);
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }


            return(View(retailer));
        }