Esempio n. 1
0
        /// <summary>
        /// Gets suggested Dishes based on User History
        /// First builds a dictionary for metascores, then find all potential dishes
        /// It then computes a score for each of the dishes based on the metascore values
        /// Finally sorts and returns the best 10 dishes
        /// </summary>
        /// <param name="u"></param>
        /// <param name="r"></param>
        public static List<KeyValuePair<menu_item, int>> GetSuggestions(user u, restaurant r)
        {
            totalOrdersCount = 0;
            totalOrderItemsCount = 0;
            totalReviewsCount = 0;
            metaDataCount = 0;
            List<menu> menus = db.menus.Where(m => m.is_active == true && m.is_deleted == false && m.resto_id == r.id).ToList();
            List<menu_category> menu_categories = new List<menu_category>();
            List<menu_item> menu_items_available = new List<menu_item>();
            Dictionary<menu_item, int> ItemScores;
            Dictionary<string, int[]> MetaScores;
            //Build the Metascores using all past orders and reviews
            MetaScores = BuildMetaScores(u);

            //Find all available items we can order
            foreach (menu m in menus)
            {
                menu_categories.AddRange(m.menu_category.Where(mc => mc.is_active == true && mc.is_deleted == false));
            }
            foreach (menu_category mc in menu_categories)
            {
                menu_items_available.AddRange(mc.menu_item.Where(mi => mi.is_active == true && mi.is_deleted == false));
            }

            //Give a score to every potential menu item currently available
            ItemScores = CalculateItemScores(MetaScores, menu_items_available);

            return SortByScore(ItemScores);
        }
Esempio n. 2
0
        public Boolean edit(user user)
        {
            UserIM im = new UserIM(db);
            user dbVersion =im.find(user.id);
            if (dbVersion.version == user.version)
            {
                user.current_table_id = dbVersion.current_table_id;

                // If password was blank (only editing details) make sure we keep it the same
                if (user.password == null)
                {
                    user.password = dbVersion.password;
                    user.ConfirmPassword = user.password;
                }

                // If the user's role is null (happens if the user is editing his/her own details)
                if (user.user_role == null)
                    user.user_role = dbVersion.user_role;

                ((IObjectContextAdapter)db).ObjectContext.Detach(dbVersion);
                db.Entry(user).State = EntityState.Modified;

                user.version = user.version + 1;
                db.SaveChanges();
                return true;
            }
            return false;
        }
Esempio n. 3
0
        /**
         * Filters the categories and only returns those that are not already assigned to the menu
         * that is passed in.
         * */
        public static order getOpenOrder(user usr)
        {
            touch_for_foodEntities db = new touch_for_foodEntities();
            order o;
            var models = db.orders
                .Where(p => p.order_status == (int)OrderStatusHelper.OrderStatusEnum.PLACED || p.order_status == (int)OrderStatusHelper.OrderStatusEnum.EDITING || p.order_status == (int)OrderStatusHelper.OrderStatusEnum.PROCESSING)
                .Where(p=>p.user_id == usr.id)
                .OrderByDescending(p=>p.timestamp)
                .ToList()
                .Select(item =>
                new order
                {
                    id = item.id,
                    timestamp = item.timestamp,
                    total = item.total,
                    order_status = item.order_status,
                    order_item = item.order_item,
                    user = item.user,
                    table = item.table
                }).AsQueryable();

            if (models.Any())
            {
                o = models.ToArray()[0];
                return o;
            }
            return null;
        }
Esempio n. 4
0
 public static void MyClassInitialize(TestContext testContext)
 {
     //Add test data (order specific)
     testDatabase = new TestDatabaseHelper();
     restaurant1 = testDatabase.AddRestaurant();
     table1 = testDatabase.AddTable(restaurant1);
     user1 = testDatabase.AddUser("*****@*****.**", table1, (int)SiteRoles.Admin);
 }
Esempio n. 5
0
        public ActionResult Create(user user)
        {
            if (ModelState.IsValid)
            {
                // Add the customer role to the new user object
                user.user_role = (int)SiteRoles.Customer;

                // Encrypt the user's password
                AES aes = new AES();
                user.password = aes.EncryptToString(user.password);
                user.ConfirmPassword = aes.EncryptToString(user.ConfirmPassword);

                // Try to add the user to the database and save the changes
                // Exception is thrown in case of errors (ex: unique field value is not respected)
                try
                {
                    if (!om.Create(user))
                    {
                        return View("Create");
                    }
                }
                catch (DbUpdateException e)
                {
                    HandleDbUpdateException(e);
                    // Return to the original page we were at. Any errors added into the model will be shown automatically by the view
                    return View("Create");
                }
                catch (Exception)
                {
                    ViewBag.Error = Global.ServerError;
                    return View("Create");
                }
                // Create a cookie with our user information
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user.username,
                                                                                DateTime.Now, DateTime.Now.AddDays(1),
                                                                                false, user.id.ToString(), FormsAuthentication.FormsCookiePath);

                // Now that the user was properly created we can add the customer role to the session
                HttpContext.Session["role"] = SiteRoles.Customer;

                // Encrypt the ticket
                string hashedTicket = FormsAuthentication.Encrypt(ticket);

                // Create the new cookie and add it into the response
                Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, hashedTicket));

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

            // If we got to this point then something went wrong
            ViewBag.Error = Global.ServerError;
            return View("Create");
        }
Esempio n. 6
0
        public static void MyClassInitialize(TestContext testContext)
        {
            //Add test data (order specific)
            testDatabase = new TestDatabaseHelper();
            restaurant1 = testDatabase.AddRestaurant();
            table1 = testDatabase.AddTable(restaurant1);
            user1 = testDatabase.AddUser("*****@*****.**", table1, (int)SiteRoles.Admin);

            //Session
            db = new touch_for_foodEntities();
            target = new HomeController();
            Session session = new Session(db, target);
            session.simulateLogin(user1.username, user1.password);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a friendship entity
        /// </summary>
        /// <param name="user1">The first user in the friendship</param>
        /// <param name="user2">The second user in the friendship</param>
        /// <returns>the created friendship entity</returns>
        public friendship AddFriendship(user user1, user user2)
        {
            //Initialise
            db = new touch_for_foodEntities();
            friendship testFriendship = new friendship();

            //Set Attributes
            testFriendship.first_user = user1.id;
            testFriendship.second_user = user2.id;

            //Save
            db.friendships.Add(testFriendship);
            db.SaveChanges();
            db.Dispose();

            return testFriendship;
        }
Esempio n. 8
0
        /// <summary>
        /// Writes a restaurant obejct to the database
        /// </summary>
        /// <param name="restaurant">The restaurant object to write</param>
        /// <param name="user">The user to associate as owner to the restaurant</param>
        /// <returns>True if successful, false otherwise</returns>
        /// <exception cref="InvalidOperationException"></exception>
        public bool Create(restaurant restaurant, user user)
        {
            if (user == null) return false;
            db.restaurants.Add(restaurant);
            if (db.SaveChanges() == 0) return false;

            // assign the user to this restaurant.
            restaurant_user ru = new restaurant_user();
            ru.user_id = user.id;
            ru.restaurant_id = restaurant.id;
            db.restaurant_user.Add(ru);

            if (db.SaveChanges() == 0)
            {
                // Remove the restaurant because the owner could not be assigned to it.
                delete(restaurant.id);
                return false;
            }
            return true;
        }
Esempio n. 9
0
        public static void MyClassInitialize(TestContext testContext)
        {
            //Add test data (order specific)
            testDatabase = new TestDatabaseHelper();
            restaurant1 = testDatabase.AddRestaurant();
            table1 = testDatabase.AddTable(restaurant1);
            user1 = testDatabase.AddUser("*****@*****.**", table1, (int)SiteRoles.Admin);
            order1 = testDatabase.AddOrder(table1);
            item1 = testDatabase.AddItem();
            category1 = testDatabase.AddCategory();
            menu1 = testDatabase.AddMenu(restaurant1);
            menuCategory1 = testDatabase.AddMenuCategory(category1, menu1);
            menuItem1 = testDatabase.AddMenuItem(item1, menuCategory1);

            //Session
            db = new touch_for_foodEntities();
            BillController target = new BillController();
            Session session = new Session(db, target);
            session.simulateLogin(user1.username, user1.password);
        }
Esempio n. 10
0
 /// <summary>
 /// Removes a user item from the database.
 /// </summary>
 /// <param name="userEntity">The user to be removed.</param>
 public void RemoveUser(user userEntity)
 {
     db = new touch_for_foodEntities();
     if (db.users.Find(userEntity.id) != null)
     {
         db.users.Remove(db.users.Find(userEntity.id));
         db.SaveChanges();
     }
     db.Dispose();
 }
Esempio n. 11
0
        /// <summary>
        /// Builds up the meta score dictionary which associates data to each metadata word
        /// The Values of this Dictionary are
        /// 0 - Total times found (count)
        /// 1 - Total times rated (rated)
        /// 2 - Total Rating value (sum)        
        /// </summary>
        /// <param name="u"></param>
        private static Dictionary<string, int[]> BuildMetaScores(user u)
        {
            List<order> Orders = u.orders.Where(o => o.order_status == (int)OrderStatusHelper.OrderStatusEnum.COMPLETE).ToList();
            //List<order> Orders = u.orders.ToList();
            Dictionary<string, int[]> MetaScores = new Dictionary<string, int[]>();
            foreach (order o in Orders)
            {
                UserUtil.totalOrdersCount++;
                foreach (order_item oi in o.order_item)
                {
                    int reviewRating = 0;
                    if (oi.review_order_item.Count() > 0)
                    {
                        reviewRating = (int)oi.review_order_item.First().rating;
                    }
                    totalOrderItemsCount++;
                    string metaData = oi.menu_item.item.metadata;
                    string[] metaWords = null;
                    if (metaData != null)
                    {
                        metaWords = metaData.Split(' ');
                    }
                    foreach (string word in metaWords)
                    {
                        metaDataCount++;
                        if (MetaScores.ContainsKey(word))
                        {
                            MetaScores[word][0]++;
                        }
                        else
                        {
                            MetaScores[word] = new int[]{1,0,0};
                        }
                        if (reviewRating != 0)
                        {
                            MetaScores[word][1]++;
                            MetaScores[word][2] += reviewRating;
                        }
                    }
                }

            }
            return MetaScores;
        }
Esempio n. 12
0
        public void MyTestInitialize()
        {
            //Add test data (order specific)
            restaurant1 = testDatabase.AddRestaurant();
            restaurant2 = new restaurant();
            table1 = testDatabase.AddTable(restaurant1);
            user1 = testDatabase.AddUser("*****@*****.**", table1, (int)SiteRoles.Admin);
            restaurantUser1 = testDatabase.AddRestaurantUser(user1, restaurant1);
            menu1 = testDatabase.AddMenu(restaurant1);
            order1 = testDatabase.AddOrder(table1);
            review1 = testDatabase.AddReview(restaurant1, order1, user1);

            //Session
            db = new touch_for_foodEntities();
            target = new RestaurantController();
            Session session = new Session(db, target);
            session.simulateLogin(user1.username, user1.password);
        }
Esempio n. 13
0
        /// <summary>
        /// Creates an entry of type restaurant_user in the database.
        /// </summary>
        /// <param name="testUser">User user_restaurant is associated to.</param>
        /// <param name="testRestaurant">Restaurant user_restaurant is associated to.</param>
        /// <returns>The created restaurant_user entity.</returns>
        public restaurant_user AddRestaurantUser(user testUser, restaurant testRestaurant)
        {
            //Initialise
            db = new touch_for_foodEntities();
            restaurant_user testRestaurantUser = new restaurant_user();

            //Set Attributes
            testRestaurantUser.user_id= testUser.id;
            testRestaurantUser.restaurant_id = testRestaurant.id;

            //Save
            db.restaurant_user.Add(testRestaurantUser);
            db.SaveChanges();
            db.Dispose();

            return testRestaurantUser;
        }
Esempio n. 14
0
 public void MyTestInitialize()
 {
     //Add test data (order specific)
     user1 = testDatabase.AddUser("*****@*****.**", table1, (int)SiteRoles.Admin);
     user2 = testDatabase.AddUser("*****@*****.**", table1, (int)SiteRoles.Admin);
     user3 = testDatabase.AddUser("*****@*****.**", table1, (int)SiteRoles.Admin);
     friendship1 = testDatabase.AddFriendship(user1, user2);
     friendship2 = new friendship();
     friendship2.first_user = user1.id;
     friendship2.second_user = user3.id;
 }
Esempio n. 15
0
        public void MyTestInitialize()
        {
            //Add test data (order specific)
            restaurant1 = testDatabase.AddRestaurant();
            table1 = testDatabase.AddTable(restaurant1);
            table2 = new table();
            user1 = testDatabase.AddUser("*****@*****.**", table1, (int)SiteRoles.Restaurant);
            order1 = testDatabase.AddOrder(table1);
            request1 = testDatabase.AddServiceRequest(table1);
            restaurantUser1 = testDatabase.AddRestaurantUser(user1, restaurant1);

            //Session
            db = new touch_for_foodEntities();
            target = new TableController();
            Session session = new Session(db, target);
            session.simulateLogin(user1.username, user1.password);
        }
Esempio n. 16
0
 /// <summary>
 /// Writes a user object to the database
 /// </summary>
 /// <param name="user">The user object to write</param>
 /// <returns>True if successful, false otherwise</returns>
 /// <exception cref="InvalidOperationException"></exception>
 /// <exception cref="DBEntityValidationException"></exception>
 /// <exception cref="DbUpdateException"></exception>
 public bool Create(user user)
 {
     db.users.Add(user);
     return (db.SaveChanges() == 1);
 }
Esempio n. 17
0
 /// <summary>
 /// Writes a review object to the database associated with a user
 /// </summary>
 /// <param name="review">The review object to write</param>
 /// <param name="user">The user to associate the review with</param>
 /// <returns>True if successful, false otherwise</returns>
 public bool Create(review review, user user)
 {
     review.user_id = user.id;
     return Create(review);
 }
Esempio n. 18
0
        public PartialViewResult GetAllReviews(user u)
        {
            List<review_order_item> reviews = new List<review_order_item>();
            foreach (review rev in u.reviews)
            {
                reviews.AddRange(rev.review_order_item);
            }
            reviews.OrderByDescending(r => r.submitted_on).ToList();

            return PartialView("PastReviewsPartial", reviews);
        }
Esempio n. 19
0
        public void MyTestInitialize()
        {
            //Add test data (order specific)
            restaurant1 = testDatabase.AddRestaurant();
            table1 = testDatabase.AddTable(restaurant1);
            user1 = testDatabase.AddUser("*****@*****.**", table1, (int)SiteRoles.Admin);
            user3 = testDatabase.AddUser("*****@*****.**", table1, (int)SiteRoles.Customer);
            friendship1 = testDatabase.AddFriendship(user1, user3);
            restaurantUser1 = testDatabase.AddRestaurantUser(user1, restaurant1);
            order1 = testDatabase.AddOrder(table1);

            // Create a valid user object with test values
            user2 = new user();
            user2.username = "******";
            user2.password = "******";
            user2.ConfirmPassword = "******";
            user2.first_name = "*****@*****.**";
            user2.last_name = "*****@*****.**";
            user2.email = "*****@*****.**";
            user2.image_url = null;
            user2.current_table_id = table1.id;
        }
Esempio n. 20
0
        /// <summary>
        /// Creates an entry of type review in the database.
        public review AddReview(restaurant r, order o, user u)
        {
            //Initialise
            db = new touch_for_foodEntities();
            review testReview = new review();

            //Set attributes
            testReview.is_anonymous = false;
            testReview.order_id = o.id;
            testReview.restaurant_id = r.id;
            testReview.user_id = u.id;
            testReview.rating = 0;

            //Save
            db.reviews.Add(testReview);
            db.SaveChanges();
            db.Dispose();

            return testReview;
        }
Esempio n. 21
0
        /// <summary>
        /// Creates an entry of type user in the database.
        /// </summary>
        /// <param name="username">A unique string to represent the user.</param>
        /// <param name="currentTable">A table in the restaurant.</param>
        /// <param name="userRole">The role of the user (ex: Administrator, Client)</param>
        /// <returns>The created user entity.</returns>
        public user AddUser(string email, table currentTable, int userRole)
        {
            //Initialise
            db = new touch_for_foodEntities();
            user testUser = new user();

            //Set attributes
            testUser.username = email;

            // Make sure the password is encrypted
            AES aes = new AES();
            testUser.password = aes.EncryptToString(email);
            testUser.ConfirmPassword = aes.EncryptToString(email);

            testUser.first_name = email;
            testUser.last_name = email;
            testUser.email = email;
            testUser.image_url = email;
            testUser.current_table_id = currentTable.id;
            testUser.version = 1;
            testUser.user_role = userRole;

            //Save
            db.users.Add(testUser);
            db.SaveChanges();
            db.Dispose();

            return testUser;
        }
Esempio n. 22
0
        public static void MyClassInitialize(TestContext testContext)
        {
            testDatabase = new TestDatabaseHelper();

            //Add test data (order specific)
            item1 = testDatabase.AddItem();
            category1 = testDatabase.AddCategory();
            restaurant1 = testDatabase.AddRestaurant();
            menu1 = testDatabase.AddMenu(restaurant1);
            menuCategory1 = testDatabase.AddMenuCategory(category1, menu1);
            table1 = testDatabase.AddTable(restaurant1);
            order1 = testDatabase.AddOrder(table1);
            user1 = testDatabase.AddUser("*****@*****.**", table1, (int)SiteRoles.Admin);
            review1 = testDatabase.AddReview(restaurant1, order1, user1);
        }
Esempio n. 23
0
        public ActionResult Edit(user user, HttpPostedFileBase file)
        {
            // Get array of errors (if any)
            var errors = ModelState.Where(x => x.Value.Errors.Count > 0)
                                    .Select(x => new { x.Key, x.Value.Errors })
                                    .ToArray();

            // Only allow entry if the ModelState is valid or if we have an invalid ModelState that's caused by a blank (null) password
            if (ModelState.IsValid ||
                (!ModelState.IsValid && errors.Length == 1
                && errors[0].Key.Equals("password", StringComparison.Ordinal)
                && (user.password == null && user.ConfirmPassword == null)))
            {
                try
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(Server.MapPath("~/uploads/user_images/"), "user_" + user.id + Path.GetExtension(fileName));
                        //Save the file in given location
                        file.SaveAs(path);
                        //Update the db to show where profile image is located
                        user.image_url = Path.Combine("~/uploads/user_images/", "user_" + user.id + Path.GetExtension(fileName));
                    }

                    // If the user did enter passwords, we hash them
                    if (user.password != null && user.ConfirmPassword != null)
                    {
                        // Encrypt the user's password
                        AES aes = new AES();
                        user.password = aes.EncryptToString(user.password);
                        user.ConfirmPassword = aes.EncryptToString(user.ConfirmPassword);
                    }

                    if (om.edit(user))
                        return RedirectToAction("Index", "Home");
                    else
                    {
                        ViewBag.Error = Global.VersioningError;
                    }
                }
                catch (Exception)
                {
                    ViewBag.Error = Global.ServerError;
                }
            }

            return View(user);
        }