study-lib-backend/API/Controllers/SubjectsController.cs

142 lines
3.9 KiB
C#
Raw Normal View History

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;
2020-12-13 01:23:12 +01:00
using Newtonsoft.Json;
2020-12-12 14:47:20 +01:00
using StudyLib.API.Data;
using StudyLib.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 SubjectsController : ControllerBase
{
private readonly StudyLibContext _context;
public SubjectsController(StudyLibContext context)
{
_context = context;
}
2020-12-23 23:35:08 +01:00
[HttpGet("list-by-group-id/{groupId}")]
public async Task<ActionResult<IEnumerable<Subject>>> GetSubjects(long groupId)
2020-12-12 14:47:20 +01:00
{
2020-12-23 23:35:08 +01:00
return await _context.Subjects.Where(s => s.Group.ID == groupId).Include(s => s.Comments).Include(s => s.Assignments).Include(s => s.Tests).ToListAsync();
2020-12-12 14:47:20 +01:00
}
2020-12-23 23:35:08 +01:00
[HttpGet("get-by-id/{id}")]
2020-12-12 14:47:20 +01:00
public async Task<ActionResult<Subject>> GetSubject(long id)
{
var subject = await _context.Subjects.Where(s => s.ID == id).Include(s => s.Comments).Include(s => s.Assignments).Include(s => s.Tests).SingleOrDefaultAsync();
if (subject == null)
{
return NotFound();
}
return subject;
}
2020-12-23 23:35:08 +01:00
[HttpPut("update/{id}")]
2020-12-12 14:47:20 +01:00
public async Task<IActionResult> PutSubject(long id, Subject subject)
{
if (id != subject.ID)
{
return BadRequest();
}
_context.Entry(subject).State = EntityState.Modified;
foreach (var comment in subject.Comments)
{
if (comment.ID >= 1)
{
_context.Entry(comment).State = EntityState.Modified;
}
else
{
_context.Comments.Add(comment);
}
}
foreach (var assignment in subject.Assignments)
{
if (assignment.ID >= 1)
{
_context.Entry(assignment).State = EntityState.Modified;
}
else
{
_context.Assignments.Add(assignment);
}
}
foreach (var test in subject.Tests)
{
if (test.ID >= 1)
{
_context.Entry(test).State = EntityState.Modified;
}
else
{
_context.Tests.Add(test);
}
}
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!SubjectExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
2020-12-23 23:35:08 +01:00
[HttpPost("add")]
2020-12-12 14:47:20 +01:00
public async Task<ActionResult<Subject>> PostSubject(Subject subject)
{
_context.Subjects.Add(subject);
await _context.SaveChangesAsync();
return CreatedAtAction("GetSubject", new { id = subject.ID }, subject);
}
// DELETE: api/Subjects/5
2020-12-23 23:35:08 +01:00
[HttpDelete("delete/{id}")]
2020-12-12 14:47:20 +01:00
public async Task<ActionResult<Subject>> DeleteSubject(long id)
{
var subject = await _context.Subjects.FindAsync(id);
if (subject == null)
{
return NotFound();
}
_context.Subjects.Remove(subject);
await _context.SaveChangesAsync();
return subject;
}
private bool SubjectExists(long id)
{
return _context.Subjects.Any(e => e.ID == id);
}
}
}