public ActionResult PostSuggestion([FromBody] SuggestionFormModel formModel) { EPortfolioDB context = new EPortfolioDB(); bool ifStudentExist = false; //check if StudentID is valid bool ifLecturerExist = false; //check if LecturerID is valid int suggestionId = -1; Suggestion s = new Suggestion(); formModel.Apply(s); TryValidateModel(s); if (ModelState.IsValid) { //Validate if the StudentId and LecturerId is existed foreach (Lecturer i in context.Lecturers) { if (s.LecturerId == i.LecturerId) { ifLecturerExist = true; break; } else { ifLecturerExist = false; } } foreach (Student i in context.Students) { if (s.StudentId == i.StudentId) { ifStudentExist = true; break; } else { ifStudentExist = false; } } using (EPortfolioDB db = new EPortfolioDB()) { //If both StudentId & LecturerID is existed DB will save changes if (ifStudentExist == true && ifLecturerExist == true) { db.Suggestions.Add(s); db.SaveChanges(); suggestionId = s.SuggestionId; } } Object response = new { suggestionId = suggestionId }; return(Json(response)); } else { return(NotFound()); } }
public ActionResult Index(SuggestionFormModel model) { var(result, exception) = new DBManager(cache).GetSuggestions(model.WantsReadilyAvailable, model.LikesCaramel, model.LikesRye, model.MaxPrice, model.LikesHighProof, model.LikesSmooth, model.LikesSweet, model.DrinkType); if (exception != null) { return(RedirectToError(exception)); } var finalResults = result.Select(a => new SuggestionModelItem { Name = a.Name, DrinkType = a.Type, EasyToFind = a.EasyToFind, TastingNotes = a.TastingNotes, Nose = a.Nose, Price = a.Price, AdditionalNotes = a.AdditionalNotes, Aged = a.Aged, ABV = a.ABV, WorthIt = a.WorthIt, Rating = a.Rating, ActualPrice = a.ActualPrice, MaxWorthItPrice = a.MaxWorthItPrice }); if (model.MostImportantAspect == MostImportantSuggestionChoices.NO_OPINION) { return(View(finalResults.ToList())); } switch (model.MostImportantAspect) { case MostImportantSuggestionChoices.LOW_PRICE: finalResults = finalResults.OrderBy(a => a.Price); break; case MostImportantSuggestionChoices.QUALITY: finalResults = finalResults.OrderByDescending(a => a.Rating); break; } return(View(finalResults.ToList())); }