Skip to content

Commit 0c1369a

Browse files
RodrigoDevRodrigoDev
authored andcommitted
ajustes para front-base
1 parent d62be33 commit 0c1369a

File tree

17 files changed

+172
-59
lines changed

17 files changed

+172
-59
lines changed

Api/Base/BaseController.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private async Task Log(object request, BaseResponse response, Exception ex)
7373
jsonConfig.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
7474

7575
_logger.Log(
76-
request.GetType().Name,
76+
request?.GetType()?.Name ?? "null",
7777
JsonSerializer.Serialize(information, options: jsonConfig),
7878
response.Success
7979
? LogTypeEnum.Information
@@ -94,28 +94,13 @@ protected async Task<ObjectResult> HandleApplicationResponse<R>(object request,
9494

9595
try
9696
{
97-
if(request == null)
98-
{
99-
return await GetApiResponse
100-
(
101-
new()
102-
{
103-
Response = null,
104-
Success = false,
105-
ErrorMessage = "Invalid request",
106-
Code = 400
107-
},
108-
request
109-
);
110-
}
111-
11297
if(!ModelState.IsValid)
11398
{
11499
string error = ModelState.Values
115100
.FirstOrDefault()
116101
?.Errors?
117102
.FirstOrDefault()?
118-
.ErrorMessage ?? "Unknow error";
103+
.ErrorMessage ?? "Invalid Request";
119104

120105
if(error.EndsWith('.'))
121106
error = error[..^1];
@@ -124,7 +109,7 @@ protected async Task<ObjectResult> HandleApplicationResponse<R>(object request,
124109
(
125110
new()
126111
{
127-
Response = null,
112+
Data = null,
128113
Success = false,
129114
ErrorMessage = error,
130115
Code = 400
@@ -148,7 +133,7 @@ protected async Task<ObjectResult> HandleApplicationResponse<R>(object request,
148133
(
149134
new()
150135
{
151-
Response = null,
136+
Data = null,
152137
Success = false,
153138
ErrorMessage = ex.Message,
154139
Code = 500

Api/Base/BaseResponse.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/// </summary>
66
public class BaseResponse
77
{
8-
public object Response { get; set; }
8+
public object Data { get; set; }
99
public bool Success { get; set; }
1010
public string ErrorMessage { get; set; }
1111
public int Code { get; set; }
@@ -16,7 +16,7 @@ public class BaseResponse
1616
/// </summary>
1717
public class BaseResponse<T>
1818
{
19-
public T Response { get; set; }
19+
public T Data { get; set; }
2020
public bool Success { get; set; }
2121
public string ErrorMessage { get; set; }
2222
public int Code { get; set; }

Api/Controllers/AuthenticationController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task<IActionResult> Login([FromBody] LoginCommand command)
3535
var response = new BaseResponse
3636
{
3737
Success = resp.Success,
38-
Response = resp.Content,
38+
Data = resp.Content,
3939
ErrorMessage = resp.Message,
4040
Code = resp.Success ? 200 : 400
4141
};
@@ -61,7 +61,7 @@ public async Task<IActionResult> SendEmailVerification([FromBody] SendEmailVerif
6161
var response = new BaseResponse
6262
{
6363
Success = resp.Success,
64-
Response = null,
64+
Data = null,
6565
ErrorMessage = resp.Message,
6666
Code = resp.Success ? 200 : 400
6767
};
@@ -87,7 +87,7 @@ public async Task<IActionResult> SendResetPasswordVerification([FromBody] SendRe
8787
var response = new BaseResponse
8888
{
8989
Success = resp.Success,
90-
Response = null,
90+
Data = null,
9191
ErrorMessage = resp.Message,
9292
Code = resp.Success ? 200 : 400
9393
};
@@ -113,7 +113,7 @@ public async Task<IActionResult> SignUp([FromBody] SignUpCommand command)
113113
var response = new BaseResponse
114114
{
115115
Success = resp.Success,
116-
Response = null,
116+
Data = null,
117117
ErrorMessage = resp.Message,
118118
Code = resp.Success ? 200 : 400
119119
};
@@ -139,7 +139,7 @@ public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordCommand c
139139
var response = new BaseResponse
140140
{
141141
Success = resp.Success,
142-
Response = null,
142+
Data = null,
143143
ErrorMessage = resp.Message,
144144
Code = resp.Success ? 200 : 400
145145
};
@@ -165,7 +165,7 @@ public async Task<IActionResult> VerifyEmailInUse([FromQuery] VerifyEmailUsedQue
165165
var response = new BaseResponse
166166
{
167167
Success = resp.Success,
168-
Response = resp.Content,
168+
Data = resp.Content,
169169
ErrorMessage = resp.Message,
170170
Code = resp.Success ? 200 : 400
171171
};

Api/Controllers/CategoryController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task<IActionResult> CreateCategory([FromBody] CreateCategoryCommand
3838
return new()
3939
{
4040
Success = resp.Success,
41-
Response = null,
41+
Data = null,
4242
ErrorMessage = resp.Message,
4343
Code = resp.Success ? 201 : 400
4444
};
@@ -63,7 +63,7 @@ public async Task<IActionResult> UpdateCategory([FromBody] UpdateCategoryCommand
6363
return new()
6464
{
6565
Success = resp.Success,
66-
Response = null,
66+
Data = null,
6767
ErrorMessage = resp.Message,
6868
Code = resp.Success ? 200 : 400
6969
};
@@ -79,7 +79,7 @@ public async Task<IActionResult> UpdateCategory([FromBody] UpdateCategoryCommand
7979
[SwaggerResponse(StatusCodes.Status200OK, Type = typeof(BaseResponse))]
8080
[SwaggerResponse(StatusCodes.Status400BadRequest, Type = typeof(BaseResponse))]
8181
[SwaggerResponse(StatusCodes.Status500InternalServerError, Type = typeof(BaseResponse))]
82-
public async Task<IActionResult> DeleteCategory([FromBody] DeleteCategoryCommand command)
82+
public async Task<IActionResult> DeleteCategory([FromQuery] DeleteCategoryCommand command)
8383
{
8484
return await HandleApplicationResponse<Operation>(
8585
command,
@@ -88,7 +88,7 @@ public async Task<IActionResult> DeleteCategory([FromBody] DeleteCategoryCommand
8888
return new()
8989
{
9090
Success = resp.Success,
91-
Response = null,
91+
Data = null,
9292
ErrorMessage = resp.Message,
9393
Code = resp.Success ? 200 : 400
9494
};
@@ -113,7 +113,7 @@ public async Task<IActionResult> GetCategory([FromQuery] GetCategoryQuery query)
113113
return new()
114114
{
115115
Success = resp.Success,
116-
Response = resp.Content,
116+
Data = resp.Content,
117117
ErrorMessage = resp.Message,
118118
Code = resp.Success ? 200 : 400
119119
};
@@ -138,7 +138,7 @@ public async Task<IActionResult> GetAll([FromQuery] GetAllCategoriesQuery query)
138138
return new()
139139
{
140140
Success = resp.Success,
141-
Response = resp.Content,
141+
Data = resp.Content,
142142
ErrorMessage = resp.Message,
143143
Code = resp.Success ? 200 : 400
144144
};

Api/Controllers/TaskController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task<IActionResult> CreateTask([FromBody] CreateTaskCommand command
3838
return new()
3939
{
4040
Success = resp.Success,
41-
Response = null,
41+
Data = null,
4242
ErrorMessage = resp.Message,
4343
Code = resp.Success ? 201 : 400
4444
};
@@ -63,7 +63,7 @@ public async Task<IActionResult> UpdateTask([FromBody] UpdateTaskCommand command
6363
return new()
6464
{
6565
Success = resp.Success,
66-
Response = null,
66+
Data = null,
6767
ErrorMessage = resp.Message,
6868
Code = resp.Success ? 200 : 400
6969
};
@@ -79,7 +79,7 @@ public async Task<IActionResult> UpdateTask([FromBody] UpdateTaskCommand command
7979
[SwaggerResponse(StatusCodes.Status200OK, Type = typeof(BaseResponse))]
8080
[SwaggerResponse(StatusCodes.Status400BadRequest, Type = typeof(BaseResponse))]
8181
[SwaggerResponse(StatusCodes.Status500InternalServerError, Type = typeof(BaseResponse))]
82-
public async Task<IActionResult> DeleteTask([FromBody] DeleteTaskCommand command)
82+
public async Task<IActionResult> DeleteTask([FromQuery] DeleteTaskCommand command)
8383
{
8484
return await HandleApplicationResponse<Operation>(
8585
command,
@@ -88,7 +88,7 @@ public async Task<IActionResult> DeleteTask([FromBody] DeleteTaskCommand command
8888
return new()
8989
{
9090
Success = resp.Success,
91-
Response = null,
91+
Data = null,
9292
ErrorMessage = resp.Message,
9393
Code = resp.Success ? 200 : 400
9494
};
@@ -113,7 +113,7 @@ public async Task<IActionResult> GetTask([FromQuery] GetTaskQuery query)
113113
return new()
114114
{
115115
Success = resp.Success,
116-
Response = resp.Content,
116+
Data = resp.Content,
117117
ErrorMessage = resp.Message,
118118
Code = resp.Success ? 200 : 400
119119
};
@@ -138,7 +138,7 @@ public async Task<IActionResult> GetPaged([FromQuery] GetPagedTasksQuery query)
138138
return new()
139139
{
140140
Success = resp.Success,
141-
Response = resp.Content,
141+
Data = resp.Content,
142142
ErrorMessage = resp.Message,
143143
Code = resp.Success ? 200 : 400
144144
};

Api/Controllers/UserController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task<IActionResult> UpdateUsername([FromBody] UpdateUsernameCommand
3434
return new()
3535
{
3636
Success = resp.Success,
37-
Response = null,
37+
Data = null,
3838
ErrorMessage = resp.Message,
3939
Code = resp.Success ? 200 : 400
4040
};
@@ -59,7 +59,7 @@ public async Task<IActionResult> UpdateUserPassword([FromBody] UpdateUserPasswor
5959
return new()
6060
{
6161
Success = resp.Success,
62-
Response = null,
62+
Data = null,
6363
ErrorMessage = resp.Message,
6464
Code = resp.Success ? 200 : 400
6565
};
@@ -84,7 +84,7 @@ public async Task<IActionResult> DeleteUser([FromBody] DeleteUserCommand command
8484
return new()
8585
{
8686
Success = resp.Success,
87-
Response = null,
87+
Data = null,
8888
ErrorMessage = resp.Message,
8989
Code = resp.Success ? 200 : 400
9090
};

Api/Program.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,27 @@ public static void Main(string[] args)
88

99
builder.Services.AddControllers();
1010
builder.Services.AddEndpointsApiExplorer();
11-
11+
builder.Services.AddCors(options =>
12+
{
13+
options.AddPolicy("Default",
14+
policy => policy
15+
.AllowAnyOrigin()
16+
.AllowAnyMethod()
17+
.AllowAnyHeader());
18+
});
19+
1220
IoC.Resolver.Invoke(builder.Services, builder.Configuration);
1321

1422
var app = builder.Build();
1523

1624
app.UseSwagger();
1725
app.UseSwaggerUI();
18-
19-
app.UseHttpsRedirection();
26+
app.UseCors("Default");
2027
app.UseAuthentication();
2128
app.UseRouting();
2229
app.UseAuthorization();
2330
app.MapControllers();
24-
31+
2532
app.Use((context, next) =>
2633
{
2734
context.Request.EnableBuffering();

Application/CQRS/Tasks/GetPagedTasks/GetPagedTasksHandler.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using MediatR;
22
using Domain.Base;
3+
using Domain.Utils;
4+
using Domain.Models.Tasks;
5+
using Domain.Models.Responses;
36
using Domain.Interfaces.Repositories;
47
using Domain.Interfaces.Authentication;
58
using TaskEntity = Domain.Entities.Task;
69
using Microsoft.Extensions.DependencyInjection;
7-
using Domain.Models.Responses;
810

911
namespace Application.CQRS.Tasks.GetPagedTasks
1012
{
@@ -27,8 +29,29 @@ public async Task<Result<GetPagedResponse<TaskEntity>>> Handle(GetPagedTasksQuer
2729
if (request == null)
2830
return Result.MakeFailure<GetPagedResponse<TaskEntity>>("Invalid request");
2931

32+
if(request.Priority != null && !request.Priority.Value.IsInRange())
33+
return Result.MakeFailure<GetPagedResponse<TaskEntity>>("Invalid priority filter in request");
34+
35+
if (request.Status != null && !request.Status.Value.IsInRange())
36+
return Result.MakeFailure<GetPagedResponse<TaskEntity>>("Invalid status filter in request");
37+
38+
if (request.Ordering != null && !request.Ordering.Value.IsInRange())
39+
return Result.MakeFailure<GetPagedResponse<TaskEntity>>("Invalid ordering filter in request");
40+
3041
var userId = _tokenService.GetToken().Id;
31-
var tasks = await _taskRepo.GetPaged(userId, request.PageNumber, request.PageSize);
42+
var tasks = await _taskRepo.GetPaged(
43+
request.PageNumber,
44+
request.PageSize,
45+
new TasksFilterModel
46+
{
47+
CategoryId = request.CategoryId,
48+
Name = request.Name,
49+
Status = request.Status,
50+
UserId = userId,
51+
Priority = request.Priority,
52+
},
53+
request.Ordering
54+
);
3255

3356
return Result.MakeSuccess(tasks);
3457
}

Application/CQRS/Tasks/GetPagedTasks/GetPagedTasksQuery.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using MediatR;
22
using Domain.Base;
3+
using Domain.Enums;
34
using Domain.Models.Responses;
45
using Task = Domain.Entities.Task;
56
using System.ComponentModel.DataAnnotations;
67

78
namespace Application.CQRS.Tasks.GetPagedTasks
89
{
910
/// <summary>
10-
/// Query to paged tasks
11+
/// Query to paged tasks, with filters and ordering
1112
/// </summary>
1213
public class GetPagedTasksQuery : IRequest<Result<GetPagedResponse<Task>>>
1314
{
@@ -18,5 +19,20 @@ public class GetPagedTasksQuery : IRequest<Result<GetPagedResponse<Task>>>
1819
[Required]
1920
[Range(1, 100)]
2021
public int PageSize { get; set; }
22+
23+
[Range(1, int.MaxValue)]
24+
public int? CategoryId { get; set; }
25+
26+
[MaxLength(128)]
27+
public string Name { get; set; }
28+
29+
[Range(0, int.MaxValue)]
30+
public TaskStatusEnum? Status { get; set; }
31+
32+
[Range(0, int.MaxValue)]
33+
public TaskPriorityEnum? Priority { get; set; }
34+
35+
[Range(0, int.MaxValue)]
36+
public TaskOrderByEnum? Ordering { get; set; }
2137
}
2238
}

Domain/Enums/TaskOrderByEnum.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Domain.Enums
2+
{
3+
public enum TaskOrderByEnum
4+
{
5+
IdAsc,
6+
IdDesc,
7+
NameAsc,
8+
NameDesc,
9+
DueDateAsc,
10+
DueDateDesc,
11+
}
12+
}

0 commit comments

Comments
 (0)