yavsc/src/Yavsc/ApiControllers/PostRateApiController.cs

48 lines
1.3 KiB
C#
Raw Normal View History

2016-09-05 19:01:43 +02:00
using System.Linq;
using System.Security.Claims;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Yavsc.Helpers;
2016-09-05 19:01:43 +02:00
using Yavsc.Models;
namespace Yavsc.Controllers
{
[Produces("application/json")]
2016-09-23 12:28:07 +02:00
[Route("~/api/PostRateApi")]
2016-09-05 19:01:43 +02:00
public class PostRateApiController : Controller
{
2020-10-09 19:35:39 +01:00
private readonly ApplicationDbContext _context;
2016-09-05 19:01:43 +02:00
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)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2016-09-05 19:01:43 +02:00
}
2017-10-04 23:53:47 +02:00
Models.Blog.BlogPost blogpost = _context.Blogspot.Single(x=>x.Id == id);
2016-09-05 19:01:43 +02:00
if (blogpost == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2016-09-05 19:01:43 +02:00
}
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
2016-09-05 19:01:43 +02:00
if (blogpost.AuthorId!=uid)
if (!User.IsInRole(Constants.AdminGroupName))
2023-03-19 17:57:55 +00:00
return BadRequest();
2016-09-05 19:01:43 +02:00
blogpost.Rate = rate;
2017-02-23 03:10:30 +01:00
_context.SaveChanges(User.GetUserId());
2016-09-05 19:01:43 +02:00
return Ok();
}
}
}