167 lines
5.4 KiB
C#
167 lines
5.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 Categories class includes all API's related to the Categories.
|
|
/// </summary>
|
|
[Route("[controller]")]
|
|
[ApiController]
|
|
public class CategoriesController : ControllerBase
|
|
{
|
|
private readonly TaskContext _context;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the class.
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
public CategoriesController(TaskContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/category
|
|
/// <summary>
|
|
/// Retrieve all Categories in DB.
|
|
/// Result can be filtered using the Params.
|
|
/// </summary>
|
|
/// <param name="title">string value</param>
|
|
/// <param name="projectid">ID of created Project</param>
|
|
/// <param name="color">string value</param>
|
|
/// <returns>JSON list of all matching Categories</returns>
|
|
#nullable enable
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<ScrumCategory>>> GetCategory([FromQuery]string? title, [FromQuery]int? projectid, [FromQuery] string? color)
|
|
{
|
|
var filtered = _context.Categories.AsQueryable();
|
|
|
|
if (title != null)
|
|
{
|
|
filtered = filtered.Where<ScrumCategory>(c => c.Title.Contains(title));
|
|
}
|
|
if (projectid != null)
|
|
{
|
|
filtered = filtered.Where<ScrumCategory>(c => c.ProjectId == projectid);
|
|
}
|
|
if (color != null)
|
|
{
|
|
filtered = filtered.Where<ScrumCategory>(c => c.Color == color);
|
|
|
|
}
|
|
|
|
return await filtered.ToListAsync();
|
|
}
|
|
#nullable disable
|
|
|
|
// GET: api/category/1
|
|
/// <summary>
|
|
/// Retrieve the Category by it's ID.
|
|
/// </summary>
|
|
/// <param name="id">ID of searched Category</param>
|
|
/// <returns>JSON object</returns>
|
|
[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
|
|
/// <summary>
|
|
/// Update the Category identified by it's ID.
|
|
/// </summary>
|
|
/// <param name="id">to edit Category's ID</param>
|
|
/// <param name="category">modified Category</param>
|
|
/// <returns>the proper status code</returns>
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutCategory(int id, ScrumCategory category)
|
|
{
|
|
// The category's ID must not be changed
|
|
if (id != category.Id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
// Save the changed category in the context
|
|
_context.Entry(category).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
// Apply the changes to the database
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
// If the category does not exist, return status code 404
|
|
if (!CategoryExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/category
|
|
/// <summary>
|
|
/// Create a new Category.
|
|
/// </summary>
|
|
/// <param name="category">new Category</param>
|
|
/// <returns>the proper status code</returns>
|
|
[HttpPost]
|
|
public async Task<ActionResult<ScrumUserstory>> PostCategory(ScrumCategory category)
|
|
{
|
|
_context.Categories.Add(category);
|
|
await _context.SaveChangesAsync();
|
|
|
|
// The new user story has been created and can be called up using the GetUserstory method
|
|
return CreatedAtAction("GetCategory", new { id = category.Id }, category);
|
|
}
|
|
|
|
// DELETE: api/category/1
|
|
/// <summary>
|
|
/// Delete a Category identified by it's ID.
|
|
/// </summary>
|
|
/// <param name="id">to delete Category's ID</param>
|
|
/// <returns>the proper status code</returns>
|
|
[HttpDelete("{id}")]
|
|
public async Task<ActionResult<ScrumCategory>> DeleteCategory(int id)
|
|
{
|
|
var category = await _context.Categories.FindAsync(id);
|
|
if (category == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
// Remove the category from the context
|
|
_context.Categories.Remove(category);
|
|
// Save the changes in the database
|
|
await _context.SaveChangesAsync();
|
|
|
|
return category;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether a category with the specified ID already exists.
|
|
/// </summary>
|
|
private bool CategoryExists(int id)
|
|
{
|
|
return _context.Categories.Any(e => e.Id == id);
|
|
}
|
|
}
|
|
} |