Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Development" #40

Merged
merged 1 commit into from
May 29, 2024
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
20 changes: 5 additions & 15 deletions src/HigiaServer.API/Endpoints/AuthenticationEndpoint.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Security.Claims;
using AutoMapper;

using HigiaServer.Application.Contracts.Requests;
using HigiaServer.Application.Contracts.Responses;
using HigiaServer.Application.Errors;
using HigiaServer.Application.Repositories;
using HigiaServer.Application.Services;
using HigiaServer.Domain.Entities;
using Microsoft.AspNetCore.Authentication;

namespace HigiaServer.API.Endpoints;

Expand Down Expand Up @@ -45,23 +44,14 @@ public static IEndpointRouteBuilder AddAuthenticationEndpoint(this IEndpointRout
# region private methods

private static async Task<IResult> HandleRegister(
HttpContext context,
RegisterRequest request,
IUserRepository repository,
IMapper mapper,
IJwtTokenService jwtTokenService
)
{
if (!context.User!.Identity!.IsAuthenticated)
{
throw new UnauthenticatedException();
}
if (context.User.FindFirstValue(ClaimTypes.Role) != "admin")
{
throw new UnauthorizedAccessException();
}

if (await repository.GetUserByEmail(request.Email) != null) throw new DuplicateEmailException(request.Email);
if (await repository.GetUserByEmail(request.Email) != null)
throw new DuplicateEmailException(request.Email);

request.Password = BCrypt.Net.BCrypt.HashPassword(request.Password);
var user = mapper.Map<User>(request);
Expand All @@ -85,8 +75,8 @@ IJwtTokenService jwtTokenService
{
if (await repository.GetUserByEmail(request.Email) is not { } user)
throw new EmailGivenNotFoundException(request.Email);
if (!BCrypt.Net.BCrypt.Verify(request.Password, user.Password)) throw new InvalidPasswordException();
if (!BCrypt.Net.BCrypt.Verify(request.Password, user.Password))
throw new InvalidPasswordException();

var authResponse = new AuthenticationResponse(
mapper.Map<UserResponse>(user),
Expand Down
59 changes: 28 additions & 31 deletions src/HigiaServer.API/Endpoints/TaskEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public static class TaskEndpoint
{
public static IEndpointRouteBuilder AddTaskEndpoint(this IEndpointRouteBuilder app)
{
var taskEndpoint = app.MapGroup("higia-server/api/tasks").WithTags("Tasks");
var authEndpoint = app.MapGroup("higia-server/api/tasks").WithTags("Tasks");

// add task
taskEndpoint.MapPost("/", HandleAddTask)
authEndpoint.MapPost("/", HandleAddTask)
.WithName("Add new task")
.Produces<TaskResponse>(StatusCodes.Status201Created)
.WithOpenApi(x =>
Expand All @@ -25,7 +25,7 @@ public static IEndpointRouteBuilder AddTaskEndpoint(this IEndpointRouteBuilder a
});

// get task by id
taskEndpoint.MapGet("/{taskId:guid}", HandleGetTask)
authEndpoint.MapGet("/{taskId:guid}", HandleGetTask)
.WithName("Get task by id")
.Produces<TaskResponse>()
.WithOpenApi(x =>
Expand All @@ -35,7 +35,7 @@ public static IEndpointRouteBuilder AddTaskEndpoint(this IEndpointRouteBuilder a
});

// update status
taskEndpoint.MapPatch("/{taskId:guid}/{status}", HandleUpdateTaskStatus)
authEndpoint.MapPatch("/{taskId:guid}/{status}", HandleUpdateTaskStatus)
.WithName("Update Task Status")
.WithOpenApi(x =>
{
Expand All @@ -44,7 +44,7 @@ public static IEndpointRouteBuilder AddTaskEndpoint(this IEndpointRouteBuilder a
});

// update task info
taskEndpoint.MapPut("/{taskId:guid}/info", HandleUpdateTaskInformation)
authEndpoint.MapPut("/{taskId:guid}/info", HandleUpdateTaskInformation)
.WithName("Update Task")
.WithOpenApi(x =>
{
Expand All @@ -53,7 +53,7 @@ public static IEndpointRouteBuilder AddTaskEndpoint(this IEndpointRouteBuilder a
});

// add collaborator to task
taskEndpoint.MapPatch("/{taskId:guid}/collaborators/{collaboratorId:guid}", HandleAddCollaboratorToTask)
authEndpoint.MapPatch("/{taskId:guid}/collaborators/{collaboratorId:guid}", HandleAddCollaboratorToTask)
.WithName("Add collaborator to task")
.WithOpenApi(x =>
{
Expand All @@ -62,7 +62,7 @@ public static IEndpointRouteBuilder AddTaskEndpoint(this IEndpointRouteBuilder a
});

// delete task
taskEndpoint.MapDelete("/{taskId:guid}", HandleDeleteTask)
authEndpoint.MapDelete("/{taskId:guid}", HandleDeleteTask)
.WithName("Delete task by id")
.WithOpenApi(x =>
{
Expand All @@ -71,16 +71,14 @@ public static IEndpointRouteBuilder AddTaskEndpoint(this IEndpointRouteBuilder a
});

// remove collaborator from task
taskEndpoint.MapPatch("/{taskId:guid}/{collaboratorId:guid}", HandleRemoveCollaboratorToTask)
authEndpoint.MapPatch("/{taskId:guid}/{collaboratorId:guid}", HandleRemoveCollaboratorToTask)
.WithName("Remove collaborator to task")
.WithOpenApi(x =>
{
x.Summary = "Remove collaborator to task";
return x;
});



return app;
}

Expand All @@ -95,19 +93,19 @@ private static async Task<IResult> HandleRemoveCollaboratorToTask(
{
CheckAuthorizationAsAdministrator(context);
if (await taskRepository.GetTaskById(taskId) is not { } task)
return Results.BadRequest(new BaseResponse("The request could not be continued because no matching tasks were found", false));
return Results.BadRequest(new BaseSuccessResponse("The request could not be continued because no matching tasks were found", false));

if (await userRepository.GetUserById(collaboratorId) is not { } collaborator)
return Results.BadRequest(new BaseResponse("The request could not be continued because no matching collaborator were found", false));
return Results.BadRequest(new BaseSuccessResponse("The request could not be continued because no matching collaborator were found", false));

if (!task.Collaborators.Any(c => c.Id == collaboratorId!))
return Results.BadRequest(new BaseResponse("Unable to update task because no matching task was found", false));
return Results.BadRequest(new BaseSuccessResponse("Unable to update task because no matching task was found", false));

context.Response.Headers.Location = $"{context.Request.Scheme}://{context.Request.Host}/{context.Request.Path}/{task.Id}";
task.RemoveCollaboratorFromTask(collaborator);

taskRepository.UpdateTask(task);
return Results.Ok(new BaseResponse("Collaborator successfully removed from task"));
return Results.Ok(new BaseSuccessResponse("Collaborator successfully removed from task"));
}

private static async Task<IResult> HandleDeleteTask(
Expand All @@ -118,11 +116,11 @@ private static async Task<IResult> HandleDeleteTask(
CheckAuthorizationAsAdministrator(context);
if (await taskRepository.GetTaskById(taskId) is not { } task)
{
return Results.BadRequest(new BaseResponse("The request could not be continued because no matching tasks were found", false));
return Results.BadRequest(new BaseSuccessResponse("The request could not be continued because no matching tasks were found", false));
}

taskRepository.DeleteTask(taskId);
return Results.Ok(new BaseResponse("task deleted successfully"));
return Results.Ok(new BaseSuccessResponse("task deleted successfully"));
}

private static async Task<IResult> HandleAddCollaboratorToTask(
Expand All @@ -134,19 +132,19 @@ private static async Task<IResult> HandleAddCollaboratorToTask(
UpdateTaskRequest request)
{
CheckAuthorizationAsAdministrator(context);

if (await taskRepository.GetTaskById(taskId) is not { } task)
return Results.BadRequest(new BaseResponse("Unable to update task because no matching task was found", false));
{
return Results.BadRequest(new BaseSuccessResponse("Unable to update task because no matching task was found", false));
}

if (await userRepository.GetUserById(collaboratorId) is not { } collaborator)
return Results.BadRequest(new BaseResponse($"Collaborator with id {collaboratorId} was not found!", false));

if (collaborator.IsAdmin)
return Results.BadRequest(new BaseResponse("Only collaborators can be added to the task.", false));
{
return Results.BadRequest(new BaseSuccessResponse($"Collaborator with id {collaboratorId} was not found!", false));
}

if (task.Collaborators.Contains(collaborator))
{
return Results.BadRequest(new BaseResponse(
return Results.BadRequest(new BaseSuccessResponse(
$"The collaborator with id {collaboratorId} is already participating in this task",
false
));
Expand All @@ -157,7 +155,7 @@ private static async Task<IResult> HandleAddCollaboratorToTask(

context.Response.Headers.Location = $"{context.Request.Scheme}://{context.Request.Host}/{context.Request.Path}/{task.Id}";

return Results.Ok(new BaseResponse("collaborator successfully added to task"));
return Results.Ok(new BaseSuccessResponse("collaborator successfully added to task"));
}

private static async Task<IResult> HandleUpdateTaskInformation(
Expand All @@ -169,7 +167,7 @@ private static async Task<IResult> HandleUpdateTaskInformation(
CheckAuthorizationAsAdministrator(context);
if (await taskRepository.GetTaskById(taskId) is not { } task)
{
return Results.BadRequest(new BaseResponse("Unable to update task because no matching task was found", false));
return Results.BadRequest(new BaseSuccessResponse("Unable to update task because no matching task was found", false));
}

task.UpdateTask(
Expand All @@ -182,7 +180,7 @@ private static async Task<IResult> HandleUpdateTaskInformation(

context.Response.Headers.Location =
$"{context.Request.Scheme}://{context.Request.Host}/{context.Request.Path}/{taskId}";
return Results.Ok(new BaseResponse("task information updated successfully"));
return Results.Ok(new BaseSuccessResponse("task information updated successfully"));
}

private static async Task<IResult> HandleUpdateTaskStatus(
Expand All @@ -192,12 +190,14 @@ private static async Task<IResult> HandleUpdateTaskStatus(
ITaskRepository taskRepository)
{
CheckAuthorizationAsAdministrator(context);
if (await taskRepository.GetTaskById(taskId) is not { } task) return Results.NoContent();
if (await taskRepository.GetTaskById(taskId) is not { } task)
return Results.NoContent();

task.UpdateTaskStatus(status);
taskRepository.UpdateTask(task);

context.Response.Headers.Location = "{context.Request.Scheme}://{context.Request.Host}/{context.Request.Path}/{task.Id}";
context.Response.Headers.Location =
$"{context.Request.Scheme}://{context.Request.Host}/{context.Request.Path}/{task.Id}";
return Results.Ok("task status updated successfully");
}

Expand All @@ -221,9 +221,6 @@ await userRepository.GetUserById(id)
)
).ToList();

var hasAdmin = collaborators.FindAll(c => c.IsAdmin).ToList();
if (hasAdmin.Count != 0) return Results.BadRequest(new BaseResponse("Only collaborators can be added to the task.", false));

task.AddCollaboratorsToTask(collaborators);
taskRepository.AddTask(task);

Expand Down
56 changes: 0 additions & 56 deletions src/HigiaServer.API/Endpoints/UserEndpoint.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace HigiaServer.Application.Contracts.Responses;

public class BaseResponse(string message, bool success = true)
public class BaseSuccessResponse(string message, bool success = true)
{
public bool Success { get; private set; } = success;
public string Message { get; private set; } = message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ public interface IUserRepository
void AddUser(User user);
Task<User?> GetUserByEmail(string email);
Task<User?> GetUserById(Guid userId);
System.Threading.Tasks.Task UpdateUser(User user);
}
Loading
Loading