public void EditDish(DishModelDetailsInfo dish) { var dishDb = Database.Dish.QueryToTable.FirstOrDefault(x => x.id == dish.ID); if (dishDb == null) { return; } dishDb.Name = dish.Name; dishDb.Description = dish.Description; dishDb.Energy = dish.Energy; dishDb.Ingridients = dish.Ingridients; dishDb.Price = dish.Price; dishDb.Weight = dish.Weight; var oldImages = Database.DishImage.QueryToTable.Where(x => x.Dish.id == dishDb.id); foreach (var oldImage in oldImages) { Database.DishImage.Delete(oldImage); } foreach (var imgPath in dish.ImagePath) { var imgRow = new DishImage { Dish = dishDb, Path = imgPath, }; dishDb.Images.Add(imgRow); } Database.Dish.Update(dishDb); Database.Save(); }
public void CreateDish(DishModelDetailsInfo dish) { var toDb = Mapper.Map <DishModelDetailsInfo, Dish>(dish); toDb.Images = new List <DishImage>(); //add images to dish foreach (var imgPath in dish.ImagePath) { var imgRow = new DishImage { Dish = toDb, Path = imgPath, }; toDb.Images.Add(imgRow); } //add dish to database Database.Dish.Add(toDb); Database.Save(); }
public async Task <HttpResponseMessage> Update() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath("~/Images/Dish"); var provider = new CustomMultipartFormDataStreamProvider(root); //new MultipartFormDataStreamProvider(root); try { // Read the form data. await Request.Content.ReadAsMultipartAsync(provider); var pathArray = provider.FileData.Select(file => file.LocalFileName.Substring(root.Length + 1)).ToArray(); var detailsDish = new DishModelDetailsInfo { ID = Convert.ToInt32(provider.FormData.Get("id")), Description = provider.FormData.Get("Description"), Energy = Convert.ToInt32(provider.FormData.Get("Energy")), ImagePath = pathArray, Ingridients = provider.FormData.Get("Ingridients"), Name = provider.FormData.Get("Name"), Price = Convert.ToInt32(provider.FormData.Get("Price")), Weight = Convert.ToInt32(provider.FormData.Get("Weight")) }; _dishService.EditDish(detailsDish); return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e)); } }