121 lines
3.5 KiB
C#
121 lines
3.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ScrumTaskboard.Controllers
|
|
{
|
|
[Route("[controller]")]
|
|
[ApiController]
|
|
public class StatusController : ControllerBase
|
|
{
|
|
private readonly TaskContext _context;
|
|
|
|
public StatusController(TaskContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/status
|
|
#nullable enable
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<ScrumStatus>>> GetStatus([FromQuery]string? title)
|
|
{
|
|
var filtered = _context.Status.AsQueryable();
|
|
|
|
if (title != null)
|
|
{
|
|
filtered = filtered.Where<ScrumStatus>(s => s.title.Contains(title));
|
|
}
|
|
|
|
return await filtered.ToListAsync();
|
|
}
|
|
#nullable enable
|
|
|
|
// GET: api/status/1
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<ScrumStatus>> GetStatus(int id)
|
|
{
|
|
var userstory = await _context.Status.FindAsync(id);
|
|
|
|
if (userstory == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return userstory;
|
|
}
|
|
|
|
// PUT: api/status/1
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutStatus(int id, ScrumStatus userstory)
|
|
{
|
|
// Die ID des Status darf nicht geändert werden
|
|
if (id != userstory.id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
// Speichere deb geänderten Status im Context
|
|
_context.Entry(userstory).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
// Übernehme die Änderungen in die Datenbank
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
// Wenn der Status nicht existiert, gib Statuscode 404 zurück
|
|
if (!StatusExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/status
|
|
[HttpPost]
|
|
public async Task<ActionResult<ScrumStatus>> PostTask(ScrumStatus userstory)
|
|
{
|
|
_context.Status.Add(userstory);
|
|
await _context.SaveChangesAsync();
|
|
|
|
// Der neue Status wurde erstellt und kann über die GetStatus Methode abgerufen werden.
|
|
return CreatedAtAction("GetStatus", new { id = userstory.id }, userstory);
|
|
}
|
|
|
|
// DELETE: api/status/1
|
|
[HttpDelete("{id}")]
|
|
public async Task<ActionResult<ScrumStatus>> DeleteStatus(int id)
|
|
{
|
|
var userstory = await _context.Status.FindAsync(id);
|
|
if (userstory == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
// Entferne den Status aus dem Context
|
|
_context.Status.Remove(userstory);
|
|
// Speichere die Änderungen in der Datenbank
|
|
await _context.SaveChangesAsync();
|
|
|
|
return userstory;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prüft, ob ein Status mit der angegebenen ID bereits existiert.
|
|
/// </summary>
|
|
private bool StatusExists(int id)
|
|
{
|
|
return _context.Status.Any(e => e.id == id);
|
|
}
|
|
}
|
|
} |