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 UserstoriesController : ControllerBase { private readonly TaskContext _context; public UserstoriesController(TaskContext context) { _context = context; } // GET: api/userstories [HttpGet] public async Task>> GetUserstory() { return await _context.Userstories.ToListAsync(); } // GET: api/userstories/1 [HttpGet("{id}")] public async Task> GetUserstory(int id) { var userstory = await _context.Userstories.FindAsync(id); if (userstory == null) { return NotFound(); } return userstory; } // PUT: api/userstories/1 [HttpPut("{id}")] public async Task PutUserstory(int id, ScrumUserstory userstory) { // Die ID der Userstory darf nicht geändert werden if (id != userstory.id) { return BadRequest(); } // Speichere die geänderten Userstory im Context _context.Entry(userstory).State = EntityState.Modified; try { // Übernehme die Änderungen in die Datenbank await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { // Wenn die Userstory nicht existiert, gib Statuscode 404 zurück if (!UserstoryExists(id)) { return NotFound(); } else { throw; } } return NoContent(); } // POST: api/userstories [HttpPost] public async Task> PostTask(ScrumUserstory userstory) { _context.Userstories.Add(userstory); await _context.SaveChangesAsync(); // Die neue Userstory wurde erstellt und kann über die GetUserstory Methode abgerufen werden. return CreatedAtAction("GetUserstory", new { id = userstory.id }, userstory); } // DELETE: api/userstories/1 [HttpDelete("{id}")] public async Task> DeleteUserstory(int id) { var userstory = await _context.Userstories.FindAsync(id); if (userstory == null) { return NotFound(); } // Entferne die Userstory aus dem Context _context.Userstories.Remove(userstory); // Speichere die Änderungen in der Datenbank await _context.SaveChangesAsync(); return userstory; } /// /// Prüft, ob eine Userstory mit der angegebenen ID bereits existiert. /// private bool UserstoryExists(int id) { return _context.Userstories.Any(e => e.id == id); } } }