public GovernateEditViewModel Update(GovernateEditViewModel GovernateEditViewModel) { Governate Governate = GovernateRepo.Update(GovernateEditViewModel.ToModel()); unitOfWork.commit(); return(Governate.ToEditableViewModel()); }
public static GovernateEditViewModel ToEditableViewModel(this Governate Model) { return(new GovernateEditViewModel() { ID = Model.ID, Name = Model.Name, }); }
public static GovernateVM GovernateToVM(Governate gov) { GovernateVM govVM = new Models.GovernateVM(); govVM.Id = gov.Id; govVM.Name = gov.Name; govVM.Cities = new List <string>(); foreach (var item in gov.Cities) { govVM.Cities.Add(item.Name); } return(govVM); }
public IHttpActionResult postbook(AddbooVM addbook) { bool govexsist = false; bool cityexsist = false; int govid; int citid; string GoverStr = addbook.Governate; string cityStr = addbook.Governate; if (addbook != null) { if (addbook.Governate.Contains(" Governorate")) { addbook.Governate = addbook.Governate.Replace(" Governorate", ""); } if (addbook.City.Contains(" Governorate")) { addbook.City = addbook.City.Replace(" Governorate", ""); } if (addbook.City.Contains("Qesm")) { addbook.City = addbook.City.Split(' ').Last(); if (addbook.City == "Asyut") { addbook.City = "Assuit"; } } var govnames = (from i in db.Governates select i.Name).ToList(); foreach (var item in govnames) { if (GoverStr == addbook.Governate) { govexsist = true; } } if (govexsist) { govid = (from i in db.Governates where i.Name == addbook.Governate select i.Id).First(); } else { Governate Gov = new Governate(); Gov.Name = addbook.Governate; db.Governates.Add(Gov); db.SaveChanges(); govid = (from i in db.Governates where i.Name == addbook.Governate select i.Id).First(); } var citynames = (from i in db.Cities select i.Name).ToList(); foreach (var item in citynames) { if (item == addbook.City) { cityexsist = true; } } if (cityexsist) { citid = (from i in db.Cities where i.Name == addbook.City select i.Id).First(); } else { City cit = new City(); cit.Name = addbook.City; cit.Governate_Id = govid; db.Cities.Add(cit); db.SaveChanges(); citid = (from i in db.Cities where i.Name == addbook.City select i.Id).First(); } var subid = (from i in db.SubCategories where i.Name == addbook.SubCatogry select i.Id).First(); Book book = new Book(); book.Name = addbook.BookName; book.Points = addbook.Points; book.Discription = addbook.Discription; book.Date_Uploaded = DateTime.Now; book.City_Id = citid; book.Subcategory_Id = subid; db.Books.Add(book); db.SaveChanges(); var bookid = (from i in db.Books where i.Name == addbook.BookName && i.Points == addbook.Points && i.Discription == addbook.Discription && i.City_Id == citid select i.Id).First(); Seller_Approved_book sab = new Seller_Approved_book(); sab.Book_Id = bookid; sab.Seller_Id = addbook.UserId; sab.Approved = "no"; db.Seller_Approved_book.Add(sab); db.SaveChanges(); foreach (var item in addbook.Img) { if (item != null) { Image im = new Image(); im.Book_Id = bookid; im.Img_URL = item; db.Images.Add(im); db.SaveChanges(); } } } return(Ok(addbook)); }
public IHttpActionResult Put(AddbooVM editbook) { if (!ModelState.IsValid) { return(BadRequest("Not a valid model")); } bool govexsist = false; bool cityexsist = false; int govid; int citid; var govnames = (from i in db.Governates select i.Name).ToList(); foreach (var item in govnames) { if (item == editbook.Governate) { govexsist = true; } } if (govexsist) { govid = (from i in db.Governates where i.Name == editbook.Governate select i.Id).First(); } else { Governate Gov = new Governate(); Gov.Name = editbook.Governate; db.Governates.Add(Gov); db.SaveChanges(); govid = (from i in db.Governates where i.Name == editbook.Governate select i.Id).First(); } var citynames = (from i in db.Cities select i.Name).ToList(); foreach (var item in citynames) { if (item == editbook.City) { cityexsist = true; } } if (cityexsist) { citid = (from i in db.Cities where i.Name == editbook.City select i.Id).First(); } else { City cit = new City(); cit.Name = editbook.City; cit.Governate_Id = govid; db.Cities.Add(cit); db.SaveChanges(); citid = (from i in db.Cities where i.Name == editbook.City select i.Id).First(); } var subid = (from i in db.SubCategories where i.Name == editbook.SubCatogry select i.Id).First(); var imglist = (from i in db.Images where i.Book_Id == editbook.BookId select i.Id).ToList(); foreach (var item in imglist) { var img = db.Images.Where(e => e.Id == item).First(); db.Images.Remove(img); db.SaveChanges(); } using (var ctx = new BooksEntities()) { var existingbook = ctx.Books.Where(s => s.Id == editbook.BookId) .FirstOrDefault <Book>(); if (existingbook != null) { existingbook.Name = editbook.BookName; existingbook.Points = editbook.Points; existingbook.Discription = editbook.Discription; existingbook.City_Id = citid; existingbook.Subcategory_Id = subid; ctx.SaveChanges(); foreach (var item in editbook.Img) { if (item != null) { Image im = new Image(); im.Book_Id = editbook.BookId; im.Img_URL = item; ctx.Images.Add(im); ctx.SaveChanges(); } } } else { return(NotFound()); } } return(Ok(editbook)); }