study-lib-backend/API/Controllers/TestController.cs
2020-12-12 14:47:20 +01:00

109 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
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]")]
[ApiController]
public class TestController : ControllerBase
{
private readonly StudyLibContext _context;
public TestController(StudyLibContext context)
{
_context = context;
}
// GET: api/MinorExams
[HttpGet]
public async Task<ActionResult<IEnumerable<Test>>> GetMinorExams()
{
return await _context.Tests.ToListAsync();
}
// GET: api/MinorExams/5
[HttpGet("{id}")]
public async Task<ActionResult<Test>> GetMinorExam(long id)
{
var minorExam = await _context.Tests.FindAsync(id);
if (minorExam == null)
{
return NotFound();
}
return minorExam;
}
// PUT: api/MinorExams/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutMinorExam(long id, Test minorExam)
{
if (id != minorExam.ID)
{
return BadRequest();
}
_context.Entry(minorExam).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!MinorExamExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/MinorExams
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Test>> PostMinorExam(Test minorExam)
{
_context.Tests.Add(minorExam);
await _context.SaveChangesAsync();
return CreatedAtAction("GetMinorExam", new { id = minorExam.ID }, minorExam);
}
// DELETE: api/MinorExams/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteMinorExam(long id)
{
var minorExam = await _context.Tests.FindAsync(id);
if (minorExam == null)
{
return NotFound();
}
_context.Tests.Remove(minorExam);
await _context.SaveChangesAsync();
return NoContent();
}
private bool MinorExamExists(long id)
{
return _context.Tests.Any(e => e.ID == id);
}
}
}