Created
September 12, 2019 11:03
-
-
Save dileno/eabc1a53de43ae6a1b42ff8981a61410 to your computer and use it in GitHub Desktop.
BlogPostsController (updated with Data Repository)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Produces("application/json")] | |
[Route("api/[controller]")] | |
[ApiController] | |
public class BlogPostsController : ControllerBase | |
{ | |
private readonly BlogPostsContext _context; | |
private readonly IDataRepository<BlogPost> _repo; | |
public BlogPostsController(BlogPostsContext context, IDataRepository<BlogPost> repo) | |
{ | |
_context = context; | |
_repo = repo; | |
} | |
// GET: api/BlogPosts | |
[HttpGet] | |
public IEnumerable<BlogPost> GetBlogPosts() | |
{ | |
return _context.BlogPosts.OrderByDescending(p => p.PostId); | |
} | |
// GET: api/BlogPosts/5 | |
[HttpGet("{id}")] | |
public async Task<IActionResult> GetBlogPost([FromRoute] int id) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var blogPost = await _context.BlogPosts.FindAsync(id); | |
if (blogPost == null) | |
{ | |
return NotFound(); | |
} | |
return Ok(blogPost); | |
} | |
// PUT: api/BlogPosts/5 | |
[HttpPut("{id}")] | |
public async Task<IActionResult> PutBlogPost([FromRoute] int id, [FromBody] BlogPost blogPost) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
if (id != blogPost.PostId) | |
{ | |
return BadRequest(); | |
} | |
_context.Entry(blogPost).State = EntityState.Modified; | |
try | |
{ | |
_repo.Update(blogPost); | |
var save = await _repo.SaveAsync(blogPost); | |
} | |
catch (DbUpdateConcurrencyException) | |
{ | |
if (!BlogPostExists(id)) | |
{ | |
return NotFound(); | |
} | |
else | |
{ | |
throw; | |
} | |
} | |
return NoContent(); | |
} | |
// POST: api/BlogPosts | |
[HttpPost] | |
public async Task<IActionResult> PostBlogPost([FromBody] BlogPost blogPost) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
_repo.Add(blogPost); | |
var save = await _repo.SaveAsync(blogPost); | |
return CreatedAtAction("GetBlogPost", new { id = blogPost.PostId }, blogPost); | |
} | |
// DELETE: api/BlogPosts/5 | |
[HttpDelete("{id}")] | |
public async Task<IActionResult> DeleteBlogPost([FromRoute] int id) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var blogPost = await _context.BlogPosts.FindAsync(id); | |
if (blogPost == null) | |
{ | |
return NotFound(); | |
} | |
_repo.Delete(blogPost); | |
var save = await _repo.SaveAsync(blogPost); | |
return Ok(blogPost); | |
} | |
private bool BlogPostExists(int id) | |
{ | |
return _context.BlogPosts.Any(e => e.PostId == id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment