yavsc/src/Api/Controllers/Blogspot/CommentsApiController.cs

135 lines
3.9 KiB
C#
Raw Normal View History

2023-03-19 17:57:55 +00:00
2024-12-14 20:21:41 +00:00
using Microsoft.AspNetCore.Authorization;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2024-12-14 20:21:41 +00:00
using Microsoft.EntityFrameworkCore.Metadata.Internal;
2023-03-19 17:57:55 +00:00
using Yavsc.Helpers;
2017-10-04 23:53:47 +02:00
using Yavsc.Models;
using Yavsc.Models.Blog;
namespace Yavsc.Controllers
{
2024-12-14 20:21:41 +00:00
[Authorize]
2017-10-04 23:53:47 +02:00
[Produces("application/json")]
[Route("api/blogcomments")]
public class CommentsApiController : Controller
{
2020-10-09 19:35:39 +01:00
private readonly ApplicationDbContext _context;
2017-10-04 23:53:47 +02:00
public CommentsApiController(ApplicationDbContext context)
{
_context = context;
}
[HttpGet("{id}", Name = "GetComment")]
public async Task<IActionResult> GetComment([FromRoute] long id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2017-10-04 23:53:47 +02:00
}
Comment comment = await _context.Comment.SingleAsync(m => m.Id == id);
if (comment == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2017-10-04 23:53:47 +02:00
}
return Ok(comment);
}
[HttpPost]
2024-12-15 13:53:30 +00:00
public async Task<IActionResult> Post([FromBody] CommentPost post)
2017-10-04 23:53:47 +02:00
{
if (!ModelState.IsValid)
{
return new BadRequestObjectResult(ModelState);
}
2025-02-08 20:06:24 +00:00
var article = await _context.BlogSpot.FirstOrDefaultAsync
2024-12-14 20:21:41 +00:00
(p=> p.Id == post.ReceiverId);
if (article==null) {
ModelState.AddModelError("ReceiverId", "not found");
return BadRequest(ModelState);
}
if (post.ParentId!=null)
2017-10-20 15:14:16 +02:00
{
2024-12-14 20:21:41 +00:00
var parentExists = _context.Comment.Any(c => c.Id == post.ParentId);
if (!parentExists)
{
ModelState.AddModelError("ParentId", "not found");
return BadRequest(ModelState);
2017-10-20 15:14:16 +02:00
}
}
2024-12-14 20:21:41 +00:00
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);
2017-10-04 23:53:47 +02:00
try
{
2024-12-14 23:49:13 +00:00
await _context.SaveChangesAsync(uid);
2017-10-04 23:53:47 +02:00
}
catch (DbUpdateException)
{
2024-12-14 20:21:41 +00:00
if (CommentExists(c.Id))
2017-10-04 23:53:47 +02:00
{
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status409Conflict);
2017-10-04 23:53:47 +02:00
}
else
{
throw;
}
}
2024-12-15 13:53:30 +00:00
return CreatedAtRoute("GetComment", new { id = c.Id }, new { id = c.Id, dateCreated = c.DateCreated });
2017-10-04 23:53:47 +02:00
}
// DELETE: api/CommentsApi/5
[HttpDelete("{id}")]
2024-12-15 13:53:30 +00:00
public async Task<IActionResult> Delete([FromRoute] long id)
2017-10-04 23:53:47 +02:00
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2017-10-04 23:53:47 +02:00
}
Comment comment = await _context.Comment.SingleAsync(m => m.Id == id);
if (comment == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2017-10-04 23:53:47 +02:00
}
2017-10-20 15:14:16 +02:00
RemoveRecursive(comment);
2017-10-05 11:57:29 +02:00
await _context.SaveChangesAsync(User.GetUserId());
2017-10-04 23:53:47 +02:00
return Ok(comment);
}
2017-10-20 15:14:16 +02:00
private void RemoveRecursive (Comment comment)
{
2024-12-14 20:21:41 +00:00
var children = _context.Comment.Where
(c=>c.ParentId==comment.Id).ToList();
2017-10-20 15:14:16 +02:00
foreach (var child in children) {
RemoveRecursive(child);
}
_context.Comment.Remove(comment);
}
2017-10-04 23:53:47 +02:00
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool CommentExists(long id)
{
return _context.Comment.Count(e => e.Id == id) > 0;
}
}
2020-10-09 19:35:39 +01:00
}