Ejemplo n.º 1
0
 public static CommentViewModel CreateSingle(Comment commentDbModel)
 {
     return new CommentViewModel
     {
         Id = commentDbModel.Id,
         Content = commentDbModel.Content,
         CreatedOn = commentDbModel.CreatedOn,
         Author = new AuthorViewModel
         {
             Username = commentDbModel.Author.UserName,
             ProfilePhotoData =
                 commentDbModel.Author.ProfilePhoto != null ?
                 commentDbModel.Author.ProfilePhoto.Base64Data : null
         }
     };
 }
Ejemplo n.º 2
0
        public IHttpActionResult AddCommentToPigeon(int pigeonId, CommentBindingModel inputComment)
        {
            if (inputComment == null)
            {
                return this.BadRequest(CommentPostModelInvalidMessage);
            }

            var loggedUserId = this.User.Identity.GetUserId();
            var loggedUser = this.Data.Users.GetById(loggedUserId);

            var pigeon = this.Data.Pigeons.GetById(pigeonId);

            if (pigeon == null)
            {
                return this.NotFound();
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var commentToAdd = new Comment
            {
                Content = inputComment.Content,
                AuthorId = loggedUserId,
                Author = loggedUser,
                PigeonId = pigeon.Id,
                Pigeon = pigeon,
                CreatedOn = DateTime.Now
            };

            this.Data.Comments.Add(commentToAdd);

            pigeon.Comments.Add(commentToAdd);
            pigeon.CommentsCount++;

            this.Data.Pigeons.Update(pigeon);
            this.Data.SaveChanges();

            var commentViewModel = CommentViewModel.CreateSingle(commentToAdd);

            return this.Ok(commentViewModel);
        }