コード例 #1
0
 private IHttpActionResult Post([FromBody] JObject json)
 {
     try
     {
         using (var db = new RestaurantContext())
         {
             IHttpActionResult result;
             DishCE            d = JsonConvert.DeserializeObject <DishCE>(json.ToString());
             if (db.Categories.Find(d.Category.ID) != null)
             {
                 Dish d1 = new Dish();
                 d1.ID          = d.ID;
                 d1.Name        = d.Name;
                 d1.ImageURL    = d.ImageURL;
                 d1.Area        = d.Area;
                 d1.Cooking     = d.Cooking;
                 d1.ID_Category = d.Category.ID;
                 db.Dishes.Add(d1);
                 foreach (var tag in d.Tags)
                 {
                     if (db.Tags.FirstOrDefault(t => t.TagName == tag) == default)
                     {
                         Tag tag1 = new Tag();
                         tag1.TagName = tag;
                         db.Tags.Add(tag1);
                     }
                 }
                 db.SaveChanges();
                 foreach (var tag in d.Tags)
                 {
                     Tag      tag1 = db.Tags.First(t => t.TagName == tag);
                     Dish_Tag dt   = new Dish_Tag();
                     dt.ID_Dish = d1.ID;
                     dt.ID_Tag  = tag1.ID;
                     db.Dish_Tag.Add(dt);
                 }
                 foreach (var ingt in d.Ingredients)
                 {
                     string          ingtQty = ingt.Substring(0, ingt.IndexOf('*'));
                     string          ingtID  = ingt.Substring(ingt.IndexOf('*') + 1);
                     Dish_Ingredient DI      = new Dish_Ingredient();
                     DI.ID_Dish       = d1.ID;
                     DI.ID_Ingredient = Convert.ToInt32(ingtID);
                     DI.Quantity      = ingtQty;
                     db.Dish_Ingredient.Add(DI);
                 }
                 db.SaveChanges();
                 d.ID   = d1.ID;
                 result = Ok(d);
             }
             else
             {
                 result = Content(HttpStatusCode.Conflict, "Error");
             }
             return(result);
         }
     }
     catch (Exception ex)
     {
         return(Content(HttpStatusCode.Conflict, ex.Message));
     }
 }
コード例 #2
0
 private IHttpActionResult Put([FromBody] JObject json)
 {
     try
     {
         using (var db = new RestaurantContext())
         {
             IHttpActionResult result;
             DishCE            d = JsonConvert.DeserializeObject <DishCE>(json.ToString());
             Dish d1             = db.Dishes.Find(d.ID);
             if (d1 != null)
             {
                 d1.Name        = !string.IsNullOrEmpty(d.Name) ? d.Name : d1.Name;
                 d1.ImageURL    = !string.IsNullOrEmpty(d.ImageURL) ? d.ImageURL : d1.ImageURL;
                 d1.Area        = !string.IsNullOrEmpty(d.Area) ? d.Area : d1.Area;
                 d1.Cooking     = !string.IsNullOrEmpty(d.Cooking) ? d.Cooking : d1.Cooking;
                 d1.ID_Category = !string.IsNullOrEmpty(d.Category.ID.ToString()) ? d.Category.ID : d1.Category.ID;
                 if (d.Tags.Count() != 0)
                 {
                     var dish_Tags = db.Dish_Tag.Where(dt => dt.ID_Dish == d.ID).ToList <Dish_Tag>();
                     foreach (Dish_Tag item in dish_Tags)
                     {
                         db.Dish_Tag.Remove(item);
                     }
                     db.SaveChanges();
                     foreach (var tag in d.Tags)
                     {
                         if (db.Tags.FirstOrDefault(t => t.TagName == tag) == default)
                         {
                             Tag tag1 = new Tag();
                             tag1.TagName = tag;
                             db.Tags.Add(tag1);
                         }
                     }
                     db.SaveChanges();
                     foreach (var tag in d.Tags)
                     {
                         Tag      tag1 = db.Tags.First(t => t.TagName == tag);
                         Dish_Tag dt1  = new Dish_Tag();
                         dt1.ID_Dish = d1.ID;
                         dt1.ID_Tag  = tag1.ID;
                         db.Dish_Tag.Add(dt1);
                     }
                 }
                 if (d.Ingredients.Count() != 0)
                 {
                     var dish_ingt = db.Dish_Ingredient.Where(dt => dt.ID_Dish == d.ID).ToList <Dish_Ingredient>();
                     foreach (Dish_Ingredient item in dish_ingt)
                     {
                         db.Dish_Ingredient.Remove(item);
                     }
                     db.SaveChanges();
                     foreach (var ingt in d.Ingredients)
                     {
                         string          ingtQty = ingt.Substring(0, ingt.IndexOf('*'));
                         string          ingtID  = ingt.Substring(ingt.IndexOf('*') + 1);
                         Dish_Ingredient DI      = new Dish_Ingredient();
                         DI.ID_Dish       = d1.ID;
                         DI.ID_Ingredient = Convert.ToInt32(ingtID);
                         DI.Quantity      = ingtQty;
                         db.Dish_Ingredient.Add(DI);
                     }
                 }
                 db.SaveChanges();
                 result = Content(HttpStatusCode.OK, "Row updated");
             }
             else
             {
                 result = Content(HttpStatusCode.NotFound, "Dish not found");
             }
             return(result);
         }
     }
     catch (Exception ex)
     {
         return(Content(HttpStatusCode.Conflict, ex.Message));
     }
 }