2020-06-18 13:10:42 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-05-26 12:50:26 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace ScrumTaskboard.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("[controller]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class TasksController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly TaskContext _context;
|
|
|
|
|
|
|
|
|
|
public TasksController(TaskContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 06:42:01 +00:00
|
|
|
|
// GET: api/tasks
|
2020-05-26 12:50:26 +00:00
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<ScrumTask>>> GetTasks()
|
|
|
|
|
{
|
2020-06-18 13:10:42 +00:00
|
|
|
|
IEnumerable<ScrumTask> filtered = _context.Tasks;
|
|
|
|
|
if (HttpContext.Request.Query["title"].ToString() != "")
|
|
|
|
|
{
|
|
|
|
|
filtered = filtered.Where<ScrumTask>(t => t.title.Contains(HttpContext.Request.Query["title"].ToString()));
|
|
|
|
|
}
|
|
|
|
|
if (Convert.ToInt32(HttpContext.Request.Query["userstoryid"]) != 0)
|
|
|
|
|
{
|
|
|
|
|
filtered = filtered.Where<ScrumTask>(t => t.userstoryid == Convert.ToInt32(HttpContext.Request.Query["userstoryid"]));
|
|
|
|
|
}
|
|
|
|
|
if (Convert.ToInt32(HttpContext.Request.Query["statusid"]) != 0)
|
|
|
|
|
{
|
|
|
|
|
filtered = filtered.Where<ScrumTask>(t => t.statusid == Convert.ToInt32(HttpContext.Request.Query["statusid"]));
|
|
|
|
|
}
|
|
|
|
|
if (Convert.ToInt32(HttpContext.Request.Query["categoryid"]) != 0)
|
|
|
|
|
{
|
|
|
|
|
filtered = filtered.Where<ScrumTask>(t => t.categoryid == Convert.ToInt32(HttpContext.Request.Query["categoryid"]));
|
|
|
|
|
}
|
|
|
|
|
if (Convert.ToInt32(HttpContext.Request.Query["assignedtoid"]) != 0)
|
|
|
|
|
{
|
|
|
|
|
filtered = filtered.Where<ScrumTask>(t => t.assignedtoid == Convert.ToInt32(HttpContext.Request.Query["assignedtoid"]));
|
|
|
|
|
}
|
|
|
|
|
if (Convert.ToInt32(HttpContext.Request.Query["projectid"]) != 0)
|
|
|
|
|
{
|
|
|
|
|
filtered = filtered.Where<ScrumTask>(t => t.projectid == Convert.ToInt32(HttpContext.Request.Query["projectid"]));
|
|
|
|
|
}
|
|
|
|
|
if (HttpContext.Request.Query["priority"].ToString() != "")
|
|
|
|
|
{
|
|
|
|
|
ScrumPrio priority;
|
|
|
|
|
Enum.TryParse<ScrumPrio>(HttpContext.Request.Query["priority"].ToString(), out priority);
|
|
|
|
|
filtered = filtered.Where<ScrumTask>(t => t.priority == priority);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return filtered.ToList();
|
2020-05-26 12:50:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 06:42:01 +00:00
|
|
|
|
// GET: api/tasks/5
|
2020-05-26 12:50:26 +00:00
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
public async Task<ActionResult<ScrumTask>> GetTask(int id)
|
|
|
|
|
{
|
|
|
|
|
var task = await _context.Tasks.FindAsync(id);
|
|
|
|
|
|
|
|
|
|
if (task == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return task;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 06:42:01 +00:00
|
|
|
|
// PUT: api/tasks/5
|
2020-05-26 12:50:26 +00:00
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
public async Task<IActionResult> PutTask(int id, ScrumTask task)
|
|
|
|
|
{
|
|
|
|
|
// Die ID des Tasks darf nicht geändert werden
|
2020-06-03 18:00:56 +00:00
|
|
|
|
if (id != task.id)
|
2020-05-26 12:50:26 +00:00
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 06:42:01 +00:00
|
|
|
|
// POST: api/tasks
|
2020-05-26 12:50:26 +00:00
|
|
|
|
[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.
|
2020-06-03 18:00:56 +00:00
|
|
|
|
return CreatedAtAction("GetTask", new { id = task.id }, task);
|
2020-05-26 12:50:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 06:42:01 +00:00
|
|
|
|
// DELETE: api/tasks/5
|
2020-05-26 12:50:26 +00:00
|
|
|
|
[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)
|
|
|
|
|
{
|
2020-06-03 18:00:56 +00:00
|
|
|
|
return _context.Tasks.Any(e => e.id == id);
|
2020-05-26 12:50:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|