Added basics for task controller
This commit is contained in:
parent
f94274755c
commit
ce4ba27653
114
ScrumTaskboard/Controllers/CategoryController.cs
Normal file
114
ScrumTaskboard/Controllers/CategoryController.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 CategoryController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly TaskContext _context;
|
||||||
|
|
||||||
|
public CategoryController(TaskContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET: api/category
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<ActionResult<IEnumerable<ScrumCategory>>> GetCategory()
|
||||||
|
{
|
||||||
|
return await _context.Categories.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET: api/category/1
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public async Task<ActionResult<ScrumCategory>> GetCategory(int id)
|
||||||
|
{
|
||||||
|
var category = await _context.Categories.FindAsync(id);
|
||||||
|
|
||||||
|
if (category == null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
return category;
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT: api/category/1
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
public async Task<IActionResult> PutCategory(int id, ScrumCategory category)
|
||||||
|
{
|
||||||
|
// Die ID der Kategorie darf nicht geändert werden
|
||||||
|
if (id != category.id)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Speichere die geänderten Kategorie im Context
|
||||||
|
_context.Entry(category).State = EntityState.Modified;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Übernehme die Änderungen in die Datenbank
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
catch (DbUpdateConcurrencyException)
|
||||||
|
{
|
||||||
|
// Wenn die Kategorie nicht existiert, gib Statuscode 404 zurück
|
||||||
|
if (!CategoryExists(id))
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST: api/category
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<ActionResult<ScrumUserstory>> PostCategory(ScrumCategory category)
|
||||||
|
{
|
||||||
|
_context.Categories.Add(category);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Die neue Userstory wurde erstellt und kann über die GetUserstory Methode abgerufen werden.
|
||||||
|
return CreatedAtAction("GetCategory", new { id = category.id }, category);
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE: api/category/1
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
public async Task<ActionResult<ScrumCategory>> DeleteCategory(int id)
|
||||||
|
{
|
||||||
|
var category = await _context.Categories.FindAsync(id);
|
||||||
|
if (category == null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entferne die Kateorie aus dem Context
|
||||||
|
_context.Categories.Remove(category);
|
||||||
|
// Speichere die Änderungen in der Datenbank
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return category;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prüft, ob eine Kategorie mit der angegebenen ID bereits existiert.
|
||||||
|
/// </summary>
|
||||||
|
private bool CategoryExists(int id)
|
||||||
|
{
|
||||||
|
return _context.Categories.Any(e => e.id == id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ namespace ScrumTaskboard
|
||||||
{
|
{
|
||||||
public DbSet<ScrumTask> Tasks { get; set; }
|
public DbSet<ScrumTask> Tasks { get; set; }
|
||||||
public DbSet<ScrumUserstory> Userstories { get; set; }
|
public DbSet<ScrumUserstory> Userstories { get; set; }
|
||||||
|
public DbSet<ScrumCategory> Categories { get; set; }
|
||||||
|
|
||||||
public TaskContext() { }
|
public TaskContext() { }
|
||||||
|
|
||||||
|
@ -42,4 +43,13 @@ namespace ScrumTaskboard
|
||||||
public int createdby { get; set; }
|
public int createdby { get; set; }
|
||||||
public int project { get; set; }
|
public int project { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ScrumCategory
|
||||||
|
{
|
||||||
|
public int id { get; set; }
|
||||||
|
public string title { get; set; }
|
||||||
|
public string description { get; set; }
|
||||||
|
public string color { get; set; }
|
||||||
|
public int project { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user