Created sprintController
This commit is contained in:
		
							
								
								
									
										114
									
								
								ScrumTaskboard/Controllers/SprintsController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										114
									
								
								ScrumTaskboard/Controllers/SprintsController.cs
									
									
									
									
									
										Normal file
									
								
							@@ -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<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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -6,6 +6,7 @@ namespace ScrumTaskboard
 | 
			
		||||
    public class TaskContext : DbContext 
 | 
			
		||||
    {
 | 
			
		||||
        public DbSet<ScrumTask> Tasks { get; set; }
 | 
			
		||||
        public DbSet<ScrumSprint> 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; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user