srumboard_backend/ScrumTaskboard/Controllers/ProjectsController.cs

161 lines
5.1 KiB
C#
Raw Normal View History

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