public IActionResult GetSong(string songIdOrName, bool includeSingers = false) { //SongDto songToReturn; Song songEntity; if (IsInt(songIdOrName)) { //songToReturn = SingersDataStore.Current.Singers.FirstOrDefault(singer => singer.Id == Convert.ToInt32(singerIdOrName)); songEntity = _musesRepository.GetSongAsync(Convert.ToInt32(songIdOrName), includeSingers).Result; } else { //songToReturn = SingersDataStore.Current.Singers.FirstOrDefault(singer => singer.Name.ToLower().Replace(" ", "") == singerIdOrName.Replace("-", " ").ToLower().Replace(" ", "")); songEntity = _musesRepository.GetSongAsync(songIdOrName, includeSingers).Result; } if (songEntity == null) { return(NotFound()); } if (includeSingers) { var songResult = _mapper.Map <SongDto>(songEntity); return(Ok(songResult)); } var songWithoutSingersResult = _mapper.Map <SongWithoutSingersDto>(songEntity); return(Ok(songWithoutSingersResult)); }
public IActionResult PutSongOfSinger(string singerIdOrName, string songIdOrName, [FromBody] SongDtoForEdit songOfSinger) { if (songOfSinger == null) { return(BadRequest()); } if (songOfSinger.Description == songOfSinger.Name) { ModelState.AddModelError("Description", "The provided description should be diffrent from the name"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //SingerDto singerToReturn; //if (IsInt(singerIdOrName)) //{ // singerToReturn = SingersDataStore.Current.Singers.FirstOrDefault(singer => singer.Id == Convert.ToInt32(singerIdOrName)); //} //else //{ // singerToReturn = SingersDataStore.Current.Singers.FirstOrDefault(singer => singer.Name.ToLower().Replace(" ", "") == singerIdOrName.Replace("-", " ").ToLower().Replace(" ", "")); //} //if (singerToReturn == null) //{ // return NotFound(); //} bool flagSingerExists; if (IsInt(singerIdOrName)) { flagSingerExists = _musesRepository.SingerExistsAsync(Convert.ToInt32(singerIdOrName)).Result; } else { flagSingerExists = _musesRepository.SingerExistsAsync(singerIdOrName).Result; } if (!flagSingerExists) { return(NotFound()); } else { if (!_musesRepository.SongExistsAsync(songOfSinger.Name).Result) { return(NotFound("This song doesn't exist")); } //SongDto songToReturn; //if (IsInt(songIdOrName)) //{ // songToReturn = singerToReturn.Songs.FirstOrDefault(song => song.Id == Convert.ToInt32(songIdOrName)); //} //else //{ // songToReturn = singerToReturn.Songs.FirstOrDefault(song => song.Name.ToLower().Replace(" ", "") == songIdOrName.Replace("-", " ").ToLower().Replace(" ", "")); //} //if(songToReturn == null) { // return NotFound(); //} Song songEntity; if (IsInt(songIdOrName)) { songEntity = _musesRepository.GetSongAsync(Convert.ToInt32(songIdOrName), false).Result; } else { songEntity = _musesRepository.GetSongAsync(songIdOrName, false).Result; } _mapper.Map(songOfSinger, songEntity); //songToReturn.Name = songOfSinger.Name; //songToReturn.TranslatedName = songOfSinger.TranslatedName; //songToReturn.Author = songOfSinger.Author; //songToReturn.Composer = songOfSinger.Composer; //songToReturn.Language = songOfSinger.Language; //songToReturn.Description = songOfSinger.Description; if (!_musesRepository.SaveAsync().Result) { return(StatusCode(500, "A problem happened while handling your request.")); } return(NoContent()); } }