111 lines
3.0 KiB
C#
111 lines
3.0 KiB
C#
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.Models;
|
|
|
|
namespace StudyLib.API.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[Authorize]
|
|
[ApiController]
|
|
public class AssignmentsController : ControllerBase
|
|
{
|
|
private readonly StudyLibContext _context;
|
|
|
|
public AssignmentsController(StudyLibContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/Assignments
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<Assignment>>> GetAssignments()
|
|
{
|
|
return await _context.Assignments.ToListAsync();
|
|
}
|
|
|
|
// GET: api/Assignments/5
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<Assignment>> GetAssignment(long id)
|
|
{
|
|
var assignment = await _context.Assignments.FindAsync(id);
|
|
|
|
if (assignment == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return assignment;
|
|
}
|
|
|
|
// PUT: api/Assignments/5
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutAssignment(long id, Assignment assignment)
|
|
{
|
|
if (id != assignment.ID)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
_context.Entry(assignment).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!AssignmentExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/Assignments
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPost]
|
|
public async Task<ActionResult<Assignment>> PostAssignment(Assignment assignment)
|
|
{
|
|
_context.Assignments.Add(assignment);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction("GetAssignment", new { id = assignment.ID }, assignment);
|
|
}
|
|
|
|
// DELETE: api/Assignments/5
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteAssignment(long id)
|
|
{
|
|
var assignment = await _context.Assignments.FindAsync(id);
|
|
if (assignment == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
_context.Assignments.Remove(assignment);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
private bool AssignmentExists(long id)
|
|
{
|
|
return _context.Assignments.Any(e => e.ID == id);
|
|
}
|
|
}
|
|
}
|