using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using MusicAPI.BaseModel; using System.Reflection; namespace MusicAPI.Controllers { [Route("api/[controller]")] [ApiController] public class SongController : Controller { [Route("GetSongs")] [HttpGet] public List GetSongs() { using (SongListContext DB = new SongListContext()) { return DB.Songs.Include("IdArtistNavigation").Include("IdGenreNavigation").ToList(); } } [Route("PostSong")] [HttpPost] public IActionResult PostSong([FromBody] Song song) { if (song == null) { return BadRequest("Песня пустая"); } song.IdSong = default(int); using (SongListContext DB = new SongListContext()) { DB.Songs.Add(song); try { DB.SaveChanges(); return Ok(); } catch (Exception ex) { return BadRequest($"{ex.Message}"); } } } [Route("PutSong")] [HttpPut] public IActionResult PutProduct([FromBody] Song song) { Song? UpdateSong; using (SongListContext DB = new SongListContext()) { UpdateSong = DB.Songs.Find(song.IdSong); if (UpdateSong is null) { return BadRequest("Песни нет"); } UpdateSong.NameSong = song.NameSong; UpdateSong.Duration = song.Duration; UpdateSong.IdArtistNavigation = song.IdArtistNavigation; UpdateSong.IdGenreNavigation = song.IdGenreNavigation; try { DB.SaveChanges(); return Ok(); } catch (Exception ex) { return BadRequest(ex.Message); } } } [Route("DeleteSong")] [HttpDelete] public IActionResult DeleteSong([FromQuery] int id) { using (SongListContext DB = new SongListContext()) { Song song = DB.Songs.Find(id); if (song is null) { return BadRequest("Такой нет"); } DB.Songs.Remove(song); try { DB.SaveChanges(); return Ok(); } catch (Exception) { return BadRequest("Что то пошло не так"); } } } } }