예제 #1
0
        public async Task <IActionResult> RateArtist(ArtistRateDto artistRateDto)
        {
            if (artistRateDto.UserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var rate = await appRepository.GetArtistRate(artistRateDto.UserId, artistRateDto.ArtistId);

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

            if (await appRepository.GetArtist(artistRateDto.ArtistId) == null)
            {
                appRepository.AddArtist(new Artist {
                    Id = artistRateDto.ArtistId
                });
            }

            rate = mapper.Map <ArtistRate>(artistRateDto);

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