using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace ScrumTaskboard.Controllers
{
///
/// This is a controller of Projects class includes all API's related to the Projects.
///
[Route("[controller]")]
[ApiController]
public class ProjectsController : ControllerBase
{
private readonly TaskContext _context;
///
/// Initializes a new instance of the class.
///
///
public ProjectsController(TaskContext context)
{
_context = context;
}
// GET: api/projects
///
/// Retrieve all Projects in DB.
/// Result can be filtered using the Params.
///
/// string value
/// boolean value
/// JSON list of all matching Projects
#nullable enable
[HttpGet]
public async Task>> GetProject([FromQuery]string? title, [FromQuery]bool? isprivate)
{
var filtered = _context.Projects.AsQueryable();
if (title != null)
{
filtered = filtered.Where(t => t.Title.Contains(title));
}
if (isprivate != null)
{
filtered = filtered.Where(t => t.IsPrivate == isprivate);
}
return await filtered.ToListAsync();
}
#nullable disable
// GET: api/projects/5
///
/// Retrieve the Project by it's ID.
///
/// ID of searched Project
/// JSON object
[HttpGet("{id}")]
public async Task> GetProjects(int id)
{
var project = await _context.Projects.FindAsync(id);
if (project == null)
{
return NotFound();
}
return project;
}
// PUT: api/Project/5
///
/// Update the Project identified by it's ID.
///
/// to edit Project's ID
/// modified Project
/// the proper status code
[HttpPut("{id}")]
public async Task 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
///
/// Create a new Project.
///
/// new Project
/// the proper status code
[HttpPost]
public async Task> 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
///
/// Delete a Project identified by it's ID.
///
/// to delete Project's ID
/// the proper status code
[HttpDelete("{id}")]
public async Task> 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;
}
///
/// Checks whether a project with the specified ID already exists.
///
private bool ProjectExists(int id)
{
return _context.Projects.Any(e => e.Id == id);
}
}
}