diff --git a/ScrumTaskboard/Controllers/UserstoriesController.cs b/ScrumTaskboard/Controllers/UserstoriesController.cs index cfb539f..9fcf9dc 100644 --- a/ScrumTaskboard/Controllers/UserstoriesController.cs +++ b/ScrumTaskboard/Controllers/UserstoriesController.cs @@ -20,11 +20,96 @@ namespace ScrumTaskboard.Controllers _context = context; } - // GET: api/Tasks + // 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); + } } } \ No newline at end of file