diff --git a/CHANGELOG.md b/CHANGELOG.md index a54ead2..58d17e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,10 +14,18 @@ Todas as alterações notáveis ​​neste projeto serão documentadas neste ar O formato é baseado em [Keep a Changelog (PT-BR)](https://keepachangelog.com/pt-BR/1.0.0/), e este projeto adere a [Versionamento Semântico (PT-BR)](https://semver.org/lang/pt-BR/). +## 1.0.5-RC-1 (Dec 29, 2024) + +- Add `AsNoTracking` AsNoTracking to improve query performance across multiple RPC services. +- Changes in `CustomerRpcService` + - Added query join to fetch customers including related `Person` and `Dependents` entities. + - Filtered customers based on `CustomerId` using `Ulid` parsed from the request cursor. + - Limited query results to 20 records, tracked without changes, and converted to a list asynchronously. + ## 1.0.4-RC-7 (Jun 11, 2024) - ID's are now required instead of having a default value to avoid some unintentional new ID problem. -- People Names are now indexed to make queries faster +- People "FullName" are now indexed but "MobilePhoneNumber" and "Cpf" are no longer indexed. ## 1.0.3-RC-7 (Jun 07, 2024) diff --git a/Services/AttendanceRpcService.cs b/Services/AttendanceRpcService.cs index d4513c3..5817bf1 100644 --- a/Services/AttendanceRpcService.cs +++ b/Services/AttendanceRpcService.cs @@ -47,8 +47,9 @@ public override async Task GetPaginatedAsync(Ge } List Attendances = await Query - .Take(20) - .ToListAsync(); + .Take(20) + .AsNoTracking() + .ToListAsync(); GetPaginatedAttendancesResponse response = new(); diff --git a/Services/CustomerRpcService.cs b/Services/CustomerRpcService.cs index 05074ba..5d4565f 100644 --- a/Services/CustomerRpcService.cs +++ b/Services/CustomerRpcService.cs @@ -53,6 +53,7 @@ public override async Task GetPaginatedAsync(GetP List Customers = await Query .Take(20) + .AsNoTracking() .ToListAsync(); GetPaginatedCustomersResponse response = new(); @@ -82,7 +83,13 @@ public override async Task GetByIdAsync(GetCustomerById request.CustomerId ); - Customer? Customer = await _dbContext.Customers.FindAsync(request.CustomerId); + IQueryable Query = _dbContext.Customers + .Include(c => c.Person) + .Include(c => c.Dependents) + .Where(x => x.CustomerId == Ulid.Parse(request.CustomerId)) + .Select(Customer => Customer.ToGetById()); + + GetCustomerByIdResponse? Customer = await Query.FirstOrDefaultAsync(); if (Customer is null) { @@ -102,8 +109,7 @@ public override async Task GetByIdAsync(GetCustomerById typeof(Customer).Name ); - return Customer.ToGetById(); - + return Customer; } public override async Task PostAsync(CreateCustomerRequest request, ServerCallContext context) @@ -133,7 +139,7 @@ public override async Task PostAsync(CreateCustomerRequest request, S return new VoidValue(); } - public override Task PutAsync(UpdateCustomerRequest request, ServerCallContext context) + public override async Task PutAsync(UpdateCustomerRequest request, ServerCallContext context) { string RequestTracerId = context.GetHttpContext().TraceIdentifier; string UserId = context.GetHttpContext().User.FindFirstValue(ClaimTypes.NameIdentifier)!; @@ -145,29 +151,41 @@ public override Task PutAsync(UpdateCustomerRequest request, ServerCa request.CustomerId ); + Customer? Customer = await _dbContext.Customers.FindAsync(Ulid.Parse(request.CustomerId)) + ?? throw new RpcException(new Status( + StatusCode.NotFound, $"registro não encontrado" + )); + + // Attach the entity to the context in the modified state + _dbContext.Customers.Attach(Customer); + + Customer.Person = Models.Person.FromProtoRequest( + request.Person, + Customer.CreatedBy + ); + Customer.BillingAddress = request.BillingAddress; + Customer.AdditionalInformation = request.AdditionalInformation; + /// TODO FIX right now it does not delete things removed from list + // Customer.Dependents = + // request.Dependents.Select( + // Dependent => new Models.Dependent + // { + // DependentId = Ulid.Parse(Dependent.DependentId), + // FullName = Dependent.Name, + // BirthDate = Dependent.BirthDate, + // CreatedBy = Customer.CreatedBy, + // } + // ).ToList(); + + await _dbContext.SaveChangesAsync(); + _logger.LogInformation( "({TraceIdentifier}) record ({RecordType}) updated successfully", RequestTracerId, typeof(Customer).Name ); - throw new NotImplementedException(); - - // TODO - // CustomerModel? Customer = await _dbContext.Customers.FirstOrDefaultAsync(x => x.Id == request.Id); - // if (Customer is null) - // { - // throw new RpcException(new Status( - // StatusCode.NotFound, $"registro não encontrado" - // )); - // } - - // Customer.Name = request.Name; - // // TODO Add Another fields - - // await _dbContext.SaveChangesAsync(); - // // TODO Log => Record (record type) ID Y was updated. Old value of (field name): (old value). New value: (new value). (This logs specific changes made to a field within a record) - // return new UpdateCustomerResponse(); + return new VoidValue(); } public override async Task DeleteAsync(DeleteCustomerRequest request, ServerCallContext context) @@ -175,14 +193,14 @@ public override async Task DeleteAsync(DeleteCustomerRequest request, string RequestTracerId = context.GetHttpContext().TraceIdentifier; string UserId = context.GetHttpContext().User.FindFirstValue(ClaimTypes.NameIdentifier)!; _logger.LogInformation( - "({TraceIdentifier}) User {UserID} deleting record ({RecordType}) with ID ({RecordId})", - RequestTracerId, - UserId, - typeof(Customer).Name, - request.CustomerId - ); + "({TraceIdentifier}) User {UserID} deleting record ({RecordType}) with ID ({RecordId})", + RequestTracerId, + UserId, + typeof(Customer).Name, + request.CustomerId + ); - Customer? Customer = await _dbContext.Customers.FindAsync(request.CustomerId); + Customer? Customer = await _dbContext.Customers.FindAsync(Ulid.Parse(request.CustomerId)); if (Customer is null) { @@ -197,7 +215,8 @@ public override async Task DeleteAsync(DeleteCustomerRequest request, )); } - /// TODO check if record is being used before deleting it use something like PK or FK + /// TODO check if record is being used before deleting it use something like PK or FK. + /// TODO check if every related table in the DB is deleted. _dbContext.Customers.Remove(Customer); await _dbContext.SaveChangesAsync(); diff --git a/Services/DisciplineRpcService.cs b/Services/DisciplineRpcService.cs index ec7b4eb..0ff56c1 100644 --- a/Services/DisciplineRpcService.cs +++ b/Services/DisciplineRpcService.cs @@ -49,8 +49,9 @@ public override async Task GetPaginatedAsync(Ge } List Disciplines = await Query - .Take(20) - .ToListAsync(); + .Take(20) + .AsNoTracking() + .ToListAsync(); GetPaginatedDisciplinesResponse response = new(); diff --git a/Services/InstructorRpcService.cs b/Services/InstructorRpcService.cs index f0fbe06..dfffc2d 100644 --- a/Services/InstructorRpcService.cs +++ b/Services/InstructorRpcService.cs @@ -48,6 +48,7 @@ public override async Task GetPaginatedAsync(Ge List Instructors = await Query .Take(20) + .AsNoTracking() .ToListAsync(); GetPaginatedInstructorsResponse response = new(); diff --git a/Services/NotificationRpcService.cs b/Services/NotificationRpcService.cs index b69779c..aa28ba3 100644 --- a/Services/NotificationRpcService.cs +++ b/Services/NotificationRpcService.cs @@ -48,6 +48,7 @@ public override async Task GetPaginatedAsync( List Notifications = await Query .Take(20) + .AsNoTracking() .ToListAsync(); GetPaginatedNotificationsResponse response = new(); diff --git a/Services/OrderRpcService.cs b/Services/OrderRpcService.cs index a183c7a..6dd93cc 100644 --- a/Services/OrderRpcService.cs +++ b/Services/OrderRpcService.cs @@ -55,6 +55,7 @@ public override async Task GetPaginatedAsync(GetPagi List Orders = await Query .Take(20) + .AsNoTracking() .ToListAsync(); GetPaginatedOrdersResponse response = new(); diff --git a/Services/PaymentRpcService.cs b/Services/PaymentRpcService.cs index dcd57b7..1db98ea 100644 --- a/Services/PaymentRpcService.cs +++ b/Services/PaymentRpcService.cs @@ -48,6 +48,7 @@ public override async Task GetPaginatedAsync(GetPa List Payments = await Query .Take(20) + .AsNoTracking() .ToListAsync(); GetPaginatedPaymentsResponse response = new(); diff --git a/Services/PromotionRpcService.cs b/Services/PromotionRpcService.cs index a7fd2a2..abd13ea 100644 --- a/Services/PromotionRpcService.cs +++ b/Services/PromotionRpcService.cs @@ -48,6 +48,7 @@ public override async Task GetPaginatedAsync(Get List Promotions = await Query .Take(20) + .AsNoTracking() .ToListAsync(); GetPaginatedPromotionsResponse response = new(); diff --git a/Services/ReturnRpcService.cs b/Services/ReturnRpcService.cs index e4621db..6444c80 100644 --- a/Services/ReturnRpcService.cs +++ b/Services/ReturnRpcService.cs @@ -52,6 +52,7 @@ public override async Task GetPaginatedAsync(GetPag List Returns = await Query .Take(20) + .AsNoTracking() .ToListAsync(); GetPaginatedReturnsResponse response = new(); diff --git a/Services/SaleBillingRpcService.cs b/Services/SaleBillingRpcService.cs index 881ee05..5ccf8b9 100644 --- a/Services/SaleBillingRpcService.cs +++ b/Services/SaleBillingRpcService.cs @@ -48,6 +48,7 @@ public override async Task GetPaginatedAsync(G List SaleBillings = await Query .Take(20) + .AsNoTracking() .ToListAsync(); GetPaginatedSaleBillingsResponse response = new(); diff --git a/Services/SaleRpcService.cs b/Services/SaleRpcService.cs index fe189fa..06e0c76 100644 --- a/Services/SaleRpcService.cs +++ b/Services/SaleRpcService.cs @@ -48,6 +48,7 @@ public override async Task GetPaginatedAsync(GetPagin List Sales = await Query .Take(20) + .AsNoTracking() .ToListAsync(); GetPaginatedSalesResponse response = new(); diff --git a/Services/SubscriptionBillingRpcService.cs b/Services/SubscriptionBillingRpcService.cs index d727433..d2814fc 100644 --- a/Services/SubscriptionBillingRpcService.cs +++ b/Services/SubscriptionBillingRpcService.cs @@ -48,6 +48,7 @@ public override async Task GetPaginate List SubscriptionBillings = await Query .Take(20) + .AsNoTracking() .ToListAsync(); GetPaginatedSubscriptionBillingsResponse response = new(); diff --git a/Services/SubscriptionRpcService.cs b/Services/SubscriptionRpcService.cs index 59d00a4..ae32678 100644 --- a/Services/SubscriptionRpcService.cs +++ b/Services/SubscriptionRpcService.cs @@ -48,6 +48,7 @@ public override async Task GetPaginatedAsync( List Subscriptions = await Query .Take(20) + .AsNoTracking() .ToListAsync(); GetPaginatedSubscriptionsResponse response = new(); diff --git a/Services/UserRpcService.cs b/Services/UserRpcService.cs index 8a0b45d..22cae8a 100644 --- a/Services/UserRpcService.cs +++ b/Services/UserRpcService.cs @@ -48,6 +48,7 @@ public override async Task GetPaginatedAsync(GetPagin List Users = await Query .Take(20) + .AsNoTracking() .ToListAsync(); GetPaginatedUsersResponse response = new();