2020-12-12 14:47:20 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-12-20 18:53:24 +01:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-12-12 14:47:20 +01:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using StudyLib.API.Data;
|
|
|
|
|
using StudyLib.API.Models;
|
|
|
|
|
|
|
|
|
|
namespace StudyLib.API.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/[controller]")]
|
2020-12-20 18:53:24 +01:00
|
|
|
|
[Authorize]
|
2020-12-12 14:47:20 +01:00
|
|
|
|
[ApiController]
|
|
|
|
|
public class CommentsController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly StudyLibContext _context;
|
|
|
|
|
|
|
|
|
|
public CommentsController(StudyLibContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: api/Comments
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<Comment>>> GetComments()
|
|
|
|
|
{
|
|
|
|
|
return await _context.Comments.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: api/Comments/5
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
public async Task<ActionResult<Comment>> GetComment(long id)
|
|
|
|
|
{
|
|
|
|
|
var comment = await _context.Comments.FindAsync(id);
|
|
|
|
|
|
|
|
|
|
if (comment == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return comment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT: api/Comments/5
|
|
|
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
public async Task<IActionResult> PutComment(long id, Comment comment)
|
|
|
|
|
{
|
|
|
|
|
if (id != comment.ID)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.Entry(comment).State = EntityState.Modified;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (DbUpdateConcurrencyException)
|
|
|
|
|
{
|
|
|
|
|
if (!CommentExists(id))
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: api/Comments
|
|
|
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<ActionResult<Comment>> PostComment(Comment comment)
|
|
|
|
|
{
|
|
|
|
|
_context.Comments.Add(comment);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return CreatedAtAction("GetComment", new { id = comment.ID }, comment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DELETE: api/Comments/5
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
public async Task<IActionResult> DeleteComment(long id)
|
|
|
|
|
{
|
|
|
|
|
var comment = await _context.Comments.FindAsync(id);
|
|
|
|
|
if (comment == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.Comments.Remove(comment);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CommentExists(long id)
|
|
|
|
|
{
|
|
|
|
|
return _context.Comments.Any(e => e.ID == id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|