123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- 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<Song> 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("Что то пошло не так");
- }
- }
- }
- }
- }
|