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
6 changes: 6 additions & 0 deletions src/Api/Controllers/AuthController.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,6 +50,8 @@ public async Task<ActionResult<Result<LoginResult>>> Login([FromBody] LoginModel

[Authorize]
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> Logout()
{
var refreshToken = Request.Cookies[nameof(RefreshToken)];
Expand All@@ -66,6 +68,8 @@ public async Task<IActionResult> Logout()
}

[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> Refresh()
{
var refreshToken = Request.Cookies[nameof(RefreshToken)];
Expand All@@ -81,6 +85,8 @@ public async Task<IActionResult> Refresh()

[Authorize]
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public IActionResult Validate()
{
return Ok();
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Controllers/DepartmentsController.cs
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
using Application.Common.Models;
using Application.Departments.Commands.CreateDepartment;
using Application.Departments.Commands.AddDepartment;
using Application.Departments.Queries.GetAllDepartments;
using Application.Identity;
using Application.Users.Queries;
Expand All@@ -20,7 +20,7 @@ public class DepartmentsController : ApiControllerBase
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<ActionResult<Result<DepartmentDto>>> CreateDepartment([FromBody] CreateDepartmentCommand command)
public async Task<ActionResult<Result<DepartmentDto>>> AddDepartment([FromBody] AddDepartmentCommand command)
{
var result = await Mediator.Send(command);
return Ok(Result<DepartmentDto>.Succeed(result));
Expand Down
8 changes: 4 additions & 4 deletions src/Api/Controllers/DocumentsController.cs
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
using Application.Common.Models;
using Application.Common.Models.Dtos.Physical;
using Application.Departments.Commands.CreateDepartment;
using Application.Documents.Commands.ImportDocument;
using Application.Documents.Queries.GetAllDocumentsPaginated;
using Application.Documents.Queries.GetDocumentById;
using Application.Documents.Queries.GetDocumentTypes;
using Application.Identity;
using Application.Users.Queries;
using Infrastructure.Identity.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers;

public class DocumentsController : ApiControllerBase
{
[RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)]
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)]
public async Task<ActionResult<Result<DocumentDto>>> ImportDocument([FromBody] ImportDocumentCommand command)
{
var result = await Mediator.Send(command);
Expand All@@ -31,7 +29,6 @@ public async Task<ActionResult<Result<DocumentDto>>> ImportDocument([FromBody] I
[HttpGet("types")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]

public async Task<ActionResult<Result<IEnumerable<string>>>> GetAllDocumentTypes()
{
var result = await Mediator.Send(new GetAllDocumentTypesQuery());
Expand DownExpand Up@@ -61,6 +58,9 @@ public async Task<ActionResult<Result<PaginatedList<DocumentDto>>>> GetAllDocume
}

[HttpGet("{id:guid}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<Result<DocumentDto>>> GetDocumentById(Guid id)
{
var query = new GetDocumentByIdQuery()
Expand Down
10 changes: 8 additions & 2 deletions src/Api/Controllers/FoldersController.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,9 +10,9 @@ namespace Api.Controllers;

public class FoldersController : ApiControllerBase
{
[RequiresRole(IdentityData.Roles.Staff)]
[RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)]
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
Expand All@@ -23,7 +23,13 @@ public async Task<ActionResult<Result<FolderDto>>> AddFolder([FromBody] AddFolde
return Ok(Result<FolderDto>.Succeed(result));
}

[RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)]
[HttpPut("disable")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<ActionResult<Result<FolderDto>>> DisableFolder([FromBody] DisableFolderCommand command)
{
var result = await Mediator.Send(command);
Expand Down
13 changes: 13 additions & 0 deletions src/Api/Controllers/LockersController.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,6 +14,7 @@ public class LockersController : ApiControllerBase
[RequiresRole(IdentityData.Roles.Staff)]
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
Expand All@@ -23,14 +24,26 @@ public async Task<ActionResult<Result<LockerDto>>> AddLocker([FromBody] AddLocke
return Ok(Result<LockerDto>.Succeed(result));
}

[RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)]
[HttpPut("disable")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<ActionResult<Result<LockerDto>>> DisableLocker([FromBody] DisableLockerCommand command)
{
var result = await Mediator.Send(command);
return Ok(Result<LockerDto>.Succeed(result));
}

[RequiresRole(IdentityData.Roles.Admin, IdentityData.Roles.Staff)]
[HttpPut("enable")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<ActionResult<Result<LockerDto>>> EnableLocker([FromBody] EnableLockerCommand command)
{
var result = await Mediator.Send(command);
Expand Down
18 changes: 12 additions & 6 deletions src/Api/Controllers/RoomsController.cs
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
using Application.Common.Models;
using Application.Common.Models.Dtos.Physical;
using Application.Identity;
using Application.Rooms.Commands.CreateRoom;
using Application.Rooms.Commands.AddRoom;
using Application.Rooms.Commands.DisableRoom;
using Application.Rooms.Commands.RemoveRoom;
using Application.Rooms.Queries.GetEmptyContainersPaginated;
Expand All@@ -15,9 +15,11 @@ public class RoomsController : ApiControllerBase
[RequiresRole(IdentityData.Roles.Admin)]
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<Result<RoomDto>>> AddRoom(CreateRoomCommand command)
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<ActionResult<Result<RoomDto>>> AddRoom(AddRoomCommand command)
{
var result = await Mediator.Send(command);
return Ok(Result<RoomDto>.Succeed(result));
Expand All@@ -34,20 +36,24 @@ public async Task<ActionResult<PaginatedList<EmptyLockerDto>>> GetEmptyContainer
return Ok(Result<PaginatedList<EmptyLockerDto>>.Succeed(result));
}

[HttpPut]
[RequiresRole(IdentityData.Roles.Admin)]
[HttpPut("disable")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<ActionResult<Result<RoomDto>>> DisableRoom(DisableRoomCommand command)
{
var result = await Mediator.Send(command);
return Ok(Result<RoomDto>.Succeed(result));
}

[RequiresRole(IdentityData.Roles.Admin)]
[HttpDelete]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<ActionResult<Result<RoomDto>>> RemoveRoom(RemoveRoomCommand command)
{
var result = await Mediator.Send(command);
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Controllers/StaffsController.cs
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
using Application.Common.Models;
using Application.Identity;
using Application.Staffs.Commands.CreateStaff;
using Application.Staffs.Commands.AddStaff;
using Application.Users.Queries.Physical;
using Infrastructure.Identity.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All@@ -14,7 +14,7 @@ public class StaffsController : ApiControllerBase
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<Result<StaffDto>>> CreateStaff([FromBody] CreateStaffCommand command)
public async Task<ActionResult<Result<StaffDto>>> AddStaff([FromBody] AddStaffCommand command)
{
var result = await Mediator.Send(command);
return Ok(Result<StaffDto>.Succeed(result));
Expand Down
11 changes: 6 additions & 5 deletions src/Api/Controllers/UsersController.cs
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
using Application.Common.Models;
using Application.Identity;
using Application.Users.Commands.CreateUser;
using Application.Users.Commands.AddUser;
using Application.Users.Commands.DisableUser;
using Application.Users.Queries;
using Application.Users.Queries.GetUsersByName;
Expand All@@ -12,19 +12,19 @@ namespace Api.Controllers;

public class UsersController : ApiControllerBase
{
[RequiresRole(IdentityData.Roles.Admin)]
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<ActionResult<Result<UserDto>>> CreateUser([FromBody] CreateUserCommand command)
public async Task<ActionResult<Result<UserDto>>> AddUser([FromBody] AddUserCommand command)
{
var result = await Mediator.Send(command);
return Ok(Result<UserDto>.Succeed(result));
}

[Authorize]

[RequiresRole(IdentityData.Roles.Admin)]
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
Expand All@@ -41,11 +41,12 @@ public async Task<ActionResult<Result<PaginatedList<UserDto>>>> GetUsersByName(s
return Ok(Result<PaginatedList<UserDto>>.Succeed(result));
}

[HttpPost("disable")]
[RequiresRole(IdentityData.Roles.Admin)]
[HttpPost("disable")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<ActionResult<Result<UserDto>>> DisableUser([FromBody] DisableUserCommand command)
{
var result = await Mediator.Send(command);
Expand Down
14 changes: 7 additions & 7 deletions src/Api/Middlewares/ExceptionMiddleware.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,25 +54,25 @@ private async Task HandleExceptionAsync(HttpContext context, Exception ex)
}


private async void HandleKeyNotFoundException(HttpContext context, Exception ex)
private static async void HandleKeyNotFoundException(HttpContext context, Exception ex)
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
await WriteExceptionMessageAsync(context, ex);
}

private async void HandleConflictException(HttpContext context, Exception ex)
private static async void HandleConflictException(HttpContext context, Exception ex)
{
context.Response.StatusCode = StatusCodes.Status409Conflict;
await WriteExceptionMessageAsync(context, ex);
}

private async void HandleNotAllowedException(HttpContext context, Exception ex)
private static async void HandleNotAllowedException(HttpContext context, Exception ex)
{
context.Response.StatusCode = StatusCodes.Status406NotAcceptable;
await WriteExceptionMessageAsync(context, ex);
}

private async void HandleRequestValidationException(HttpContext context, Exception ex)
private static async void HandleRequestValidationException(HttpContext context, Exception ex)
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;

Expand All@@ -86,13 +86,13 @@ private async void HandleRequestValidationException(HttpContext context, Excepti
await context.Response.Body.WriteAsync(SerializeToUtf8BytesWeb(result));
}

private async void HandleLimitExceededException(HttpContext context, Exception ex)
private static async void HandleLimitExceededException(HttpContext context, Exception ex)
{
context.Response.StatusCode = StatusCodes.Status409Conflict;
await WriteExceptionMessageAsync(context, ex);
}

private async void HandleAuthenticationException(HttpContext context, Exception ex)
private static async void HandleAuthenticationException(HttpContext context, Exception ex)
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await WriteExceptionMessageAsync(context, ex);
Expand All@@ -104,7 +104,7 @@ private static async void HandleUnauthorizedAccessException(HttpContext context,
await WriteExceptionMessageAsync(context, ex);
}

private async void HandleInvalidOperationException(HttpContext context, Exception ex)
private static async void HandleInvalidOperationException(HttpContext context, Exception ex)
{
context.Response.StatusCode = StatusCodes.Status409Conflict;
await WriteExceptionMessageAsync(context, ex);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,30 +6,30 @@
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Application.Departments.Commands.CreateDepartment;
namespace Application.Departments.Commands.AddDepartment;

public record CreateDepartmentCommand : IRequest<DepartmentDto>
public record AddDepartmentCommand : IRequest<DepartmentDto>
{
public string Name { get; init; }
public string Name { get; init; } = null!;
}

public class CreateDepartmentCommandHandler : IRequestHandler<CreateDepartmentCommand, DepartmentDto>
public class AddDepartmentCommandHandler : IRequestHandler<AddDepartmentCommand, DepartmentDto>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
public CreateDepartmentCommandHandler(IApplicationDbContext context, IMapper mapper)
public AddDepartmentCommandHandler(IApplicationDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}

public async Task<DepartmentDto> Handle(CreateDepartmentCommand request, CancellationToken cancellationToken)
public async Task<DepartmentDto> Handle(AddDepartmentCommand request, CancellationToken cancellationToken)
{
var department = await _context.Departments.FirstOrDefaultAsync(x => x.Name.Equals(request.Name), cancellationToken);
var department = await _context.Departments.FirstOrDefaultAsync(x => x.Name.Trim().ToLower().Equals(request.Name.Trim().ToLower()), cancellationToken);

if (department is not null)
{
throw new ConflictException("Department name already exists");
throw new ConflictException("Department name already exists.");
}
var entity = new Department
{
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,9 +10,9 @@ namespace Application.Documents.Commands.ImportDocument;

public record ImportDocumentCommand : IRequest<DocumentDto>
{
public string Title { get; init; }
public string Title { get; init; } = null!;
public string? Description { get; init; }
public string DocumentType { get; init; }
public string DocumentType { get; init; } = null!;
public Guid ImporterId { get; init; }
public Guid FolderId { get; init; }
}
Expand All@@ -35,23 +35,23 @@ public async Task<DocumentDto> Handle(ImportDocumentCommand request, Cancellatio
.FirstOrDefaultAsync(x => x.Id == request.ImporterId, cancellationToken);
if (importer is null)
{
throw new KeyNotFoundException("User does not exist");
throw new KeyNotFoundException("User does not exist.");
}

var document = _context.Documents.FirstOrDefault(x =>
x.Title.Equals(request.Title)
x.Title.Trim().ToLower().Equals(request.Title.Trim().ToLower())
&& x.Importer != null
&& x.Importer.Id == request.ImporterId);
if (document is not null)
{
throw new ConflictException($"Document title already exists for user {importer.LastName}");
throw new ConflictException($"Document title already exists for user {importer.LastName}.");
}

var folder = await _context.Folders
.FirstOrDefaultAsync(x => x.Id == request.FolderId, cancellationToken);
if (folder is null)
{
throw new KeyNotFoundException("Folder does not exist");
throw new KeyNotFoundException("Folder does not exist.");
}

var entity = new Document()
Expand Down
Loading