Files
yavsc/src/Api/Controllers/PostRateApiController.cs
Paul Schneider a9b809f5e5
Some checks failed
Dotnet build and test / log-the-inputs (push) Failing after 1s
Dotnet build and test / build (push) Failing after 1s
Refactoring
The main server owns the migrations, it's the server part,
it's simpler.

It's Yavsc, not one of its lib.
2025-07-15 20:17:39 +01:00

49 lines
1.3 KiB
C#

using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Yavsc.Helpers;
using Yavsc.Models;
using Yavsc.Server.Helpers;
namespace Yavsc.Controllers
{
[Produces("application/json")]
[Route("~/api/PostRateApi")]
public class PostRateApiController : Controller
{
private readonly ApplicationDbContext _context;
public PostRateApiController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/PostRateApi/5
[HttpPut("{id}"),Authorize]
public IActionResult PutPostRate([FromRoute] long id, [FromBody] int rate)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Models.Blog.BlogPost blogpost = _context.BlogSpot.Single(x=>x.Id == id);
if (blogpost == null)
{
return NotFound();
}
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (blogpost.AuthorId!=uid)
if (!User.IsInRole(Constants.AdminGroupName))
return BadRequest();
_context.SaveChanges(User.GetUserId());
return Ok();
}
}
}