diff --git a/Controllers/ProjectsController.cs b/Controllers/ProjectsController.cs index d2bff55..8091d06 100644 --- a/Controllers/ProjectsController.cs +++ b/Controllers/ProjectsController.cs @@ -1,7 +1,4 @@ -using System; using System.Collections.Generic; -using System.Linq; -using System.Net.Mime; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -9,7 +6,6 @@ using Microsoft.EntityFrameworkCore; using TicketManager.Data; using TicketManager.Models; - namespace TicketManager.Controllers { [Produces("application/json")] @@ -17,210 +13,212 @@ namespace TicketManager.Controllers [ApiController] public class ProjectsController : ControllerBase { - private UnitOfWork _context; + private AppDbContext _dbContext; + public ProjectsController(AppDbContext context) + { + _dbContext = context; + } /// - /// Returns all existing projects. + /// Returns all projects stored in the database. /// /// /// Sample request: /// - /// GET: api/Projects + /// GET: api/v1/Projects /// /// - /// Returns all existing projects + /// Returns a list of projects [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] public async Task> GetProjects() { + UnitOfWork _context = new UnitOfWork(_dbContext); return await _context.Projects.List(); } /// - /// Returns a specific project. + /// Locate a specific project stored in the database by its Id /// /// /// Sample request: /// - /// GET: api/Projects/2 + /// GET: api/v1/Projects/2 /// /// - /// Returns a specific project + /// Returns a project object /// If the required project is null [HttpGet("{id}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> GetProject(int id) { + UnitOfWork _context = new UnitOfWork(_dbContext); Project project = await _context.Projects.Get(id); if (project == null) { return NotFound(); } return project; } - // /// - // /// Updates a specific project. - // /// - // /// - // /// Sample request: - // /// - // /// PUT: api/Projects/3 - // /// { - // /// "id": "357727fd-5262-4522-b8a3-38271d43de84", - // /// "firstName": "Thomas", - // /// "lastName": "Price", - // /// "presentation": "New Team?!", - // /// "email": "tp@mail.com", - // /// "phone": "0198237645" - // /// } - // /// - // /// - // /// Returns the modified project - // /// Request was succesful but no content is changed - // /// If the required project is null - // [HttpPut("{id}")] - // [ProducesResponseType(StatusCodes.Status200OK)] - // [ProducesResponseType(StatusCodes.Status204NoContent)] - // [ProducesResponseType(StatusCodes.Status404NotFound)] - // public async Task PutProject(int id, Project project) - // { - // if (id != project.Id) { return BadRequest(); } + /// + /// Updates the specific project with Id. + /// + /// + /// Sample request: + /// + /// PUT: api/v1/Projects/3 + /// { + /// "id": "357727fd-5262-4522-b8a3-38271d43de84", + /// "firstName": "Thomas", + /// "lastName": "Price", + /// "presentation": "New Team?!", + /// "email": "tp@mail.com", + /// "phone": "0198237645" + /// } + /// + /// + /// Request was succesful but no content is changed + /// If the required project is null + [HttpPut("{id}")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task PutProject(int id, Project project) + { + UnitOfWork _context = new UnitOfWork(_dbContext); + if (id != project.Id) { return BadRequest(); } + try + { + _context.Projects.Update(project); + await _context.Complete(); + } + catch (DbUpdateConcurrencyException) + { + if (!_context.Projects.Exists(id)) { return NotFound(); } + else { throw; } + } + return NoContent(); + } - // try - // { - // await _projectRepo.Update(project); - // } - // catch (DbUpdateConcurrencyException) - // { - // if (!_projectRepo.Exists(id)) - // { - // return NotFound(); - // } - // else - // { - // throw; - // } - // } + /// + /// Creates a project. + /// + /// + /// Sample request: + /// + /// POST: api/v1/Projects/ + /// { + /// "firstName": "Thomas", + /// "lastName": "Price", + /// "presentation": "New Team?!", + /// "email": "tp@mail.com", + /// "phone": "0198237645" + /// } + /// + /// + /// Returns the created project + [HttpPost] + [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task> PostProject(Project project) + { + UnitOfWork _context = new UnitOfWork(_dbContext); + if (!ModelState.IsValid) { return BadRequest(); } + _context.Projects.Add(project); + await _context.Complete(); + return CreatedAtAction("GetProject", new { id = project.Id }, project); + } - // return NoContent(); - // } + /// + /// Deletes the project identified by its Id + /// + /// + /// Sample request: + /// + /// DELETE: api/v1/Projects/5 + /// + /// + /// Returns the deleted project + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [HttpDelete("{id}")] + public async Task DeleteProject(int id) + { + UnitOfWork _context = new UnitOfWork(_dbContext); + var project = await _context.Projects.Get(id); + if (project == null) + { + return NotFound(); + } + _context.Projects.Delete(project); + await _context.Complete(); + return Ok(); + } - // /// - // /// Creates a project. - // /// - // /// - // /// Sample request: - // /// - // /// POST: api/Projects/ - // /// { - // /// "firstName": "Thomas", - // /// "lastName": "Price", - // /// "presentation": "New Team?!", - // /// "email": "tp@mail.com", - // /// "phone": "0198237645" - // /// } - // /// - // /// - // /// Returns the created project - // [HttpPost] - // [ProducesResponseType(StatusCodes.Status201Created)] - // [ProducesResponseType(StatusCodes.Status404NotFound)] - // public async Task> PostProject(Project project) - // { - // if (!ModelState.IsValid) { return BadRequest(); } - // await _projectRepo.AddAsync(project); + /// + /// Gets a project members. + /// + /// + /// Sample request: + /// + /// GET: api/v1/Projects/5/Members + /// + /// + /// Returns the project members as a list of users. + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [HttpGet("{id}/members")] + public async Task>> GetProjectMembers(int id) + { + UnitOfWork _context = new UnitOfWork(_dbContext); + Project project = await _context.Projects.Get(id); + if (project == null) + { return NotFound(); } + return project.GetMembers(); + } - // return CreatedAtAction("GetProject", new { id = project.Id }, project); - // } - - - - - // /// - // /// Deletes a project. - // /// - // /// - // /// Sample request: - // /// - // /// DELETE: api/Projects/5 - // /// - // /// - // /// Returns the deleted project - // [ProducesResponseType(StatusCodes.Status200OK)] - // [ProducesResponseType(StatusCodes.Status404NotFound)] - // [HttpDelete("{id}")] - // public async Task> DeleteProject(int id) - // { - // var project = await _projectRepo.GetByIdAsync(id); - // if (project == null) - // { - // return NotFound(); - // } - // await _projectRepo.DeleteAsync(id); - // return project; - // } - - // /// - // /// Gets a project members. - // /// - // /// - // /// Sample request: - // /// - // /// GET: api/Projects/5/Members - // /// - // /// - // /// Returns the project members - // [ProducesResponseType(StatusCodes.Status200OK)] - // [ProducesResponseType(StatusCodes.Status404NotFound)] - // [HttpGet("{id}/members")] - // public async Task>> GetProjectMembers(int id) - // { - // Project project = await _projectRepo.GetByIdAsync(id); - // if (project == null) - // { return NotFound(); } - // return project.GetMembers(); - // } - - // /// - // /// Updates a project members. - // /// - // /// - // /// Sample request: - // /// - // /// PUT: api/Projects/5/Members - // /// { - // /// "id": "357727fd-5262-4522-b8a3-38271d43de84", - // /// "firstName": "Thomas", - // /// "lastName": "Price", - // /// "presentation": "New Team?!", - // /// "email": "tp@mail.com", - // /// "phone": "0198237645" - // /// } - // /// - // /// No content - // [ProducesResponseType(StatusCodes.Status204NoContent)] - // [ProducesResponseType(StatusCodes.Status404NotFound)] - // [HttpPut("{id}/members")] - // public async Task> SetProjectMembers(int id, List projectMembers) - // { - // Project project = await _projectRepo.GetByIdAsync(id); - // if (project == null) - // { - // return NotFound(); - // } - // project.SetMembers(projectMembers); - // try - // { - // await _projectRepo.UpdateAsync(project); - // } - // catch (DbUpdateException /* ex */) - // { - // //Log the error (uncomment ex variable name and write a log.) - // ModelState.AddModelError("", "Unable to save changes. " + - // "Try again, and if the problem persists, " + - // "see your system administrator."); - // } - // return NoContent(); - // } + /// + /// Updates a project members. + /// + /// + /// Sample request: + /// + /// PUT: api/v1/Projects/5/Members + /// { + /// "id": "357727fd-5262-4522-b8a3-38271d43de84", + /// "firstName": "Thomas", + /// "lastName": "Price", + /// "presentation": "New Team?!", + /// "email": "tp@mail.com", + /// "phone": "0198237645" + /// } + /// + /// No content + /// Not Found + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [HttpPut("{id}/members")] + public async Task> SetProjectMembers(int id, List projectMembers) + { + UnitOfWork _context = new UnitOfWork(_dbContext); + Project project = await _context.Projects.Get(id); + if (project == null) + { + return NotFound(); + } + project.SetMembers(projectMembers); + try + { + _context.Projects.Update(project); + await _context.Complete(); + } + catch (DbUpdateException /* ex */) + { + //Log the error (uncomment ex variable name and write a log.) + ModelState.AddModelError("", "Unable to save changes. " + + "Try again, and if the problem persists, " + + "see your system administrator."); + } + return NoContent(); + } // // /// // // /// Assign a user to a project. @@ -228,7 +226,7 @@ namespace TicketManager.Controllers // // /// // // /// Sample request: // // /// - // // /// POST: api/Projects/addmembers + // // /// POST: api/v1/Projects/addmembers // // /// [{ // // /// "id": "357727fd-5262-4522-b8a3-38271d43de84", // // /// "firstName": "Thomas", @@ -271,7 +269,7 @@ namespace TicketManager.Controllers // // /// // // /// Sample request: // // /// - // // /// PUT: api/Projects/removemembers + // // /// PUT: api/v1/Projects/removemembers // // /// [{ // // /// "id": "357727fd-5262-4522-b8a3-38271d43de84", // // /// "firstName": "Thomas", @@ -303,9 +301,5 @@ namespace TicketManager.Controllers // // } // // return NoContent(); // // } - - - - } }