public dynamic Get(int id)
 {
     using (ShippingContext _context = new ShippingContext())
     {
         return(_context.ShippingDetails.Where(si => si.Id == id).Single());
     }
 }
        public async Task <IHttpActionResult> AddToCart(dynamic data)
        {
            var cartId    = (int)data.CartId;
            var productId = (int)data.ProductId;
            var quantity  = (int)data.Quantity;

            using (var db = new ShippingContext())
            {
                var shippingDetails = db.ProductsShippingDetails
                                      .Where(o => o.ProductId == productId)
                                      .Single();

                var cartItem = new ShoppingCartItem()
                {
                    CartId               = cartId,
                    RequestId            = Request.Headers.GetValues("request-id").Single(),
                    ProductId            = productId,
                    Quantity             = quantity,
                    ItemShippingCost     = shippingDetails.Cost,
                    FreeShippingEligible = shippingDetails.FreeShippingEligible
                };

                db.ShoppingCartItems.Add(cartItem);
                await db.SaveChangesAsync();
            }
            return(StatusCode(HttpStatusCode.OK));
        }
 public ProductsController(
     ShippingContext context,
     IMapper mapper)
 {
     _context = context;
     _mapper  = mapper;
 }
Ejemplo n.º 4
0
        public IActionResult Initialize()
        {
            var boxesModel = new List <Box>();

            using (var db = new ShippingContext())
            {
                db.Boxes.RemoveRange(db.Boxes);
                db.Boxes.Add(new Box {
                    BoxId = 123450, Weight = 3
                });
                db.Boxes.Add(new Box {
                    BoxId = 123461, Weight = 8
                });
                db.Boxes.Add(new Box {
                    BoxId = 123472, Weight = 11
                });
                db.Boxes.Add(new Box {
                    BoxId = 123483, Weight = 3
                });
                db.Boxes.Add(new Box {
                    BoxId = 123494, Weight = 13
                });
                db.SaveChanges();
                boxesModel = db.Boxes.ToList();
            }
            return(View(boxesModel));
        }
Ejemplo n.º 5
0
        public IActionResult CalculatePartDetails()
        {
            PartsModel partsModel;

            using (var db = new ShippingContext())
            {
                db.Parts.RemoveRange(db.Parts);
                foreach (var box in db.Boxes)
                {
                    var W     = box.Weight;
                    var PC    = box.PartCount;
                    var floor = (int)(W / PC);
                    var mod   = W % PC;
                    for (var p = 0; p < PC; p++)
                    {
                        var part = new Part
                        {
                            BoxId      = box.BoxId,
                            PartWeight = p == 0 ? floor + mod : floor
                        };
                        part.PartCost = CalculateCost(part.PartWeight);
                        db.Parts.Add(part);
                    }
                }
                db.SaveChanges();
                partsModel = new PartsModel {
                    Parts = db.Parts.OrderBy(p => p.BoxId).ToList()
                };
            }
            return(View(partsModel));
        }
        public dynamic Get(int id)
        {
            using var db = new ShippingContext();
            var item = db.ProductShippingOptions
                       .Include(pso => pso.Options)
                       .SingleOrDefault(o => o.ProductId == id);

            return(item);
        }
        public dynamic Order(int id)
        {
            using (var db = new ShippingContext())
            {
                var info = db.ShippingInfos
                           .Where(si => si.OrderId == id)
                           .SingleOrDefault();

                return(info);
            }
        }
Ejemplo n.º 8
0
        public dynamic Get(int id)
        {
            using (var db = new ShippingContext())
            {
                var item = db.ProductsShippingDetails
                           .Where(o => o.ProductId == id)
                           .SingleOrDefault();

                return(item);
            }
        }
Ejemplo n.º 9
0
        public IEnumerable <dynamic> Get(string ids)
        {
            using (var db = new ShippingContext())
            {
                var productIds = ids.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(s => int.Parse(s)).ToArray();
                var items      = db.ProductsShippingDetails
                                 .Where(status => productIds.Any(id => id == status.ProductId))
                                 .ToArray();

                return(items);
            }
        }
        public IEnumerable <dynamic> Orders(string ids)
        {
            using (var db = new ShippingContext())
            {
                var _ids = ids.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(s => int.Parse(s)).ToArray();
                var info = db.ShippingInfos
                           .Where(si => _ids.Any(id => id == si.OrderId))
                           .ToArray();

                return(info);
            }
        }
Ejemplo n.º 11
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            ShippingContext.CreateSeedData();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors("AllowAllOrigins");
            app.UseMvc();
        }
 public async Task Handle(CleanupCart message, IMessageHandlerContext context)
 {
     using (var db = new ShippingContext())
     {
         var shoppingCartItem = db.ShoppingCartItems.SingleOrDefault(item => item.RequestId == message.RequestId);
         if (shoppingCartItem != null)
         {
             db.ShoppingCartItems.Remove(shoppingCartItem);
             await db.SaveChangesAsync();
         }
     }
 }
        public dynamic Get(int id)
        {
            using (var db = ShippingContext.Create())
            {
                var item = db.ProductShippingOptions
                           .Include(pso => pso.Options)
                           .Where(o => o.ProductId == id)
                           .SingleOrDefault();

                return(item);
            }
        }
Ejemplo n.º 14
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var dataBaseRoot = new InMemoryDatabaseRoot();

            services.AddDbContext <ApiContext>(opt => opt.UseInMemoryDatabase("BooksDb"));
            services.AddDbContext <OrderContext>(opt => opt.UseInMemoryDatabase("OrdersDb"));
            services.AddDbContext <ShippingContext>(opt => opt.UseInMemoryDatabase("ShippingDb", dataBaseRoot));
            services.AddTransient <PurchaseOrderReceivedHandler>();
            var sp  = services.BuildServiceProvider();
            var bus = new InMemoryMessageBus();

            var optionsBuilder = new DbContextOptionsBuilder <ShippingContext>();

            optionsBuilder.UseInMemoryDatabase("ShippingDb", dataBaseRoot);
            var context = new ShippingContext(optionsBuilder.Options);
            var shippingOrderHandler = new PurchaseOrderReceivedHandler(bus, context);

            bus.RegisterHandler <PurchaseOrderReceived>(e => shippingOrderHandler.Handle(e));

            services.AddSingleton <InMemoryMessageBus>(bus);

            // services.AddAuthentication(options =>
            //   {
            //     options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            //   })
            //     .AddJwtBearer(jwtOptions =>
            //     {
            //       jwtOptions.Authority = $"https://login.microsoftonline.com/tfp/{Configuration["AzureAdB2C:Tenant"]}/{Configuration["AzureAdB2C:Policy"]}/v2.0/";
            //       jwtOptions.Audience = Configuration["AzureAdB2C:ClientId"];
            //       jwtOptions.Events = new JwtBearerEvents
            //       {
            //         OnAuthenticationFailed = AuthenticationFailed
            //       };
            //     });

            services.AddAzureAdB2CAuthentication();

            services.AddMvc(setupAction => {
                var inputFormatter = setupAction.InputFormatters.OfType <JsonInputFormatter>().FirstOrDefault();

                if (inputFormatter != null)
                {
                    inputFormatter.SupportedMediaTypes.Add("application/json-patch+json");
                }
            });
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "Books Store", Version = "v1"
                });
            });
        }
Ejemplo n.º 15
0
        public static ProcessingResult ProcessRules(ShippingContext shippingContext, List <Rule> rules)
        {
            ProcessingResult ruleProcessingResult = new ProcessingResult();

            rules.ForEach(rule =>
            {
                ruleProcessingResult = rule.Func(shippingContext);
                shippingContext.processingResult.discount = ruleProcessingResult.discount;
            });

            shippingContext.totalDiscount += ruleProcessingResult.discount;

            return(ruleProcessingResult);
        }
        public IEnumerable <dynamic> ByStockItem(string ids)
        {
            using (ShippingContext _context = new ShippingContext())
            {
                var _ids = ids.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                           .Select(id => int.Parse(id))
                           .ToList();

                var query = from si in _context.ShippingDetails
                            where _ids.Contains(si.StockItemId)
                            select si;

                return(query.ToList());
            }
        }
Ejemplo n.º 17
0
        public async Task Handle(CleanupFailedCartRequest message, IMessageHandlerContext context)
        {
            using (var db = ShippingContext.Create())
            {
                var requestItem = await db.ShoppingCartItems
                                  .Where(o => o.RequestId == message.RequestId)
                                  .SingleOrDefaultAsync();

                if (requestItem != null)
                {
                    db.ShoppingCartItems.Remove(requestItem);
                    await db.SaveChangesAsync();
                }
            }
        }
Ejemplo n.º 18
0
        public async Task Handle(ShoppingCartGotInactive message, IMessageHandlerContext context)
        {
            Console.WriteLine($"Ready to wipe cart {message.CartId}.", Color.Yellow);

            using (var db = ShippingContext.Create())
            {
                var cartItems = await db.ShoppingCartItems
                                .Where(o => o.CartId == message.CartId)
                                .ToListAsync();

                db.ShoppingCartItems.RemoveRange(cartItems);
                await db.SaveChangesAsync();
            }

            Console.WriteLine($"Cart {message.CartId} wiped.", Color.Green);
        }
        public dynamic Order(int orderNumber)
        {
            using (var db = new ShippingContext())
            {
                var info = db.ShippingInfos
                           .Where(si => si.OrderNumber == orderNumber)
                           .SingleOrDefault();

                return(new
                {
                    info.OrderNumber,
                    info.Courier,
                    info.Status
                });
            }
        }
Ejemplo n.º 20
0
        public dynamic Order(int orderNumber)
        {
            using (var db = new ShippingContext())
            {
                var shipment = db.Shipments
                               .Where(s => s.OrderNumber == orderNumber)
                               .SingleOrDefault();

                return(new
                {
                    shipment.OrderNumber,
                    shipment.Courier,
                    shipment.Status
                });
            }
        }
Ejemplo n.º 21
0
        public IActionResult CalculatePartCounts()
        {
            var boxesModel = new List <Box>();

            using (var db = new ShippingContext())
            {
                var orderedBoxes = db.Boxes.OrderBy(b => b.Weight);
                var partCount    = 1;
                foreach (var box in orderedBoxes)
                {
                    box.PartCount = ++partCount;
                }
                db.SaveChanges();
                boxesModel = db.Boxes.ToList();
            }
            return(View(boxesModel));
        }
Ejemplo n.º 22
0
        public async Task <dynamic> Order(int orderNumber)
        {
            using (var db = new ShippingContext())
            {
                var shipment = await db.Shipments
                               .Where(s => s.OrderNumber == orderNumber)
                               .Select(s => new { s.OrderNumber, s.Courier, s.Status })
                               .SingleOrDefaultAsync();

                return(new
                {
                    shipment.OrderNumber,
                    shipment.Courier,
                    shipment.Status
                });
            }
        }
Ejemplo n.º 23
0
        public async Task <IEnumerable <dynamic> > Orders(string orderNumbers)
        {
            using (var db = new ShippingContext())
            {
                var orderNumbersArray = orderNumbers.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(s => int.Parse(s)).ToArray();
                var shipments         = await db.Shipments
                                        .Where(s => orderNumbersArray.Any(id => id == s.OrderNumber))
                                        .Select(s => new
                {
                    s.OrderNumber,
                    s.Courier,
                    s.Status
                })
                                        .ToArrayAsync();

                return(shipments);
            }
        }
        public IEnumerable <dynamic> Orders(string orderNumbers)
        {
            using (var db = new ShippingContext())
            {
                var orderNumbersArray = orderNumbers.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(s => int.Parse(s)).ToArray();
                var infos             = db.ShippingInfos
                                        .Where(si => orderNumbersArray.Any(id => id == si.OrderNumber))
                                        .Select(si => new
                {
                    si.OrderNumber,
                    si.Courier,
                    si.Status
                })
                                        .ToArray();

                return(infos);
            }
        }
Ejemplo n.º 25
0
        public IEnumerable <dynamic> GetCart(string ids)
        {
            using (var db = ShippingContext.Create())
            {
                var productIds = ids.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(s => int.Parse(s)).ToArray();

                var cartItems = db.ShoppingCartItems
                                .Where(item => productIds.Any(id => id == item.ProductId))
                                .ToArray()
                                .GroupBy(cartItem => cartItem.ProductId)
                                .Select(group => new
                {
                    ProductId = group.Key,
                    group.First().DeliveryEstimate
                })
                                .ToArray();

                return(cartItems);
            }
        }
Ejemplo n.º 26
0
        public async Task Handle(AddItemToCart message, IMessageHandlerContext context)
        {
            using (var db = ShippingContext.Create())
            {
                var requestAlreadyHandled = await db.ShoppingCartItems
                                            .Where(o => o.RequestId == message.RequestId)
                                            .SingleOrDefaultAsync() != null;

                if (!requestAlreadyHandled)
                {
                    var shippingOptions = db.ProductShippingOptions
                                          .Include(so => so.Options)
                                          .Where(o => o.ProductId == message.ProductId)
                                          .Single();

                    var shortest = shippingOptions.Options.Min(o => o.EstimatedMinDeliveryDays);
                    var longest  = shippingOptions.Options.Max(o => o.EstimatedMaxDeliveryDays);
                    var estimate = "";
                    if (shortest == int.MaxValue && longest == int.MaxValue)
                    {
                        estimate = "ah ah ah ah ah ah";
                    }
                    else
                    {
                        estimate = $"between {shortest} and {longest} days";
                    }

                    db.ShoppingCartItems.Add(new ShoppingCartItem()
                    {
                        CartId           = message.CartId,
                        RequestId        = message.RequestId,
                        ProductId        = message.ProductId,
                        DeliveryEstimate = estimate,
                        Quantity         = message.Quantity
                    });

                    await db.SaveChangesAsync();
                }
            }
        }
        public async Task Handle(InactiveCartWiped message, IMessageHandlerContext context)
        {
            var backup = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.WriteLine($"Ready to wipe cart {message.CartId}...");

            using (var db = new ShippingContext())
            {
                var cartItems = db.ShoppingCartItems.Where(i => i.CartId == message.CartId);
                foreach (var item in cartItems)
                {
                    db.ShoppingCartItems.Remove(item);
                }
                await db.SaveChangesAsync();
            }

            Console.WriteLine($"Cart {message.CartId} wiped.");

            Console.ForegroundColor = backup;
        }
        public IEnumerable <dynamic> GetCart(string ids)
        {
            using (var db = new ShippingContext())
            {
                var productIds = ids.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(s => int.Parse(s)).ToArray();

                var cartItems = db.ShoppingCartItems
                                .Where(item => productIds.Any(id => id == item.ProductId))
                                .ToArray()
                                .GroupBy(cartItem => cartItem.ProductId)
                                .Select(group =>
                {
                    var cartId = group.First().CartId;
                    var freeShippingEligible = group.First().FreeShippingEligible;
                    var quantity             = group.Sum(cartItem => cartItem.Quantity);
                    var itemShippingCost     = group.First().ItemShippingCost;

                    var shippingCost = itemShippingCost;
                    if (quantity > 1)
                    {
                        shippingCost = itemShippingCost + (((itemShippingCost * 10) / 100) * quantity);
                    }

                    return(new
                    {
                        ProductId = group.Key,
                        CartId = cartId,
                        FreeShippingEligible = freeShippingEligible,
                        Quantity = quantity,
                        ShippingCost = shippingCost
                    });
                })
                                .ToArray();

                return(cartItems);
            }
        }
 public ShippingRepository(ShippingContext context)
 {
     _context = context;
 }
Ejemplo n.º 30
0
 public SchedulesController(ShippingContext context)
 {
     _context = context;
 }