public IHttpActionResult AddImageAlbumComment([FromUri] int id, [FromBody] CommentCreateBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest();
            }

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

            var imageAlbum = this.Data.ImageAlbums.Find(id);

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

            var user = this.Data.Users.Find(this.User.Identity.GetUserId());

            var newComment = new Comment
            {
                Content = model.Content,
                AuthorId = user.Id,
                PostedOn = DateTime.Now,
                ImageAlbumId = id
            };

            this.Data.Comments.Add(newComment);
            this.Data.SaveChanges();

            var commentToReturn = Mapper.Map<CommentViewModel>(newComment);
            commentToReturn.IsMine = true;

            return this.Ok(commentToReturn);
        }
예제 #2
0
        public IHttpActionResult AddSongComment([FromUri] int id, [FromBody] CommentCreateBindingModel comment)
        {
            if (comment == null)
            {
                return this.BadRequest("Cannot create an empty comment model.");
            }

            var song = this.Data.Songs.Find(id);

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

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

            var user = this.Data.Users.Find(this.User.Identity.GetUserId());

            var newSongComment = new Comment
                                     {
                                         Content = comment.Content,
                                         AuthorId = user.Id,
                                         PostedOn = DateTime.Now,
                                         SongId = id
                                     };

            this.Data.Comments.Add(newSongComment);
            this.Data.SaveChanges();

            var commentToReturn = Mapper.Map<CommentViewModel>(newSongComment);

            return this.Ok(commentToReturn);
        }