public async Task <ActionResult <MusicResource> > CreateMusic(MusicToSaveResource musicToSave) { try { // Get current User var userId = User.Identity.Name; // Validation of input Data var validation = new MusicToSaveResourceValidator(); var validationResult = await validation.ValidateAsync(musicToSave); if (!validationResult.IsValid) { return(BadRequest(validationResult.Errors)); } // Mapping var music = _mapperService.Map <MusicToSaveResource, Music>(musicToSave); // Create music var musicCreated = await _musicService.CreateMusic(music); // Mapping var musicResult = _mapperService.Map <Music, MusicResource>(musicCreated); return(Ok(musicResult)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <ActionResult> UpdateMusic(int id, MusicToSaveResource musicToUpdate) { try { // Validation of input Data var validation = new MusicToSaveResourceValidator(); var validationResult = await validation.ValidateAsync(musicToUpdate); if (!validationResult.IsValid) { return(BadRequest(validationResult.Errors)); } // Verify existing music var musicExist = await _musicService.GetMusicById(id); if (musicExist == null) { return(NotFound()); } // Mapping var musicUpdate = _mapperService.Map <MusicToSaveResource, Music>(musicToUpdate); // Update music await _musicService.UpdateMusic(musicExist, musicUpdate); return(Ok("The music is updated")); } catch (Exception ex) { return(BadRequest(ex.Message)); } }