161 lines
5.2 KiB
C#
161 lines
5.2 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 Projects class includes all API's related to the Projects.
|
|
/// </summary>
|
|
[Route("[controller]")]
|
|
[ApiController]
|
|
public class ProjectsController : ControllerBase
|
|
{
|
|
private readonly TaskContext _context;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the class.
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
public ProjectsController(TaskContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/projects
|
|
/// <summary>
|
|
/// Retrieve all Projects in DB.
|
|
/// Result can be filtered using the Params.
|
|
/// </summary>
|
|
/// <param name="title">string value</param>
|
|
/// <param name="isprivate">boolean value</param>
|
|
/// <returns>JSON list of all matching Projects</returns>
|
|
#nullable enable
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<ScrumProject>>> GetProject([FromQuery]string? title, [FromQuery]bool? isprivate)
|
|
{
|
|
var filtered = _context.Projects.AsQueryable();
|
|
|
|
if (title != null)
|
|
{
|
|
filtered = filtered.Where<ScrumProject>(t => t.Title.Contains(title));
|
|
}
|
|
if (isprivate != null)
|
|
{
|
|
filtered = filtered.Where<ScrumProject>(t => t.IsPrivate == isprivate);
|
|
}
|
|
|
|
return await filtered.ToListAsync();
|
|
}
|
|
#nullable disable
|
|
|
|
// GET: api/projects/5
|
|
/// <summary>
|
|
/// Retrieve the Project by it's ID.
|
|
/// </summary>
|
|
/// <param name="id">ID of searched Project</param>
|
|
/// <returns>JSON object</returns>
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<ScrumProject>> GetProjects(int id)
|
|
{
|
|
var project = await _context.Projects.FindAsync(id);
|
|
|
|
if (project == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return project;
|
|
}
|
|
|
|
// PUT: api/Project/5
|
|
/// <summary>
|
|
/// Update the Project identified by it's ID.
|
|
/// </summary>
|
|
/// <param name="id">to edit Project's ID</param>
|
|
/// <param name="projects">modified Project</param>
|
|
/// <returns>the proper status code</returns>
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutProject(int id, ScrumProject projects)
|
|
{
|
|
// The project's ID must not be changed
|
|
if (id != projects.Id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
// Save the changed project in the context
|
|
_context.Entry(projects).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
// Apply the changes to the database
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
// If the project does not exist, return status code 404
|
|
if (!ProjectExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/Project
|
|
/// <summary>
|
|
/// Create a new Project.
|
|
/// </summary>
|
|
/// <param name="projects">new Project</param>
|
|
/// <returns>the proper status code</returns>
|
|
[HttpPost]
|
|
public async Task<ActionResult<ScrumProject>> PostProject(ScrumProject projects)
|
|
{
|
|
_context.Projects.Add(projects);
|
|
await _context.SaveChangesAsync();
|
|
|
|
// The new project has been created and can be called up using the GetProject method
|
|
return CreatedAtAction("GetProject", new { id = projects.Id }, projects);
|
|
}
|
|
|
|
// DELETE: api/Project/5
|
|
/// <summary>
|
|
/// Delete a Project identified by it's ID.
|
|
/// </summary>
|
|
/// <param name="id">to delete Project's ID</param>
|
|
/// <returns>the proper status code</returns>
|
|
[HttpDelete("{id}")]
|
|
public async Task<ActionResult<ScrumProject>> DeleteProject(int id)
|
|
{
|
|
var scrumProject = await _context.Projects.FindAsync(id);
|
|
if (scrumProject == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
// Remove the project from the context
|
|
_context.Projects.Remove(scrumProject);
|
|
// Save the changes in the database
|
|
await _context.SaveChangesAsync();
|
|
|
|
return scrumProject;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether a project with the specified ID already exists.
|
|
/// </summary>
|
|
private bool ProjectExists(int id)
|
|
{
|
|
return _context.Projects.Any(e => e.Id == id);
|
|
}
|
|
}
|
|
} |