public async Task <AddDishResponse> AddDish(AddDishRequest addDishRequest) { AddDishResponse addDishResponse = addDishRequest.IsValid(); if (addDishResponse.Success) { try { Dish dish = MapToDataBaseModel(addDishRequest); await _dishRepository.Create(dish); addDishResponse.Id = dish.Id.ToString(); } catch (TimeoutException) { addDishResponse.SetError(ErrorMessage.TimeoutError); } catch (Exception ex) { addDishResponse.SetError(ex); } } return(addDishResponse); }
public static AddDishResponse IsValid(this AddDishRequest addDishRequest) { AddDishResponse addDishResponse = new AddDishResponse(); if (string.IsNullOrEmpty(addDishRequest.Title)) { addDishResponse.SetError(ErrorMessage.TitleParameterNotValid); return(addDishResponse); } if (string.IsNullOrEmpty(addDishRequest.Category)) { addDishResponse.SetError(ErrorMessage.CategoryParameterNotValid); return(addDishResponse); } if (string.IsNullOrEmpty(addDishRequest.SubCategory)) { addDishResponse.SetError(ErrorMessage.SubCategoryParameterNotValid); return(addDishResponse); } if (addDishRequest.Price <= 0) { addDishResponse.SetError(ErrorMessage.PriceParameterNotValid); return(addDishResponse); } if (addDishRequest.EstimatedTime <= 0) { addDishResponse.SetError(ErrorMessage.EstimatedTimeParameterNotValid); return(addDishResponse); } foreach (var item in addDishRequest.MealType) { if (!Enum.IsDefined(typeof(MealType), item)) { addDishResponse.SetError(ErrorMessage.MealTypeParameterNotValid); return(addDishResponse); } } foreach (var item in addDishRequest.Days) { if (!Enum.IsDefined(typeof(Day), item)) { addDishResponse.SetError(ErrorMessage.DayParameterNotValid); return(addDishResponse); } } if (addDishRequest.Ingredients.Count > 0) { foreach (var item in addDishRequest.Ingredients) { if (string.IsNullOrEmpty(item.Key) || string.IsNullOrEmpty(item.Value)) { addDishResponse.SetError(ErrorMessage.IngredientsParameterNotValid); return(addDishResponse); } } } return(addDishResponse); }
private Dish MapToDataBaseModel(AddDishRequest addDishRequest) { Dish dish = new Dish { Title = addDishRequest.Title.ToTitleForStore(), AvailableCount = addDishRequest.AvailableCount, MealType = addDishRequest.MealType.Select(r => (MealType)r).ToList(), Days = addDishRequest.Days.Select(r => (Day)r).ToList(), Category = addDishRequest.Category.ToTitleForStore(), SubCategory = addDishRequest.SubCategory.ToTitleForStore(), EstimatedTime = addDishRequest.EstimatedTime, Ingredients = addDishRequest.Ingredients, IsDiactive = addDishRequest.IsDiactive, Price = addDishRequest.Price, Description = addDishRequest.Description }; return(dish); }
public async Task <BaseResponse> Update(string id, AddDishRequest addDishRequest) { BaseResponse addDishResponse = addDishRequest.IsValid(); if (addDishResponse.Success) { try { var dbDish = await _dishRepository.Get(ObjectId.Parse(id)); if (dbDish == null) { addDishResponse.SetError(ErrorMessage.RecordNotFound); return(addDishResponse); } Dish newDish = MapToDataBaseModel(addDishRequest); newDish.Id = dbDish.Id; await _dishRepository.Update(newDish); } catch (TimeoutException) { addDishResponse.SetError(ErrorMessage.TimeoutError); } catch (FormatException) { addDishResponse.SetError(ErrorMessage.FormatError); } catch (Exception ex) { addDishResponse.SetError(ex); } } return(addDishResponse); }
public async Task <IActionResult> AddDish(AddDishRequest addDishRequest) { var result = await _dishService.AddDish(addDishRequest); return(Ok(result)); }
public async Task <IActionResult> UpdateDish(string id, AddDishRequest addDishRequest) { var result = await _dishService.Update(id, addDishRequest); return(Ok(result)); }
public void DishModel_Validate() { AddDishRequest addDishRequest = new AddDishRequest(); //Test Empty Model var validateResponse = addDishRequest.IsValid(); Assert.AreEqual(false, validateResponse.Success); addDishRequest = new AddDishRequest { Title = "peperoni", AvailableCount = 60, Category = "Main Course", SubCategory = "Pizza", EstimatedTime = 40, IsDiactive = false, Price = 10, MealType = new List <int>(), Days = new List <int>(), Ingredients = new Dictionary <string, string>(), }; addDishRequest.MealType.Add(1); addDishRequest.MealType.Add(2); addDishRequest.Days.Add(1); addDishRequest.Ingredients.Add(new KeyValuePair <string, string>("Sausage", "100g")); addDishRequest.Ingredients.Add(new KeyValuePair <string, string>("Cheese", "150g")); //Test Wrong MealType addDishRequest.MealType.Add(0); //0 is not valid validateResponse = addDishRequest.IsValid(); Assert.AreEqual(false, validateResponse.Success); Assert.AreEqual(ErrorMessage.MealTypeParameterNotValid, validateResponse.Message); addDishRequest.MealType.Remove(0); //Test Wrong Day addDishRequest.Days.Add(0);// 0 is not valid validateResponse = addDishRequest.IsValid(); Assert.AreEqual(false, validateResponse.Success); Assert.AreEqual(ErrorMessage.DayParameterNotValid, validateResponse.Message); addDishRequest.Days.Remove(0); //Test wrong Category addDishRequest.Category = string.Empty; validateResponse = addDishRequest.IsValid(); Assert.AreEqual(false, validateResponse.Success); Assert.AreEqual(ErrorMessage.CategoryParameterNotValid, validateResponse.Message); addDishRequest.Category = "MainCourse"; //Test wrong SubCategory addDishRequest.SubCategory = string.Empty; validateResponse = addDishRequest.IsValid(); Assert.AreEqual(false, validateResponse.Success); Assert.AreEqual(ErrorMessage.SubCategoryParameterNotValid, validateResponse.Message); addDishRequest.SubCategory = "Pizza"; //Test wrong Price addDishRequest.Price = default; validateResponse = addDishRequest.IsValid(); Assert.AreEqual(false, validateResponse.Success); Assert.AreEqual(ErrorMessage.PriceParameterNotValid, validateResponse.Message); addDishRequest.Price = 3; //Test wrong EstimatedTime addDishRequest.EstimatedTime = default; validateResponse = addDishRequest.IsValid(); Assert.AreEqual(false, validateResponse.Success); Assert.AreEqual(ErrorMessage.EstimatedTimeParameterNotValid, validateResponse.Message); addDishRequest.EstimatedTime = 30; //Test wrong Title addDishRequest.Title = string.Empty; validateResponse = addDishRequest.IsValid(); Assert.AreEqual(false, validateResponse.Success); Assert.AreEqual(ErrorMessage.TitleParameterNotValid, validateResponse.Message); addDishRequest.Title = "something"; //Test wrong Ingredients addDishRequest.Ingredients = new Dictionary <string, string>(); addDishRequest.Ingredients.Add(new KeyValuePair <string, string>(string.Empty, "")); validateResponse = addDishRequest.IsValid(); Assert.IsFalse(validateResponse.Success); Assert.AreEqual(ErrorMessage.IngredientsParameterNotValid, validateResponse.Message); }