Controllers cleanup, removed unused endpoints and return type
This commit is contained in:
parent
d2b9a8b40c
commit
66a9c1edb4
@ -21,14 +21,18 @@ namespace YTManager.Controllers {
|
|||||||
|
|
||||||
// GET api/Channels
|
// GET api/Channels
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IEnumerable<Models.Channel>> Get() {
|
public async Task<IActionResult> Get() {
|
||||||
return await db.Channels.OrderByDescending(c => c.AddedtoDB).ToListAsync();
|
return Ok(await db.Channels.OrderByDescending(c => c.AddedtoDB).ToListAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET api/Channels/5
|
// GET api/Channels/5
|
||||||
[HttpGet("{YTchannelID}")]
|
[HttpGet("{YTchannelID}")]
|
||||||
public Task<Models.Channel> Get([FromQuery] string YTchannelID) {
|
public async Task<IActionResult> Get([FromQuery] string YTchannelID) {
|
||||||
return db.Channels.SingleOrDefaultAsync(c => c.YoutubeID == YTchannelID);
|
var channel = db.Channels.SingleOrDefaultAsync(c => c.YoutubeID == YTchannelID);
|
||||||
|
if (channel == null)
|
||||||
|
return NotFound();
|
||||||
|
else
|
||||||
|
return Ok(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: api/Channels/sdfs6DFS65f/Videos (using YouTube channel ID)
|
// GET: api/Channels/sdfs6DFS65f/Videos (using YouTube channel ID)
|
||||||
@ -69,24 +73,6 @@ namespace YTManager.Controllers {
|
|||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST api/Channels
|
|
||||||
[HttpPost]
|
|
||||||
public async Task<IActionResult> Post([FromBody] Channel channel) {
|
|
||||||
// Check if we were able to parse.
|
|
||||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
||||||
|
|
||||||
// Verify the channel doesn't already exist.
|
|
||||||
if (db.Channels.Any(c => c.YoutubeID == channel.YoutubeID))
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
// Seems good, so add it.
|
|
||||||
db.Channels.Add(channel);
|
|
||||||
await db.SaveChangesAsync();
|
|
||||||
|
|
||||||
// And all is well.
|
|
||||||
return Ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
// DELETE api/Channels/5
|
// DELETE api/Channels/5
|
||||||
[HttpDelete("{YTChannelID}")]
|
[HttpDelete("{YTChannelID}")]
|
||||||
public async Task<IActionResult> Delete([FromRoute] string YTChannelID) {
|
public async Task<IActionResult> Delete([FromRoute] string YTChannelID) {
|
||||||
@ -94,14 +80,19 @@ namespace YTManager.Controllers {
|
|||||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||||
|
|
||||||
// Attempt to find the channel.
|
// Attempt to find the channel.
|
||||||
var channel = await db.Channels.SingleOrDefaultAsync(c => c.YoutubeID == YTChannelID);
|
var channel = await db.Channels.Include(c => c.Videos).SingleOrDefaultAsync(c => c.YoutubeID == YTChannelID);
|
||||||
|
|
||||||
// Check if such an entry exists already.
|
// Check if such an entry exists already.
|
||||||
if (channel == null)
|
if (channel == null)
|
||||||
return NotFound();
|
return NotFound();
|
||||||
|
|
||||||
// Remove.
|
// Remove all the channels videos.
|
||||||
|
channel.Videos.ForEach(v => db.Videos.Remove(v));
|
||||||
|
|
||||||
|
// Remove the channel itself.
|
||||||
db.Channels.Remove(channel);
|
db.Channels.Remove(channel);
|
||||||
|
|
||||||
|
// Save all the changes.
|
||||||
await db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
return Ok(channel);
|
return Ok(channel);
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,8 @@ namespace YTManager.Controllers {
|
|||||||
|
|
||||||
// GET: api/Videos
|
// GET: api/Videos
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<List<Video>> GetVideos() {
|
public async Task<IActionResult> GetVideos() {
|
||||||
return await db.Videos.OrderByDescending(i => i.AddedtoDB).ToListAsync();
|
return Ok(await db.Videos.OrderByDescending(i => i.AddedtoDB).ToListAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: api/Videos/5
|
// GET: api/Videos/5
|
||||||
@ -36,44 +36,8 @@ namespace YTManager.Controllers {
|
|||||||
// If the video wasn't found then send back not foud.
|
// If the video wasn't found then send back not foud.
|
||||||
if (video == null)
|
if (video == null)
|
||||||
return NotFound();
|
return NotFound();
|
||||||
else
|
|
||||||
return Ok(video);
|
|
||||||
}
|
|
||||||
|
|
||||||
// POST: api/Videos
|
// Otherwise send back the video.
|
||||||
[HttpPost]
|
|
||||||
public async Task<IActionResult> PostVideo([FromBody] Video video){
|
|
||||||
// Check if we were able to parse.
|
|
||||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
||||||
|
|
||||||
// Check if such a database exists already.
|
|
||||||
if (await db.Videos.AnyAsync(d => d.YoutubeID == video.YoutubeID))
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
// Add our video to the database and tell db to update.
|
|
||||||
db.Videos.Add(video);
|
|
||||||
await db.SaveChangesAsync();
|
|
||||||
|
|
||||||
// Say that the creation was succesfull.
|
|
||||||
return Ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
// DELETE: api/Videos/alfkeo4f5
|
|
||||||
[HttpDelete("{ytid}")]
|
|
||||||
public async Task<IActionResult> DeleteVideo([FromRoute] string YTVideoID){
|
|
||||||
// Check if we were able to parse.
|
|
||||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
||||||
|
|
||||||
// Attempt to find the video.
|
|
||||||
var video = await db.Videos.SingleOrDefaultAsync(m => m.YoutubeID == YTVideoID);
|
|
||||||
|
|
||||||
// Check if such a database exists already.
|
|
||||||
if (video == null)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
// Remove.
|
|
||||||
db.Videos.Remove(video);
|
|
||||||
await db.SaveChangesAsync();
|
|
||||||
return Ok(video);
|
return Ok(video);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user