public ActionResult SuggestToRecipient(int id) { if (Session["LogedUserID"] != null) { GifterEntities entities = new GifterEntities(); Suggestion s = new Suggestion(); Recipient r = entities.Recipients.Where(model => model.id.Equals(id)).FirstOrDefault(); if (r.idUser == Convert.ToInt32(Session["LogedUserID"])) { s.age = r.age; s.relationship = r.relationship; s.relationshipLength = r.relationshipLength; s.idRecipient = r.id; return View(s); } else { return RedirectToAction("Index"); } } else { return RedirectToAction("Index"); } }
public ActionResult Suggest(Suggestion s) { GifterEntities entities = new GifterEntities(); Gift gift = entities.Gifts.Where(model => model.ageFrom < s.age && model.ageTo > s.age && model.occasion.Contains(s.occasion) && model.relationship.Contains(s.relationship) && model.relationshipLengthFrom < s.relationshipLength && model.relationshipLengthTo > s.relationshipLength).FirstOrDefault(); List<Gift> possibleGifts = GetPossibleGifts(s); if (possibleGifts == null) { return AfterSuggest(s); } else { List<Suggestion> similarSuggestions = GetSimilarSuggestions(s); List<Gift> highestRatingGifts = GetGiftstWithHighestRating(similarSuggestions); possibleGifts = RemoveGiftsWithRatingLowerThenFive(similarSuggestions, possibleGifts); s.idGift = gift.id; if (Session["LogedUserID"] != null) { s.idUser = Convert.ToInt32(Session["LogedUserID"]); } entities.Suggestions.Add(s); try { entities.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } return RedirectToAction("AfterSuggest", s); } }
public List<Gift> GetGiftstWithHighestRating(List<Suggestion> similarSuggestions) { int maxRating = 0; GifterEntities entities = new GifterEntities(); List<Gift> highestRatingGifts = new List<Gift>(); foreach(Suggestion suggestion in similarSuggestions) { if (suggestion.rating == maxRating) { highestRatingGifts.Add(entities.Gifts.Where(model => model.id.Equals(suggestion.idGift)).FirstOrDefault()); } if (maxRating < suggestion.rating) { maxRating = (int)suggestion.rating; highestRatingGifts.Clear(); highestRatingGifts.Add(entities.Gifts.Where(model => model.id.Equals(suggestion.idGift)).FirstOrDefault()); } } return highestRatingGifts; }
public List<Suggestion> GetSimilarSuggestions(Suggestion s) { GifterEntities entities = new GifterEntities(); List<Suggestion> similarSuggestions = entities.Suggestions.Where(model => model.age.Equals(s.age) && model.occasion.Equals(s.occasion) && model.relationship.Equals(s.relationship) && model.relationshipLength.Equals(s.relationshipLength) && !(model.rating.Equals(null))).ToList(); return similarSuggestions; }
public List<Gift> GetPossibleGifts(Suggestion s) { GifterEntities entities = new GifterEntities(); List<Gift> possibleGifts = entities.Gifts.Where(model => model.ageFrom < s.age && model.ageTo > s.age && model.occasion.Contains(s.occasion) && model.relationship.Contains(s.relationship) && model.relationshipLengthFrom < s.relationshipLength && model.relationshipLengthTo > s.relationshipLength).ToList(); return possibleGifts; }