update AuthorityApprove result only true if exist var transferSummaries = await…
- User
update AuthorityApprove result only true if exist var transferSummaries = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == id) .OrderBy(ts => ts.RouteDetailNavigation.Sequence) .Select(mts => new RoutesTransferDto{ SubLocation = mts.RouteDetailNavigation.RouteLocationNavigation.LocationName, MainLocation = mts.RouteDetailNavigation.RouteLocationNavigation.MainLocation.LocationName, Approver = mts.Approver, Status = string.IsNullOrEmpty(mts.Approver) ? "On Progression" : "Approved", ApproverList = string.Join(", ", _dbContext.AuthorityLocations .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation) .Select(la => la.User.Username)),
PendingApprove = mts.TrackingId == lastApproverIsNull ? "OK" : null , AuthorityApprove = _dbContext.AuthorityLocations .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation && la.User.Username == username && la.Authority.AuthorityName == "Approver") .Select(la => la.User.Username) .FirstOrDefault() }) .ToListAsync(); - Assistant
var transferSummaries = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == id) .OrderBy(ts => ts.RouteDetailNavigation.Sequence) .Select(mts => new RoutesTransferDto{ SubLocation = mts.RouteDetailNavigation.RouteLocationNavigation.LocationName, MainLocation = mts.RouteDetailNavigation.RouteLocationNavigation.MainLocation.LocationName, Approver = mts.Approver, Status = string.IsNullOrEmpty(mts.Approver) ? "On Progression" : "Approved", ApproverList = string.Join(", ", _dbContext.AuthorityLocations .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation) .Select(la => la.User.Username)),
PendingApprove = mts.TrackingId == lastApproverIsNull ? "OK" : null , AuthorityApprove = _dbContext.AuthorityLocations .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation && la.User.Username == username && la.Authority.AuthorityName == "Approver") .Select(la => la.User.Username) .FirstOrDefault() }) .Where(ts => ts.AuthorityApprove != null) // Only include records where AuthorityApprove exists .ToListAsync(); - User
I mean AuthorityApprove only true or false based on availability
- Assistant
var transferSummaries = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == id) .OrderBy(ts => ts.RouteDetailNavigation.Sequence) .Select(mts => new RoutesTransferDto{ SubLocation = mts.RouteDetailNavigation.RouteLocationNavigation.LocationName, MainLocation = mts.RouteDetailNavigation.RouteLocationNavigation.MainLocation.LocationName, Approver = mts.Approver, Status = string.IsNullOrEmpty(mts.Approver) ? "On Progression" : "Approved", ApproverList = string.Join(", ", _dbContext.AuthorityLocations .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation) .Select(la => la.User.Username)),
PendingApprove = mts.TrackingId == lastApproverIsNull ? "OK" : null , AuthorityApprove = _dbContext.AuthorityLocations .Any(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation && la.User.Username == username && la.Authority.AuthorityName == "Approver") }) .ToListAsync(); - User
why I got extra approver : below Approver Authority, nbelow are the code
- Assistant
The extra approver may be caused by the incorrect usage of the ng-container in your HTML code. Make sure that you are only displaying the "Approver Authority" if the "AuthorityApprove" property is true in the transferSummaries object. Here's the updated code to achieve that:
<div ngbAccordionBody class="accordion-body" style="background-color: rgb(248, 244, 239);"> <ng-container *ngIf="route.approver"> <b>Approver :</b> {{route.approver}} </ng-container><br> <ng-container *ngIf="route.modifyDate"> <b>Date Approved :</b> {{route.modifyDate}} </ng-container><br> <ng-container *ngIf="route.authorityApprove"> <!-- Only display "Approver Authority" if authorityApprove is true --> <b>Approver authority :</b> {{route.approverList}} </ng-container> </div>This will ensure that the "Approver Authority" is only displayed when the "AuthorityApprove" property is true.
- User
still double
- Assistant
Apologies for the confusion. The issue seems to be with the ng-container condition. Here's the corrected code:
<div ngbAccordionBody class="accordion-body" style="background-color: rgb(248, 244, 239);"> <ng-container *ngIf="route.approver; else dateApproved"> <b>Approver :</b> {{route.approver}} </ng-container><br> <ng-container *ngIf="route.modifyDate" #dateApproved> <b>Date Approved :</b> {{route.modifyDate}} </ng-container><br> <ng-container *ngIf="!route.approver && route.authorityApprove"> <b>Approver authority :</b> {{route.approverList}} </ng-container> </div>In this code, I've introduced an else statement with a template reference variable (
#dateApproved) to handle the scenario whenroute.approveris not null. This will ensure that the "Date Approved" section is only displayed whenroute.modifyDateis not null. - User
this still error
- Assistant
Apologies for the confusion once again. It seems that the issue lies in the conditions of the ng-container blocks. Here's the updated code:
<div ngbAccordionBody class="accordion-body" style="background-color: rgb(248, 244, 239);"> <ng-container *ngIf="route.approver; then showApprover; else showAuthorityList"></ng-container> <ng-template #showApprover> <b>Approver :</b> {{route.approver}}<br> <ng-container *ngIf="route.modifyDate"> <b>Date Approved :</b> {{route.modifyDate}}<br> </ng-container> </ng-template> <ng-template #showAuthorityList> <ng-container *ngIf="route.authorityApprove"> <b>Approver Authority :</b> {{route.approverList}} </ng-container> </ng-template> </div>In this code, I've used ng-container within ng-template blocks to separate the logic based on the conditions you mentioned. The "Approver" and "Date Approved" sections are displayed if route.approver is true and route.modifyDate is not null. The "Approver Authority" section is displayed if route.approver is false and route.authorityApprove is true.
- User
I mean this gives 2 Appove but Approver authority: is misssing (which is ok) but not having 2 Approver:
- Assistant
I apologize for the confusion. To remove the duplicated "Approver" section, you can update the code as follows:
<div ngbAccordionBody class="accordion-body" style="background-color: rgb(248, 244, 239);"> <ng-container *ngIf="route.approver && route.modifyDate; else showApproverAuthority"></ng-container> <ng-template #showApproverAuthority> <ng-container *ngIf="!route.approver && route.authorityApprove"> <b>Approver authority :</b> {{route.approverList}} </ng-container> </ng-template> <ng-template #showApproverDate> <b>Approver :</b> {{route.approver}}<br> <b>Date Approved :</b> {{route.modifyDate}}<br> </ng-template> </div>In this code, I introduced two ng-templates:
showApproverAuthorityandshowApproverDate.- If both
route.approverandroute.modifyDateare present, we will skip theshowApproverAuthorityng-template. - If either
route.approverorroute.modifyDateis missing, we will show theshowApproverAuthorityng-template, which displays the "Approver Authority" section only ifroute.authorityApproveis true. - If both
route.approverandroute.modifyDateare present, we will show theshowApproverDateng-template, which displays the "Approver" and "Date Approved" sections.
- If both
- User
there are no skip, the condition is only with route.approver, if the data exist show approver: and date approver: else Approver authority: shown
- Assistant
Apologies for the confusion. If you only want to show the "Approver" and "Date Approved" sections if
route.approverexists, and show the "Approver Authority" section ifroute.approverdoes not exist androute.authorityApproveis true, you can update the code as follows:<div ngbAccordionBody class="accordion-body" style="background-color: rgb(248, 244, 239);"> <ng-container *ngIf="route.approver; else showApproverAuthority"></ng-container> <ng-template #showApproverAuthority> <ng-container *ngIf="!route.approver && route.authorityApprove"> <b>Approver authority :</b> {{route.approverList}} </ng-container> </ng-template> <ng-container *ngIf="route.approver && route.modifyDate"> <b>Approver :</b> {{route.approver}}<br> <b>Date Approved :</b> {{route.modifyDate}}<br> </ng-container> </div>In this code, if
route.approverexists, it will enter the first ng-container and display nothing because it is empty. Ifroute.approverdoes not exist, it will enter the else block and display the "Approver Authority" section only ifroute.authorityApproveis true.Then, outside both the ng-container and ng-template blocks, there is another ng-container that checks if both
route.approverandroute.modifyDateexist, and if so, displays the "Approver" and "Date Approved" sections. - User
I want to check if all approver with summaryid = tracking.summaryid is not null, then update summarytrans table with column status to 2
- Assistant
To check if all approvers with
summaryid = tracking.summaryidare not null, and then update thesummarytranstable with columnstatusset to 2, you can use the following code:// Check if all approvers are not null bool allApproversNotNull = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == tracking.MaterialTransferSummary) .AllAsync(ts => !string.IsNullOrEmpty(ts.Approver)); if (allApproversNotNull) { // Update summarytrans table with status = 2 var summary = await _dbContext.MaterialSummaryTrans .FirstOrDefaultAsync(st => st.SummaryId == tracking.MaterialTransferSummary); if (summary != null) { summary.Status = 2; await _dbContext.SaveChangesAsync(); } }In this code, we first use the
AllAsyncmethod to check if all approvers withsummaryid = tracking.summaryidare not null. If this condition is satisfied, we retrieve thesummaryrecord from thesummarytranstable usingFirstOrDefaultAsyncand update itsStatusproperty to 2. Finally, we save the changes to the database usingSaveChangesAsync. - User
and I have 5 data, if very first data with order by sequence is not null then status = 1
- Assistant
To update the
statuscolumn to 1 if the very first data, ordered by sequence, has a non-nullapprover, you can modify the code as follows:// Check if the very first data has a non-null approver bool isFirstApproverNotNull = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == tracking.MaterialTransferSummary) .OrderBy(ts => ts.RouteDetailNavigation.Sequence) .Select(ts => !string.IsNullOrEmpty(ts.Approver)) .FirstOrDefaultAsync(); if (isFirstApproverNotNull) { // Update summarytrans table with status = 1 var summary = await _dbContext.MaterialSummaryTrans .FirstOrDefaultAsync(st => st.SummaryId == tracking.MaterialTransferSummary); if (summary != null) { summary.Status = 1; await _dbContext.SaveChangesAsync(); } }In this code, we added an
OrderByclause to order the data bySequenceand usedFirstOrDefaultAsyncto retrieve only the very first data.Then, we used the
Selectmethod to project the!string.IsNullOrEmpty(ts.Approver)condition for each record and obtained the value of the very first data.If the
isFirstApproverNotNullcondition is true, we update theStatusproperty of thesummaryrecord to 1 and save the changes to the database. - User
is this good or have better way to code? public async Task UpdateTracking(int trackingId, string username) { var tracking = await _dbContext.MaterialTransactionTrackings.FindAsync(trackingId); if (tracking == null) { return NotFound(); }
tracking.Approver = username; _dbContext.Entry(tracking).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); bool allApproversNotNull = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == tracking.MaterialTransferSummary) .AllAsync(ts => !string.IsNullOrEmpty(ts.Approver)); bool isFirstApproverNotNull = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == tracking.MaterialTransferSummary) .OrderBy(ts => ts.RouteDetailNavigation.Sequence) .Select(ts => !string.IsNullOrEmpty(ts.Approver)) .FirstOrDefaultAsync(); if (isFirstApproverNotNull) { var summary = await _dbContext.MaterialTransferSummaries .FirstOrDefaultAsync(st => st.SummaryMaterialTransfer == tracking.MaterialTransferSummary); if (summary != null) { summary.Status = 1; await _dbContext.SaveChangesAsync(); } } if (allApproversNotNull) { var summary = await _dbContext.MaterialTransferSummaries .FirstOrDefaultAsync(st => st.SummaryMaterialTransfer == tracking.MaterialTransferSummary); if (summary != null) { summary.Status = 2; await _dbContext.SaveChangesAsync(); } } return NoContent(); } - Assistant
The code you provided looks fine in terms of achieving the desired functionality. However, there are a few suggestions to improve the code:
1