Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 101 additions & 44 deletions Controllers/TaskManagerController.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Todo.Data;
using Todo.Migrations;
using System.Security.Claims;
using Todo.Models;
using Todo.Services;

namespace Todo.Controllers
{
[ApiController]

public class TaskManagerController : ControllerBase
{
private readonly TaskManagerServices _taskManagerServices;
Expand All @@ -29,7 +25,7 @@ public IActionResult Get()
var tasksToDo = _taskManagerServices.GetTasksToDo();
return Ok(tasksToDo);
}
catch (System.Exception)
catch
{
return BadRequest();
}
Expand All @@ -38,14 +34,15 @@ public IActionResult Get()
[HttpGet("/ListTaskDone")]
public IActionResult ListTaskDone()
{
try
{
try
{
var tasksDone = _taskManagerServices.GetTaskDone();
return Ok(tasksDone);
}catch(System.Exception)
}
catch
{
return BadRequest();
}
return BadRequest();
}
}

[Authorize]
Expand All @@ -56,22 +53,23 @@ public IActionResult ListAllTasks()
{
var allTasks = _taskManagerServices.GetAllTasks();
return Ok(allTasks);
}catch(System.Exception)
{
}
catch
{
return BadRequest();
}
}

[Authorize]
[HttpGet("ListTaskByUser/{userId}")]
public IActionResult ListTarefaByUser(
[FromRoute] int userId)
public IActionResult ListTarefaByUser([FromRoute] int userId)
{
try
{
var tasksByUser = _taskManagerServices.GetTasksByUser(userId);
return Ok(tasksByUser);
}catch(System.Exception)
}
catch
{
return BadRequest();
}
Expand All @@ -81,92 +79,151 @@ public IActionResult ListTarefaByUser(
[HttpGet("/GetById/{id:int}")]
public IActionResult GetById([FromRoute] int id)
{
try
{
try
{
var taskById = _taskManagerServices.GetById(id);
return Ok(taskById);
}catch(System.Exception)
}
catch
{
return BadRequest();
}
return BadRequest();
}
}

[Authorize]
[HttpPost("/insertTask/{userId}")]
public IActionResult Post(
[FromBody] TaskModel model,
[FromRoute] int userId)
public IActionResult Post([FromBody] TaskModel model, [FromRoute] int userId)
{
try
{
var task = _taskManagerServices.InsertTask(model, userId);
return Ok(task);
}catch(System.Exception)
}
catch
{
return BadRequest();
}
}
}

[Authorize]
[HttpPut("/edit/{id:int}")]
public IActionResult Put(
[FromRoute] int id,
[FromBody] TaskModel model)
public IActionResult Put([FromRoute] int id, [FromBody] TaskModel model)
{
try
{
try
{
var taskToEdit = _taskManagerServices.EditTask(model, id);
return Ok(taskToEdit);
}catch(System.Exception)
{
}
catch
{
return BadRequest();
}
}
}

[Authorize]
[HttpDelete("/delete/{id:int}")]
public IActionResult Delete(
[FromRoute] int id)
public IActionResult Delete([FromRoute] int id)
{
try
{
var editeTask = _taskManagerServices.DeleteTask(id);
return Ok(editeTask);
}catch(System.Exception)
}
catch
{
return BadRequest();
}
}

[Authorize]
[HttpPut("/done/{id:int}")]
public IActionResult Done(
[FromRoute] int id)
public IActionResult Done([FromRoute] int id)
{
try
{
var taskDone = _taskManagerServices.DoneTask(id);
return Ok(taskDone);
}catch(System.Exception)
}
catch
{
return BadRequest();
}
}

[Authorize]
[HttpPost("/asignTask")]
public IActionResult AsignTask(
[FromBody] TaskModel model)
public IActionResult AsignTask([FromBody] TaskModel model)
{
try
{
var asignTask = _taskManagerServices.AsignTask(model);
return Ok(asignTask);
}catch(System.Exception)
}
catch
{
return BadRequest();
}
}

[Authorize]
[HttpPatch("/api/tasks/{id:int}")]
public async Task<IActionResult> PatchTask([FromRoute] int id, [FromBody] TaskPatchRequest request)
{
var (userId, tenantId) = ResolveIdentity();
if (userId == null || tenantId == null)
return Unauthorized(new ProblemDetails { Title = "Token inválido", Status = 401 });

var ifMatchHeader = Request.Headers.IfMatch.FirstOrDefault()?.Trim('"');
var ifMatch = TaskManagerServices.ParseRowVersion(ifMatchHeader);

var (_, result) = await _taskManagerServices.PatchTaskAsync(id, request, userId.Value, tenantId.Value, ifMatch);
return result;
}

[Authorize]
[HttpPost("/api/tasks/{id:int}/move")]
public async Task<IActionResult> MoveTask([FromRoute] int id, [FromBody] TaskMoveRequest request)
{
var (userId, tenantId) = ResolveIdentity();
if (userId == null || tenantId == null)
return Unauthorized(new ProblemDetails { Title = "Token inválido", Status = 401 });

var (_, result) = await _taskManagerServices.MoveTaskAsync(id, request, userId.Value, tenantId.Value);
return result;
}

[Authorize]
[HttpPut("/api/tasks/{id:int}/time")]
public async Task<IActionResult> UpdateTime([FromRoute] int id, [FromBody] TaskTimeRequest request)
{
var (userId, tenantId) = ResolveIdentity();
if (userId == null || tenantId == null)
return Unauthorized(new ProblemDetails { Title = "Token inválido", Status = 401 });

var (_, result) = await _taskManagerServices.UpdateTaskTimeAsync(id, request, userId.Value, tenantId.Value);
return result;
}

[Authorize]
[HttpGet("/api/boards/{boardId:int}/tasks")]
public async Task<IActionResult> GetBoardTasks([FromRoute] int boardId)
{
var (userId, tenantId) = ResolveIdentity();
if (userId == null || tenantId == null)
return Unauthorized(new ProblemDetails { Title = "Token inválido", Status = 401 });

return await _taskManagerServices.GetBoardTasksAsync(boardId, userId.Value, tenantId.Value);
}

private (int? userId, int? tenantId) ResolveIdentity()
{
var userIdClaim = User.FindFirstValue(ClaimTypes.NameIdentifier);
var tenantClaim = User.FindFirstValue("tenant_id") ?? User.FindFirstValue("tenantId");

if (!int.TryParse(userIdClaim, out var userId)) return (null, null);
if (!int.TryParse(tenantClaim, out var tenantId)) tenantId = 1;

return (userId, tenantId);
}
}
}
}
21 changes: 18 additions & 3 deletions Data/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
using Microsoft.EntityFrameworkCore;
using Todo.Domain;

namespace Todo.Data {
public class AppDbContext : DbContext {
namespace Todo.Data
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<TaskEntity> Tasks { get; set; }
public DbSet<UserEntity> Users { get; set; }
public DbSet<CategorieTaskEntity> CategorieTasks { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<TaskEntity>()
.Property(x => x.RowVersion)
.IsRowVersion();

modelBuilder.Entity<TaskEntity>()
.Property(x => x.State)
.HasConversion<string>();
}
}
}
}
18 changes: 14 additions & 4 deletions Entitys/TaskEntity.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;

namespace Todo.Domain;

public class TaskEntity
public class TaskEntity
{
public int Id { get; set; }
public string? Title { get; set; }
public string? Description { get; set; }
public bool Done { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime FinishedAt { get; set; }
public DateTime? FinishedAt { get; set; }
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
public int UpdatedBy { get; set; }
public int CategorieTaskId { get; set; }
public int Order { get; set; }
public TaskState State { get; set; } = TaskState.Todo;
public int? EstimateMinutes { get; set; }
public int? SpentMinutes { get; set; }
public DateTime? DueDate { get; set; }
public int TenantId { get; set; } = 1;

[Timestamp]
public byte[] RowVersion { get; set; } = Array.Empty<byte>();

[ForeignKey("CategorieTaskId")]
public virtual CategorieTaskEntity? Category { get; set; }
Expand All @@ -20,5 +31,4 @@ public class TaskEntity
public virtual UserEntity? User { get; set; }

public int UserId { get; set; }

}
9 changes: 9 additions & 0 deletions Entitys/TaskState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Todo.Domain
{
public enum TaskState
{
Todo = 0,
InProgress = 1,
Done = 2
}
}
1 change: 1 addition & 0 deletions Entitys/UserEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
public class UserEntity
{
public int Id { get; set; }
public string Username { get; set; }

Check warning on line 8 in Entitys/UserEntity.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Username' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string Password { get; set; }

Check warning on line 9 in Entitys/UserEntity.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Password' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public bool IsAdmin { get; set; }
public bool IsLogged { get; set; }
public int TenantId { get; set; } = 1;
public byte[] ProfilePicture { get; set; }

Check warning on line 13 in Entitys/UserEntity.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ProfilePicture' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public DateTime CreatedAt { get; set; } = DateTime.Now;
public virtual ICollection<TaskEntity> Tasks { get; set; } = new List<TaskEntity>();

Expand Down
Loading
Loading