public async Task <ActionResult <MusicDTO> > UpdateMusic(int id, [FromBody] SaveMusicDTO saveMusicResource)
        {
            var validator        = new SaveMusicResourceValidator();
            var validationResult = await validator.ValidateAsync(saveMusicResource);

            var requestIsInvalid = id == 0 || !validationResult.IsValid;

            if (requestIsInvalid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var musicToBeUpdate = await _musicService.GetMusicWithArtistById(id);

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

            var music = _mapper.Map <SaveMusicDTO, Music>(saveMusicResource);

            await _musicService.UpdateMusic(musicToBeUpdate, music);

            var updatedMusic = await _musicService.GetMusicWithArtistById(id);

            var updatedMusicResource = _mapper.Map <Music, MusicDTO>(updatedMusic);

            return(Ok(updatedMusicResource));
        }
Esempio n. 2
0
        public async Task <MusicDTO> CreateMusic(SaveMusicDTO saveMusicDTO)
        {
            var musicToCreate = _mapper.Map <SaveMusicDTO, Music>(saveMusicDTO);

            await uow.Musics.AddAsync(musicToCreate);

            await uow.CommitAsync();

            var music = await GetMusicById(musicToCreate.Id);

            return(music);
        }
Esempio n. 3
0
        public async Task <MusicDTO> UpdateMusic(MusicDTO musicToBeUpdated, SaveMusicDTO music)
        {
            var newmusic      = _mapper.Map <SaveMusicDTO, Music>(music);
            var newmusicBacis = _mapper.Map <MusicDTO, Music>(musicToBeUpdated);

            newmusicBacis.Name     = newmusic.Name;
            newmusicBacis.ArtistId = newmusic.ArtistId;
            await uow.CommitAsync();

            var updatedMusic = await GetMusicById(newmusic.Id);

            return(updatedMusic);
        }
Esempio n. 4
0
        public async Task <ActionResult <MusicDTO> > CreateMusic([FromBody] SaveMusicDTO saveMusicDTO)
        {
            var validator       = new SaveMusicDTOValidator();
            var validatorResult = await validator.ValidateAsync(saveMusicDTO);

            if (!validatorResult.IsValid)
            {
                return(BadRequest(validatorResult.Errors));
            }

            var musicDTO = await _musicService.CreateMusic(saveMusicDTO);

            return(Ok(musicDTO));
        }
        public async Task <ActionResult <MusicDTO> > CreateMusic([FromBody] SaveMusicDTO saveMusicResource)
        {
            var validator        = new SaveMusicResourceValidator();
            var validationResult = await validator.ValidateAsync(saveMusicResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var musicToCreate = _mapper.Map <SaveMusicDTO, Music>(saveMusicResource);
            var newMusic      = await _musicService.CreateMusic(musicToCreate);

            var music = await _musicService.GetMusicWithArtistById(newMusic.Id);

            var musicResource = _mapper.Map <Music, MusicDTO>(music);

            return(Ok(musicResource));
        }
Esempio n. 6
0
        public async Task <ActionResult <MusicDTO> > UpdateMusic(int id, [FromBody] SaveMusicDTO saveMusicDTO)
        {
            var validator       = new SaveMusicDTOValidator();
            var validatorResult = await validator.ValidateAsync(saveMusicDTO);

            var requestIsInvalid = id == 0 || !validatorResult.IsValid;

            if (requestIsInvalid)
            {
                return(BadRequest(validatorResult.Errors));
            }

            var musicToBeUpdate = await _musicService.GetMusicById(id);

            if (musicToBeUpdate == null)
            {
                return(NotFound());
            }
            var updatedMusicDTO = await _musicService.UpdateMusic(musicToBeUpdate, saveMusicDTO);

            return(Ok(updatedMusicDTO));
        }