Esempio n. 1
0
        public async Task <BondoraCustomer> InsertOrUpdate(string username)
        {
            try
            {
                using (var context = new ApplicationDbContext())
                {
                    var dataUnique = await(from a in context.BondoraCustomer
                                           where a.Username == username
                                           select a).FirstOrDefaultAsync();
                    if (dataUnique == null)
                    {
                        var newItem = new BondoraCustomer();
                        newItem.Username             = username;
                        newItem.LoyaltyPoints        = 0;
                        context.Entry(newItem).State = EntityState.Added;
                        await context.SaveChangesAsync();

                        return(newItem);
                    }
                    else
                    {
                        dataUnique.Username             = username;
                        context.Entry(dataUnique).State = EntityState.Modified;
                        await context.SaveChangesAsync();

                        return(dataUnique);
                    }
                }
            } catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <Tuple <List <ModelCart>, BondoraOrder, BondoraCustomer> > Get(string token)
        {
            List <ModelCart> list            = null;
            BondoraOrder     order           = null;
            BondoraCustomer  customer        = null;
            CalculateViewer  calculateViewer = new CalculateViewer(new Calculate());
            string           PriceCur        = "€";

            try
            {
                using (var context = new ApplicationDbContext())
                {
                    order = await(from a in context.BondoraOrder
                                  where a.Token == token
                                  select a).FirstOrDefaultAsync();
                    if (order != null)
                    {
                        customer = await(from a in context.BondoraCustomer
                                         where a.CustomerId == order.CustomerId
                                         select a).FirstOrDefaultAsync();

                        list = await(from a in context.BondoraInventory
                                     join b in context.BondoraInventoryTypes on a.TypeId equals b.TypeId
                                     join c in context.BondoraCart on a.InventoryId equals c.InventoryId
                                     where c.Token == token
                                     select new ModelCart()
                        {
                            InventoryId = a.InventoryId,
                            Name        = a.Name,
                            TypeId      = a.TypeId,
                            TypeName    = b.TypeName,
                            Days        = c.Days,
                            Price       = 0
                        }).ToListAsync();
                        foreach (var item in list)
                        {
                            item.Price    = calculateViewer.CalculatePrices(item.TypeId, item.Days);
                            item.PriceCur = PriceCur;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(new Tuple <List <ModelCart>, BondoraOrder, BondoraCustomer>(list, order, customer));
        }
        public async Task <IActionResult> Order(string token)
        {
            string           priceCur = ConstClass.priceCurrency;
            int              count = 0, loyaltyPoints = 0;
            decimal          total           = 0;
            int              customerId      = 0;
            List <ModelCart> list            = null;
            CalculateViewer  calculateViewer = new CalculateViewer(new Calculate());
            StringBuilder    strb            = new StringBuilder();

            ModelParameters modelParameters = cache.Get <ModelParameters>("globalParams");

            if (modelParameters != null)
            {
                token      = modelParameters.Token;
                customerId = modelParameters.CustomerId;
            }

            if (localizer != null)
            {
                ViewData["Title"]          = localizer["Title"].Value;
                ViewBag.Name               = localizer["Name"].Value;
                ViewBag.Type               = localizer["Type"].Value;
                ViewBag.Duration           = localizer["Duration"].Value;
                ViewBag.DateOrderLabel     = localizer["DateOrder"].Value;
                ViewBag.ProductsCountLabel = localizer["ProductsCount"].Value;
                ViewBag.CustomerNameLabel  = localizer["Customer"].Value;
                ViewBag.LoyaltyPointsLabel = localizer["LoyaltyPoints"].Value;
                ViewBag.StartNewOrder      = localizer["StartNewOrder"].Value;
                ViewBag.Download           = localizer["Download"].Value;
                ViewBag.Print              = localizer["Print"].Value;
            }

            try
            {
                ApiOrderController orderController = new ApiOrderController(apiOrderRepository);
                Tuple <List <ModelCart>, BondoraOrder, BondoraCustomer> tuple = await orderController.Get(token);

                if (tuple != null)
                {
                    list = tuple.Item1;
                    BondoraOrder    order    = tuple.Item2;
                    BondoraCustomer customer = tuple.Item3;
                    if (logger != null)
                    {
                        logger.LogInformation("ApiOrderController result: {0}", list);
                    }

                    if (list == null)
                    {
                        ViewBag.StatusMessage = localizer["NotFound"].Value;
                    }
                    else
                    {
                        ViewBag.ProductsCount = list.Count();
                        if (order != null)
                        {
                            ViewBag.OrderTitle = localizer["OrderNo"].Value + " " + order.OrderId;
                            ViewBag.DateOrder  = order.DateOrder;
                        }
                        if (customer != null)
                        {
                            ViewBag.CustomerName = customer.Username;
                        }


                        strb.Append("<br /><table width='100%'>");
                        strb.Append("<tr><th>" + localizer["Name"].Value + "</th><th>" + localizer["Type"].Value + "</th><th>" + localizer["Days"].Value + "</th><th>" + localizer["Price"].Value + "</th></tr>");
                        foreach (var item in list)
                        {
                            count         += 1;
                            loyaltyPoints += calculateViewer.CalculatePoints(item.TypeId);
                            item.Price     = calculateViewer.CalculatePrices(item.TypeId, item.Days);
                            item.PriceCur  = priceCur;
                            total         += item.Price;

                            strb.Append("<tr bgcolor='" + IsParity(2, count - 1) + "'><td>" + item.Name + "</td><td>" + item.TypeName + "</td><td>" + item.Days + "</td><td>" + item.Price + " " + item.PriceCur + "</td></tr>");
                        }
                        strb.Append("<tr><td colspan=2></td><td align=right>" + localizer["Total"].Value + ":</td><td class='price'>" + total + " " + priceCur + "</td></tr>");
                        strb.Append("</table>");
                        ViewBag.OrderProducts = strb.ToString();
                        ViewBag.LoyaltyPoints = loyaltyPoints;

                        ApiCustomerController apiCustomerController = new ApiCustomerController(apiCustomerRepository);
                        await apiCustomerController.updateLoyalty(customerId, loyaltyPoints);
                    }
                }
                return(View(list));
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message + "; " + ex.InnerException);
                ViewBag.StatusMessage = ex.Message + "<br />" + ex.InnerException;
                return(View());
            }
        }