Controllers cleanup, removed unused endpoints and return type

This commit is contained in:
hak8or 2018-02-20 00:29:23 -05:00
parent d2b9a8b40c
commit 66a9c1edb4
2 changed files with 18 additions and 63 deletions

View File

@ -21,14 +21,18 @@ namespace YTManager.Controllers {
// GET api/Channels
[HttpGet]
public async Task<IEnumerable<Models.Channel>> Get() {
return await db.Channels.OrderByDescending(c => c.AddedtoDB).ToListAsync();
public async Task<IActionResult> Get() {
return Ok(await db.Channels.OrderByDescending(c => c.AddedtoDB).ToListAsync());
}
// GET api/Channels/5
[HttpGet("{YTchannelID}")]
public Task<Models.Channel> Get([FromQuery] string YTchannelID) {
return db.Channels.SingleOrDefaultAsync(c => c.YoutubeID == YTchannelID);
public async Task<IActionResult> Get([FromQuery] string 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)
@ -69,24 +73,6 @@ namespace YTManager.Controllers {
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
[HttpDelete("{YTChannelID}")]
public async Task<IActionResult> Delete([FromRoute] string YTChannelID) {
@ -94,14 +80,19 @@ namespace YTManager.Controllers {
if (!ModelState.IsValid) return BadRequest(ModelState);
// 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.
if (channel == null)
return NotFound();
// Remove.
// Remove all the channels videos.
channel.Videos.ForEach(v => db.Videos.Remove(v));
// Remove the channel itself.
db.Channels.Remove(channel);
// Save all the changes.
await db.SaveChangesAsync();
return Ok(channel);
}

View File

@ -20,8 +20,8 @@ namespace YTManager.Controllers {
// GET: api/Videos
[HttpGet]
public async Task<List<Video>> GetVideos() {
return await db.Videos.OrderByDescending(i => i.AddedtoDB).ToListAsync();
public async Task<IActionResult> GetVideos() {
return Ok(await db.Videos.OrderByDescending(i => i.AddedtoDB).ToListAsync());
}
// GET: api/Videos/5
@ -36,44 +36,8 @@ namespace YTManager.Controllers {
// If the video wasn't found then send back not foud.
if (video == null)
return NotFound();
else
return Ok(video);
}
// POST: api/Videos
[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();
// Otherwise send back the video.
return Ok(video);
}
}