-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectsController.cs
More file actions
120 lines (105 loc) · 4.56 KB
/
Copy pathProjectsController.cs
File metadata and controls
120 lines (105 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ProjectManagementSystem.Data;
using ProjectManagementSystem.Models;
using System.Security.Claims;
namespace ProjectManagementSystem.Controllers;
[ApiController]
[Route("api/projects")]
[Authorize]
public class ProjectsController : ControllerBase
{
private readonly AppDbContext _context;
private readonly UserManager<ApplicationUser> _userManager;
public ProjectsController(AppDbContext context, UserManager<ApplicationUser> userManager)
{
_context = context;
_userManager = userManager;
}
[HttpGet]
public async Task<IActionResult> GetProjects()
{
var userId = GetUserId();
var projects = await _context.Projects.Where(p => p.OwnerId == userId).ToListAsync();
var numberOfTasks = new Dictionary<int, int>();
foreach(var project in projects)
{
var tasks = await _context.Tasks.Where(t => t.ProjectId == project.ProjectId).ToListAsync();
numberOfTasks[project.ProjectId] = tasks.Count();
}
return Ok(new { projects, numberOfTasks });
}
[HttpGet("{projectId}/tasks")]
public async Task<IActionResult> GetProjectTasks(int projectId)
{
var userId = GetUserId();
var project = await _context.Projects.FindAsync(projectId);
if (project == null || project.OwnerId != userId)
return Unauthorized(new { message = "Project not found for this user" });
var tasks = await _context.Tasks.Where(t => t.ProjectId == projectId).ToListAsync();
if (tasks.Count == 0)
return NotFound(new { message = "No tasks found for this project" });
return Ok(tasks);
}
[HttpGet("{projectId}")]
public async Task<IActionResult> GetProject(int projectId)
{
var userId = GetUserId();
var project = await _context.Projects.FindAsync(projectId);
if (project == null)
return Unauthorized(new { message = "No Project Found with Matching ID" });
if (project.OwnerId != userId)
return Unauthorized(new { message = "You are unauthorized to view this project" });
return Ok(project);
}
[HttpPost]
public async Task<IActionResult> AddProject([FromBody] Project project)
{
var userId = GetUserId();
var sameName = await _context.Projects.Where(p => p.Name.Equals(project.Name) && p.OwnerId.Equals(userId)).ToListAsync();
if (sameName.Count > 0)
return Unauthorized(new { message = $"Project Titled: {project.Name} Already Exists" });
project.OwnerId = userId;
_context.Projects.Add(project);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetProjects), new { id = project.ProjectId }, new { project = project, projectId = project.ProjectId });
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateProject(int id, [FromBody] Project project)
{
// Assuming this is in a controller with User property
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // Get user ID from claims
var existingProject = await _context.Projects.FindAsync(id);
if (existingProject == null)
return NotFound("Project not found");
if (existingProject.OwnerId != userId)
return Unauthorized("You are not authorized to update this project");
// Update basic properties
existingProject.Name = project.Name;
existingProject.Description = project.Description;
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetProjects), new { id = project.ProjectId }, new { project = project, projectId = project.ProjectId });
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteProject(int id)
{
var userId = GetUserId();
var existingProject = await _context.Projects.FindAsync(id);
if (existingProject == null)
return NotFound("Project not found");
if (existingProject.OwnerId != userId)
return Unauthorized("You are not authorized to delete this project");
_context.Projects.Remove(existingProject);
await _context.SaveChangesAsync();
return Ok("Project Deleted");
}
private string GetUserId()
{
var userId = _userManager.GetUserId(User);
if (userId == null)
throw new UnauthorizedAccessException("You are not authorized to access this resource.");
return userId;
}
}