Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Validation and notification logic before update #6717


User avatar
0
mgcode created

Hi, with the following snippet of code I have implemented a customAppService method (SubmitQuestionnaireForm) in which I retrieve the object quesionnaire from questionnaireRepository and I set the status property to "UploadedByClient" value.

Then I call the CreateOrEdit default method in which I try to enforce some logic using the previous and new values. In real time, I realised that both questionnaire and input had the new value.

Please advice on which is the proper way to achieve this functionality and perform validation and notifications logic prior to update.

        public async Task<bool> SubmitQuestionnaireForm(object obj, int id)
        {
            var questionnaire = await _questionnaireRepository.GetAsync(id);
            questionnaire.Status = QuestionnaireStatus.UploadedByClient;

            CreateOrEditQuestionnaireDto questionnaireDto = new CreateOrEditQuestionnaireDto();
            questionnaireDto = ObjectMapper.Map<CreateOrEditQuestionnaireDto>(questionnaire);
            await _questionnairesAppService.CreateOrEdit(questionnaireDto);

            return true;
        }    
  
  
  public async Task CreateOrEdit(CreateOrEditQuestionnaireDto input)
         {
            if(input.Id == null){
				await Create(input);
			}
			else{
				await Update(input);
			}
         }
         
         

        private async Task Update(CreateOrEditQuestionnaireDto input)
        {
            var questionnaire = await _questionnaireRepository.FirstOrDefaultAsync((int)input.Id);
            
            bool sendIMS_Questionnaire_Update_Status_QuestionnaireUploadedNotification = false;

            if (questionnaire.Status != QuestionnaireStatus.UploadedByClient && input.Status == QuestionnaireStatus.UploadedByClient)
            {
                sendIMS_Questionnaire_Update_Status_QuestionnaireUploadedNotification = true;
            }

            ObjectMapper.Map(input, questionnaire);

            if (sendIMS_Questionnaire_Update_Status_QuestionnaireUploadedNotification)
            {
                await _appNotifier.IMS_Questionnaire_Update_Status_QuestionnaireUploaded(questionnaire);
            }
        }

Thank you in advance for your time and help


1 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Don't modify questionaire directly.

    - var questionnaire = await _questionnaireRepository.GetAsync(id);
    - questionnaire.Status = QuestionnaireStatus.UploadedByClient;
    -
    - CreateOrEditQuestionnaireDto questionnaireDto = new CreateOrEditQuestionnaireDto();
    - questionnaireDto = ObjectMapper.Map<CreateOrEditQuestionnaireDto>(questionnaire);
    + var questionnaireDto = await _questionnaireRepository.GetAll()
    +     .Where(q => q.Id == id)
    +     .Select(q => ObjectMapper.Map<CreateOrEditQuestionnaireDto>(q))
    +     .FirstAsync();
    + questionnaireDto.Status = QuestionnaireStatus.UploadedByClient;