Base solution for your next web application
Open Closed

Best practice #11959


User avatar
0
Bernard created

Hi,

I wanted to know if the method below was correct and corresponded to the right way to do things given the framework : I have to update an entity named "Conventions" after adding a row of the "LigneConvention" entity in order to make the total on a field called TotalHT and update it in the Convention table

Here is the code :

` public virtual async Task CreateOrEdit(CreateOrEditLigneConventionDto input) {

 if (input.Id == null)
 {
     input.Intitule = input.FormationDisplayProperty;
     await Create(input);

     //Update Convention values
     var totalHTLg = _ligneConventionRepository.GetAll().Where(x => x.ConventionId == input.ConventionId).Sum(x => x.TotalHT);
     var conventionToUpdate = await _lookup_conventionRepository.FirstOrDefaultAsync((int)input.ConventionId);
     conventionToUpdate.TotalHT = totalHTLg + input.TotalHT;

     await _lookup_conventionRepository.UpdateAsync(conventionToUpdate);

 }
 else
 {
     await Update(input);
     
     //Update Convention values
     var ligneconvention = await _ligneConventionRepository.GetAllListAsync();
     var totalHTlg=ligneconvention.Where(x => x.ConventionId == input.ConventionId).Sum(x => x.TotalHT);
     var conventionToUpdate = await _lookup_conventionRepository.FirstOrDefaultAsync((int)input.ConventionId);
     conventionToUpdate.TotalHT = totalHTlg;

     await _lookup_conventionRepository.UpdateAsync(conventionToUpdate);
 }

}`


2 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Bernard

    Yes, you can use this approach.

  • User Avatar
    0
    Bernard created

    Thks very much 😀