public async Task <IActionResult> CreateComment(int userId, string albumId, CommentForCreationDto commentForCreationDto)
        {
            var commenter = await this.repo.GetUser(userId);

            if (commenter.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            commentForCreationDto.AlbumId     = albumId;
            commentForCreationDto.CommenterId = userId;

            var album = await this.repo.GetAlbum(commentForCreationDto.AlbumId);

            if (album == null)
            {
                repo.AddAlbum(new Album {
                    Id = commentForCreationDto.AlbumId
                });
            }

            var comment = this.mapper.Map <Comment>(commentForCreationDto);

            this.repo.Add(comment);

            if (!await this.repo.SaveAll())
            {
                throw new Exception("Creating the comment failed on save");
            }
            var commentToReturn = this.mapper.Map <CommentToReturnDto>(comment);

            return(CreatedAtRoute("GetComment", new { userId, albumId, id = comment.Id }, commentToReturn));
        }
Пример #2
0
        public async Task <IActionResult> RateAlbum(AlbumRateDto albumRateDto)
        {
            if (albumRateDto.UserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var rate = await appRepository.GetAlbumRate(albumRateDto.UserId, albumRateDto.AlbumId);

            if (rate != null)
            {
                rate.Rate      = albumRateDto.Rate;
                rate.RatedDate = albumRateDto.RatedDate;
                if (await appRepository.SaveAll())
                {
                    return(Ok());
                }
                return(BadRequest("Failed to rate album"));
            }

            if (await appRepository.GetAlbum(albumRateDto.AlbumId) == null)
            {
                appRepository.AddAlbum(new Album {
                    Id = albumRateDto.AlbumId
                });
            }

            rate = mapper.Map <AlbumRate>(albumRateDto);

            appRepository.Add(rate);
            if (await appRepository.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Failed to rate album"));
        }