public async Task <Unit> Handle(DeleteSurveyCommand request, CancellationToken cancellationToken) { var email = await _getUserInformation.GetUser(); var entity = await _context.Surveys .FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(Survey), request.Id); } if (entity.CreatedBy != email || entity.isDeleted) { throw new NotFoundException(nameof(Survey), request.Id); } entity.isDeleted = true; entity.ModifiedBy = email; entity.ModifiedOn = System.DateTime.UtcNow; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
/// <summary> /// Handle the request /// </summary> /// <param name="request">request object</param> /// <param name="cancellationToken">cancellation token</param> /// <returns>SurveyListViewModel</returns> public async Task <SurveyListViewModel> Handle(GetSurveyListQuery request, CancellationToken cancellationToken) { var email = await _getUserInformation.GetUser(); return(new SurveyListViewModel { Surveys = await _context.Surveys.Where(x => x.CreatedBy == email && !x.isDeleted).ProjectTo <SurveyLookupModel>(_mapper.ConfigurationProvider).ToListAsync(cancellationToken) }); }
public async Task <Unit> Handle(UpdateSurveyCommand request, CancellationToken cancellationToken) { var email = await _getUserInformation.GetUser(); var entity = await _context.Surveys .SingleOrDefaultAsync(c => c.Id == request.Id, cancellationToken); if (entity == null) { throw new NotFoundException(nameof(Survey), request.Id); } if (entity.CreatedBy != email || entity.isDeleted) { throw new NotFoundException(nameof(Survey), request.Id); } entity.Title = request.Title; if (request.FormFields != null && request.FormFields.Count > 0) { if (entity.SurveyForms == null) { entity.SurveyForms = new List <SurveyFormField>(); } request.FormFields.ToList().ForEach(x => { entity.SurveyForms.Add(new SurveyFormField() { FormConfiguration = x.FormConfiguration, FormTypes = x.FormTypes, Title = x.Title }); }); } entity.ModifiedBy = email; entity.ModifiedOn = DateTime.UtcNow; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <SurveyDetailModel> Handle(GetSurveyDetailQuery request, CancellationToken cancellationToken) { var email = await _getUserInformation.GetUser(); var entity = await _context.Surveys .FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(Surveys), request.Id); } if (entity.CreatedBy == email && !entity.isDeleted) { return(SurveyDetailModel.Create(entity)); } else { throw new NotFoundException(nameof(Surveys), request.Id); } }
/// <summary> /// Handle is called by mediaR to perform the operation and notify the event subscriber. /// </summary> /// <param name="request">request object</param> /// <param name="cancellationToken">cancellation token</param> /// <returns>Unit</returns> public async Task <Unit> Handle(CreateSurveyCommand request, CancellationToken cancellationToken) { var email = await _getUserInformation.GetUser(); var entity = new SurveyEntity.Survey { Title = request.Title, CreatedBy = email, CreatedOn = DateTime.UtcNow, ModifiedBy = email, ModifiedOn = DateTime.UtcNow, isDeleted = false }; entity.SurveyForms = new List <SurveyEntity.SurveyFormField>(); if (request.FormFields != null && request.FormFields.Count > 0) { request.FormFields.ToList().ForEach(x => { entity.SurveyForms.Add(new SurveyEntity.SurveyFormField() { FormConfiguration = x.FormConfiguration, FormTypes = x.FormTypes, Title = x.Title }); }); } _context.Surveys.Add(entity); await _context.SaveChangesAsync(cancellationToken); await _mediator.Publish(new SurveyCreated(), cancellationToken); return(Unit.Value); }