public List <Dictionary <string, object> > GetPurchaseByUser(string user)
        {
            List <Dictionary <string, object> > jewelries = new List <Dictionary <string, object> >();

            ColmanInternetiotContext context = new ColmanInternetiotContext();

            // Get list of current account purchases
            List <Purchase> purchases = context.Purchase.Where(x => x.UserId == Account.GetCurrAccountId(User)).ToList();

            foreach (Purchase purchase in purchases)
            {
                Dictionary <string, object> dictJewelry = new Dictionary <string, object>();

                //Get the jewelry object of the jewelry that bought in this purchase
                Jewelry jewelry = context.Jewelry.First(x => x.Id == purchase.JewelryId);

                jewelry.Purchase = null;

                // Convert from Jewelry object to Dictionary
                dictJewelry = jewelry.GetType()
                              .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                              .ToDictionary(prop => prop.Name.ToLower(), prop => prop.GetValue(jewelry, null));

                // Add a summary attribute
                dictJewelry.Add("summary", purchase.Amount + " X " + jewelry.Price);

                jewelries.Add(dictJewelry);
            }

            return(jewelries);
        }
Пример #2
0
        public object GetJewelries(int id)
        {
            ColmanInternetiotContext _context = new ColmanInternetiotContext();

            var result = from jw in _context.Jewelry
                         join ctgr in _context.Category on jw.CategoryId equals ctgr.Id
                         join jwset in _context.JewelrySet on jw.SetId equals jwset.Id
                         where jw.Id == id
                         select new
            {
                jw.Id,
                jw.Amount,
                jw.Price,
                jw.Cart,
                jw.Name,
                jw.Description,
                jw.ImagePath,
                jw.Size,
                jw.Weight,
                jw.CategoryId,
                category = ctgr.Name,
                jw.SetId,
                set = jwset.Name
            };

            return(result.FirstOrDefault());
        }
        public Dictionary <string, int> getNumOfJewelriesInSet(string count)
        {
            ColmanInternetiotContext context = new ColmanInternetiotContext();

            Dictionary <string, int> dictJewelriesInSet = new Dictionary <string, int>();

            // Get a list of number of jewelries per set by group by query from DB
            var result = (from je in context.Jewelry
                          join jewSet in context.JewelrySet on je.SetId equals jewSet.Id
                          group je by jewSet.Name into cntJewls
                          select new
            {
                setName = cntJewls.Key,
                cntJewelries = cntJewls.ToList()
            });

            // Convery the result to dictionary
            foreach (var line in result)
            {
                dictJewelriesInSet[line.setName] = line.cntJewelries.Count;
            }

            // Return the dictionary
            return(dictJewelriesInSet);
        }
Пример #4
0
        public static int CreatedThisMonthCount()
        {
            ColmanInternetiotContext _context = new ColmanInternetiotContext();

            int countOfCraetedThisMonth = _context.Users.Where(c => c.CreationDate.Value.Year == DateTime.Today.Date.Year &&
                                                               c.CreationDate.Value.Month == DateTime.Today.Date.Month).Count();

            return(countOfCraetedThisMonth);
        }
        public static Dictionary <int, string> GetCategories()
        {
            Dictionary <int, string> categoriesDict = new Dictionary <int, string>();

            List <Category> categoriesFromDB = new ColmanInternetiotContext().Category.ToList();

            foreach (Category category in categoriesFromDB)
            {
                categoriesDict[category.Id] = category.Name;
            }

            return(categoriesDict);
        }
        public async Task <IActionResult> Edit(int id, [Bind("Weight,Price,Cart,Size,Description,Amount,Discount,Id,Diamonds,ImagePath,Name,CategoryId,SetId")] Jewelry jewelry)
        {
            if (id != jewelry.Id)
            {
                return(NotFound());
            }

            if (Account.isAdmin(User))
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        ColmanInternetiotContext newContext = new ColmanInternetiotContext();

                        int?currAmount = newContext.Jewelry.First(x => x.Id == jewelry.Id).Amount;

                        _context.Update(jewelry);
                        await _context.SaveChangesAsync();

                        if (currAmount != null)
                        {
                            string postToFacebook = ("לתכשיט " + jewelry.Name + " נוספו " + (jewelry.Amount - int.Parse(currAmount.ToString())) + " פריטים חדשים. היכנסו לאתר בשביל לקנות");

                            FacebookPost post = new FacebookPost();

                            post.PublishToFacebookNoPhoto(postToFacebook);
                        }
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!JewelryExists(jewelry.Id))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                    return(RedirectToAction("Index"));
                }
                ViewData["CategoryId"] = new SelectList(_context.Category, "Id", "Name", jewelry.CategoryId);
                ViewData["SetId"]      = new SelectList(_context.JewelrySet, "Id", "Name", jewelry.SetId);
                return(View(jewelry));
            }
            else
            {
                return(new RedirectToActionResult("NotAuthorized", "Home", null));
            }
        }
Пример #7
0
        public Dictionary <string, string> Get()
        {
            if (!Account.isAdmin(User))
            {
                return(null);
            }

            Dictionary <string, string> result = new Dictionary <string, string>();

            ColmanInternetiotContext dbcontext = new ColmanInternetiotContext();
            DateTime today            = DateTime.Today;
            DateTime quarterBeginning = new DateTime(today.Year, (today.Month - 1) / 3 * 3 + 1, 1);

            result.Add("earnings_since_the_beginning_of_the_day",
                       dbcontext.Purchase.Where(p => DateTime.Compare(today, p.Date.Value) <= 0)
                       .Select(p => p.Amount).Sum().ToString());

            result.Add("earnings_since_the_beginning_of_the_quarter",
                       dbcontext.Purchase.Where(p => DateTime.Compare(quarterBeginning, p.Date.Value) <= 0)
                       .Select(p => p.Amount).Sum().ToString());

            result.Add("earnings_since_the_beginning_of_the_quarter_by_country",
                       JsonConvert.SerializeObject(dbcontext.Purchase.Where(p => p.Country != null && p.Date != null && DateTime.Compare(quarterBeginning, p.Date.Value) <= 0)
                                                   .GroupBy(p => p.Country).Select(g => new
            {
                Country  = g.Key,
                Earnings = g.Select(p => p.Amount).Sum()
            }).OrderByDescending(g => g.Earnings).ToList()));

            result.Add("earnings_at_last_14_months_by_month",
                       JsonConvert.SerializeObject(dbcontext.Purchase.Where(p => DateTime.Compare(today.AddMonths(-14), p.Date.Value) <= 0)
                                                   .GroupBy(p => p.Date.Value.Month + "." + p.Date.Value.Year).Select(g => new
            {
                Month    = g.Key,
                Earnings = g.Select(p => p.Amount).Sum()
            }).OrderByDescending(g => g.Earnings).ToList()));

            result.Add("10_last_purchases",
                       JsonConvert.SerializeObject(dbcontext.Purchase.OrderByDescending(p => p.Id).Take(10).Select(p => new
            {
                jewrley = p.Jewelry.Name,
                client  = p.User.FName + " " + p.User.LName,
                money   = p.Jewelry.Price * p.Amount,
                img     = p.Jewelry.ImagePath
            }).ToList()));

            return(result);
        }
Пример #8
0
        public List <Jewelry> GetJewelries(string setOrCatagory, int id)
        {
            ColmanInternetiotContext context = new ColmanInternetiotContext();

            List <Jewelry> listJewelriesOnSetOrCatagory = new List <Jewelry>();

            if (setOrCatagory.ToLower() == "set")
            {
                listJewelriesOnSetOrCatagory = context.Jewelry.Where(x => x.SetId == id).ToList();
            }
            else if (setOrCatagory.ToLower() == "category")
            {
                listJewelriesOnSetOrCatagory = context.Jewelry.Where(x => x.CategoryId == id).ToList();
            }

            return(listJewelriesOnSetOrCatagory);
        }
Пример #9
0
        public async Task <Jewelry> Get()
        {
            ColmanInternetiotContext dbcontext = new ColmanInternetiotContext();
            string currentUserCountry          = await new PurchaseApiController().countrycode();
            string currentUserGender           = (Account.isLoggedIn(User) ? Account.getDetails(User)["gender"] : "female").ToLower();
            Random rnd = new Random();

            var dataset = dbcontext.Purchase.OrderByDescending(p => p.Id).Take(100);
            var lables  = dbcontext.JewelrySet.Select(set => set.Id).ToArray();
            Dictionary <int, double> probabilities = new Dictionary <int, double>();
            int datasetSize = dataset.Count();

            if (datasetSize > 0 && lables.Length > 0)
            {
                foreach (int label in lables)
                {
                    var subset     = dataset.Where(p => p.Jewelry.SetId == label);
                    int subsetSize = subset.Count();
                    if (subsetSize > 0)
                    {
                        double prior      = (double)subsetSize / (double)datasetSize;
                        double likelihood = 1;
                        likelihood *= (double)(subset.Where(p => p.Country == currentUserCountry).Count()) / (double)subsetSize;
                        likelihood *= (double)(subset.Where(p => p.User.Gender.ToLower() == currentUserGender).Count()) / (double)subsetSize;

                        probabilities[label] = prior * likelihood;
                    }
                    else
                    {
                        probabilities[label] = 0;
                    }
                }

                int selectedSet = probabilities.OrderByDescending(p => p.Value).First().Key;

                // select a random jewrley within the set
                var setJewrleys = dbcontext.Jewelry.Where(j => j.SetId == selectedSet).ToArray();
                return(setJewrleys[rnd.Next(setJewrleys.Length)]);
            }

            // if the dataset not contains data, return a random jewrley
            var allJewrleys = dbcontext.Jewelry.ToArray();

            return(allJewrleys[rnd.Next(allJewrleys.Length)]);
        }
        public static bool isStoreOwner(ClaimsPrincipal principal, int storeId)
        {
            if (Account.isLoggedIn(principal))
            {
                ColmanInternetiotContext context = new ColmanInternetiotContext();

                int userId = context.Users.FirstOrDefault(c => c.NameId == Account.getDetails(principal)["nameid"]).Id;

                int?managerId = context.Branch.FirstOrDefault(c => c.Id == storeId).ManagerId;

                if (managerId != null)
                {
                    return(userId == managerId);
                }
            }

            return(false);
        }
Пример #11
0
        public List <Jewelry> GetJewelries(string catagory, double price, int cart, bool diamonds)
        {
            ColmanInternetiotContext context = new ColmanInternetiotContext();

            List <Jewelry> listCategories = new List <Jewelry>();

            if (catagory != "all")
            {
                listCategories = context.Jewelry.Where(x => x.CategoryId == (context.Category.FirstOrDefault(c => c.Name == catagory)).Id).ToList();
            }
            else
            {
                listCategories = context.Jewelry.ToList();
            }

            List <Jewelry> listCart = new List <Jewelry>();

            if (cart != 0)
            {
                listCart = context.Jewelry.Where(x => x.Cart == cart).ToList();
            }
            else
            {
                listCart = context.Jewelry.ToList();
            }

            List <Jewelry> listOthers = new List <Jewelry>();

            if (diamonds)
            {
                listOthers = context.Jewelry.Where(x => x.Price >= price && x.Diamonds == diamonds).ToList();
            }
            else
            {
                listOthers = context.Jewelry.Where(x => x.Price >= price).ToList();
            }

            return(listCategories.Intersect(listCart).Intersect(listOthers).ToList());
        }
 public CategoriesController(ColmanInternetiotContext context)
 {
     _context = context;
 }
 public PurchasesController(ColmanInternetiotContext context)
 {
     _context = context;
 }
 public JewelriesController(ColmanInternetiotContext context)
 {
     _context = context;
 }
        public static List <Jewelry> getAllJewelries()
        {
            ColmanInternetiotContext context = new ColmanInternetiotContext();

            return(context.Jewelry.ToList());
        }
 public BranchesController(ColmanInternetiotContext context)
 {
     _context = context;
 }
Пример #17
0
 public UsersController(ColmanInternetiotContext context)
 {
     _context = context;
 }