diff --git a/ScrumTaskboard/Controllers/SprintsController.cs b/ScrumTaskboard/Controllers/SprintsController.cs new file mode 100644 index 0000000..4fc5d7f --- /dev/null +++ b/ScrumTaskboard/Controllers/SprintsController.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 SprintsController : ControllerBase + { + private readonly TaskContext _context; + + public SprintsController(TaskContext context) + { + _context = context; + } + + // GET: api/sprint + [HttpGet] + public async Task>> GetSprints() + { + return await _context.Sprints.ToListAsync(); + } + + // GET: api/sprint/5 + [HttpGet("{id}")] + public async Task> GetSprint(int id) + { + var Sprint = await _context.Sprints.FindAsync(id); + + if (Sprint == null) + { + return NotFound(); + } + + return Sprint; + } + + // PUT: api/Sprint/5 + [HttpPut("{id}")] + public async Task PutSprint(int id, ScrumSprint sprint) + { + // Die ID des Sprints darf nicht geändert werden + if (id != sprint.id) + { + return BadRequest(); + } + + // Speichere den geänderten Sprint im Context + _context.Entry(sprint).State = EntityState.Modified; + + try + { + // Übernehme die Änderungen in die Datenbank + await _context.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + // Wenn der Sprint nicht existiert, gib Statuscode 404 zurück + if (!SprintExists(id)) + { + return NotFound(); + } + else + { + throw; + } + } + + return NoContent(); + } + + // POST: api/Sprint + [HttpPost] + public async Task> PostSprint(ScrumSprint sprint) + { + _context.Sprints.Add(sprint); + await _context.SaveChangesAsync(); + + // Der neue Sprint wurde erstellt und kann über die GetSprint Methode abgerufen werden. + return CreatedAtAction("GetSprint", new { id = sprint.id }, sprint); + } + + // DELETE: api/Sprint/5 + [HttpDelete("{id}")] + public async Task> DeleteSprint(int id) + { + var scrumSprint = await _context.Sprints.FindAsync(id); + if (scrumSprint == null) + { + return NotFound(); + } + + // Entferne den Sprint aus dem Context + _context.Sprints.Remove(scrumSprint); + // Speichere die Änderungen in der Datenbank + await _context.SaveChangesAsync(); + + return scrumSprint; + } + + /// + /// Prüft, ob ein Sprint mit der angegebenen ID bereits existiert. + /// + private bool SprintExists(int id) + { + return _context.Sprints.Any(e => e.id == id); + } + } +} diff --git a/ScrumTaskboard/TaskContext.cs b/ScrumTaskboard/TaskContext.cs index 014c535..850b568 100644 --- a/ScrumTaskboard/TaskContext.cs +++ b/ScrumTaskboard/TaskContext.cs @@ -6,6 +6,7 @@ namespace ScrumTaskboard public class TaskContext : DbContext { public DbSet Tasks { get; set; } + public DbSet Sprints { get; set; } public TaskContext() { } @@ -29,4 +30,13 @@ namespace ScrumTaskboard public int project { get; set; } public int userstory { get; set; } } + public class ScrumSprint + { + public int id { get; set; } + public string title { get; set; } + public string description { get; set; } + public DateTime startDate { get; set; } + public DateTime endDate { get; set; } + public int project { get; set; } + } }