示例#1
0
        public void AddToChart(string id)
        {
            MongodbFunctions mongo = new MongodbFunctions();

            Databases.DomainModel.User  user  = mongo.GetUser(User.Identity.Name);
            Databases.DomainModel.Order order = mongo.GetOpenOrder(user.Id);

            if (order == null)
            {
                List <MongoDBRef> products = new List <MongoDBRef>();
                products.Add(new MongoDBRef("products", new ObjectId(id)));

                order = new Databases.DomainModel.Order
                {
                    Date     = DateTime.Now,
                    Status   = "opened",
                    Products = products
                };

                mongo.AddUpdateOrder(order, user.Email, "add");
            }
            else
            {
                order.Products.Add(new MongoDBRef("products", new ObjectId(id)));
                mongo.AddUpdateOrder(order, user.Email, "update");
            }
        }
示例#2
0
        public List <Suggestion> GetFirstSuggestions(UserBehaviorDatabase db, Databases.DomainModel.User user, int numSuggestions)
        {
            var userActionGroup = db.UserActions
                                  .GroupBy(x => new { x.UserID })
                                  .Select(g => new { g.Key.UserID })
                                  .ToList();

            List <Suggestion> suggestions = new List <Suggestion>();
            MongodbFunctions  mongo       = new MongodbFunctions();

            foreach (var a in userActionGroup)
            {
                Databases.DomainModel.User u = mongo.GetUser(a.UserID);

                if (u.Gender.Equals(user.Gender) || u.BirthDate.Year == user.BirthDate.Year)
                {
                    int        userIndex = ratings.UserIndexToID.IndexOf(u.Id);
                    List <int> products  = GetHighestRatedProductsForUser(userIndex).Take(3).ToList();

                    foreach (int productIndex in products)
                    {
                        ObjectId productId = ratings.ProductIndexToID[productIndex];
                        suggestions.Add(new Suggestion(u.Id, productId, ratings.Users[userIndex].ProductRatings[productIndex]));
                    }
                }
            }

            suggestions.Sort((c, n) => n.Rating.CompareTo(c.Rating));

            return(suggestions.Take(numSuggestions).ToList());
        }
        public bool ActivateDiscount()
        {
            MongodbFunctions mongo = new MongodbFunctions();

            Databases.DomainModel.User user = mongo.GetUser(User.Identity.Name);
            bool sent = false;

            TimescaledbFunctions tdb = new TimescaledbFunctions();

            if (!tdb.ActivatedDiscount(user.Id.ToString()))
            {
                Databases.DomainModel.Notification notification = new Databases.DomainModel.Notification
                {
                    Content = "Poštovani, ostvarili ste popust od 10% na sledeću kupovinu, koji možete iskoristiti u roku od nedelju dana.",
                    Title   = "Popust 10%",
                    Date    = DateTime.Now.Date,
                    Tag     = "l_popust",
                    Read    = false,
                    User    = new MongoDB.Driver.MongoDBRef("users", user.Id)
                };

                tdb.SendNotification(user.Id.ToString(), mongo.AddNotification(notification, user.Email).ToString(), "l_popust");

                sent = true;
            }

            tdb.CloseConnection();
            return(sent);
        }
示例#4
0
        public JsonResult UpdateChart()
        {
            MongodbFunctions mongo = new MongodbFunctions();

            Databases.DomainModel.User  user  = mongo.GetUser(User.Identity.Name);
            Databases.DomainModel.Order order = mongo.GetOpenOrder(user.Id);//vraca opened order, samo 1 po useru moze da postoji

            int count;
            List <Databases.DomainModel.ProductShow> products = new List <Databases.DomainModel.ProductShow>();

            if (order == null)
            {
                count = 0;
            }
            else
            {
                count = order.Products.Count;

                foreach (MongoDBRef r in order.Products)
                {
                    Databases.DomainModel.Product product = mongo.GetProduct(new ObjectId(r.Id.ToString()));

                    Databases.DomainModel.ProductShow pr = new Databases.DomainModel.ProductShow
                    {
                        Id    = product.Id.ToString(),
                        Name  = product.Name,
                        Price = product.Price
                    };

                    products.Add(pr);
                }
            }

            return(Json(new { number = count, prod = products }, JsonRequestBehavior.AllowGet));
        }
        public ActionResult ProductDetails(string id)
        {
            MongodbFunctions mongo = new MongodbFunctions();

            if (id.Equals(""))
            {
                return(RedirectToAction("Home", "Index"));
            }

            Databases.DomainModel.Product product = mongo.GetProduct(new ObjectId(id));

            if (product != null)
            {
                if (User.IsInRole("User"))
                {
                    Databases.DomainModel.User user = mongo.GetUser(User.Identity.Name);
                    TimescaledbFunctions       tdb  = new TimescaledbFunctions();
                    tdb.ViewProduct(user.Id.ToString(), id);
                    List <ObjectId> customers = tdb.GetCustomersOfProduct(id);
                    List <Tuple <ObjectId, int> > ratingProducts = new List <Tuple <ObjectId, int> >();

                    foreach (ObjectId objectId in customers)
                    {
                        List <ObjectId> productsIds = tdb.GetBoughtProducts(objectId, id);

                        foreach (ObjectId prodId in productsIds)
                        {
                            if (!ratingProducts.Exists(x => x.Item1.Equals(prodId)))
                            {
                                ratingProducts.Add(new Tuple <ObjectId, int>(prodId, 0));
                            }
                            else
                            {
                                int index = ratingProducts.FindIndex(x => x.Item1.Equals(prodId));
                                ratingProducts.Insert(index, new Tuple <ObjectId, int>(prodId, ratingProducts[index].Item2 + 1));
                            }
                        }
                    }

                    tdb.CloseConnection();

                    ratingProducts.Sort((c, n) => n.Item2.CompareTo(c.Item2));
                    List <Databases.DomainModel.Product> products = new List <Databases.DomainModel.Product>();

                    foreach (Tuple <ObjectId, int> p in ratingProducts)
                    {
                        products.Add(mongo.GetProduct(p.Item1));
                    }

                    ViewBag.AlsoBought = products.Take(4).ToList();
                }
                return(View(product));
            }
            else
            {
                return(HttpNotFound());
            }
        }
示例#6
0
        public void DeleteOrder()
        {
            MongodbFunctions mongo = new MongodbFunctions();

            Databases.DomainModel.User  user  = mongo.GetUser(User.Identity.Name);
            Databases.DomainModel.Order order = mongo.GetOpenOrder(user.Id);

            mongo.DeleteOrder(order.Id);
        }
        public JsonResult GetSuggestions()
        {
            List <string> products = new List <string>();

            if (User.Identity.IsAuthenticated && User.IsInRole("User"))
            {
                MongodbFunctions           mongo = new MongodbFunctions();
                Databases.DomainModel.User user  = mongo.GetUser(User.Identity.Name);

                RecommendationEngine.Interfaces.IRater    rater    = new LinearRater(1.0, 5.0);
                RecommendationEngine.Interfaces.IComparer comparer = new CosineComparer();
                RecommendationEngine.Recommenders.ItemCollaborativeFilterRecommender recommender1 = new RecommendationEngine.Recommenders.ItemCollaborativeFilterRecommender(comparer, rater, 20);

                RecommendationEngine.Parsers.UserBehaviorDatabaseParser parser  = new RecommendationEngine.Parsers.UserBehaviorDatabaseParser();
                List <Databases.DomainModel.RecommenderAction>          actions = parser.LoadForSearch();
                RecommendationEngine.Parsers.UserBehaviorDatabase       db      = parser.LoadUserBehaviorDatabase(actions);

                recommender1.Train(db);

                List <RecommendationEngine.Objects.Suggestion> suggestions      = new List <RecommendationEngine.Objects.Suggestion>();
                List <Databases.DomainModel.Product>           CategoryProducts = new List <Databases.DomainModel.Product>();


                if (actions.Count(x => x.UserId.Equals(user.Id)) > 0)
                {
                    suggestions = recommender1.GetSuggestions(user.Id, 5);
                }
                else
                {
                    foreach (string subcat in user.Interests)
                    {
                        CategoryProducts.AddRange(mongo.GetCategoryProducts(subcat).Take(2));
                    }

                    foreach (Databases.DomainModel.Product p in CategoryProducts)
                    {
                        if (!products.Exists(x => x.Equals(p.Name)))
                        {
                            products.Add(p.Name);
                        }
                    }
                    suggestions = recommender1.GetFirstSuggestions(db, user, 5);
                }

                foreach (RecommendationEngine.Objects.Suggestion s in suggestions)
                {
                    Databases.DomainModel.Product product = mongo.GetProduct(s.ProductID);
                    if (!products.Exists(x => x.Equals(product.Name)))
                    {
                        products.Add(product.Name);
                    }
                }
            }

            return(Json(new { prods = products }, JsonRequestBehavior.AllowGet));
        }
        public JsonResult GetComments(string id)
        {
            MongodbFunctions mongo = new MongodbFunctions();

            ObjectId objID = new ObjectId(id);

            Databases.DomainModel.Product product = mongo.GetProduct(objID);
            List <MongoDBRef>             mess    = product.Messages;
            int    count = product.Messages.Count;
            string role;

            if (User.IsInRole("Admin"))
            {
                role = "Admin";
            }
            else
            {
                role = "User";
            }

            List <Databases.DomainModel.MessageShow> comments = new List <Databases.DomainModel.MessageShow>();
            List <Databases.DomainModel.UserShow>    users    = new List <Databases.DomainModel.UserShow>();

            foreach (MongoDBRef r in mess)
            {
                Databases.DomainModel.Message comment = mongo.GetComment(new ObjectId(r.Id.ToString()));
                Databases.DomainModel.User    user    = mongo.GetUser(new ObjectId(comment.User.Id.ToString()));
                List <string> responses = new List <string>();

                foreach (MongoDBRef rr in comment.Responses)
                {
                    Databases.DomainModel.AdminResponse response = mongo.GetResponse(new ObjectId(rr.Id.ToString()));
                    responses.Add(response.Content);
                }

                Databases.DomainModel.MessageShow messShow = new Databases.DomainModel.MessageShow
                {
                    Id        = comment.Id.ToString(),
                    Content   = comment.Content,
                    Responses = responses
                };
                Databases.DomainModel.UserShow userShow = new Databases.DomainModel.UserShow
                {
                    Id      = user.Id,
                    Name    = user.Name,
                    Surname = user.Surname,
                    Email   = user.Email,
                    Address = user.Address
                };
                comments.Add(messShow);
                users.Add(userShow);
            }

            return(Json(new { number = count, status = role, com = comments, people = users }, JsonRequestBehavior.AllowGet));
        }
示例#9
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // Code for users to db
                    var roleStore   = new RoleStore <IdentityRole>(new ApplicationDbContext());
                    var roleManager = new RoleManager <IdentityRole>(roleStore);
                    await roleManager.CreateAsync(new IdentityRole("User"));

                    await UserManager.AddToRoleAsync(user.Id, "User");

                    // Uncomment to add admin
                    //await roleManager.CreateAsync(new IdentityRole("Admin"));
                    //await UserManager.AddToRoleAsync(user.Id, "Admin");
                    //////////////////////////////////////////////////////


                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);


                    // Adding user to mongodb
                    MongodbFunctions mongo = new MongodbFunctions();

                    Databases.DomainModel.User newUser = new Databases.DomainModel.User
                    {
                        Email     = model.Email,
                        Name      = model.FirstName,
                        BirthDate = model.BirthDate,
                        Phone     = model.Phone,
                        Surname   = model.Surname,
                        Gender    = model.Gender,
                        Interests = model.Interests.Where(x => x.Value == true).Select(x => x.Key).ToList()
                    };
                    newUser.Address.Add(model.Address);

                    mongo.InsertUser(newUser);

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public JsonResult GetReviews(string id)
        {
            MongodbFunctions mongo = new MongodbFunctions();

            ObjectId objID = new ObjectId(id);

            Databases.DomainModel.Product product = mongo.GetProduct(objID);
            List <MongoDBRef>             rev     = product.Reviews;
            int count = product.Reviews.Count;

            List <Databases.DomainModel.ReviewShow> reviews = new List <Databases.DomainModel.ReviewShow>();
            List <Databases.DomainModel.UserShow>   users   = new List <Databases.DomainModel.UserShow>();

            foreach (MongoDBRef r in rev)
            {
                Databases.DomainModel.Review review = mongo.GetReview(new ObjectId(r.Id.ToString()));
                Databases.DomainModel.User   user   = mongo.GetUser(new ObjectId(review.User.Id.ToString()));

                Databases.DomainModel.UserShow userShow = new Databases.DomainModel.UserShow
                {
                    Id      = user.Id,
                    Name    = user.Name,
                    Surname = user.Surname,
                    Email   = user.Email,
                    Address = user.Address
                };

                Databases.DomainModel.ReviewShow reviewShow = new Databases.DomainModel.ReviewShow
                {
                    Id      = review.Id,
                    Grade   = review.Grade,
                    Comment = review.Comment
                };
                reviews.Add(reviewShow);
                users.Add(userShow);
            }

            if (User.IsInRole("User"))
            {
                Databases.DomainModel.User u = mongo.GetUser(User.Identity.Name);

                TimescaledbFunctions tdb = new TimescaledbFunctions();
                tdb.SeeReviews(u.Id.ToString(), id);
                tdb.CloseConnection();
            }

            return(Json(new { number = count, revs = reviews, people = users }, JsonRequestBehavior.AllowGet));
        }
        public void AddReview(string id, int grade, string comment)
        {
            MongodbFunctions     mongo = new MongodbFunctions();
            TimescaledbFunctions tdb   = new TimescaledbFunctions();

            Databases.DomainModel.User   user   = mongo.GetUser(User.Identity.Name);
            Databases.DomainModel.Review review = mongo.GetReview(user.Id, new ObjectId(id));

            if (review == null)
            {
                Databases.DomainModel.Review newReview = new Databases.DomainModel.Review
                {
                    Grade   = grade,
                    Comment = comment,
                    Product = new MongoDBRef("products", new ObjectId(id))
                };

                mongo.AddReview(newReview, id, User.Identity.Name);

                tdb.ReviewProduct(user.Id.ToString(), id, grade);
            }
            else
            {
                mongo.UpdateReview(review.Id, grade, comment);
                tdb.UpdateReview(user.Id.ToString(), id, grade);
            }

            if (tdb.LowGrades(user.Id.ToString()) >= 4)
            {
                Databases.DomainModel.Notification notification = new Databases.DomainModel.Notification
                {
                    Content = "Poštovani, primetili smo da ste više proizvoda ocenili lošom ocenom. Ako želite da zamenite neki od njih, " +
                              "pozovite naš call centar i dogovorite se sa operaterom. Takođe, imate opciju popusta od 10% prilikom sledeće kupovine. " +
                              "Popust možete aktivirati klikom na link u ovom obaveštenju i važi nedelju dana. U jednom trenutku možete aktivirati samo jedan popust.",
                    Title = "Niste zadovoljni kupljenim proizvodima?",
                    Date  = DateTime.Now.Date,
                    Tag   = "lose_ocene",
                    Read  = false,
                    User  = new MongoDB.Driver.MongoDBRef("users", user.Id)
                };

                tdb.SendNotification(user.Id.ToString(), mongo.AddNotification(notification, user.Email).ToString(), "lose_ocene");
            }

            tdb.CloseConnection();
        }
示例#12
0
        public void DeleteFromChart(string id)
        {
            MongodbFunctions mongo = new MongodbFunctions();

            Databases.DomainModel.User  user  = mongo.GetUser(User.Identity.Name);
            Databases.DomainModel.Order order = mongo.GetOpenOrder(user.Id);

            order.Products.Remove(new MongoDBRef("products", new ObjectId(id)));

            if (order.Products.Count > 0)
            {
                mongo.RemoveProduct(order);
            }
            else
            {
                mongo.DeleteOrder(order.Id);
            }
        }
示例#13
0
        public ActionResult Checkout()
        {
            MongodbFunctions     mongo = new MongodbFunctions();
            TimescaledbFunctions tdb   = new TimescaledbFunctions();

            Databases.DomainModel.CheckoutDetails details = new Databases.DomainModel.CheckoutDetails();

            Databases.DomainModel.User  user  = mongo.GetUser(User.Identity.Name);
            Databases.DomainModel.Order order = mongo.GetOpenOrder(user.Id);
            details.User = user;
            List <Databases.DomainModel.Product>      products  = new List <Databases.DomainModel.Product>();
            List <Databases.DomainModel.Notification> discounts = new List <Databases.DomainModel.Notification>();
            List <string> disc = tdb.GetDiscounts(user.Id.ToString());

            if (order != null)
            {
                foreach (MongoDBRef r in order.Products)
                {
                    Databases.DomainModel.Product product = mongo.GetProduct(new ObjectId(r.Id.ToString()));
                    products.Add(product);
                }

                if (disc.Count != 0)
                {
                    foreach (string d in disc)
                    {
                        Databases.DomainModel.Notification notification = mongo.GetNotification(new ObjectId(d));
                        discounts.Add(notification);
                    }
                }
            }
            details.Products  = products;
            details.Discounts = discounts;
            tdb.CloseConnection();

            return(View(details));
        }
        public JsonResult GetNotifications()
        {
            MongodbFunctions mongo = new MongodbFunctions();

            Databases.DomainModel.User user = mongo.GetUser(User.Identity.Name);

            TimescaledbFunctions tdb           = new TimescaledbFunctions();
            List <string>        notifications = tdb.GetNotifications(user.Id.ToString());

            List <Databases.DomainModel.NotificationShow> nots = new List <Databases.DomainModel.NotificationShow>();

            int count = 0;

            if (notifications.Count != 0)
            {
                foreach (string notId in notifications)
                {
                    Databases.DomainModel.Notification not = mongo.GetNotification(new ObjectId(notId));
                    if (!not.Read)
                    {
                        count++;
                    }
                    Databases.DomainModel.NotificationShow nshow = new Databases.DomainModel.NotificationShow
                    {
                        Id    = not.Id.ToString(),
                        Title = not.Title,
                        Date  = not.Date.Date.ToString("dd/MM/yyyy"),
                        Read  = not.Read
                    };
                    nots.Add(nshow);
                }
            }
            tdb.CloseConnection();

            return(Json(new { number = count, alerts = nots, total = notifications.Count }, JsonRequestBehavior.AllowGet));
        }
示例#15
0
        public ActionResult Index()
        {
            List <Databases.DomainModel.Product> products = new List <Databases.DomainModel.Product>();
            MongodbFunctions mongo = new MongodbFunctions();

            if (User.Identity.IsAuthenticated && User.IsInRole("User"))
            {
                TimescaledbFunctions tdb = new TimescaledbFunctions();

                Databases.DomainModel.User user = mongo.GetUser(User.Identity.Name);

                if (!tdb.NotificationSent(user.Id.ToString()) && (30000 - tdb.MonthShopping(user.Id.ToString())) < 3000)
                {
                    Databases.DomainModel.Notification notification = new Databases.DomainModel.Notification
                    {
                        Content = "Poštovani, do ostvarivanja popusta od 20% ostalo Vam je manje od 3000 dinara.",
                        Title   = "Mali iznos do popusta",
                        Date    = DateTime.Now.Date,
                        Tag     = "do_popusta",
                        Read    = false,
                        User    = new MongoDB.Driver.MongoDBRef("users", user.Id)
                    };


                    tdb.SendNotification(user.Id.ToString(), mongo.AddNotification(notification, user.Email).ToString(), "do_popusta");
                }

                //recommend za proizvode
                RecommendationEngine.Interfaces.IRater    rater    = new LinearRater(1.0, 5.0);
                RecommendationEngine.Interfaces.IComparer comparer = new CosineComparer();
                RecommendationEngine.Recommenders.ItemCollaborativeFilterRecommender recommender1 = new RecommendationEngine.Recommenders.ItemCollaborativeFilterRecommender(comparer, rater, 3);
                RecommendationEngine.Recommenders.UserCollaborativeFilterRecommender recommender2 = new RecommendationEngine.Recommenders.UserCollaborativeFilterRecommender(comparer, rater, 5);

                RecommendationEngine.Parsers.UserBehaviorDatabaseParser parser  = new RecommendationEngine.Parsers.UserBehaviorDatabaseParser();
                List <Databases.DomainModel.RecommenderAction>          actions = parser.LoadForHome();
                RecommendationEngine.Parsers.UserBehaviorDatabase       db      = parser.LoadUserBehaviorDatabase(actions);

                recommender1.Train(db);
                recommender2.Train(db);

                if (actions.Count(x => x.UserId.Equals(user.Id)) > 0)//if user has actions, recommendation can be done
                {
                    List <RecommendationEngine.Objects.Suggestion> suggestions1 = recommender1.GetSuggestions(user.Id, 6);
                    List <RecommendationEngine.Objects.Suggestion> suggestions2 = recommender2.GetSuggestions(user.Id, 3);

                    foreach (RecommendationEngine.Objects.Suggestion s in suggestions1)
                    {
                        Databases.DomainModel.Product product = mongo.GetProduct(s.ProductID);
                        if (!products.Exists(x => x.Id.Equals(product.Id)))
                        {
                            products.Add(product);
                        }
                    }

                    foreach (RecommendationEngine.Objects.Suggestion s in suggestions2)
                    {
                        Databases.DomainModel.Product product = mongo.GetProduct(s.ProductID);
                        if (!products.Exists(x => x.Id.Equals(product.Id)))
                        {
                            products.Add(product);
                        }
                    }
                }
                else//no actions, recommendation using gender and birth date
                {
                    List <Databases.DomainModel.Product> CategoryProducts = new List <Databases.DomainModel.Product>();

                    foreach (string subcat in user.Interests)
                    {
                        CategoryProducts.AddRange(mongo.GetCategoryProducts(subcat).Take(2));
                    }

                    List <RecommendationEngine.Objects.Suggestion> suggestions1 = recommender1.GetFirstSuggestions(db, user, 10);

                    foreach (RecommendationEngine.Objects.Suggestion s in suggestions1)
                    {
                        Databases.DomainModel.Product product = mongo.GetProduct(s.ProductID);
                        if (!products.Exists(x => x.Id.Equals(product.Id)))
                        {
                            products.Add(product);
                        }
                    }

                    foreach (Databases.DomainModel.Product p in CategoryProducts)
                    {
                        if (!products.Exists(x => x.Id.Equals(p.Id)))
                        {
                            products.Add(p);
                        }
                    }
                }

                //recommend za reklame
                List <Databases.DomainModel.Advert> adverts = new List <Databases.DomainModel.Advert>();

                actions = parser.LoadForAdverts();
                db      = parser.LoadUserBehaviorDatabase(actions);

                recommender1.Train(db);

                List <RecommendationEngine.Objects.Suggestion> suggestionsU     = new List <RecommendationEngine.Objects.Suggestion>();
                List <RecommendationEngine.Objects.Suggestion> suggestionsI     = new List <RecommendationEngine.Objects.Suggestion>();
                List <Databases.DomainModel.Product>           categoryProducts = new List <Databases.DomainModel.Product>();

                if (actions.Count(x => x.UserId.Equals(user.Id)) > 0)
                {
                    suggestionsU = recommender2.GetSuggestions(user.Id, 5);
                    suggestionsI = recommender1.GetSuggestions(user.Id, 5);
                }
                else
                {
                    foreach (string subcat in user.Interests)
                    {
                        categoryProducts.AddRange(mongo.GetCategoryProducts(subcat).Take(2));
                    }

                    foreach (Databases.DomainModel.Product p in categoryProducts)
                    {
                        foreach (Databases.DomainModel.Advert advert in mongo.GetAdverts(p.Subcategory))
                        {
                            if (!adverts.Exists(x => x.Id.Equals(advert.Id)))
                            {
                                adverts.Add(advert);
                            }
                        }
                    }

                    suggestionsI = recommender1.GetFirstSuggestions(db, user, 10);
                }

                foreach (RecommendationEngine.Objects.Suggestion s in suggestionsI)
                {
                    Databases.DomainModel.Product product = mongo.GetProduct(s.ProductID);

                    foreach (Databases.DomainModel.Advert advert in mongo.GetAdverts(product.Subcategory))
                    {
                        if (!adverts.Exists(x => x.Id.Equals(advert.Id)))
                        {
                            adverts.Add(advert);
                        }
                    }
                }

                foreach (RecommendationEngine.Objects.Suggestion s in suggestionsU)
                {
                    Databases.DomainModel.Product product = mongo.GetProduct(s.ProductID);

                    foreach (Databases.DomainModel.Advert advert in mongo.GetAdverts(product.Subcategory))
                    {
                        if (!adverts.Exists(x => x.Id.Equals(advert.Id)))
                        {
                            adverts.Add(advert);
                        }
                    }
                }
                ViewBag.Adverts = adverts;

                //last seen
                List <MongoDB.Bson.ObjectId>         prodIds  = tdb.LastSeen(user.Id.ToString());
                List <Databases.DomainModel.Product> lastSeen = new List <Databases.DomainModel.Product>();

                foreach (MongoDB.Bson.ObjectId id in prodIds)
                {
                    lastSeen.Add(mongo.GetProduct(id));
                }

                tdb.CloseConnection();
                ViewBag.LastSeen = lastSeen;
            }
            else //admin logged
            {
                products = mongo.GetProducts();
            }

            return(View(products));
        }
示例#16
0
        public void SubmitOrder(string address, string note, string pay)
        {
            MongodbFunctions     mongo = new MongodbFunctions();
            TimescaledbFunctions tdb   = new TimescaledbFunctions();

            Databases.DomainModel.User  user  = mongo.GetUser(User.Identity.Name);
            Databases.DomainModel.Order order = mongo.GetOpenOrder(user.Id);

            List <string> disc = tdb.GetDiscounts(user.Id.ToString());

            if (!user.Address.Contains(address))
            {
                user.Address.Add(address);
                mongo.UpdateAddresses(user);
            }

            order.Note         = note;
            order.PayingMethod = pay;
            order.Address      = address;

            mongo.CloseOrder(order);

            foreach (string d in disc)
            {
                Databases.DomainModel.Notification notification = mongo.GetNotification(new ObjectId(d));

                if (notification.Tag.Equals("popust"))
                {
                    mongo.UpdateNotification(notification.Id, "iskorisceno");
                    tdb.UpdateNotification(user.Id.ToString(), d, "iskorisceno");
                }
                else if (notification.Tag.Equals("l_popust"))
                {
                    mongo.UpdateNotification(notification.Id, "l_iskorisceno");
                    tdb.UpdateNotification(user.Id.ToString(), d, "l_iskorisceno");
                }
            }

            //send email with details
            MailMessage message = new MailMessage();

            message.From = new MailAddress("*****@*****.**");
            message.To.Add(new MailAddress(User.Identity.Name));
            message.Subject = "Potvrda proudžbine";
            message.Body    = "Uspešno ste poručili sledeće proizvode:\n";

            foreach (MongoDBRef p in order.Products)
            {
                Databases.DomainModel.Product prod = mongo.GetProduct(new ObjectId(p.Id.ToString()));
                tdb.BuyProduct(user.Id.ToString(), prod.Id.ToString(), prod.Price);
                message.Body += prod.Name + "\n";
            }

            message.Body += "Proizvodi će Vam biti isporučeni najkasnije za 7 dana na adresu " + order.Address;
            SmtpClient smtpClient = new SmtpClient();

            smtpClient.Credentials    = new System.Net.NetworkCredential("*****@*****.**", "Krisgold02$");
            smtpClient.Host           = "smtp.office365.com";
            smtpClient.Port           = 587;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl      = true;

            //smtpClient.Send(message);

            //send notifications
            if (!tdb.NotificationSent(user.Id.ToString()) && (30000 - tdb.MonthShopping(user.Id.ToString())) < 3000 && (30000 - tdb.MonthShopping(user.Id.ToString())) > 0)//notifikacija do popusta
            {
                Databases.DomainModel.Notification notification = new Databases.DomainModel.Notification
                {
                    Content = "Poštovani, do ostvarivanja popusta od 20% ostalo Vam je manje od 3000 dinara.",
                    Title   = "Mali iznos do popusta",
                    Date    = DateTime.Now.Date,
                    Tag     = "do_popusta",
                    Read    = false,
                    User    = new MongoDBRef("users", user.Id)
                };


                tdb.SendNotification(user.Id.ToString(), mongo.AddNotification(notification, user.Email).ToString(), "do_popusta");
            }
            else if (tdb.MonthShopping(user.Id.ToString()) >= 30000)
            {
                Databases.DomainModel.Notification notification = new Databases.DomainModel.Notification
                {
                    Content = "Poštovani, ostvarili ste popust od 20% na sledeću kupovinu, koji možete iskoristiti u roku od nedelju dana.",
                    Title   = "Popust 20%",
                    Date    = DateTime.Now.Date,
                    Tag     = "popust",
                    Read    = false,
                    User    = new MongoDBRef("users", user.Id)
                };

                tdb.SendNotification(user.Id.ToString(), mongo.AddNotification(notification, user.Email).ToString(), "popust");
            }

            tdb.CloseConnection();
        }