Projekt-Vorlage mit Tasks Controller
This commit is contained in:
115
ScrumTaskboard/Controllers/TasksController.cs
Normal file
115
ScrumTaskboard/Controllers/TasksController.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ScrumTaskboard;
|
||||
|
||||
namespace ScrumTaskboard.Controllers
|
||||
{
|
||||
[Route("[controller]")]
|
||||
[ApiController]
|
||||
public class TasksController : ControllerBase
|
||||
{
|
||||
private readonly TaskContext _context;
|
||||
|
||||
public TasksController(TaskContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: api/Tasks
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<ScrumTask>>> GetTasks()
|
||||
{
|
||||
return await _context.Tasks.ToListAsync();
|
||||
}
|
||||
|
||||
// GET: api/Tasks/5
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<ScrumTask>> GetTask(int id)
|
||||
{
|
||||
var task = await _context.Tasks.FindAsync(id);
|
||||
|
||||
if (task == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
// PUT: api/Tasks/5
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> PutTask(int id, ScrumTask task)
|
||||
{
|
||||
// Die ID des Tasks darf nicht geändert werden
|
||||
if (id != task.Id)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
// Speichere den geänderten Task im Context
|
||||
_context.Entry(task).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
// Übernehme die Änderungen in die Datenbank
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
// Wenn der Task nicht existiert, gib Statuscode 404 zurück
|
||||
if (!TaskExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
// POST: api/Tasks
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<ScrumTask>> PostTask(ScrumTask task)
|
||||
{
|
||||
_context.Tasks.Add(task);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
// Der neue Task wurde erstellt und kann über die GetTask Methode abgerufen werden.
|
||||
return CreatedAtAction("GetTask", new { id = task.Id }, task);
|
||||
}
|
||||
|
||||
// DELETE: api/Tasks/5
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<ActionResult<ScrumTask>> DeleteTask(int id)
|
||||
{
|
||||
var scrumTask = await _context.Tasks.FindAsync(id);
|
||||
if (scrumTask == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
// Entferne den Task aus dem Context
|
||||
_context.Tasks.Remove(scrumTask);
|
||||
// Speichere die Änderungen in der Datenbank
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return scrumTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prüft, ob ein Task mit der angegebenen ID bereits existiert.
|
||||
/// </summary>
|
||||
private bool TaskExists(int id)
|
||||
{
|
||||
return _context.Tasks.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user