2020-06-18 13:10:42 +00:00
|
|
|
|
using System;
|
2020-07-08 16:10:58 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace ScrumTaskboard.Controllers
|
|
|
|
|
{
|
2020-07-08 11:42:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is a controller of Sprints class includes all API's related to the Sprints.
|
2020-07-14 16:03:33 +00:00
|
|
|
|
/// </summary>
|
2020-07-08 16:10:58 +00:00
|
|
|
|
[Route("[controller]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class SprintsController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly TaskContext _context;
|
|
|
|
|
|
2020-07-08 11:42:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the class.
|
|
|
|
|
/// </summary>
|
2020-07-14 16:03:33 +00:00
|
|
|
|
/// <param name="context"></param>
|
2020-07-08 16:10:58 +00:00
|
|
|
|
public SprintsController(TaskContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
2020-06-23 15:43:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: api/sprint
|
2020-07-08 16:10:58 +00:00
|
|
|
|
#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);
|
|
|
|
|
}
|
2020-06-18 14:38:14 +00:00
|
|
|
|
if (startDate != null)
|
2020-06-18 13:10:42 +00:00
|
|
|
|
{
|
2020-07-08 16:10:58 +00:00
|
|
|
|
filtered = filtered.Where<ScrumSprint>(s => s.StartDate == startDate);
|
2020-06-18 13:10:42 +00:00
|
|
|
|
}
|
2020-06-18 14:38:14 +00:00
|
|
|
|
if (endDate != null)
|
2020-07-08 16:10:58 +00:00
|
|
|
|
{
|
|
|
|
|
filtered = filtered.Where<ScrumSprint>(s => s.EndDate == endDate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await filtered.ToListAsync();
|
2020-06-18 13:10:42 +00:00
|
|
|
|
}
|
2020-06-23 15:43:17 +00:00
|
|
|
|
#nullable disable
|
2020-06-18 13:10:42 +00:00
|
|
|
|
|
|
|
|
|
// GET: api/sprint/5
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
public async Task<ActionResult<ScrumSprint>> GetSprint(int id)
|
|
|
|
|
{
|
2020-07-08 16:10:58 +00:00
|
|
|
|
var sprint = await _context.Sprints.FindAsync(id);
|
2020-06-18 13:10:42 +00:00
|
|
|
|
|
2020-07-08 16:10:58 +00:00
|
|
|
|
if (sprint == null)
|
2020-06-18 13:10:42 +00:00
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 16:10:58 +00:00
|
|
|
|
return sprint;
|
2020-06-18 13:10:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT: api/sprint/5
|
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
public async Task<IActionResult> PutSprint(int id, ScrumSprint sprint)
|
|
|
|
|
{
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// The sprint's ID must not be changed
|
2020-07-14 16:10:57 +00:00
|
|
|
|
if (id != sprint.Id)
|
2020-06-18 13:10:42 +00:00
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// Save the changed sprint in the context
|
2020-06-18 13:10:42 +00:00
|
|
|
|
_context.Entry(sprint).State = EntityState.Modified;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// Apply the changes to the database
|
2020-06-18 13:10:42 +00:00
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (DbUpdateConcurrencyException)
|
|
|
|
|
{
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// If the sprint does not exist, return status code 404
|
2020-06-18 13:10:42 +00:00
|
|
|
|
if (!SprintExists(id))
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: api/Sprint
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<ActionResult<ScrumSprint>> PostSprint(ScrumSprint sprint)
|
|
|
|
|
{
|
|
|
|
|
_context.Sprints.Add(sprint);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// The new sprint has been created and can be called up using the GetSprint method
|
2020-07-14 16:10:57 +00:00
|
|
|
|
return CreatedAtAction("GetSprint", new { id = sprint.Id }, sprint);
|
2020-06-18 13:10:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DELETE: api/Sprint/5
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
public async Task<ActionResult<ScrumSprint>> DeleteSprint(int id)
|
|
|
|
|
{
|
|
|
|
|
var scrumSprint = await _context.Sprints.FindAsync(id);
|
|
|
|
|
if (scrumSprint == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// Remove the sprint from the context
|
2020-06-18 13:10:42 +00:00
|
|
|
|
_context.Sprints.Remove(scrumSprint);
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// Save the changes in the database
|
2020-06-18 13:10:42 +00:00
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return scrumSprint;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 16:10:58 +00:00
|
|
|
|
/// <summary>
|
2020-07-14 16:03:33 +00:00
|
|
|
|
/// Checks whether a sprint with the specified ID already exists.
|
2020-07-08 16:10:58 +00:00
|
|
|
|
/// </summary>
|
2020-06-18 13:10:42 +00:00
|
|
|
|
private bool SprintExists(int id)
|
|
|
|
|
{
|
2020-07-08 16:10:58 +00:00
|
|
|
|
return _context.Sprints.Any(e => e.Id == id);
|
2020-06-18 13:10:42 +00:00
|
|
|
|
}
|
2020-07-08 16:10:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|