115 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			115 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| 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<ActionResult<IEnumerable<ScrumSprint>>> GetSprints()
 | |
|         {
 | |
|             return await _context.Sprints.ToListAsync();
 | |
|         }
 | |
| 
 | |
|         // GET: api/sprint/5
 | |
|         [HttpGet("{id}")]
 | |
|         public async Task<ActionResult<ScrumSprint>> 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<IActionResult> 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<ActionResult<ScrumSprint>> 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<ActionResult<ScrumSprint>> 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;
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// Prüft, ob ein Sprint mit der angegebenen ID bereits existiert.
 | |
|         /// </summary>
 | |
|         private bool SprintExists(int id)
 | |
|         {
 | |
|             return _context.Sprints.Any(e => e.id == id);
 | |
|         }
 | |
|     }
 | |
| }
 | 
