srumboard_backend/ScrumTaskboard/Controllers/TasksController.cs

183 lines
5.8 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
/// <summary>
/// Retrieve all Tasks in DB.
/// Result can be filtered using the Params.
/// </summary>
/// <param name="title">string value</param>
/// <param name="userstoryid">ID of created Userstory</param>
/// <param name="statusid">ID of created Status</param>
/// <param name="assignedtoid">ID of the Assignedto</param>
/// <param name="projectid">ID of created Project</param>
/// <param name="priority">enum value</param>
/// <returns>JSON list of all matching Tasks</returns>
#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
/// <summary>
/// Retrieve the Task by it's ID.
/// </summary>
/// <param name="id">ID of searched Task</param>
/// <returns>JSON object</returns>
[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
/// <summary>
/// Update the Task identified by it's ID.
/// </summary>
/// <param name="id">to edit Task's ID</param>
/// <param name="task">modified Task</param>
/// <returns>the proper status code</returns>
[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
/// <summary>
/// Create a new Task.
/// </summary>
/// <param name="task">new Task</param>
/// <returns>the proper status code</returns>
[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
/// <summary>
/// Delete a Task identified by it's ID.
/// </summary>
/// <param name="id">to delete Task's ID</param>
/// <returns>the proper status code</returns>
[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);
}
}
}