mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-12 03:36:39 +00:00
Unit of Work removed. Save done in the Repositories. Proj, Tickets & AppUsers Repo done.
This commit is contained in:
parent
c3d1218f3c
commit
d5d5d58866
13 changed files with 163 additions and 451 deletions
|
|
@ -14,31 +14,29 @@ namespace TicketManager.Controllers
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class UsersController : ControllerBase
|
public class UsersController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly AppDbContext _context;
|
private readonly IAppUserRepository _users;
|
||||||
|
|
||||||
public UsersController(AppDbContext context)
|
public UsersController(IAppUserRepository users)
|
||||||
{
|
{
|
||||||
_context = context;
|
_users = users;
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: api/Users
|
// GET: api/Users
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<IEnumerable<AppUser>>> GetUsers()
|
public async Task<IEnumerable<AppUser>> GetUsers()
|
||||||
{
|
{
|
||||||
return await getAllAppUsersAsync();
|
return await _users.List();
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: api/Users/5
|
// GET: api/Users/5
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public async Task<ActionResult<AppUser>> GetUser(Guid id)
|
public async Task<ActionResult<AppUser>> GetUser(Guid id)
|
||||||
{
|
{
|
||||||
var user = await getAppUserByIdAsync(id);
|
var user = await _users.GetUser(id);
|
||||||
|
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,16 +50,13 @@ namespace TicketManager.Controllers
|
||||||
{
|
{
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
_context.Entry(user).State = EntityState.Modified;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _context.SaveChangesAsync();
|
await _users.Update(user);
|
||||||
}
|
}
|
||||||
catch (DbUpdateConcurrencyException)
|
catch (DbUpdateConcurrencyException)
|
||||||
{
|
{
|
||||||
if (!UserExists(id))
|
if (!_users.Exists(id))
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
@ -70,7 +65,6 @@ namespace TicketManager.Controllers
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NoContent();
|
return NoContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,32 +74,27 @@ namespace TicketManager.Controllers
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<ActionResult<AppUser>> PostUser(AppUser user)
|
public async Task<ActionResult<AppUser>> PostUser(AppUser user)
|
||||||
{
|
{
|
||||||
_context.AppUsers.Add(user);
|
await _users.Add(user);
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
return CreatedAtAction("GetUser", new { id = user.Id }, user);
|
return CreatedAtAction("GetUser", new { id = user.Id }, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
// DELETE: api/Users/5
|
// DELETE: api/Users/5
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
public async Task<ActionResult<AppUser>> DeleteUser(int id)
|
public async Task<ActionResult<AppUser>> DeleteUser(Guid id)
|
||||||
{
|
{
|
||||||
var user = await _context.AppUsers.FindAsync(id);
|
var user = await _users.GetUser(id);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
await _users.Delete(user);
|
||||||
_context.AppUsers.Remove(user);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{id}/projects")]
|
[HttpGet("{id}/projects")]
|
||||||
public async Task<ActionResult<IEnumerable<Project>>> GetAppUserProjects(Guid id)
|
public async Task<ActionResult<IEnumerable<Project>>> GetAppUserProjects(Guid id)
|
||||||
{
|
{
|
||||||
AppUser user = await getAppUserByIdAsync(id);
|
AppUser user = await _users.GetUser(id);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
|
|
@ -116,36 +105,12 @@ namespace TicketManager.Controllers
|
||||||
[HttpGet("{id}/tickets/")]
|
[HttpGet("{id}/tickets/")]
|
||||||
public async Task<ActionResult<IEnumerable<Ticket>>> GetAppUserTickets(Guid id)
|
public async Task<ActionResult<IEnumerable<Ticket>>> GetAppUserTickets(Guid id)
|
||||||
{
|
{
|
||||||
AppUser user = await getAppUserByIdAsync(id);
|
AppUser user = await _users.GetUser(id);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
return user.GetTickets();
|
return user.GetTickets();
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool UserExists(Guid id)
|
|
||||||
{
|
|
||||||
return _context.AppUsers.Any(e => e.Id == id);
|
|
||||||
}
|
|
||||||
|
|
||||||
private IQueryable<AppUser> appUserQuery()
|
|
||||||
{
|
|
||||||
return _context.AppUsers
|
|
||||||
.Include(p => p.Assignments)
|
|
||||||
.ThenInclude(a => a.Project)
|
|
||||||
.ThenInclude(p => p.Tickets)
|
|
||||||
.Include(p => p.Edits);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<ActionResult<IEnumerable<AppUser>>> getAllAppUsersAsync()
|
|
||||||
{
|
|
||||||
return await appUserQuery().ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<AppUser> getAppUserByIdAsync(Guid id)
|
|
||||||
{
|
|
||||||
return await appUserQuery().FirstOrDefaultAsync(a => a.Id == id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,10 +13,10 @@ namespace TicketManager.Controllers
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class ProjectsController : ControllerBase
|
public class ProjectsController : ControllerBase
|
||||||
{
|
{
|
||||||
private AppDbContext _dbContext;
|
private IProjectRepository _projects;
|
||||||
public ProjectsController(AppDbContext context)
|
public ProjectsController(IProjectRepository context)
|
||||||
{
|
{
|
||||||
_dbContext = context;
|
_projects = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -33,8 +33,7 @@ namespace TicketManager.Controllers
|
||||||
[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 _projects.List();
|
||||||
return await _context.Projects.List();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -53,8 +52,7 @@ namespace TicketManager.Controllers
|
||||||
[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 _projects.Get(id);
|
||||||
Project project = await _context.Projects.Get(id);
|
|
||||||
if (project == null) { return NotFound(); }
|
if (project == null) { return NotFound(); }
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
|
|
@ -83,16 +81,14 @@ namespace TicketManager.Controllers
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
public async Task<IActionResult> PutProject(int id, Project project)
|
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
|
try
|
||||||
{
|
{
|
||||||
_context.Projects.Update(project);
|
await _projects.Update(project);
|
||||||
await _context.Complete();
|
|
||||||
}
|
}
|
||||||
catch (DbUpdateConcurrencyException)
|
catch (DbUpdateConcurrencyException)
|
||||||
{
|
{
|
||||||
if (!_context.Projects.Exists(id)) { return NotFound(); }
|
if (!_projects.Exists(id)) { return NotFound(); }
|
||||||
else { throw; }
|
else { throw; }
|
||||||
}
|
}
|
||||||
return NoContent();
|
return NoContent();
|
||||||
|
|
@ -120,10 +116,8 @@ namespace TicketManager.Controllers
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
public async Task<ActionResult<Project>> PostProject(Project project)
|
public async Task<ActionResult<Project>> PostProject(Project project)
|
||||||
{
|
{
|
||||||
UnitOfWork _context = new UnitOfWork(_dbContext);
|
|
||||||
if (!ModelState.IsValid) { return BadRequest(); }
|
if (!ModelState.IsValid) { return BadRequest(); }
|
||||||
_context.Projects.Add(project);
|
await _projects.Add(project);
|
||||||
await _context.Complete();
|
|
||||||
return CreatedAtAction("GetProject", new { id = project.Id }, project);
|
return CreatedAtAction("GetProject", new { id = project.Id }, project);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -142,14 +136,12 @@ namespace TicketManager.Controllers
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
public async Task<IActionResult> DeleteProject(int id)
|
public async Task<IActionResult> DeleteProject(int id)
|
||||||
{
|
{
|
||||||
UnitOfWork _context = new UnitOfWork(_dbContext);
|
var project = await _projects.Get(id);
|
||||||
var project = await _context.Projects.Get(id);
|
|
||||||
if (project == null)
|
if (project == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
_context.Projects.Delete(project);
|
await _projects.Delete(project);
|
||||||
await _context.Complete();
|
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -168,8 +160,7 @@ namespace TicketManager.Controllers
|
||||||
[HttpGet("{id}/members")]
|
[HttpGet("{id}/members")]
|
||||||
public async Task<ActionResult<List<AppUser>>> GetProjectMembers(int id)
|
public async Task<ActionResult<List<AppUser>>> GetProjectMembers(int id)
|
||||||
{
|
{
|
||||||
UnitOfWork _context = new UnitOfWork(_dbContext);
|
var project = await _projects.Get(id);
|
||||||
Project project = await _context.Projects.Get(id);
|
|
||||||
if (project == null)
|
if (project == null)
|
||||||
{ return NotFound(); }
|
{ return NotFound(); }
|
||||||
return project.GetMembers();
|
return project.GetMembers();
|
||||||
|
|
@ -198,8 +189,7 @@ namespace TicketManager.Controllers
|
||||||
[HttpPut("{id}/members")]
|
[HttpPut("{id}/members")]
|
||||||
public async Task<ActionResult<Project>> SetProjectMembers(int id, List<AppUser> projectMembers)
|
public async Task<ActionResult<Project>> SetProjectMembers(int id, List<AppUser> projectMembers)
|
||||||
{
|
{
|
||||||
UnitOfWork _context = new UnitOfWork(_dbContext);
|
Project project = await _projects.Get(id);
|
||||||
Project project = await _context.Projects.Get(id);
|
|
||||||
if (project == null)
|
if (project == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
|
|
@ -207,8 +197,7 @@ namespace TicketManager.Controllers
|
||||||
project.SetMembers(projectMembers);
|
project.SetMembers(projectMembers);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_context.Projects.Update(project);
|
await _projects.Update(project);
|
||||||
await _context.Complete();
|
|
||||||
}
|
}
|
||||||
catch (DbUpdateException /* ex */)
|
catch (DbUpdateException /* ex */)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,317 +0,0 @@
|
||||||
// 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;
|
|
||||||
// using Microsoft.EntityFrameworkCore;
|
|
||||||
// using TicketManager.Data;
|
|
||||||
// using TicketManager.Models;
|
|
||||||
|
|
||||||
|
|
||||||
// namespace TicketManager.Controllers
|
|
||||||
// {
|
|
||||||
// [Produces("application/json")]
|
|
||||||
// [Route("api/v1/[controller]")]
|
|
||||||
// [ApiController]
|
|
||||||
// public class ProjectsController : ControllerBase
|
|
||||||
// {
|
|
||||||
// private readonly IProjectRepository _projectRepo;
|
|
||||||
|
|
||||||
// public ProjectsController(IProjectRepository projectRepo)
|
|
||||||
// {
|
|
||||||
// _projectRepo = projectRepo;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /// <summary>
|
|
||||||
// /// Returns all existing projects.
|
|
||||||
// /// </summary>
|
|
||||||
// /// <remarks>
|
|
||||||
// /// Sample request:
|
|
||||||
// ///
|
|
||||||
// /// GET: api/Projects
|
|
||||||
// ///
|
|
||||||
// /// </remarks>
|
|
||||||
// /// <response code="200">Returns all existing projects</response>
|
|
||||||
// [HttpGet]
|
|
||||||
// [ProducesResponseType(StatusCodes.Status200OK)]
|
|
||||||
// public async Task<IEnumerable<Project>> GetProjects()
|
|
||||||
// {
|
|
||||||
// return await _projectRepo.ListAsync();
|
|
||||||
// // GetAllProjectsAsync();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /// <summary>
|
|
||||||
// /// Returns a specific project.
|
|
||||||
// /// </summary>
|
|
||||||
// /// <remarks>
|
|
||||||
// /// Sample request:
|
|
||||||
// ///
|
|
||||||
// /// GET: api/Projects/2
|
|
||||||
// ///
|
|
||||||
// /// </remarks>
|
|
||||||
// /// <response code="200">Returns a specific project</response>
|
|
||||||
// /// <response code="404">If the required project is null</response>
|
|
||||||
// [HttpGet("{id}")]
|
|
||||||
// [ProducesResponseType(StatusCodes.Status200OK)]
|
|
||||||
// [ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
||||||
// public async Task<ActionResult<Project>> GetProject(int id)
|
|
||||||
// {
|
|
||||||
// Project project = await _projectRepo.GetByIdAsync(id);
|
|
||||||
// if (project == null) { return NotFound(); }
|
|
||||||
// return project;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /// <summary>
|
|
||||||
// /// Updates a specific project.
|
|
||||||
// /// </summary>
|
|
||||||
// /// <remarks>
|
|
||||||
// /// 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"
|
|
||||||
// /// }
|
|
||||||
// ///
|
|
||||||
// /// </remarks>
|
|
||||||
// /// <response code="200">Returns the modified project</response>
|
|
||||||
// /// <response code="204">Request was succesful but no content is changed</response>
|
|
||||||
// /// <response code="404">If the required project is null</response>
|
|
||||||
// [HttpPut("{id}")]
|
|
||||||
// [ProducesResponseType(StatusCodes.Status200OK)]
|
|
||||||
// [ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
||||||
// [ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
||||||
// public async Task<IActionResult> PutProject(int id, Project project)
|
|
||||||
// {
|
|
||||||
// if (id != project.Id) { return BadRequest(); }
|
|
||||||
|
|
||||||
// try
|
|
||||||
// {
|
|
||||||
// await _projectRepo.UpdateAsync(project);
|
|
||||||
// }
|
|
||||||
// catch (DbUpdateConcurrencyException)
|
|
||||||
// {
|
|
||||||
// if (!_projectRepo.Exists(id))
|
|
||||||
// {
|
|
||||||
// return NotFound();
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// throw;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return NoContent();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /// <summary>
|
|
||||||
// /// Creates a project.
|
|
||||||
// /// </summary>
|
|
||||||
// /// <remarks>
|
|
||||||
// /// Sample request:
|
|
||||||
// ///
|
|
||||||
// /// POST: api/Projects/
|
|
||||||
// /// {
|
|
||||||
// /// "firstName": "Thomas",
|
|
||||||
// /// "lastName": "Price",
|
|
||||||
// /// "presentation": "New Team?!",
|
|
||||||
// /// "email": "tp@mail.com",
|
|
||||||
// /// "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)
|
|
||||||
// {
|
|
||||||
// if (!ModelState.IsValid) { return BadRequest(); }
|
|
||||||
// await _projectRepo.AddAsync(project);
|
|
||||||
|
|
||||||
// return CreatedAtAction("GetProject", new { id = project.Id }, project);
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// /// <summary>
|
|
||||||
// /// Deletes a project.
|
|
||||||
// /// </summary>
|
|
||||||
// /// <remarks>
|
|
||||||
// /// Sample request:
|
|
||||||
// ///
|
|
||||||
// /// DELETE: api/Projects/5
|
|
||||||
// ///
|
|
||||||
// /// </remarks>
|
|
||||||
// /// <response code="200">Returns the deleted project</response>
|
|
||||||
// [ProducesResponseType(StatusCodes.Status200OK)]
|
|
||||||
// [ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
||||||
// [HttpDelete("{id}")]
|
|
||||||
// public async Task<ActionResult<Project>> DeleteProject(int id)
|
|
||||||
// {
|
|
||||||
// var project = await _projectRepo.GetByIdAsync(id);
|
|
||||||
// if (project == null)
|
|
||||||
// {
|
|
||||||
// return NotFound();
|
|
||||||
// }
|
|
||||||
// await _projectRepo.DeleteAsync(id);
|
|
||||||
// return project;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /// <summary>
|
|
||||||
// /// Gets a project members.
|
|
||||||
// /// </summary>
|
|
||||||
// /// <remarks>
|
|
||||||
// /// Sample request:
|
|
||||||
// ///
|
|
||||||
// /// GET: api/Projects/5/Members
|
|
||||||
// ///
|
|
||||||
// /// </remarks>
|
|
||||||
// /// <response code="200">Returns the project members</response>
|
|
||||||
// [ProducesResponseType(StatusCodes.Status200OK)]
|
|
||||||
// [ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
||||||
// [HttpGet("{id}/members")]
|
|
||||||
// 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>
|
|
||||||
// // /// Assign a user to a project.
|
|
||||||
// // /// </summary>
|
|
||||||
// // /// <remarks>
|
|
||||||
// // /// Sample request:
|
|
||||||
// // ///
|
|
||||||
// // /// POST: api/Projects/addmembers
|
|
||||||
// // /// [{
|
|
||||||
// // /// "id": "357727fd-5262-4522-b8a3-38271d43de84",
|
|
||||||
// // /// "firstName": "Thomas",
|
|
||||||
// // /// "lastName": "Price",
|
|
||||||
// // /// "presentation": "New Team?!",
|
|
||||||
// // /// "email": "tp@mail.com",
|
|
||||||
// // /// "phone": "0198237645"
|
|
||||||
// // /// }]
|
|
||||||
// // ///
|
|
||||||
// // /// </remarks>
|
|
||||||
// // /// <response code="204">Returns the created project</response>
|
|
||||||
// // [ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
||||||
// // [ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
||||||
// // [HttpPut("{id}/addMembers")]
|
|
||||||
// // public async Task<ActionResult<Project>> AddMembersToProject(int id, List<AppUser> usersToAdd)
|
|
||||||
// // {
|
|
||||||
// // if (usersToAdd == null)
|
|
||||||
// // {
|
|
||||||
// // return BadRequest();
|
|
||||||
// // }
|
|
||||||
// // Project project = await GetProjectByIdAsync(id);
|
|
||||||
// // project.AddMembers(usersToAdd);
|
|
||||||
// // try
|
|
||||||
// // {
|
|
||||||
// // await _context.SaveChangesAsync();
|
|
||||||
// // }
|
|
||||||
// // 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>
|
|
||||||
// // /// Remove a user to a project.
|
|
||||||
// // /// </summary>
|
|
||||||
// // /// <remarks>
|
|
||||||
// // /// Sample request:
|
|
||||||
// // ///
|
|
||||||
// // /// PUT: api/Projects/removemembers
|
|
||||||
// // /// [{
|
|
||||||
// // /// "id": "357727fd-5262-4522-b8a3-38271d43de84",
|
|
||||||
// // /// "firstName": "Thomas",
|
|
||||||
// // /// "lastName": "Price",
|
|
||||||
// // /// "presentation": "New Team?!",
|
|
||||||
// // /// "email": "tp@mail.com",
|
|
||||||
// // /// "phone": "0198237645"
|
|
||||||
// // /// }]
|
|
||||||
// // ///
|
|
||||||
// // /// </remarks>
|
|
||||||
// // /// <response code="204">Returns the created project</response>
|
|
||||||
// // [ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
||||||
// // [ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
||||||
// // [HttpPut("{id}/removeMembers")]
|
|
||||||
// // public async Task<ActionResult<Project>> RemoveMembersFromProject(int id, List<AppUser> usersToRemove)
|
|
||||||
// // {
|
|
||||||
// // Project project = await GetProjectByIdAsync(id);
|
|
||||||
// // project.RemoveMembers(usersToRemove);
|
|
||||||
// // try
|
|
||||||
// // {
|
|
||||||
// // await _context.SaveChangesAsync();
|
|
||||||
// // }
|
|
||||||
// // 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();
|
|
||||||
// // }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
@ -14,31 +14,29 @@ namespace TicketManager.Controllers
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class TicketsController : ControllerBase
|
public class TicketsController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly AppDbContext _context;
|
private readonly ITicketRepository _tickets;
|
||||||
|
|
||||||
public TicketsController(AppDbContext context)
|
public TicketsController(ITicketRepository tickets)
|
||||||
{
|
{
|
||||||
_context = context;
|
_tickets = tickets;
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: api/Tickets
|
// GET: api/Tickets
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<IEnumerable<Ticket>>> GetTickets()
|
public async Task<IEnumerable<Ticket>> GetTickets()
|
||||||
{
|
{
|
||||||
return await getAllTicketsAsync();
|
return await _tickets.List();
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: api/Tickets/5
|
// GET: api/Tickets/5
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public async Task<ActionResult<Ticket>> GetTicket(int id)
|
public async Task<ActionResult<Ticket>> GetTicket(int id)
|
||||||
{
|
{
|
||||||
var ticket = await getTicketByIdAsync(id);
|
var ticket = await _tickets.Get(id);
|
||||||
|
|
||||||
if (ticket == null)
|
if (ticket == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
return ticket;
|
return ticket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,16 +50,13 @@ namespace TicketManager.Controllers
|
||||||
{
|
{
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
_context.Entry(ticket).State = EntityState.Modified;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _context.SaveChangesAsync();
|
await _tickets.Update(ticket);
|
||||||
}
|
}
|
||||||
catch (DbUpdateConcurrencyException)
|
catch (DbUpdateConcurrencyException)
|
||||||
{
|
{
|
||||||
if (!TicketExists(id))
|
if (!_tickets.Exists(id))
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
@ -70,7 +65,6 @@ namespace TicketManager.Controllers
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NoContent();
|
return NoContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,9 +74,7 @@ namespace TicketManager.Controllers
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<ActionResult<Ticket>> PostTicket(Ticket ticket)
|
public async Task<ActionResult<Ticket>> PostTicket(Ticket ticket)
|
||||||
{
|
{
|
||||||
_context.Tickets.Add(ticket);
|
await _tickets.Add(ticket);
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
return CreatedAtAction("GetTicket", new { id = ticket.Id }, ticket);
|
return CreatedAtAction("GetTicket", new { id = ticket.Id }, ticket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,59 +82,28 @@ namespace TicketManager.Controllers
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
public async Task<ActionResult<Ticket>> DeleteTicket(int id)
|
public async Task<ActionResult<Ticket>> DeleteTicket(int id)
|
||||||
{
|
{
|
||||||
var ticket = await _context.Tickets.FindAsync(id);
|
var ticket = await _tickets.Get(id);
|
||||||
if (ticket == null)
|
if (ticket == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
await _tickets.Delete(ticket);
|
||||||
_context.Tickets.Remove(ticket);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
return ticket;
|
return ticket;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{id}/assignees")]
|
[HttpGet("{id}/assignees")]
|
||||||
public async Task<ActionResult<List<AppUser>>> GetTicketAssignees(int id)
|
public async Task<ActionResult<List<AppUser>>> GetTicketAssignees(int id)
|
||||||
{
|
{
|
||||||
Ticket ticket = await getTicketByIdAsync(id);
|
Ticket ticket = await _tickets.Get(id);
|
||||||
return ticket.GetAssignees();
|
return ticket.GetAssignees();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{id}/closed")]
|
[HttpPut("{id}/closed")]
|
||||||
public async Task<ActionResult> CloseTicket(int id)
|
public async Task<IActionResult> CloseTicket(int id)
|
||||||
{
|
{
|
||||||
Ticket ticket = await getTicketByIdAsync(id);
|
Ticket ticket = await _tickets.Get(id);
|
||||||
ticket.Close();
|
ticket.Close();
|
||||||
return NoContent();
|
return await PutTicket(ticket.Id, ticket);
|
||||||
}
|
|
||||||
|
|
||||||
private bool TicketExists(int id)
|
|
||||||
{
|
|
||||||
return _context.Tickets.Any(e => e.Id == id);
|
|
||||||
}
|
|
||||||
|
|
||||||
private IQueryable<Ticket> ticketQuery() // problem with link
|
|
||||||
{
|
|
||||||
return _context.Tickets
|
|
||||||
.Include(p => p.Project)
|
|
||||||
.ThenInclude(a => a.Assignments)
|
|
||||||
.ThenInclude(p => p.User)
|
|
||||||
// .Include(p => p.Edits)
|
|
||||||
// .Include(p => p.Notes)
|
|
||||||
// .Include(p => p.Files)
|
|
||||||
// .Include(p => p.Creator)
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<ActionResult<IEnumerable<Ticket>>> getAllTicketsAsync()
|
|
||||||
{
|
|
||||||
return await ticketQuery().ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<Ticket> getTicketByIdAsync(int id)
|
|
||||||
{
|
|
||||||
return await ticketQuery().FirstOrDefaultAsync(a => a.Id == id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
38
Data/AppUserRepository.cs
Normal file
38
Data/AppUserRepository.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TicketManager.Models;
|
||||||
|
using System.Linq;
|
||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace TicketManager.Data
|
||||||
|
{
|
||||||
|
public class AppUserRepository : GenericRepository<AppUser>, IAppUserRepository
|
||||||
|
{
|
||||||
|
private readonly IQueryable<AppUser> _query;
|
||||||
|
public AppUserRepository(AppDbContext context) : base(context)
|
||||||
|
{
|
||||||
|
_query = _dbSet
|
||||||
|
.Include(p => p.Assignments)
|
||||||
|
.ThenInclude(a => a.Project)
|
||||||
|
.ThenInclude(p => p.Tickets)
|
||||||
|
.Include(p => p.Edits)
|
||||||
|
.AsNoTracking();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<AppUser> GetUser(Guid id)
|
||||||
|
{
|
||||||
|
return await _query.FirstOrDefaultAsync(p => p.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task<IEnumerable<AppUser>> List()
|
||||||
|
{
|
||||||
|
return await _query.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Exists(Guid id)
|
||||||
|
{
|
||||||
|
return _dbSet.Any(e => e.Id == id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,16 +17,18 @@ namespace TicketManager.Data
|
||||||
_dbSet = _context.Set<T>();
|
_dbSet = _context.Set<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add(T entity)
|
public async Task<int> Add(T entity)
|
||||||
{
|
{
|
||||||
_dbSet.Add(entity);
|
_dbSet.Add(entity);
|
||||||
|
return await _context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Delete(T entity)
|
public async Task<int> Delete(T entity)
|
||||||
{
|
{
|
||||||
if (_context.Entry(entity).State == EntityState.Detached)
|
if (_context.Entry(entity).State == EntityState.Detached)
|
||||||
{ _dbSet.Attach(entity); }
|
{ _dbSet.Attach(entity); }
|
||||||
_dbSet.Remove(entity);
|
_dbSet.Remove(entity);
|
||||||
|
return await _context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<T>> Find(int id, Expression<Func<T, bool>> expr)
|
public async Task<IEnumerable<T>> Find(int id, Expression<Func<T, bool>> expr)
|
||||||
|
|
@ -43,10 +45,11 @@ namespace TicketManager.Data
|
||||||
return await _dbSet.AsNoTracking().ToListAsync();
|
return await _dbSet.AsNoTracking().ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(T entity)
|
public async Task<int> Update(T entity)
|
||||||
{
|
{
|
||||||
_dbSet.Attach(entity);
|
_dbSet.Attach(entity);
|
||||||
_context.Entry(entity).State = EntityState.Modified;
|
_context.Entry(entity).State = EntityState.Modified;
|
||||||
|
return await _context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
12
Data/Interfaces/IAppUserRepository.cs
Normal file
12
Data/Interfaces/IAppUserRepository.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TicketManager.Models;
|
||||||
|
|
||||||
|
namespace TicketManager.Data
|
||||||
|
{
|
||||||
|
public interface IAppUserRepository : IGenericRepository<AppUser>
|
||||||
|
{
|
||||||
|
Task<AppUser> GetUser(Guid id);
|
||||||
|
bool Exists(Guid id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,10 +11,10 @@ namespace TicketManager.Data
|
||||||
Task<T> Get(int id);
|
Task<T> Get(int id);
|
||||||
Task<IEnumerable<T>> Find(int id, Expression<Func<T, bool>> expr);
|
Task<IEnumerable<T>> Find(int id, Expression<Func<T, bool>> expr);
|
||||||
|
|
||||||
void Add(T entity);
|
Task<int> Add(T entity);
|
||||||
|
|
||||||
void Update(T entity);
|
Task<int> Update(T entity);
|
||||||
|
|
||||||
void Delete(T entity);
|
Task<int> Delete(T entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
11
Data/Interfaces/ITicketRepository.cs
Normal file
11
Data/Interfaces/ITicketRepository.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TicketManager.Models;
|
||||||
|
|
||||||
|
namespace TicketManager.Data
|
||||||
|
{
|
||||||
|
public interface ITicketRepository : IGenericRepository<Ticket>
|
||||||
|
{
|
||||||
|
bool Exists(int id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,8 @@ namespace TicketManager.Data
|
||||||
public interface IUnitOfWork : IDisposable
|
public interface IUnitOfWork : IDisposable
|
||||||
{
|
{
|
||||||
IProjectRepository Projects { get; }
|
IProjectRepository Projects { get; }
|
||||||
|
IAppUserRepository AppUsers { get; }
|
||||||
|
ITicketRepository Tickets { get; }
|
||||||
Task<int> Complete();
|
Task<int> Complete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -30,7 +30,9 @@ namespace TicketManager.Data
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Exists(int id)
|
public bool Exists(int id)
|
||||||
{ return _dbSet.Any(e => e.Id == id); }
|
{
|
||||||
|
return _dbSet.Any(e => e.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<AppUser>> GetMembers(int id)
|
public async Task<IEnumerable<AppUser>> GetMembers(int id)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
40
Data/TicketRepository.cs
Normal file
40
Data/TicketRepository.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Linq;
|
||||||
|
using TicketManager.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace TicketManager.Data
|
||||||
|
{
|
||||||
|
public class TicketRepository : GenericRepository<Ticket>, ITicketRepository
|
||||||
|
{
|
||||||
|
private IQueryable<Ticket> _query;
|
||||||
|
public TicketRepository(AppDbContext context) : base(context)
|
||||||
|
{
|
||||||
|
_query = _dbSet
|
||||||
|
.Include(p => p.Project)
|
||||||
|
.ThenInclude(a => a.Assignments)
|
||||||
|
.ThenInclude(p => p.User)
|
||||||
|
// .Include(p => p.Edits)
|
||||||
|
// .Include(p => p.Notes)
|
||||||
|
// .Include(p => p.Files)
|
||||||
|
// .Include(p => p.Creator)
|
||||||
|
.AsNoTracking();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task<Ticket> Get(int id)
|
||||||
|
{
|
||||||
|
return await _query.FirstOrDefaultAsync(p => p.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task<IEnumerable<Ticket>> List()
|
||||||
|
{
|
||||||
|
return await _query.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Exists(int id)
|
||||||
|
{
|
||||||
|
return _dbSet.Any(e => e.Id == id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,10 +10,16 @@ namespace TicketManager.Data
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
Projects = new ProjectRepository(_context);
|
Projects = new ProjectRepository(_context);
|
||||||
|
Tickets = new TicketRepository(_context);
|
||||||
|
AppUsers = new AppUserRepository(_context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IProjectRepository Projects { get; private set; }
|
public IProjectRepository Projects { get; private set; }
|
||||||
|
|
||||||
|
public IAppUserRepository AppUsers { get; private set; }
|
||||||
|
|
||||||
|
public ITicketRepository Tickets { get; private set; }
|
||||||
|
|
||||||
public async Task<int> Complete()
|
public async Task<int> Complete()
|
||||||
{
|
{
|
||||||
return await _context.SaveChangesAsync();
|
return await _context.SaveChangesAsync();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue