コード例 #1
0
        public async Task <ActionResult <LibraryDTO> > UpdateArtist(int id, [FromBody] SaveLibraryDTO saveArtistResource)
        {
            var validator        = new SaveLibraryResourceValidator();
            var validationResult = await validator.ValidateAsync(saveArtistResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
            }
            var artistToBeUpdated = await _artistService.GetArtistById(id);

            if (artistToBeUpdated == null)
            {
                return(NotFound());
            }

            var artist = _mapper.Map <SaveLibraryDTO, Artist>(saveArtistResource);

            await _artistService.UpdateArtist(artistToBeUpdated, artist);

            var updatedArtist = await _artistService.GetArtistById(id);

            var updatedArtistResource = _mapper.Map <Artist, LibraryDTO>(updatedArtist);

            return(Ok(updatedArtistResource));
        }
コード例 #2
0
        public async Task <ActionResult <LibraryDTO> > CreateArtist([FromBody] SaveLibraryDTO saveArtistResource)
        {
            var validator        = new SaveLibraryResourceValidator();
            var validationResult = await validator.ValidateAsync(saveArtistResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
            }
            var artistToCreate = _mapper.Map <SaveLibraryDTO, Artist>(saveArtistResource);

            var newArtist = await _artistService.CreateArtist(artistToCreate);

            var artist = await _artistService.GetArtistById(newArtist.Id);

            var artistResource = _mapper.Map <Artist, LibraryDTO>(artist);

            return(Ok(artistResource));
        }