public IActionResult PartiallyUpdateLike(string id, [FromBody] JsonPatchDocument <CommentLike> jsonPatch)
        {
            CommentLike toPatch = likeRepo.GetSingle(likeRepo.guidColumnName, id);

            jsonPatch.ApplyTo(toPatch, ModelState);
            TryValidateModel(ModelState);
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var model = new {
                patchedUser     = toPatch,
                patchOperations = jsonPatch
            };

            if (jsonPatch.Operations.Count > 1)
            {
                if (!likeRepo.Update(toPatch, this.likeRepo.guidColumnName, id))
                {
                    return(new StatusCodeResult(500));
                }
            }
            else
            {
                string column = jsonPatch.Operations.Single().path.Substring(1);
                string value  = jsonPatch.Operations.Single().value.ToString();
                if (!likeRepo.Update(column, value, this.likeRepo.guidColumnName, id))
                {
                    return(new StatusCodeResult(500));
                }
            }

            /*
             * How to send patch from body
             * [
             * {
             * "op": "replace",
             * "path": "/<property name>",
             * "value": "<property value>"
             * },
             * {
             * "op": "replace",
             * "path": "/<property name>",
             * "value": "<property value>"
             * },
             * ]
             */
            return(Ok(model));
        }
        public JsonResult Like(long id)
        {
            var result = ORRepository.Get(id);

            if (result == null)
            {
                return(Json(new { Success = false, Error = "Не удалось найти результат" }));
            }

            // Получить текущего юзера
            var curUser = GetCurrentUser();

            var like = LikeRepository.IsFavoriteResult(curUser.Id, id);

            if (like != null)
            {
                LikeRepository.Delete(like);
                return(Json(new { Success = true, Name = "like" }));
            }

            // Создать лайк
            like = LikeRepository.Create();

            // Заполнить свойства
            like.UserId   = curUser.Id;
            like.ResultId = result.Id;

            // Сохранить
            LikeRepository.Update(like);

            // Вернуться к списку операций
            return(Json(new { Success = true, Name = "dislike" }));
        }
Esempio n. 3
0
        public async Task <object> SetLikeToComment(string id)
        {
            var          userId            = (await GetCurrentUser()).Id;
            CommentModel comment           = _comentRepository.Get(new Guid(id));
            LikeModel    likeToThisComment = comment.Likes
                                             .FirstOrDefault(l => l.UserId == new Guid(userId));

            if (likeToThisComment == null)
            {
                LikeModel like = new LikeModel()
                {
                    CommentId = new Guid(id),
                    UserId    = new Guid(userId)
                };
                comment.Likes.Add(like);
                _likeRepository.Update(like);
                return(new { Success = true, Id = id, Likes = comment.Likes.Count() });
            }
            return(new { Success = false, Id = id });
        }
        public JsonResult Like(long id)
        {
            var result = ORRepository.Get(id);

            if (result == null)
            {
                return(Json(new { Success = false, Error = "Не удалось найти результат" }));
            }

            var curUser = GetCurrentUser();

            //var like = LikeRepository.GetLikes(curUser.Id)
            //    .FirstOrDefault(it => it.UserId == curUser.Id && it.ResultId == id);

            var like = LikeRepository.GetAll().FirstOrDefault(it => it.User.Id == curUser.Id && it.Result.Id == id);

            if (like != null)
            {
                //Dislike
                LikeRepository.Delete(like);
                return(Json(new { Success = true, Name = "Like" }));
            }

            // Создать лайк
            like = LikeRepository.Create();

            // Заполнить свойства
            like.User   = curUser;
            like.Result = result;

            // Сохранить
            LikeRepository.Update(like);

            // Вернуться к списку операций
            return(Json(new { Success = true, Name = "Dislike" }));
        }