public IActionResult Update([FromBody] UserLunchViewModel model)
        {
            var user = _userService.UpdateUser(model.User);

            model.User = user;
            return(Ok(null));
        }
Example #2
0
        private UserLunchViewModel CreateNewLunch(Menu activeMenu)
        {
            var sections = _context.MenuSections.Select(s => new UserLunchSectionViewModel()
            {
                MenuSectionId = s.MenuSectionId,
                MenuId        = activeMenu.MenuId,
                Name          = s.Name,
                Number        = s.Number,
                Items         = _context.MenuItems
                                .Where(i => i.MenuSectionId == s.MenuSectionId && i.MenuId == activeMenu.MenuId)
                                .Select(i => new UserLunchItemViewModel()
                {
                    MenuSectionId = i.MenuSectionId,
                    Name          = i.Name,
                    MenuItemId    = i.MenuItemId,
                    Number        = i.Number,
                    Checked       = false
                })
                                .OrderBy(i => i.Number)
                                .ToList()
            })
                           .OrderBy(s => s.Number)
                           .ToList();
            var model = new UserLunchViewModel()
            {
                MenuId   = activeMenu.MenuId,
                Sections = sections,
            };

            return(model);
        }
Example #3
0
 public void AddUserLunchToOrder(UserLunchViewModel userLunch)
 {
     using (var tr = _context.Database.BeginTransaction())
     {
         try
         {
             var order          = GetOrCreateOrder(userLunch.MenuId);
             var userLunchOrder =
                 _context.OrderUserLunches.FirstOrDefault(
                     u => u.UserLunchId == userLunch.UserLunchId && u.OrderId == order.OrderId);
             if (userLunchOrder != null)
             {
                 return;
             }
             userLunchOrder = new OrderUserLunch()
             {
                 OrderId     = order.OrderId,
                 UserLunchId = userLunch.UserLunchId
             };
             _context.OrderUserLunches.Add(userLunchOrder);
             _context.SaveChanges();
             tr.Commit();
         }
         catch (Exception e)
         {
             tr.Rollback();
             throw new Exception(e.Message);
         }
     }
 }
 private UserLunchViewModel UpdateLunchItems(UserLunchViewModel model)
 {
     foreach (var sec in model.Sections)
     {
         foreach (var item in sec.Items)
         {
             UpdateUserLunchItem(item, model.UserLunchId);
         }
     }
     return(model);
 }
Example #5
0
        public UserLunchViewModel UpdateUserLunch(UserLunchViewModel model)
        {
            var userLunch = new UserLunch()
            {
                MenuId      = model.MenuId,
                UserId      = model.User.Id,
                UserLunchId = model.UserLunchId,
                Submitted   = false
            };

            return(null);
        }
 public UserLunchViewModel UpdateUserLunch(UserLunchViewModel model)
 {
     using (var tr = _context.Database.BeginTransaction())
     {
         try
         {
             var newLunch  = false;
             var userLunch = _context.UserLunches.FirstOrDefault(x => x.UserLunchId == model.UserLunchId);
             if (userLunch == null)
             {
                 newLunch  = true;
                 userLunch = new UserLunch();
             }
             userLunch.MenuId        = model.MenuId;
             userLunch.UserId        = model.User.Id;
             userLunch.UserLunchId   = model.UserLunchId;
             userLunch.Submitted     = true;
             userLunch.Editable      = true;
             userLunch.SubmitionDate = DateTime.Now;
             if (newLunch)
             {
                 userLunch.CreationDate = DateTime.Now;
                 _context.UserLunches.Add(userLunch);
                 _context.SaveChanges();
             }
             var lunchId = userLunch.UserLunchId;
             model.UserLunchId = lunchId;
             UpdateLunchItems(model);
             var menu = _context.Menus.FirstOrDefault(m => m.MenuId == model.MenuId);
             if (menu == null)
             {
                 return(model);
             }
             menu.Editable = false;
             _context.SaveChanges();
             tr.Commit();
             return(model);
         }
         catch (Exception e)
         {
             tr.Rollback();
             throw new Exception(e.Message, e.InnerException);
         }
     }
 }
Example #7
0
        public UserLunchViewModel GetCurrentLunch(int userId)
        {
            var activeMenu = _context.Menus.FirstOrDefault(m => m.Active);

            if (activeMenu == null)
            {
                return(null);
            }
            var lunch = _context.UserLunches.FirstOrDefault(l => l.UserId == userId && l.Menu.Active);

            if (lunch == null)
            {
                var newLunch = CreateNewLunch(activeMenu);
                return(newLunch);
            }
            var model = new UserLunchViewModel()
            {
                UserLunchId = lunch.UserLunchId,
                MenuId      = lunch.MenuId,
                Sections    = _context.MenuSections.Select(s => new UserLunchSectionViewModel()
                {
                    MenuSectionId = s.MenuSectionId,
                    MenuId        = activeMenu.MenuId,
                    Name          = s.Name,
                    Number        = s.Number,
                    Items         = _context.MenuItems
                                    .Where(i => i.MenuSectionId == s.MenuSectionId && i.MenuId == activeMenu.MenuId)
                                    .Select(i => new UserLunchItemViewModel()
                    {
                        MenuSectionId = i.MenuSectionId,
                        Name          = i.Name,
                        MenuItemId    = i.MenuItemId,
                        Number        = i.Number,
                        Checked       =
                            _context.UserLunchItems.Any(x => x.MenuItemId == i.MenuItemId)
                    })
                                    .OrderBy(i => i.Number)
                                    .ToList()
                })
                              .OrderBy(s => s.Number)
                              .ToList()
            };

            return(model);
        }
        private UserLunchViewModel GetCurrentLunch(Menu activeMenu, UserLunch lunch)
        {
            var user     = _context.Users.FirstOrDefault(u => u.Id == lunch.UserId);
            var userName = user != null ? user.Name : string.Empty;
            var model    = new UserLunchViewModel()
            {
                Editable    = !_menuService.CheckIfOrderForMenuSubmitted(lunch.MenuId),
                UserLunchId = lunch.UserLunchId,
                MenuId      = lunch.MenuId,
                LunchDate   = activeMenu.LunchDate.ToString(_dateFormat),
                Price       = activeMenu.Price,
                User        = new ViewModels.User.UserViewModel()
                {
                    Id   = lunch.UserId,
                    Name = userName
                },
                Sections = _context.MenuSections.Select(s => new UserLunchSectionViewModel()
                {
                    MenuSectionId = s.MenuSectionId,
                    MenuId        = activeMenu.MenuId,
                    Name          = s.Name,
                    Number        = s.Number,
                    Items         = _context.MenuItems
                                    .Where(i => i.MenuSectionId == s.MenuSectionId && i.MenuId == activeMenu.MenuId)
                                    .Select(i => new UserLunchItemViewModel()
                    {
                        MenuSectionId = i.MenuSectionId,
                        Name          = i.Name,
                        MenuItemId    = i.MenuItemId,
                        Number        = i.Number,
                        Checked       =
                            _context.UserLunchItems.Any(x => x.MenuItemId == i.MenuItemId)
                    })
                                    .OrderBy(i => i.Number)
                                    .ToList()
                })
                           .OrderBy(s => s.Number)
                           .ToList(),
                SelectedForOrder = true
            };

            return(model);
        }
 public IActionResult Update([FromBody] UserLunchViewModel model)
 {
     try
     {
         var results = _validator.Validate(model);
         if (!results.IsValid)
         {
             throw new Exception(results.Errors[0].ErrorMessage);
         }
         var user = _userService.UpdateUser(model.User);
         model.User = user;
         var refreshedModel = _userLunchService.UpdateUserLunch(model);
         _orderService.AddUserLunchToOrder(refreshedModel);
         return(Ok(refreshedModel));
     }
     catch (Exception e)
     {
         return(BadRequest(e));
     }
 }
Example #10
0
        private UserLunchViewModel CreateNewLunch(Menu activeMenu)
        {
            if (_menuService.CheckIfOrderForMenuSubmitted(activeMenu.MenuId))
            {
                throw new Exception(LocalizationStrings.OrderHasBeenSent);
            }
            var sections = _context.MenuSections.Select(s => new UserLunchSectionViewModel()
            {
                MenuSectionId = s.MenuSectionId,
                MenuId        = activeMenu.MenuId,
                Name          = s.Name,
                Number        = s.Number,
                Items         = _context.MenuItems
                                .Where(i => i.MenuSectionId == s.MenuSectionId && i.MenuId == activeMenu.MenuId)
                                .Select(i => new UserLunchItemViewModel()
                {
                    MenuSectionId = i.MenuSectionId,
                    Name          = i.Name,
                    MenuItemId    = i.MenuItemId,
                    Number        = i.Number,
                    Checked       = false
                })
                                .OrderBy(i => i.Number)
                                .ToList()
            })
                           .OrderBy(s => s.Number)
                           .ToList();
            var model = new UserLunchViewModel()
            {
                MenuId    = activeMenu.MenuId,
                Sections  = sections,
                Price     = activeMenu.Price,
                LunchDate = activeMenu.LunchDate.ToString(_dateFormat),
                Editable  = !_menuService.CheckIfOrderForMenuSubmitted(activeMenu.MenuId)
            };

            return(model);
        }