171 lines
5.3 KiB
C#
171 lines
5.3 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 Sprints class includes all API's related to the Sprints.
|
|
/// </summary>
|
|
[Route("[controller]")]
|
|
[ApiController]
|
|
public class SprintsController : ControllerBase
|
|
{
|
|
private readonly TaskContext _context;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the class.
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
public SprintsController(TaskContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/sprint
|
|
/// <summary>
|
|
/// Retrieve all Sprints in DB.
|
|
/// </summary>
|
|
/// <param name="title"></param>
|
|
/// <param name="projectid"></param>
|
|
/// <param name="startDate"></param>
|
|
/// <param name="endDate"></param>
|
|
/// <returns></returns>
|
|
#nullable enable
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<ScrumSprint>>> GetSprint([FromQuery]string? title, [FromQuery]int? projectid, [FromQuery]DateTime? startDate, [FromQuery]DateTime? endDate)
|
|
{
|
|
var filtered = _context.Sprints.AsQueryable();
|
|
|
|
if (title != null)
|
|
{
|
|
filtered = filtered.Where<ScrumSprint>(s => s.Title.Contains(title));
|
|
}
|
|
if (projectid != null)
|
|
{
|
|
filtered = filtered.Where<ScrumSprint>(s => s.ProjectId == projectid);
|
|
}
|
|
if (startDate != null)
|
|
{
|
|
filtered = filtered.Where<ScrumSprint>(s => s.StartDate == startDate);
|
|
}
|
|
if (endDate != null)
|
|
{
|
|
filtered = filtered.Where<ScrumSprint>(s => s.EndDate == endDate);
|
|
}
|
|
|
|
return await filtered.ToListAsync();
|
|
}
|
|
#nullable disable
|
|
|
|
// GET: api/sprint/5
|
|
/// <summary>
|
|
/// Retrieve the Sprint by it's ID.
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<ScrumSprint>> GetSprint(int id)
|
|
{
|
|
var sprint = await _context.Sprints.FindAsync(id);
|
|
|
|
if (sprint == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return sprint;
|
|
}
|
|
|
|
// PUT: api/sprint/5
|
|
/// <summary>
|
|
/// Update the Sprint identified by it's ID.
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="sprint"></param>
|
|
/// <returns></returns>
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutSprint(int id, ScrumSprint sprint)
|
|
{
|
|
// The sprint's ID must not be changed
|
|
if (id != sprint.Id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
// Save the changed sprint in the context
|
|
_context.Entry(sprint).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
// Apply the changes to the database
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
// If the sprint does not exist, return status code 404
|
|
if (!SprintExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/Sprint
|
|
/// <summary>
|
|
/// Create a new Sprint.
|
|
/// </summary>
|
|
/// <param name="sprint"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ActionResult<ScrumSprint>> PostSprint(ScrumSprint sprint)
|
|
{
|
|
_context.Sprints.Add(sprint);
|
|
await _context.SaveChangesAsync();
|
|
|
|
// The new sprint has been created and can be called up using the GetSprint method
|
|
return CreatedAtAction("GetSprint", new { id = sprint.Id }, sprint);
|
|
}
|
|
|
|
// DELETE: api/Sprint/5
|
|
/// <summary>
|
|
/// Delete a Sprint identified by it's ID.
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpDelete("{id}")]
|
|
public async Task<ActionResult<ScrumSprint>> DeleteSprint(int id)
|
|
{
|
|
var scrumSprint = await _context.Sprints.FindAsync(id);
|
|
if (scrumSprint == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
// Remove the sprint from the context
|
|
_context.Sprints.Remove(scrumSprint);
|
|
// Save the changes in the database
|
|
await _context.SaveChangesAsync();
|
|
|
|
return scrumSprint;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether a sprint with the specified ID already exists.
|
|
/// </summary>
|
|
private bool SprintExists(int id)
|
|
{
|
|
return _context.Sprints.Any(e => e.Id == id);
|
|
}
|
|
}
|
|
}
|