public static ICollection <Restaurant> FindRestaurantsByName(string key) { using (db = new RestaurantReviewsEntities()) { return(db.Restaurants.Where(x => x.Name.Contains(key)).Include("Reviews").ToList()); } }
public static IEnumerable <Review> ReadReviews() { using (db = new RestaurantReviewsEntities()) { return(db.Reviews.ToList()); } }
public static ICollection <Restaurant> ReadRestaurantsSortByRating() { using (db = new RestaurantReviewsEntities()) { return(db.Restaurants.OrderByDescending(x => x.AvgRating).Include("Reviews").ToList()); } }
public static Restaurant FindRestaurantByID(int id) { using (db = new RestaurantReviewsEntities()) { return(db.Restaurants.Find(id)); } }
public static ICollection <Review> FindReviewsByRestaurantID(int restID) { using (db = new RestaurantReviewsEntities()) { return(db.Reviews.Where(x => x.RestaurantID == restID).ToList()); } }
public static ICollection <Restaurant> ReadRestaurants() { using (db = new RestaurantReviewsEntities()) { return(db.Restaurants.Include("Reviews").ToList()); } }
public static void DeleteRestaurantByID(int id) { using (db = new RestaurantReviewsEntities()) { Logger log = LogManager.GetCurrentClassLogger(); StringBuilder msg = new StringBuilder(); Restaurant rest = db.Restaurants.Find(id); db.Restaurants.Remove(rest); ICollection <Review> reviews = db.Reviews.Where(x => x.RestaurantID == rest.ID).ToList(); foreach (Review rev in reviews) { db.Reviews.Remove(rev); } try { db.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { msg.Append(rest.Name) .Append("\n--\n"); foreach (var eve in e.EntityValidationErrors) { msg.Append("Entity of type \"") .Append(eve.Entry.Entity.GetType().Name) .Append("\" in state \"") .Append(eve.Entry.State) .Append("\" has the following validation errors:\n"); foreach (var ve in eve.ValidationErrors) { msg.Append("- Property: \"") .Append(ve.PropertyName) .Append("\", Error: \"") .Append(ve.ErrorMessage) .Append("\"\n"); } } FinishExceptionHandling(log, e, msg.ToString()); } catch (Exception ex) { FinishExceptionHandling(log, ex, ex.StackTrace); } } }
public static void UpdateReview(Review review) { using (db = new RestaurantReviewsEntities()) { Logger log = LogManager.GetCurrentClassLogger(); StringBuilder msg = new StringBuilder(); Review rev = db.Reviews.Find(review.ID); rev.Rating = review.Rating; rev.RestaurantID = review.RestaurantID; rev.ReviewerID = review.ReviewerID; rev.Description = review.Description; try { db.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { msg.Append(rev.ID + " " + rev.RestaurantID + " " + rev.Rating) .Append("\n--\n"); foreach (var eve in e.EntityValidationErrors) { msg.Append("Entity of type \"") .Append(eve.Entry.Entity.GetType().Name) .Append("\" in state \"") .Append(eve.Entry.State) .Append("\" has the following validation errors:\n"); foreach (var ve in eve.ValidationErrors) { msg.Append("- Property: \"") .Append(ve.PropertyName) .Append("\", Error: \"") .Append(ve.ErrorMessage) .Append("\"\n"); } } FinishExceptionHandling(log, e, msg.ToString()); } catch (Exception ex) { FinishExceptionHandling(log, ex, ex.StackTrace); } } }
public static bool DeleteRestaurant(Restaurant restaurant) { using (db = new RestaurantReviewsEntities()) { Logger log = LogManager.GetCurrentClassLogger(); StringBuilder msg = new StringBuilder(); int count = db.Restaurants.Count(); db.Restaurants.Remove(restaurant); try { db.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { msg.Append(restaurant.Name) .Append("\n--\n"); foreach (var eve in e.EntityValidationErrors) { msg.Append("Entity of type \"") .Append(eve.Entry.Entity.GetType().Name) .Append("\" in state \"") .Append(eve.Entry.State) .Append("\" has the following validation errors:\n"); foreach (var ve in eve.ValidationErrors) { msg.Append("- Property: \"") .Append(ve.PropertyName) .Append("\", Error: \"") .Append(ve.ErrorMessage) .Append("\"\n"); } } FinishExceptionHandling(log, e, msg.ToString()); } catch (Exception ex) { FinishExceptionHandling(log, ex, ex.StackTrace); } return(count == db.Restaurants.Count() + 1); } }
public static void UpdateRestaurant(Restaurant newRestaurant) { using (db = new RestaurantReviewsEntities()) { Logger log = LogManager.GetCurrentClassLogger(); StringBuilder msg = new StringBuilder(); Restaurant oldRestaurant = db.Restaurants.Find(newRestaurant.ID); oldRestaurant.Address = newRestaurant.Address; oldRestaurant.City = newRestaurant.City; oldRestaurant.ID = newRestaurant.ID; oldRestaurant.Name = newRestaurant.Name; oldRestaurant.PhoneNum = newRestaurant.PhoneNum; oldRestaurant.State = newRestaurant.State; oldRestaurant.ZIP = newRestaurant.ZIP; ICollection <Review> reviews = db.Reviews.Where(x => x.RestaurantID == newRestaurant.ID).ToList(); if (reviews.Count > 0) { oldRestaurant.AvgRating = (float)reviews.Average(x => x.Rating); } else { oldRestaurant.AvgRating = 0f; } try { db.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { msg.Append(newRestaurant.Name) .Append("\n--\n"); foreach (var eve in e.EntityValidationErrors) { msg.Append("Entity of type \"") .Append(eve.Entry.Entity.GetType().Name) .Append("\" in state \"") .Append(eve.Entry.State) .Append("\" has the following validation errors:\n"); foreach (var ve in eve.ValidationErrors) { msg.Append("- Property: \"") .Append(ve.PropertyName) .Append("\", Error: \"") .Append(ve.ErrorMessage) .Append("\"\n"); } } FinishExceptionHandling(log, e, msg.ToString()); } catch (Exception ex) { FinishExceptionHandling(log, ex, ex.StackTrace); } } }
public static void UpdateRestaurant(Restaurant newRestaurant) { using (db = new RestaurantReviewsEntities()) { Logger log = LogManager.GetCurrentClassLogger(); StringBuilder msg = new StringBuilder(); Restaurant oldRestaurant = db.Restaurants.Find(newRestaurant.ID); oldRestaurant.Address = newRestaurant.Address; oldRestaurant.AvgRating = newRestaurant.AvgRating; oldRestaurant.City = newRestaurant.City; oldRestaurant.ID = newRestaurant.ID; oldRestaurant.Name = newRestaurant.Name; oldRestaurant.PhoneNum = newRestaurant.PhoneNum; oldRestaurant.State = newRestaurant.State; oldRestaurant.ZIP = newRestaurant.ZIP; Review tmpReview = null; Review oldReview; int i = 0; while (i < oldRestaurant.Reviews.Count) { oldReview = oldRestaurant.Reviews.ElementAt(i); tmpReview = newRestaurant.Reviews.Where(x => x.ID == oldReview.ID).FirstOrDefault(); if (tmpReview != null) { oldReview.Description = tmpReview.Description; oldReview.Rating = tmpReview.Rating; oldReview.ReviewerID = tmpReview.ReviewerID; i++; } else { oldRestaurant.Reviews.Remove(oldReview); } } foreach (Review newReview in newRestaurant.Reviews) { tmpReview = oldRestaurant.Reviews.Where(x => x.ID == newReview.ID).FirstOrDefault(); if (tmpReview == null) { oldRestaurant.Reviews.Add(newReview); } } try { db.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { msg.Append(newRestaurant.Name) .Append("\n--\n"); foreach (var eve in e.EntityValidationErrors) { msg.Append("Entity of type \"") .Append(eve.Entry.Entity.GetType().Name) .Append("\" in state \"") .Append(eve.Entry.State) .Append("\" has the following validation errors:\n"); foreach (var ve in eve.ValidationErrors) { msg.Append("- Property: \"") .Append(ve.PropertyName) .Append("\", Error: \"") .Append(ve.ErrorMessage) .Append("\"\n"); } } FinishExceptionHandling(log, e, msg.ToString()); } catch (Exception ex) { FinishExceptionHandling(log, ex, ex.StackTrace); } } }