This commit is contained in:
Paul Schneider 2024-12-14 20:21:41 +00:00
commit fdf5ee915e
67 changed files with 48812 additions and 37055 deletions

View file

@ -1,12 +1,15 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Yavsc.Helpers;
using Yavsc.Models;
using Yavsc.Models.Blog;
namespace Yavsc.Controllers
{
[Authorize]
[Produces("application/json")]
[Route("api/blogcomments")]
public class CommentsApiController : Controller
@ -81,27 +84,45 @@ namespace Yavsc.Controllers
// POST: api/CommentsApi
[HttpPost]
public async Task<IActionResult> PostComment([FromBody] Comment comment)
public async Task<IActionResult> PostComment([FromBody] CommentPost post)
{
if (!ModelState.IsValid)
{
return new BadRequestObjectResult(ModelState);
}
if (!User.IsInRole(Constants.AdminGroupName))
var article = await _context.Blogspot.FirstOrDefaultAsync
(p=> p.Id == post.ReceiverId);
if (article==null) {
ModelState.AddModelError("ReceiverId", "not found");
return BadRequest(ModelState);
}
if (post.ParentId!=null)
{
if (User.GetUserId()!=comment.AuthorId) {
ModelState.AddModelError("Content","Vous ne pouvez pas poster au nom d'un autre.");
return new BadRequestObjectResult(ModelState);
var parentExists = _context.Comment.Any(c => c.Id == post.ParentId);
if (!parentExists)
{
ModelState.AddModelError("ParentId", "not found");
return BadRequest(ModelState);
}
}
_context.Comment.Add(comment);
string uid = User.GetUserId();
Comment c = new Comment{
ReceiverId = post.ReceiverId,
Content = post.Content,
ParentId = post.ParentId,
AuthorId = uid,
UserModified = uid
};
_context.Comment.Add(c);
try
{
await _context.SaveChangesAsync(User.GetUserId());
}
catch (DbUpdateException)
{
if (CommentExists(comment.Id))
if (CommentExists(c.Id))
{
return new StatusCodeResult(StatusCodes.Status409Conflict);
}
@ -110,7 +131,7 @@ namespace Yavsc.Controllers
throw;
}
}
return CreatedAtRoute("GetComment", new { id = comment.Id }, comment);
return CreatedAtRoute("GetComment", new { id = c.Id }, post);
}
// DELETE: api/CommentsApi/5
@ -135,7 +156,8 @@ namespace Yavsc.Controllers
}
private void RemoveRecursive (Comment comment)
{
var children = _context.Comment.Where(c=>c.ParentId==comment.Id).ToList();
var children = _context.Comment.Where
(c=>c.ParentId==comment.Id).ToList();
foreach (var child in children) {
RemoveRecursive(child);
}