srumboard_backend/ScrumTaskboard/Controllers/TasksController.cs

183 lines
5.8 KiB
C#
Raw Normal View History

2020-07-08 16:10:58 +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
{
/// <summary>
/// This is a controller of Tasks class includes all API's related to the Tasks.
2020-07-08 11:42:42 +00:00
/// </summary>
2020-05-26 12:50:26 +00:00
[Route("[controller]")]
[ApiController]
public class TasksController : ControllerBase
{
private readonly TaskContext _context;
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
2020-07-08 11:42:42 +00:00
/// <param name="context"></param>
2020-05-26 12:50:26 +00:00
public TasksController(TaskContext context)
{
_context = context;
2020-07-08 16:10:58 +00:00
}
// GET: api/tasks
2020-07-15 14:21:43 +00:00
/// <summary>
/// Retrieve all Tasks in DB.
2020-07-19 11:09:48 +00:00
/// Result can be filtered using the Params.
2020-07-15 14:21:43 +00:00
/// </summary>
2020-07-19 11:09:48 +00:00
/// <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>
2020-07-15 14:21:43 +00:00
#nullable enable
2020-05-26 12:50:26 +00:00
[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)
2020-05-26 12:50:26 +00:00
{
var filtered = _context.Tasks.AsQueryable();
2020-07-08 16:10:58 +00:00
if (title != null)
{
filtered = filtered.Where<ScrumTask>(t => t.Title.Contains(title));
}
2020-07-08 16:10:58 +00:00
if (userstoryid != null)
{
filtered = filtered.Where<ScrumTask>(t => t.UserstoryId == userstoryid);
}
2020-07-08 16:10:58 +00:00
if (statusid != null)
{
filtered = filtered.Where<ScrumTask>(t => t.StatusId == statusid);
}
2020-07-08 16:10:58 +00:00
if (assignedtoid != null)
{
filtered = filtered.Where<ScrumTask>(t => t.AssignedToId == assignedtoid);
}
2020-07-08 16:10:58 +00:00
if (projectid != null)
{
filtered = filtered.Where<ScrumTask>(t => t.ProjectId == projectid);
}
2020-07-08 16:10:58 +00:00
if (priority != null)
{
filtered = filtered.Where<ScrumTask>(t => t.Priority == priority);
}
2020-07-08 16:10:58 +00:00
return await filtered.ToListAsync();
2020-07-08 16:10:58 +00:00
}
2020-07-15 14:21:43 +00:00
#nullable disable
2020-07-08 16:10:58 +00:00
// GET: api/tasks/5
2020-07-15 14:21:43 +00:00
/// <summary>
/// Retrieve the Task by it's ID.
/// </summary>
2020-07-19 11:09:48 +00:00
/// <param name="id">ID of searched Task</param>
/// <returns>JSON object</returns>
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-07-15 14:21:43 +00:00
/// <summary>
/// Update the Task identified by it's ID.
/// </summary>
2020-07-19 15:46:41 +00:00
/// <param name="id">to edit Task's ID</param>
2020-07-19 11:09:48 +00:00
/// <param name="task">modified Task</param>
2020-07-19 15:46:41 +00:00
/// <returns>the proper status code</returns>
2020-05-26 12:50:26 +00:00
[HttpPut("{id}")]
public async Task<IActionResult> PutTask(int id, ScrumTask task)
{
2020-07-08 11:42:42 +00:00
// The task's ID must not be changed
if (id != task.Id)
2020-05-26 12:50:26 +00:00
{
return BadRequest();
}
2020-07-08 11:42:42 +00:00
// Save the changed task in the context
2020-05-26 12:50:26 +00:00
_context.Entry(task).State = EntityState.Modified;
try
{
2020-07-08 11:42:42 +00:00
// Apply the changes to the database
2020-05-26 12:50:26 +00:00
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
2020-07-08 11:42:42 +00:00
// If the task does not exist, return status code 404
2020-05-26 12:50:26 +00:00
if (!TaskExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
2020-06-04 06:42:01 +00:00
// POST: api/tasks
2020-07-15 14:21:43 +00:00
/// <summary>
/// Create a new Task.
/// </summary>
2020-07-19 15:46:41 +00:00
/// <param name="task">new Task</param>
/// <returns>the proper status code</returns>
2020-05-26 12:50:26 +00:00
[HttpPost]
public async Task<ActionResult<ScrumTask>> PostTask(ScrumTask task)
{
_context.Tasks.Add(task);
await _context.SaveChangesAsync();
2020-07-08 11:42:42 +00:00
// The new task has been created and can be called up using the GetTask method
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-07-15 14:21:43 +00:00
/// <summary>
/// Delete a Task identified by it's ID.
/// </summary>
2020-07-19 15:46:41 +00:00
/// <param name="id">to delete Task's ID</param>
/// <returns>the proper status code</returns>
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();
}
2020-07-08 11:42:42 +00:00
// Remove the task from the context
2020-05-26 12:50:26 +00:00
_context.Tasks.Remove(scrumTask);
2020-07-08 11:42:42 +00:00
// Save the changes in the database
2020-05-26 12:50:26 +00:00
await _context.SaveChangesAsync();
return scrumTask;
}
/// <summary>
2020-07-08 11:42:42 +00:00
/// Checks whether a task with the specified ID already exists.
2020-05-26 12:50:26 +00:00
/// </summary>
private bool TaskExists(int id)
{
2020-07-08 16:10:58 +00:00
return _context.Tasks.Any(e => e.Id == id);
2020-05-26 12:50:26 +00:00
}
}
}