protected void RateControl_Rate(object sender, RateEventArgs e) { var recipeData = this.data.Recipes.Find(recipeId); RecipeRating rating = new RecipeRating() { RecipeID = recipeId, UserID = this.loggedUserId, Rating = e.RateValue, RatedOn = DateTime.Now }; this.data.RecipeRatings.Add(rating); this.data.SaveChanges(); var control = sender as Rates; var recipeRating = 0.0; if (recipeData.Ratings.Count > 0) { recipeRating = (double)recipeData.Ratings.Sum(r => r.Rating) / recipeData.Ratings.Count; } this.RateControl.Value = Convert.ToInt32(Math.Ceiling(recipeRating)); this.RateControl.UserVote = true; this.RateControl.RatingsCount = recipeData.Ratings.Count; this.RateControl.mustUpdate = true; }
public async Task <IActionResult> PutRecipeRating(string userId, int RecipeId, ApiRecipeRating apiObj) { //convert input to database object var updatedRating = new RecipeRating() { UserId = userId, RecipeId = RecipeId, Rating = apiObj.Rating }; //notifier to entity framework core _context.Entry(updatedRating).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RecipeRatingExists(updatedRating)) { return(NotFound()); } throw; } return(NoContent()); }
public async Task VoteForRecipeAsync(string recipeId, string userId, RecipeRating score) { if (score == RecipeRating.noVotes) { throw new ArgumentException("Score is Invalid 0 not allowed"); } var recipeFd = await recipeRepo.All().Include(r => r.Votes).FirstOrDefaultAsync(r => r.Id == recipeId); if (recipeFd is null) { throw new ArgumentException("Unfound Recipe with given Id"); } var voteFd = recipeFd.Votes.Where(x => !x.IsDeleted).FirstOrDefault(x => x.VoterId == userId); if (voteFd != null) { if (score == voteFd.Score) { throw new ArgumentException("Same vote can not be given!"); } voteFd.Score = score; voteFd.DateOfLastEdit = DateTime.UtcNow; await recipeRepo.SaveChangesAsync(); return; } recipeFd.Votes.Add(new RecipeVote { VoterId = userId, Score = score, RecipeId = recipeId }); await recipeRepo.SaveChangesAsync(); }
public ActionResult DeleteConfirmed(int id) { RecipeRating recipeRating = db.RecipeRatings.Find(id); db.RecipeRatings.Remove(recipeRating); db.SaveChanges(); return(RedirectToAction("Index")); }
public void UpdateByUserAndRecipe(RecipeRating rating) { using (var conn = Connection) { conn.Execute($@" update recipe_rating set score = @score where [user] = @user and recipe = @recipe ", new { user = rating.User.Id, score = rating.Score, recipe = rating.RecipeId }); } }
public int Add(RecipeRating rating) { using (var conn = Connection) { return(conn.ExecuteScalar <int>($@" insert recipe_rating (recipe, [user], score) values (@recipe, @user, @score) select scope_identity() ", new { recipe = rating.RecipeId, user = rating.User.Id, score = rating.Score })); } }
public ActionResult Edit([Bind(Include = "UserId,ReceipeId,Rating")] RecipeRating recipeRating) { if (ModelState.IsValid) { db.Entry(recipeRating).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ReceipeId = new SelectList(db.Recipies, "Id", "Name", recipeRating.ReceipeId); ViewBag.UserId = new SelectList(db.Users, "Id", "Login", recipeRating.UserId); return(View(recipeRating)); }
public int AddRating(RecipeRating rating) { try { db.RecipeRatings.Add(rating); db.SaveChanges(); return(rating.RecipeRatingId); } catch (Exception e) { return(-1); } }
// GET: RecipeRatings/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } RecipeRating recipeRating = db.RecipeRatings.Find(id); if (recipeRating == null) { return(HttpNotFound()); } return(View(recipeRating)); }
public IActionResult EditScoreRecipe(int recipe_id, string score, string order_by = null) { int.TryParse(score, out var score_int); var recipe_rating = new RecipeRating { RecipeId = recipe_id, User = GetCurrentUser(), Score = score_int }; _recipeRatingRepository.UpdateByUserAndRecipe(recipe_rating); return(RedirectToRecipes(recipe_id, order_by)); }
// GET: RecipeRatings/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } RecipeRating recipeRating = db.RecipeRatings.Find(id); if (recipeRating == null) { return(HttpNotFound()); } ViewBag.ReceipeId = new SelectList(db.Recipies, "Id", "Name", recipeRating.ReceipeId); ViewBag.UserId = new SelectList(db.Users, "Id", "Login", recipeRating.UserId); return(View(recipeRating)); }
public bool CreateRecipeRating(CreateRecipeRating model) { var entity = new RecipeRating() { Rater = _userId, RecipeId = model.RecipeId, Rating = model.Rating }; using (var ctx = new ApplicationDbContext()) { ctx.RecipeRatings.Add(entity); return(ctx.SaveChanges() == 1); } }
public IActionResult DeleteRecipe(string id) { var el = dbContext.Recipe.Where(x => x.RecipeId == int.Parse(id)).FirstOrDefault(); if (el != null) { var todel = dbContext.ImageAndRecipeConnection.Where(x => x.RecipeId == el.RecipeId); var todel2 = dbContext.RecipeAndCategoryConnectionTable.Where(x => x.RecipeId == el.RecipeId); var todel3 = dbContext.RecipeComment.Where(x => x.ReceipeId == el.RecipeId); var todel4 = dbContext.RecipeRating.Where(x => x.ReceipeId == el.RecipeId); var todel5 = dbContext.UserAndRecipeAddedToFavouriteConnectionTable.Where(x => x.RecipeId == el.RecipeId); foreach (var item in todel) { ImageAndRecipeConnection m = item; dbContext.ImageAndRecipeConnection.Remove(m); } foreach (var item in todel2) { RecipeAndCategoryConnectionTable m = item; dbContext.RecipeAndCategoryConnectionTable.Remove(m); } foreach (var item in todel3) { RecipeComment m = item; dbContext.RecipeComment.Remove(m); } foreach (var item in todel4) { RecipeRating m = item; dbContext.RecipeRating.Remove(m); } foreach (var item in todel5) { UserAndRecipeAddedToFavouriteConnectionTable m = item; dbContext.UserAndRecipeAddedToFavouriteConnectionTable.Remove(m); } dbContext.Recipe.Remove(el); dbContext.SaveChanges(); } return(RedirectToAction("Browse")); }
public async Task <IActionResult> DeleteRecipeRating(ApiRecipeRating apiObj) { var ratingToDelete = RecipeRating.FromApiToRecipeRating(apiObj); _context.Entry(ratingToDelete).State = EntityState.Deleted; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RecipeRatingExists(ratingToDelete)) { return(NotFound()); } throw; } return(NoContent()); }
public IActionResult RateRecipe(int RecipeId, int Rating) { try { RecipeRating rating = new RecipeRating { RecipeId = RecipeId, Rating = Rating, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now }; RecipesData.AddRating(rating); Recipe recipe = RecipesData.GetRecipeByID(RecipeId); return(Json(new { success = true, rating = recipe.Rating, votes = recipe.RatingCount })); } catch (Exception e) { return(Json(new { success = false, msg = $"{e.Message}" })); } }
public async Task <ActionResult <ApiRecipeRating> > PostRecipeRating(ApiRecipeRating apiRecipeRating) { //convert api model to regular model var recipeRating = RecipeRating.FromApiToRecipeRating(apiRecipeRating); await _context.RecipeRating.AddAsync(recipeRating); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (RecipeRatingExists(recipeRating)) { return(Conflict()); } else { throw; } } return(CreatedAtAction("GetRecipeRating", new { id = apiRecipeRating.UserId }, apiRecipeRating)); }
public static void Add(RecipeRating item) { item.Id = Guid.NewGuid().ToString(); _repository[item.Id] = item; }
public IActionResult AddRecipieRating([FromBody] RecipeRating recipeRating) { var response = _RecipeService.AddRating(recipeRating).Result; return(new OkObjectResult(response)); }
public static void Update(RecipeRating item) { _repository[item.Id] = item; }
public async Task <RecipeRating> AddRating(RecipeRating rating) { return(await _recipeRatingRepository.AddAsync(rating)); }
public RecipeRatingViewModel(RecipeRating recipeRating) { This = recipeRating; }
public ProfileCreator AddRating(RecipeRating rating) { this.ratings.Add(rating); return(this); }
private bool RecipeRatingExists(RecipeRating r) { return(_context.RecipeRating.Any(e => e.UserId == r.UserId && e.RecipeId == r.RecipeId)); }