diff --git a/Controllers/ProjectsController2.cs b/Controllers/ProjectsController2.cs new file mode 100644 index 0000000..a76c51f --- /dev/null +++ b/Controllers/ProjectsController2.cs @@ -0,0 +1,344 @@ +// 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 ProjectsController2 : ControllerBase +// { +// private readonly AppDbContext _context; + +// public ProjectsController2(AppDbContext context) +// { +// _context = context; +// } + +// /// +// /// Returns all existing projects. +// /// +// /// +// /// Sample request: +// /// +// /// GET: api/Projects +// /// +// /// +// /// Returns all existing projects +// [HttpGet] +// [ProducesResponseType(StatusCodes.Status200OK)] +// public async Task>> GetProjects() +// { +// return await GetAllProjectsAsync(); +// } + +// /// +// /// Returns a specific project. +// /// +// /// +// /// Sample request: +// /// +// /// GET: api/Projects/2 +// /// +// /// +// /// Returns a specific project +// /// If the required project is null +// [HttpGet("{id}")] +// [ProducesResponseType(StatusCodes.Status200OK)] +// [ProducesResponseType(StatusCodes.Status404NotFound)] +// public async Task> GetProject(int id) +// { +// Project project = await GetProjectByIdAsync(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(); +// } + +// _context.Entry(project).State = EntityState.Modified; + +// try +// { +// await _context.SaveChangesAsync(); +// } +// catch (DbUpdateConcurrencyException) +// { +// if (!ProjectExists(id)) +// { +// return NotFound(); +// } +// else +// { +// throw; +// } +// } + +// return NoContent(); +// } + +// /// +// /// 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) +// { +// _context.Projects.Add(project); +// await _context.SaveChangesAsync(); + +// 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 _context.Projects.FindAsync(id); +// if (project == null) +// { +// return NotFound(); +// } +// _context.Projects.Remove(project); +// await _context.SaveChangesAsync(); +// 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 GetProjectByIdAsync(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 GetProjectByIdAsync(id); +// project.SetMembers(projectMembers); +// 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(); +// } + +// // /// +// // /// Assign a user to a project. +// // /// +// // /// +// // /// 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" +// // /// }] +// // /// +// // /// +// // /// Returns the created project +// // [ProducesResponseType(StatusCodes.Status204NoContent)] +// // [ProducesResponseType(StatusCodes.Status404NotFound)] +// // [HttpPut("{id}/addMembers")] +// // public async Task> AddMembersToProject(int id, List 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(); +// // } + +// // /// +// // /// Remove a user to a project. +// // /// +// // /// +// // /// 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" +// // /// }] +// // /// +// // /// +// // /// Returns the created project +// // [ProducesResponseType(StatusCodes.Status204NoContent)] +// // [ProducesResponseType(StatusCodes.Status404NotFound)] +// // [HttpPut("{id}/removeMembers")] +// // public async Task> RemoveMembersFromProject(int id, List 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(); +// // } + +// private bool ProjectExists(int id) +// { +// return _context.Projects.Any(e => e.Id == id); +// } + + +// private async Task>> GetAllProjectsAsync() +// { +// return await makeProjectsQueryAsync() +// .ToListAsync(); +// } +// private async Task GetProjectByIdAsync(int id) +// { +// return await makeProjectsQueryAsync() +// .FirstOrDefaultAsync(p => p.Id == id); +// } + +// private IQueryable makeProjectsQueryAsync() +// { +// return _context.Projects +// .Include(p => p.Assignments) +// .ThenInclude(a => a.User) +// .Include(p => p.Tickets) +// .Include(p => p.Manager) +// .Include(p => p.Files); +// } +// } +// } diff --git a/Data/IProjectRepository.cs b/Data/IProjectRepository.cs new file mode 100644 index 0000000..aba71e3 --- /dev/null +++ b/Data/IProjectRepository.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using TicketManager.Models; + +namespace TicketManager.Data +{ + public interface IProjectRepository + { + Task> ListAsync(); + Task GetByIdAsync(int id); + Task AddAsync(Project project); + Task UpdateAsync(Project project); + Task DeleteAsync(int id); + bool Exists(int id); + + } +} \ No newline at end of file diff --git a/Data/ProjectRepository.cs b/Data/ProjectRepository.cs new file mode 100644 index 0000000..6a5ced5 --- /dev/null +++ b/Data/ProjectRepository.cs @@ -0,0 +1,58 @@ +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 _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 DeleteAsync(int id) + { + Project project = await GetByIdAsync(id); + _context.Projects.Remove(project); + return await _context.SaveChangesAsync(); + } + + public async Task GetByIdAsync(int id) + { + return await _query.FirstOrDefaultAsync(p => p.Id == id); + } + + public async Task> 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); } + + } +} \ No newline at end of file