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

187 lines
5.5 KiB
C#
Raw Normal View History

using System.Linq;
using System.Security.Claims;
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.Relationship;
using Yavsc.Server.Helpers;
2019-01-01 16:28:47 +00:00
namespace Yavsc.Blogs.Controllers
2019-01-01 16:28:47 +00:00
{
[Produces("application/json")]
[Route("api/circle")]
2019-01-01 16:28:47 +00:00
public class CircleApiController : Controller
{
2020-10-09 19:35:39 +01:00
private readonly ApplicationDbContext _context;
2019-01-01 16:28:47 +00:00
public CircleApiController(ApplicationDbContext context)
{
_context = context;
}
/// <summary>
/// Returns the caller's own circles. Circles are personal —
/// the API never exposes another user's circles, even by id.
/// </summary>
// GET: api/circle
2019-01-01 16:28:47 +00:00
[HttpGet]
public IEnumerable<Circle> GetCircle()
{
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
return _context.Circle.Where(c => c.OwnerId == uid);
2019-01-01 16:28:47 +00:00
}
/// <summary>
/// Returns a single circle only when it belongs to the caller.
/// </summary>
// GET: api/circle/5
2019-01-01 16:28:47 +00:00
[HttpGet("{id}", Name = "GetCircle")]
public async Task<IActionResult> GetCircle([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
}
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
Circle circle = await _context.Circle.SingleOrDefaultAsync(
m => m.Id == id && m.OwnerId == uid);
2019-01-01 16:28:47 +00:00
if (circle == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
return Ok(circle);
}
/// <summary>
/// Replaces a circle. The caller must own it; the server
/// reasserts ownership regardless of any OwnerId the client
/// tries to put in the body.
/// </summary>
// PUT: api/circle/5
2019-01-01 16:28:47 +00:00
[HttpPut("{id}")]
public async Task<IActionResult> PutCircle([FromRoute] long id, [FromBody] Circle circle)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
if (id != circle.Id)
{
2023-03-19 17:57:55 +00:00
return BadRequest();
2019-01-01 16:28:47 +00:00
}
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
var existing = await _context.Circle.SingleOrDefaultAsync(
c => c.Id == id && c.OwnerId == uid);
if (existing is null)
{
return new ChallengeResult();
}
// Force OwnerId to the caller; the body value is ignored.
circle.OwnerId = uid;
2019-01-01 16:28:47 +00:00
_context.Entry(circle).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync(User.GetUserId());
}
catch (DbUpdateConcurrencyException)
{
if (!CircleExists(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
}
/// <summary>
/// Creates a circle owned by the caller. The server overwrites
/// any OwnerId the client sends in the body.
/// </summary>
// POST: api/circle
2019-01-01 16:28:47 +00:00
[HttpPost]
public async Task<IActionResult> PostCircle([FromBody] Circle circle)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
circle.OwnerId = uid;
2019-01-01 16:28:47 +00:00
_context.Circle.Add(circle);
try
{
await _context.SaveChangesAsync(User.GetUserId());
}
catch (DbUpdateException)
{
if (CircleExists(circle.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("GetCircle", new { id = circle.Id }, circle);
}
/// <summary>
/// Deletes a circle only if the caller owns it. Returns 404
/// (not 403) when the circle does not exist or is not owned
/// by the caller, to avoid leaking the existence of someone
/// else's circle.
/// </summary>
// DELETE: api/circle/5
2019-01-01 16:28:47 +00:00
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteCircle([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
}
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
Circle circle = await _context.Circle.SingleOrDefaultAsync(
m => m.Id == id && m.OwnerId == uid);
2019-01-01 16:28:47 +00:00
if (circle == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
_context.Circle.Remove(circle);
await _context.SaveChangesAsync(User.GetUserId());
return Ok(circle);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool CircleExists(long id)
{
return _context.Circle.Count(e => e.Id == id) > 0;
}
}
2020-10-09 19:35:39 +01:00
}