using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; 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]")] [Authorize] [ApiController] public class CommentsController : ControllerBase { private readonly StudyLibContext _context; public CommentsController(StudyLibContext context) { _context = context; } // GET: api/Comments [HttpGet] public async Task>> GetComments() { return await _context.Comments.ToListAsync(); } // GET: api/Comments/5 [HttpGet("{id}")] public async Task> 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 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> 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 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); } } }