-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTasksController.cs
More file actions
137 lines (126 loc) · 5.23 KB
/
Copy pathTasksController.cs
File metadata and controls
137 lines (126 loc) · 5.23 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ProjectManagementSystem.Data;
using ProjectManagementSystem.Models;
using TaskStatus = ProjectManagementSystem.Models.TaskStatus;
namespace ProjectManagementSystem.Controllers;
[ApiController]
[Route("api/tasks")]
[Authorize]
public class TasksController : ControllerBase
{
private readonly AppDbContext _context;
private readonly UserManager<ApplicationUser> _userManager;
public TasksController(AppDbContext context, UserManager<ApplicationUser> userManager)
{
_context = context;
_userManager = userManager;
}
[HttpGet("{projectId}")]
public async Task<IActionResult> GetTasksInProject(int projectId)
{
var userId = GetUserId();
var project = await _context.Projects.FindAsync(projectId);
if (project == null)
return NotFound(new { message = "Project not found" });
if (project.OwnerId != userId)
return Unauthorized(new { message = "You are not allowed to perform action" });
var tasks = await _context.Tasks.Where(t => t.ProjectId == projectId).ToListAsync();
if (tasks == null)
return NotFound(new { message = "Tasks not found" });
return Ok(tasks);
}
[HttpGet("{projectId}/{taskId}/task")]
public async Task<IActionResult> GetTask(int projectId, int taskId)
{
var userId = GetUserId();
var project = await _context.Projects.FindAsync(projectId);
if (project == null)
return NotFound(new { message = "Project not found!" });
var task = await _context.Tasks.Where(t => t.TaskItemId == taskId && t.ProjectId == projectId).FirstAsync();
if (task == null)
return NotFound(new { message = "Task Item not found" });
return Ok(task);
}
[HttpGet("{taskId}/assignees")]
public async Task<IActionResult> GetAssignedUsers(int taskId)
{
var users = await _context.AssignTasks.Where(a => a.TaskItemId == taskId).ToListAsync();
if (users.Count == 0)
return NotFound(new { message = "Task not found" });
return Ok(users);
}
[HttpPost]
public async Task<IActionResult> CreateTask([FromBody] TaskItem taskitem)
{
var userId = GetUserId();
var project = await _context.Projects.Where(p => p.ProjectId == taskitem.ProjectId && p.OwnerId == userId).FirstAsync();
if (project == null)
return NotFound(new { message = "Project not found" });
if (project.OwnerId != userId)
return Unauthorized(new { message = "You are not authorized to perform this action" });
taskitem.Status = GetTaskStatus(taskitem.Status);
_context.Tasks.Add(taskitem);
await _context.SaveChangesAsync();
return Ok(new { status = "success" });
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateTask(int id, [FromBody] TaskItem taskItem)
{
Console.WriteLine("Updating Task: " + id);
var userId = GetUserId();
var project = await _context.Projects.Where(p => p.OwnerId == userId && p.ProjectId == taskItem.ProjectId).FirstAsync();
if (project == null)
return NotFound(new { message = "Project not found!" });
var existingItem = await _context.Tasks.Where(t => t.ProjectId == project.ProjectId && t.TaskItemId == id).FirstAsync();
if (existingItem == null)
return NotFound(new { message = "Task not found" });
existingItem.Title = taskItem.Title;
existingItem.Description = taskItem.Description;
existingItem.DueDate = taskItem.DueDate;
existingItem.Status = GetTaskStatus(taskItem.Status);
existingItem.ProjectId = taskItem.ProjectId;
await _context.SaveChangesAsync();
return Ok(new { status = "success" });
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteTask(int id)
{
var userId = GetUserId();
var existingItem = await _context.Tasks.FindAsync(id);
if (existingItem == null)
return NotFound(new { message = "Task not found" });
var project = await _context.Projects.FindAsync(existingItem.ProjectId);
if (project == null)
return NotFound(new { message = "Project not found" });
if (project.OwnerId != userId)
return Unauthorized(new { message = "You are not authorized to perform this action" });
_context.Tasks.Remove(existingItem);
await _context.SaveChangesAsync();
return Ok("Task 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;
}
private string GetTaskStatus(string status)
{
if (status == "Ready")
return TaskStatus.Ready;
if (status == "InProgress")
return TaskStatus.InProgress;
if (status == "Completed")
return TaskStatus.Completed;
if (status == "OnHold")
return TaskStatus.OnHold;
else
{
return "";
}
}
}