diff --git a/ScrumTaskboard/Controllers/StatusController.cs b/ScrumTaskboard/Controllers/StatusController.cs new file mode 100644 index 0000000..30e98d0 --- /dev/null +++ b/ScrumTaskboard/Controllers/StatusController.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +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 + [HttpGet] + public async Task>> GetStatus() + { + return await _context.Status.ToListAsync(); + } + + // GET: api/status/1 + [HttpGet("{id}")] + public async Task> 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 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> 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> 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; + } + + /// + /// Prüft, ob ein Status mit der angegebenen ID bereits existiert. + /// + private bool StatusExists(int id) + { + return _context.Status.Any(e => e.id == id); + } + } +} \ No newline at end of file diff --git a/ScrumTaskboard/TaskContext.cs b/ScrumTaskboard/TaskContext.cs index 3dbdf86..9e00e6b 100644 --- a/ScrumTaskboard/TaskContext.cs +++ b/ScrumTaskboard/TaskContext.cs @@ -9,6 +9,7 @@ namespace ScrumTaskboard public DbSet Userstories { get; set; } public DbSet Categories { get; set; } public DbSet Sprints { get; set; } + public DbSet Status { get; set; } public TaskContext() { } @@ -63,4 +64,11 @@ namespace ScrumTaskboard public DateTime endDate { get; set; } public int project { get; set; } } + + public class ScrumStatus + { + public int id { get; set; } + public string title { get; set; } + public string description { get; set; } + } }