//[ResponseType(typeof(PromotionItem))] public async Task <IHttpActionResult> GetLike(long id) { try { PromotionItem item = _ctx.PromotionItems.Find(id); if (item == null) { throw new BusinessLogicException("не удается найти акцию"); } if (item.GetUserLike(UserId)?.Value == LikeType.Like) { return(Ok(new { item.LikeCount, item.DislikeCount })); } ; if (item.GetUserLike(UserId)?.Value == LikeType.Dislike) { Like like = item.GetUserLike(UserId); like.Value = LikeType.Like; item.DislikeCount = item.DislikeCount - 1; item.LikeCount = item.LikeCount + 1; } if (item.GetUserLike(UserId) == null) { _ctx.Likes.Add(new Like(UserId, item.Id, LikeType.Like)); item.LikeCount = item.LikeCount + 1; } /* * The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Like_dbo.User_UserId". The conflict occurred in database "tukiflydatabase", table "dbo.User", column 'Id'. * The statement has been terminated. */ if (_ctx.ChangeTracker.HasChanges()) { await _ctx.SaveChangesAsync(); } return(Ok(new { item.LikeCount, item.DislikeCount }));; } catch (Exception ex) { Log.Error(ex.Message); throw; } }
//[ResponseType(typeof(PromotionItem))] public async Task <IHttpActionResult> GetDislike(long id) { try { PromotionItem item = _ctx.PromotionItems.Find(id); if (item == null) { throw new BusinessLogicException("не удается найти акцию"); } if (item.GetUserLike(UserId)?.Value == LikeType.Dislike) { return(Ok(new { item.LikeCount, item.DislikeCount })); } if (item.GetUserLike(UserId)?.Value == LikeType.Like) { Like like = item.GetUserLike(UserId); like.Value = LikeType.Dislike; item.LikeCount = item.LikeCount - 1; item.DislikeCount = item.DislikeCount + 1; } if (item.GetUserLike(UserId) == null) { _ctx.Likes.Add(new Like(UserId, item.Id, LikeType.Dislike)); item.DislikeCount = item.DislikeCount + 1; } if (_ctx.ChangeTracker.HasChanges()) { await _ctx.SaveChangesAsync(); } return(Ok(new { item.LikeCount, item.DislikeCount })); } catch (Exception ex) { Log.Error(ex.Message); throw; } }