151 lines
4.4 KiB
C#
151 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ScrumTaskboard.Controllers
|
|
{
|
|
/// <summary>
|
|
/// This is a controller of Tasks class includes all API's related to the Tasks.
|
|
/// </summary>
|
|
[Route("[controller]")]
|
|
[ApiController]
|
|
public class TasksController : ControllerBase
|
|
{
|
|
private readonly TaskContext _context;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the class.
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
public TasksController(TaskContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/tasks
|
|
#nullable enable
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<ScrumTask>>> GetTasks([FromQuery]string? title, [FromQuery]int? userstoryid, [FromQuery]int? statusid, [FromQuery]int? assignedtoid, [FromQuery]int? projectid, [FromQuery]ScrumPrio? priority)
|
|
{
|
|
var filtered = _context.Tasks.AsQueryable();
|
|
|
|
if (title != null)
|
|
{
|
|
filtered = filtered.Where<ScrumTask>(t => t.Title.Contains(title));
|
|
}
|
|
if (userstoryid != null)
|
|
{
|
|
filtered = filtered.Where<ScrumTask>(t => t.UserstoryId == userstoryid);
|
|
}
|
|
if (statusid != null)
|
|
{
|
|
filtered = filtered.Where<ScrumTask>(t => t.StatusId == statusid);
|
|
}
|
|
if (assignedtoid != null)
|
|
{
|
|
filtered = filtered.Where<ScrumTask>(t => t.AssignedToId == assignedtoid);
|
|
}
|
|
if (projectid != null)
|
|
{
|
|
filtered = filtered.Where<ScrumTask>(t => t.ProjectId == projectid);
|
|
}
|
|
if (priority != null)
|
|
{
|
|
filtered = filtered.Where<ScrumTask>(t => t.Priority == priority);
|
|
}
|
|
|
|
|
|
return await filtered.ToListAsync();
|
|
}
|
|
#nullable disable
|
|
|
|
// 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)
|
|
{
|
|
// The task's ID must not be changed
|
|
if (id != task.id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
// Save the changed task in the context
|
|
_context.Entry(task).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
// Apply the changes to the database
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
// If the task does not exist, return status code 404
|
|
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();
|
|
|
|
// The new task has been created and can be called up using the GetTask method
|
|
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();
|
|
}
|
|
|
|
// Remove the task from the context
|
|
_context.Tasks.Remove(scrumTask);
|
|
// Save the changes in the database
|
|
await _context.SaveChangesAsync();
|
|
|
|
return scrumTask;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether a task with the specified ID already exists.
|
|
/// </summary>
|
|
private bool TaskExists(int id)
|
|
{
|
|
return _context.Tasks.Any(e => e.Id == id);
|
|
}
|
|
}
|
|
}
|