Exemplo n.º 1
0
        public async Task <IActionResult> UpdateArtist([FromRoute] int artistId, [FromBody] ArtistUpdateRequest putRequest)
        {
            try
            {
                if (artistId != putRequest.ArtistId)
                {
                    return(BadRequest(new ErrorResponse(ErrorMessages.Artist.MismatchId)));
                }

                var exists = await _artistService.ArtistIdExistsAsync(artistId);

                if (!exists)
                {
                    return(NotFound(new ErrorResponse(ErrorMessages.Artist.DoesNotExist)));
                }

                var artistModel = _mapper.Map <ArtistModel>(putRequest);
                exists = await _artistService.ArtistNameExistsAsync(artistModel.Name);

                if (exists)
                {
                    return(BadRequest(new ErrorResponse(ErrorMessages.Artist.NameExists)));
                }

                var updated = await _artistService.UpdateArtistAsync(artistModel);

                if (updated != null)
                {
                    return(Ok(new Response <ArtistResponse>(_mapper.Map <ArtistResponse>(updated))));
                }

                return(NotFound());
            }
            catch (Exception ex)
            {
                if (ex.GetType() == typeof(DbUpdateConcurrencyException))
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse(ErrorMessages.Artist.ConcurrencyIssue)));
                }

                return(StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse(ErrorMessages.Artist.FailedUpdate)));
            }
        }