mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-06 08:46:39 +00:00
generic and project repository done
This commit is contained in:
parent
f6151bb24e
commit
172252132b
9 changed files with 433 additions and 345 deletions
|
|
@ -38,8 +38,7 @@ namespace TicketManager.Controllers
|
|||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<IEnumerable<Project>> GetProjects()
|
||||
{
|
||||
return await _projectRepo.ListAsync();
|
||||
// GetAllProjectsAsync();
|
||||
return await _projectRepo.List();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -58,204 +57,165 @@ namespace TicketManager.Controllers
|
|||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<Project>> GetProject(int id)
|
||||
{
|
||||
Project project = await _projectRepo.GetByIdAsync(id);
|
||||
Project project = await _projectRepo.Get(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);
|
||||
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.
|
||||
// /// Updates a specific project.
|
||||
// /// </summary>
|
||||
// /// <remarks>
|
||||
// /// Sample request:
|
||||
// ///
|
||||
// /// POST: api/Projects/addmembers
|
||||
// /// [{
|
||||
// /// 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="204">Returns the created project</response>
|
||||
// /// <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)]
|
||||
// [HttpPut("{id}/addMembers")]
|
||||
// public async Task<ActionResult<Project>> AddMembersToProject(int id, List<AppUser> usersToAdd)
|
||||
// public async Task<IActionResult> PutProject(int id, Project project)
|
||||
// {
|
||||
// if (usersToAdd == null)
|
||||
// {
|
||||
// return BadRequest();
|
||||
// }
|
||||
// Project project = await GetProjectByIdAsync(id);
|
||||
// project.AddMembers(usersToAdd);
|
||||
// if (id != project.Id) { return BadRequest(); }
|
||||
|
||||
// try
|
||||
// {
|
||||
// await _context.SaveChangesAsync();
|
||||
// await _projectRepo.Update(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 */)
|
||||
// {
|
||||
|
|
@ -267,44 +227,87 @@ namespace TicketManager.Controllers
|
|||
// 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();
|
||||
// }
|
||||
// // /// <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();
|
||||
// // }
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -15,13 +15,13 @@
|
|||
// [Produces("application/json")]
|
||||
// [Route("api/v1/[controller]")]
|
||||
// [ApiController]
|
||||
// public class ProjectsController2 : ControllerBase
|
||||
// public class ProjectsController : ControllerBase
|
||||
// {
|
||||
// private readonly AppDbContext _context;
|
||||
// private readonly IProjectRepository _projectRepo;
|
||||
|
||||
// public ProjectsController2(AppDbContext context)
|
||||
// public ProjectsController(IProjectRepository projectRepo)
|
||||
// {
|
||||
// _context = context;
|
||||
// _projectRepo = projectRepo;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
|
|
@ -36,9 +36,10 @@
|
|||
// /// <response code="200">Returns all existing projects</response>
|
||||
// [HttpGet]
|
||||
// [ProducesResponseType(StatusCodes.Status200OK)]
|
||||
// public async Task<ActionResult<IEnumerable<Project>>> GetProjects()
|
||||
// public async Task<IEnumerable<Project>> GetProjects()
|
||||
// {
|
||||
// return await GetAllProjectsAsync();
|
||||
// return await _projectRepo.ListAsync();
|
||||
// // GetAllProjectsAsync();
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
|
|
@ -57,11 +58,8 @@
|
|||
// [ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
// public async Task<ActionResult<Project>> GetProject(int id)
|
||||
// {
|
||||
// Project project = await GetProjectByIdAsync(id);
|
||||
// if (project == null)
|
||||
// {
|
||||
// return NotFound();
|
||||
// }
|
||||
// Project project = await _projectRepo.GetByIdAsync(id);
|
||||
// if (project == null) { return NotFound(); }
|
||||
// return project;
|
||||
// }
|
||||
|
||||
|
|
@ -91,20 +89,15 @@
|
|||
// [ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
// public async Task<IActionResult> PutProject(int id, Project project)
|
||||
// {
|
||||
// if (id != project.Id)
|
||||
// {
|
||||
// return BadRequest();
|
||||
// }
|
||||
|
||||
// _context.Entry(project).State = EntityState.Modified;
|
||||
// if (id != project.Id) { return BadRequest(); }
|
||||
|
||||
// try
|
||||
// {
|
||||
// await _context.SaveChangesAsync();
|
||||
// await _projectRepo.UpdateAsync(project);
|
||||
// }
|
||||
// catch (DbUpdateConcurrencyException)
|
||||
// {
|
||||
// if (!ProjectExists(id))
|
||||
// if (!_projectRepo.Exists(id))
|
||||
// {
|
||||
// return NotFound();
|
||||
// }
|
||||
|
|
@ -139,8 +132,8 @@
|
|||
// [ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
// public async Task<ActionResult<Project>> PostProject(Project project)
|
||||
// {
|
||||
// _context.Projects.Add(project);
|
||||
// await _context.SaveChangesAsync();
|
||||
// if (!ModelState.IsValid) { return BadRequest(); }
|
||||
// await _projectRepo.AddAsync(project);
|
||||
|
||||
// return CreatedAtAction("GetProject", new { id = project.Id }, project);
|
||||
// }
|
||||
|
|
@ -163,13 +156,12 @@
|
|||
// [HttpDelete("{id}")]
|
||||
// public async Task<ActionResult<Project>> DeleteProject(int id)
|
||||
// {
|
||||
// var project = await _context.Projects.FindAsync(id);
|
||||
// var project = await _projectRepo.GetByIdAsync(id);
|
||||
// if (project == null)
|
||||
// {
|
||||
// return NotFound();
|
||||
// }
|
||||
// _context.Projects.Remove(project);
|
||||
// await _context.SaveChangesAsync();
|
||||
// await _projectRepo.DeleteAsync(id);
|
||||
// return project;
|
||||
// }
|
||||
|
||||
|
|
@ -188,7 +180,7 @@
|
|||
// [HttpGet("{id}/members")]
|
||||
// public async Task<ActionResult<List<AppUser>>> GetProjectMembers(int id)
|
||||
// {
|
||||
// Project project = await GetProjectByIdAsync(id);
|
||||
// Project project = await _projectRepo.GetByIdAsync(id);
|
||||
// if (project == null)
|
||||
// { return NotFound(); }
|
||||
// return project.GetMembers();
|
||||
|
|
@ -216,11 +208,15 @@
|
|||
// [HttpPut("{id}/members")]
|
||||
// public async Task<ActionResult<Project>> SetProjectMembers(int id, List<AppUser> projectMembers)
|
||||
// {
|
||||
// Project project = await GetProjectByIdAsync(id);
|
||||
// Project project = await _projectRepo.GetByIdAsync(id);
|
||||
// if (project == null)
|
||||
// {
|
||||
// return NotFound();
|
||||
// }
|
||||
// project.SetMembers(projectMembers);
|
||||
// try
|
||||
// {
|
||||
// await _context.SaveChangesAsync();
|
||||
// await _projectRepo.UpdateAsync(project);
|
||||
// }
|
||||
// catch (DbUpdateException /* ex */)
|
||||
// {
|
||||
|
|
@ -314,31 +310,8 @@
|
|||
// // return NoContent();
|
||||
// // }
|
||||
|
||||
// private bool ProjectExists(int id)
|
||||
// {
|
||||
// return _context.Projects.Any(e => e.Id == id);
|
||||
// }
|
||||
|
||||
|
||||
// private async Task<ActionResult<IEnumerable<Project>>> GetAllProjectsAsync()
|
||||
// {
|
||||
// return await makeProjectsQueryAsync()
|
||||
// .ToListAsync();
|
||||
// }
|
||||
// private async Task<Project> GetProjectByIdAsync(int id)
|
||||
// {
|
||||
// return await makeProjectsQueryAsync()
|
||||
// .FirstOrDefaultAsync(p => p.Id == id);
|
||||
// }
|
||||
|
||||
// private IQueryable<Project> makeProjectsQueryAsync()
|
||||
// {
|
||||
// return _context.Projects
|
||||
// .Include(p => p.Assignments)
|
||||
// .ThenInclude(a => a.User)
|
||||
// .Include(p => p.Tickets)
|
||||
// .Include(p => p.Manager)
|
||||
// .Include(p => p.Files);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
52
Data/GenericRepository.cs
Normal file
52
Data/GenericRepository.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace TicketManager.Data
|
||||
{
|
||||
public class GenericRepository<T> : IGenericRepository<T> where T : class
|
||||
{
|
||||
protected readonly AppDbContext _context;
|
||||
protected readonly DbSet<T> _dbSet;
|
||||
public GenericRepository(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
_dbSet = _context.Set<T>();
|
||||
}
|
||||
|
||||
public void Add(T entity)
|
||||
{
|
||||
_dbSet.Add(entity);
|
||||
}
|
||||
|
||||
public void Delete(T entity)
|
||||
{
|
||||
if (_context.Entry(entity).State == EntityState.Detached)
|
||||
{ _dbSet.Attach(entity); }
|
||||
_dbSet.Remove(entity);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<T>> Find(int id, Expression<Func<T, bool>> expr)
|
||||
{
|
||||
return await _dbSet.Where(expr).AsNoTracking().ToListAsync();
|
||||
}
|
||||
|
||||
public virtual async Task<T> Get(int id)
|
||||
{
|
||||
return await _dbSet.FindAsync(id);
|
||||
}
|
||||
public virtual async Task<IEnumerable<T>> List()
|
||||
{
|
||||
return await _dbSet.AsNoTracking().ToListAsync();
|
||||
}
|
||||
|
||||
public void Update(T entity)
|
||||
{
|
||||
_dbSet.Attach(entity);
|
||||
_context.Entry(entity).State = EntityState.Modified;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using TicketManager.Models;
|
||||
|
||||
namespace TicketManager.Data
|
||||
{
|
||||
public interface IProjectRepository
|
||||
{
|
||||
Task<IEnumerable<Project>> ListAsync();
|
||||
Task<Project> GetByIdAsync(int id);
|
||||
Task AddAsync(Project project);
|
||||
Task UpdateAsync(Project project);
|
||||
Task<int> DeleteAsync(int id);
|
||||
bool Exists(int id);
|
||||
|
||||
}
|
||||
}
|
||||
20
Data/Interfaces/IGenericRepository.cs
Normal file
20
Data/Interfaces/IGenericRepository.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TicketManager.Data
|
||||
{
|
||||
public interface IGenericRepository<T> where T : class
|
||||
{
|
||||
Task<IEnumerable<T>> List();
|
||||
Task<T> Get(int id);
|
||||
Task<IEnumerable<T>> Find(int id, Expression<Func<T, bool>> expr);
|
||||
|
||||
void Add(T entity);
|
||||
|
||||
void Update(T entity);
|
||||
|
||||
void Delete(T entity);
|
||||
}
|
||||
}
|
||||
13
Data/Interfaces/IProjectRepository.cs
Normal file
13
Data/Interfaces/IProjectRepository.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using TicketManager.Models;
|
||||
|
||||
namespace TicketManager.Data
|
||||
{
|
||||
public interface IProjectRepository : IGenericRepository<Project>
|
||||
{
|
||||
bool Exists(int id);
|
||||
Task<IEnumerable<AppUser>> GetMembers(int id);
|
||||
Task SetMembers(int id, List<AppUser> usersToAdd);
|
||||
}
|
||||
}
|
||||
57
Data/ProjectRepository working.cs
Normal file
57
Data/ProjectRepository working.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
// using System.Threading.Tasks;
|
||||
// using TicketManager.Models;
|
||||
// using System.Linq;
|
||||
// using Microsoft.EntityFrameworkCore;
|
||||
// using System.Collections.Generic;
|
||||
// using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
// namespace TicketManager.Data
|
||||
// {
|
||||
// public class ProjectRepository : IProjectRepository
|
||||
// {
|
||||
// private readonly AppDbContext _context;
|
||||
// private readonly IQueryable<Project> _query;
|
||||
// public ProjectRepository(AppDbContext context)
|
||||
// {
|
||||
// _context = context;
|
||||
// _query = _context.Projects
|
||||
// .Include(p => p.Assignments)
|
||||
// .ThenInclude(a => a.User)
|
||||
// .Include(p => p.Tickets)
|
||||
// .Include(p => p.Manager)
|
||||
// .Include(p => p.Files);
|
||||
// }
|
||||
|
||||
// public Task AddAsync(Project project)
|
||||
// {
|
||||
// _context.Projects.Add(project);
|
||||
// return _context.SaveChangesAsync();
|
||||
// }
|
||||
|
||||
// public async Task<int> DeleteAsync(int id)
|
||||
// {
|
||||
// Project project = await GetByIdAsync(id);
|
||||
// _context.Projects.Remove(project);
|
||||
// return await _context.SaveChangesAsync();
|
||||
// }
|
||||
|
||||
// public async Task<Project> GetByIdAsync(int id)
|
||||
// {
|
||||
// return await _query.FirstOrDefaultAsync(p => p.Id == id);
|
||||
// }
|
||||
|
||||
// public async Task<IEnumerable<Project>> ListAsync()
|
||||
// {
|
||||
// return await _query.ToListAsync();
|
||||
// }
|
||||
|
||||
// public Task UpdateAsync(Project project)
|
||||
// {
|
||||
// _context.Entry(project).State = EntityState.Modified;
|
||||
// return _context.SaveChangesAsync();
|
||||
// }
|
||||
// public bool Exists(int id)
|
||||
// { return _context.Projects.Any(e => e.Id == id); }
|
||||
|
||||
// }
|
||||
// }
|
||||
|
|
@ -3,56 +3,44 @@ using TicketManager.Models;
|
|||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace TicketManager.Data
|
||||
{
|
||||
public class ProjectRepository : IProjectRepository
|
||||
public class ProjectRepository : GenericRepository<Project>, IProjectRepository
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
private readonly IQueryable<Project> _query;
|
||||
public ProjectRepository(AppDbContext context)
|
||||
public ProjectRepository(AppDbContext context) : base(context)
|
||||
{
|
||||
_context = context;
|
||||
_query = _context.Projects
|
||||
.Include(p => p.Assignments)
|
||||
.ThenInclude(a => a.User)
|
||||
_query = _dbSet
|
||||
.Include(p => p.Assignments).ThenInclude(a => a.User)
|
||||
.Include(p => p.Tickets)
|
||||
.Include(p => p.Manager)
|
||||
.Include(p => p.Files);
|
||||
.Include(p => p.Files)
|
||||
.AsNoTracking();
|
||||
}
|
||||
|
||||
public Task AddAsync(Project project)
|
||||
{
|
||||
_context.Projects.Add(project);
|
||||
return _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> DeleteAsync(int id)
|
||||
{
|
||||
Project project = await GetByIdAsync(id);
|
||||
_context.Projects.Remove(project);
|
||||
return await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<Project> GetByIdAsync(int id)
|
||||
public override async Task<Project> Get(int id)
|
||||
{
|
||||
return await _query.FirstOrDefaultAsync(p => p.Id == id);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Project>> ListAsync()
|
||||
public override async Task<IEnumerable<Project>> List()
|
||||
{
|
||||
return await _query.ToListAsync();
|
||||
}
|
||||
|
||||
public Task UpdateAsync(Project project)
|
||||
{
|
||||
_context.Entry(project).State = EntityState.Modified;
|
||||
return _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public bool Exists(int id)
|
||||
{ return _context.Projects.Any(e => e.Id == id); }
|
||||
{ return _dbSet.Any(e => e.Id == id); }
|
||||
|
||||
public async Task<IEnumerable<AppUser>> GetMembers(int id)
|
||||
{
|
||||
Project project = await Get(id);
|
||||
return project.GetMembers();
|
||||
}
|
||||
public async Task SetMembers(int id, List<AppUser> usersToAdd)
|
||||
{
|
||||
Project project = await Get(id);
|
||||
project.SetMembers(usersToAdd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,30 +1,30 @@
|
|||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:15556",
|
||||
"sslPort": 44341
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "weatherforecast",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"TicketManager": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "weatherforecast",
|
||||
"applicationUrl": "https://localhost:5001;http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:15556",
|
||||
"sslPort": 44341
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "weatherforecast",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"TicketManager": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "weatherforecast",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "https://localhost:5001;http://localhost:5000"
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue