srumboard_backend/ScrumTaskboard/Controllers/UserstoriesController.cs

187 lines
6.5 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 Userstories class includes all API's related to the Userstories.
/// </summary>
[Route("[controller]")]
[ApiController]
public class UserstoriesController : ControllerBase
{
private readonly TaskContext _context;
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
/// <param name="context"></param>
public UserstoriesController(TaskContext context)
{
_context = context;
}
// GET: api/userstories
/// <summary>
/// Retrieve all Userstories in DB.
/// Result can be filtered using the Params.
/// </summary>
/// <param name="title">string value</param>
/// <param name="statusid">ID of created Status</param>
/// <param name="categoryid">ID of created Category</param>
/// <param name="createdbyid">ID of the Author</param>
/// <param name="projectid">ID of created Project</param>
/// <param name="sprintid">ID of created Project</param>
/// <param name="priority">enum value</param>
/// <returns>JSON list of all matching Userstories</returns>
#nullable enable
[HttpGet]
public async Task<ActionResult<IEnumerable<ScrumUserstory>>> GetUserstory([FromQuery]string? title, [FromQuery]int? statusid, [FromQuery]int? categoryid, [FromQuery]int? createdbyid, [FromQuery]int? projectid, [FromQuery]int? sprintid, [FromQuery]ScrumPrio? priority)
{
var filtered = _context.Userstories.AsQueryable();
if (title != null)
{
filtered = filtered.Where<ScrumUserstory>(t => t.Title.Contains(title));
}
if (statusid != null)
{
filtered = filtered.Where<ScrumUserstory>(t => t.StatusId == statusid);
}
if (categoryid != null)
{
filtered = filtered.Where<ScrumUserstory>(t => t.CategoryId == categoryid);
}
if (createdbyid != null)
{
filtered = filtered.Where<ScrumUserstory>(t => t.CreatedById == createdbyid);
}
if (projectid != null)
{
filtered = filtered.Where<ScrumUserstory>(t => t.ProjectId == projectid);
}
if (sprintid != null)
{
filtered = filtered.Where<ScrumUserstory>(t => t.SprintId == sprintid);
}
if (priority != null)
{
filtered = filtered.Where<ScrumUserstory>(t => t.Priority == priority);
}
return await filtered.ToListAsync();
}
#nullable disable
// GET: api/userstories/1
/// <summary>
/// Retrieve the Userstory by it's ID.
/// </summary>
/// <param name="id">ID of searched Userstory</param>
/// <returns>JSON object</returns>
[HttpGet("{id}")]
public async Task<ActionResult<ScrumUserstory>> GetUserstory(int id)
{
var userstory = await _context.Userstories.FindAsync(id);
if (userstory == null)
{
return NotFound();
}
return userstory;
}
// PUT: api/userstories/1
/// <summary>
/// Update the Userstory identified by it's ID.
/// </summary>
/// <param name="id">to edit Userstory's ID</param>
/// <param name="userstory">modified Userstory</param>
/// <returns>the proper status code</returns>
[HttpPut("{id}")]
public async Task<IActionResult> PutUserstory(int id, ScrumUserstory userstory)
{
// The userstory's ID must not be changed
if (id != userstory.Id)
{
return BadRequest();
}
// Save the changed userstory in the context
_context.Entry(userstory).State = EntityState.Modified;
try
{
// Apply the changes to the database
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
// If the userstory does not exist, return status code 404
if (!UserstoryExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/userstories
/// <summary>
/// Create a new Userstory.
/// </summary>
/// <param name="userstory">new Userstory</param>
/// <returns>the proper status code</returns>
[HttpPost]
public async Task<ActionResult<ScrumUserstory>> PostTask(ScrumUserstory userstory)
{
_context.Userstories.Add(userstory);
await _context.SaveChangesAsync();
// The new userstory has been created and can be called up using the GetUserstory method
return CreatedAtAction("GetUserstory", new { id = userstory.Id }, userstory);
}
// DELETE: api/userstories/1
/// <summary>
/// Delete a Userstory identified by it's ID.
/// </summary>
/// <param name="id">to delete Userstory's ID</param>
/// <returns>the proper status code</returns>
[HttpDelete("{id}")]
public async Task<ActionResult<ScrumUserstory>> DeleteUserstory(int id)
{
var userstory = await _context.Userstories.FindAsync(id);
if (userstory == null)
{
return NotFound();
}
// Remove the userstory from the context
_context.Userstories.Remove(userstory);
// Save the changes in the database
await _context.SaveChangesAsync();
return userstory;
}
/// <summary>
/// Checks whether a userstory with the specified ID already exists.
/// </summary>
private bool UserstoryExists(int id)
{
return _context.Userstories.Any(e => e.Id == id);
}
}
}