uow proj done

This commit is contained in:
Ruidy Nemausat 2020-02-17 23:46:04 +01:00
parent beeee5b3ba
commit c3d1218f3c

View file

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