using System; 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 SprintsController : ControllerBase { private readonly TaskContext _context; public SprintsController(TaskContext context) { _context = context; } // GET: api/sprint [HttpGet] public async Task>> GetSprint([FromQuery]string? title, [FromQuery]int? projectid, [FromQuery]DateTime? startDate, [FromQuery]DateTime? endDate) { var filtered = _context.Sprints.AsQueryable(); if (title != null) { filtered = filtered.Where(s => s.title.Contains(title)); } if (projectid != null) { filtered = filtered.Where(s => s.projectid == projectid); } if (startDate != null) { filtered = filtered.Where(s => s.startDate == startDate); } if (endDate != null) { filtered = filtered.Where(s => s.endDate == endDate); } return await filtered.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); } } }