yavsc/src/Yavsc.Blogs/Controllers/BlogApiController.cs

147 lines
3.6 KiB
C#
Raw Normal View History

2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2019-01-01 16:28:47 +00:00
using Yavsc.Models;
using Yavsc.Models.Blog;
using Yavsc.Server.Helpers;
2019-01-01 16:28:47 +00:00
namespace Yavsc.Controllers
{
[Produces("application/json")]
[Route("api/blog")]
2019-05-18 09:42:50 +01:00
2019-01-01 16:28:47 +00:00
public class BlogApiController : Controller
{
2020-10-09 19:35:39 +01:00
private readonly ApplicationDbContext _context;
2019-01-01 16:28:47 +00:00
public BlogApiController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/BlogApi
[HttpGet]
2025-02-23 20:23:23 +00:00
public IEnumerable<BlogPost> GetBlogspot(int start=0, int take=25)
2019-01-01 16:28:47 +00:00
{
2025-02-23 20:23:23 +00:00
return _context.BlogSpot.OrderByDescending(b => b.UserModified)
.Skip(start).Take(take);
2019-01-01 16:28:47 +00:00
}
// GET: api/BlogApi/5
[HttpGet("{id}", Name = "GetBlog")]
public IActionResult GetBlog([FromRoute] long id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
2025-02-08 20:06:24 +00:00
BlogPost blog = _context.BlogSpot.Single(m => m.Id == id);
2019-01-01 16:28:47 +00:00
if (blog == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
return Ok(blog);
}
// PUT: api/BlogApi/5
[HttpPut("{id}")]
public IActionResult PutBlog(long id, [FromBody] BlogPost blog)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
if (id != blog.Id)
{
2023-03-19 17:57:55 +00:00
return BadRequest();
2019-01-01 16:28:47 +00:00
}
_context.Entry(blog).State = EntityState.Modified;
try
{
_context.SaveChanges(User.GetUserId());
}
catch (DbUpdateConcurrencyException)
{
if (!BlogExists(id))
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
else
{
throw;
}
}
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status204NoContent);
2019-01-01 16:28:47 +00:00
}
// POST: api/BlogApi
[HttpPost]
public IActionResult PostBlog([FromBody] Models.Blog.BlogPost blog)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
2025-02-08 20:06:24 +00:00
_context.BlogSpot.Add(blog);
2019-01-01 16:28:47 +00:00
try
{
_context.SaveChanges(User.GetUserId());
}
catch (DbUpdateException)
{
if (BlogExists(blog.Id))
{
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status409Conflict);
2019-01-01 16:28:47 +00:00
}
else
{
throw;
}
}
return CreatedAtRoute("GetBlog", new { id = blog.Id }, blog);
}
// DELETE: api/BlogApi/5
[HttpDelete("{id}")]
public IActionResult DeleteBlog(long id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
2025-02-08 20:06:24 +00:00
BlogPost blog = _context.BlogSpot.Single(m => m.Id == id);
2019-01-01 16:28:47 +00:00
if (blog == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
2025-02-08 20:06:24 +00:00
_context.BlogSpot.Remove(blog);
2019-01-01 16:28:47 +00:00
_context.SaveChanges(User.GetUserId());
return Ok(blog);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool BlogExists(long id)
{
2025-02-08 20:06:24 +00:00
return _context.BlogSpot.Count(e => e.Id == id) > 0;
2019-01-01 16:28:47 +00:00
}
}
2019-05-18 09:42:50 +01:00
}