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 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 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); }