public Menu Fetch <T>() where T : Menu { Menu res = null; JObject jObject = JsonToJObjectHelper.Convert(jsonPath); if (jObject == null) { return(null); } try { res = jObject.ToObject <T>(); if (!MealValidator.IsOk(res)) { logger.LogError("Invalid Current Menu object found in the database!"); throw new InvalidDataException("Invalid Current Menu found!"); //TODO: very ugly case scenario. What to do? } } catch (JsonSerializationException e) { logger.LogError("Current Menu data has invalid values " + e.Message); } return(res); }
public void Setup() { _mealDataServiceMock = new Mock <IMealDataService>(); _navigationServiceMock = new Mock <INavigationService>(); _mealValidator = new MealValidator(); _mediaFileServiceMock = new Mock <IMediaFileService>(); _dialogServiceMock = new Mock <IDialogService>(); _sut = new MealCreateViewModel( _navigationServiceMock.Object, _mealDataServiceMock.Object, _mealValidator, _dialogServiceMock.Object, _mediaFileServiceMock.Object); }
public async Task <IActionResult> Create(CreateMealViewModel model) { var userId = _userManager.GetUserId(User); EndUser endUser = await _context.EndUsers.FirstOrDefaultAsync(x => x.ApplicationUserId == userId); string uploadedFile = UploadedFile(model.MealImage); if (string.IsNullOrEmpty(uploadedFile)) { ModelState.AddModelError(string.Empty, "Image is not selected"); } if (endUser != null && ModelState.IsValid) { model.Meal.EndUserId = endUser.EndUserId; model.Meal.PictureMeal = uploadedFile; var validator = new MealValidator(_context.Meals); var result = validator.Validate(model.Meal); if (result.IsValid) { if (model.SelectedCategories != null) { List <MealCategory> newCategories = new List <MealCategory>(); foreach (int CategoryId in model.SelectedCategories) { newCategories.Add(new MealCategory { MealId = model.Meal.MealId, CategoryId = CategoryId }); } _context.Add(model.Meal); await _context.SaveChangesAsync(); Meal meal = await _context.Meals .Include(mc => mc.MealCategories) .SingleOrDefaultAsync(meal => meal.MealId == model.Meal.MealId); meal.MealCategories.AddRange(newCategories); _context.Update(meal); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { ModelState.AddModelError(string.Empty, "Select at least one category!"); } } else { foreach (var item in result.Errors) { ModelState.AddModelError(string.Empty, item.ErrorMessage); } } } model.LevelPreperations = new SelectList(_context.LevelPreperations, "LevelPreperationId", "Level", model.Meal.LevelPreperationId); model.PreperationTimes = new SelectList(_context.PreperationTimes, "PreperationTimeId", "RangeMinutes", model.Meal.PreperationTimeId); model.Category = new SelectList(_context.Categories, "CategoryId", "Name"); model.SelectedCategories = new List <int>(); return(View(model)); }
public async Task <IActionResult> Edit(int id, EditMealViewModel model) { if (id != model.Meal.MealId) { return(RedirectToAction("Error", "Home")); } Meal meal = await _context.Meals.Include(c => c.MealCategories).SingleOrDefaultAsync(x => x.MealId == id); if (meal != null) { meal.Name = model.Meal.Name; meal.ShortDiscription = model.Meal.ShortDiscription; meal.PreperationDiscribtion = model.Meal.PreperationDiscribtion; meal.NumberOfPeople = model.Meal.NumberOfPeople; if (meal.PictureMeal != model.Meal.PictureMeal) { meal.PictureMeal = UploadedFile(model.MealImage); } Meal checkMealName = await _context.Meals.SingleOrDefaultAsync(x => x.Name == model.Meal.Name && x.MealId == model.Meal.MealId); var result = new FluentValidation.Results.ValidationResult();//Bypass the name validator and didn't know how to do it any other way if (checkMealName != null) { var validator = new EditMealValidator(); result = validator.Validate(model.Meal); } else { var validator = new MealValidator(_context.Meals); result = validator.Validate(model.Meal); } if (result.IsValid) { if (model.SelectedCategories != null) { List <MealCategory> newCategories = new List <MealCategory>(); foreach (int CategoryId in model.SelectedCategories) { newCategories.Add(new MealCategory { MealId = model.Meal.MealId, CategoryId = CategoryId }); } meal.MealCategories.RemoveAll(mc => !newCategories.Contains(mc)); meal.MealCategories.AddRange(newCategories.Where(nc => !meal.MealCategories.Contains(nc))); _context.Update(meal); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { model.ErrorMessage += "Select at least one categorie"; } } else { foreach (var item in result.Errors) { ModelState.AddModelError(string.Empty, item.ErrorMessage); } } } else { model.ErrorMessage += "Select at least one categorie"; } return(View(model)); }