public IActionResult DeleteSong(string songIdOrName) { bool flagSongExists; if (IsInt(songIdOrName)) { flagSongExists = _musesRepository.SongExistsAsync(Convert.ToInt32(songIdOrName)).Result; } else { flagSongExists = _musesRepository.SongExistsAsync(songIdOrName).Result; } if (!flagSongExists) { return(NotFound()); } Song songEntity; if (IsInt(songIdOrName)) { songEntity = _musesRepository.GetSongAsync(Convert.ToInt32(songIdOrName), false).Result; } else { songEntity = _musesRepository.GetSongAsync(songIdOrName, false).Result; } _musesRepository.DeleteSongAsync(songEntity); if (!_musesRepository.SaveAsync().Result) { return(StatusCode(500, "A problem happened while handling your request.")); } return(NoContent()); }
public async Task <IActionResult> PostSongOfSinger(string singerIdOrName, [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(" ", "")); //} bool flagSingerExists; if (IsInt(singerIdOrName)) { //flagSingerExists = _musesRepository.SingerExistsAsync(Convert.ToInt32(singerIdOrName)).Result; flagSingerExists = await _musesRepository.SingerExistsAsync(Convert.ToInt32(singerIdOrName)); } else { flagSingerExists = await _musesRepository.SingerExistsAsync(singerIdOrName); } if (!flagSingerExists) { return(NotFound()); } else { if (await _musesRepository.SongExistsAsync(songOfSinger.Name)) { _logger.LogWarning("This song already exists: " + songOfSinger.Name); return(BadRequest("This song already exists: " + songOfSinger.Name)); } //var maxSongOfSingerId = SingersDataStore.Current.Singers.SelectMany(s => s.Songs).Max(s => s.Id); var finalSongOfSinger = _mapper.Map <Entities.Song>(songOfSinger); //singerToReturn.Songs.Add(finalSongOfSinger); if (IsInt(singerIdOrName)) { await _musesRepository.AddSongForSingerAsync(Convert.ToInt32(singerIdOrName), finalSongOfSinger); } else { await _musesRepository.AddSongForSingerAsync(singerIdOrName, finalSongOfSinger); } if (!await _musesRepository.SaveAsync()) { return(StatusCode(500, "A problem happened while handling your request.")); } var createdSongofSingerToReturn = _mapper.Map <Models.SongWithoutSingersDto>(finalSongOfSinger); return(CreatedAtRoute("GetSongOfSinger", new { singerIdOrName = singerIdOrName, songIdOrName = createdSongofSingerToReturn.Name }, createdSongofSingerToReturn)); } }