Пример #1
0
        public HttpResponseMessage PostVoteForPicture(VoteModel vote, int pictureId, string sessionKey)
        {
            try
            {
                var userService = new UserService();
                var user        = userService.GetUserBySessionKey(sessionKey);

                Validator.ValidateUser(user, "Cannot vote for picture");

                var pictureService = new PictureService();
                var picture        = pictureService.GetPictureById(pictureId);

                Validator.ValidatePicture(picture, PICTURE_NOT_FOUND);

                if (picture.Album.User.Id != user.Id)
                {
                    throw new Exception(USER_ACCESS_DENIED);
                }

                var isVoted = picture.Votes.Count(v => v.User.Id == user.Id) > 0;
                if (isVoted)
                {
                    throw new Exception("Already voted..");
                }

                var newVote = pictureService.AddVoteToPicture(vote, picture, user);

                var pictureToReturn = new PictureModel()
                {
                    Comments = from comment in picture.Comments
                               select new CommentModel()
                    {
                        Text      = comment.Text,
                        UserName  = comment.User.UserName,
                        CreatedAt = comment.CreatedAt
                    },
                    CreateDate    = picture.CreateDate,
                    Id            = picture.Id,
                    Url           = picture.Url,
                    Title         = picture.Title,
                    PositiveVotes = picture.Votes.Count(v => v.isPositive == true),
                    NegativeVotes = picture.Votes.Count(v => v.isPositive == false)
                };

                return(this.Request.CreateResponse(HttpStatusCode.OK, pictureToReturn));
            }
            catch (Exception ex)
            {
                return(this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message));
            }
        }