mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-12 11:46:40 +00:00
projRepo integrated
This commit is contained in:
parent
8394708aa1
commit
e57b7359c7
3 changed files with 23 additions and 51 deletions
|
|
@ -17,11 +17,11 @@ namespace TicketManager.Controllers
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class ProjectsController : ControllerBase
|
public class ProjectsController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly AppDbContext _context;
|
private readonly IProjectRepository _projectRepo;
|
||||||
|
|
||||||
public ProjectsController(AppDbContext context)
|
public ProjectsController(IProjectRepository projectRepo)
|
||||||
{
|
{
|
||||||
_context = context;
|
_projectRepo = projectRepo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -36,9 +36,10 @@ namespace TicketManager.Controllers
|
||||||
/// <response code="200">Returns all existing projects</response>
|
/// <response code="200">Returns all existing projects</response>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[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>
|
/// <summary>
|
||||||
|
|
@ -57,11 +58,8 @@ 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)
|
||||||
{
|
{
|
||||||
Project project = await GetProjectByIdAsync(id);
|
Project project = await _projectRepo.GetByIdAsync(id);
|
||||||
if (project == null)
|
if (project == null) { return NotFound(); }
|
||||||
{
|
|
||||||
return NotFound();
|
|
||||||
}
|
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,20 +89,15 @@ 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)
|
||||||
{
|
{
|
||||||
if (id != project.Id)
|
if (id != project.Id) { return BadRequest(); }
|
||||||
{
|
|
||||||
return BadRequest();
|
|
||||||
}
|
|
||||||
|
|
||||||
_context.Entry(project).State = EntityState.Modified;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _context.SaveChangesAsync();
|
await _projectRepo.UpdateAsync(project);
|
||||||
}
|
}
|
||||||
catch (DbUpdateConcurrencyException)
|
catch (DbUpdateConcurrencyException)
|
||||||
{
|
{
|
||||||
if (!ProjectExists(id))
|
if (!_projectRepo.Exists(id))
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
@ -139,8 +132,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)
|
||||||
{
|
{
|
||||||
_context.Projects.Add(project);
|
if (!ModelState.IsValid) { return BadRequest(); }
|
||||||
await _context.SaveChangesAsync();
|
await _projectRepo.AddAsync(project);
|
||||||
|
|
||||||
return CreatedAtAction("GetProject", new { id = project.Id }, project);
|
return CreatedAtAction("GetProject", new { id = project.Id }, project);
|
||||||
}
|
}
|
||||||
|
|
@ -163,13 +156,12 @@ namespace TicketManager.Controllers
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
public async Task<ActionResult<Project>> DeleteProject(int 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)
|
if (project == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
_context.Projects.Remove(project);
|
await _projectRepo.DeleteAsync(id);
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -188,7 +180,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)
|
||||||
{
|
{
|
||||||
Project project = await GetProjectByIdAsync(id);
|
Project project = await _projectRepo.GetByIdAsync(id);
|
||||||
if (project == null)
|
if (project == null)
|
||||||
{ return NotFound(); }
|
{ return NotFound(); }
|
||||||
return project.GetMembers();
|
return project.GetMembers();
|
||||||
|
|
@ -216,11 +208,11 @@ 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)
|
||||||
{
|
{
|
||||||
Project project = await GetProjectByIdAsync(id);
|
Project project = await _projectRepo.GetByIdAsync(id);
|
||||||
project.SetMembers(projectMembers);
|
project.SetMembers(projectMembers);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _context.SaveChangesAsync();
|
await _projectRepo.UpdateAsync(project);
|
||||||
}
|
}
|
||||||
catch (DbUpdateException /* ex */)
|
catch (DbUpdateException /* ex */)
|
||||||
{
|
{
|
||||||
|
|
@ -314,31 +306,8 @@ namespace TicketManager.Controllers
|
||||||
// return NoContent();
|
// 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ namespace TicketManager.Models
|
||||||
{
|
{
|
||||||
public class Assignment
|
public class Assignment
|
||||||
{
|
{
|
||||||
// public int Id { get; set; }
|
|
||||||
public AppUser User { get; set; }
|
public AppUser User { get; set; }
|
||||||
public Guid UserId { get; set; }
|
public Guid UserId { get; set; }
|
||||||
public Project Project { get; set; }
|
public Project Project { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ namespace TicketManager
|
||||||
{
|
{
|
||||||
services.AddDbContext<AppDbContext>(options =>
|
services.AddDbContext<AppDbContext>(options =>
|
||||||
options.UseSqlite(Configuration.GetConnectionString("Sqlite")));
|
options.UseSqlite(Configuration.GetConnectionString("Sqlite")));
|
||||||
|
services.AddScoped<IProjectRepository, ProjectRepository>();
|
||||||
services.AddControllers()
|
services.AddControllers()
|
||||||
.AddNewtonsoftJson(options =>
|
.AddNewtonsoftJson(options =>
|
||||||
{
|
{
|
||||||
|
|
@ -72,11 +73,14 @@ namespace TicketManager
|
||||||
|
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
if (env.IsDevelopment())
|
if (env.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
|
var repository = serviceProvider.GetRequiredService<IProjectRepository>();
|
||||||
|
|
||||||
|
// InitializeDatabaseAsync(repository).Wait()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue