protected override async Task <IEnumerable <ExamResultReadModel> > ProcessAsync(ExamResultCommand <ExamResultCreateModel> message, CancellationToken cancellationToken) { var exam = await _dataContext.TrainingExams .ProjectTo <TrainingExamReadModel>(_configurationProvider) .ToListAsync(cancellationToken) .ConfigureAwait(false); if (exam.Count == 0) { throw new DomainException(422, $"Training Exam is null., Please Create question in exam module.,"); } var dbSet = _dataContext.Set <Data.Entities.TraineeExamResult>(); foreach (var item in exam) { var map = _mapper.Map(item, message.Exam); dbSet.AddIfNotExists(x => new { x.Id, x.QuestionId, x.CourseId, x.UserId }, _mapper.Map <TraineeExamResult>(map)); } var result = await _dataContext .SaveChangesAsync(cancellationToken) .ConfigureAwait(false); var examresult = _dataContext.ExamResults.Include(x => x.Question) .Where(x => x.UserId == message.Exam.UserId && x.CourseId == message.Exam.CourseId); return(_mapper.Map <IEnumerable <ExamResultReadModel> >(examresult)); }
public static bool CheckTableExists <T>(this TrainDTrainorContext context) where T : class { try { context.Set <T>().Count(); return(true); } catch (Exception) { return(false); } }
protected override async Task <PaymentReadModel> ProcessAsync(PaymentApprovalCommand <Guid, PaymentTransaction, PaymentReadModel> message, CancellationToken cancellationToken) { var command = new EntityIdentifierQuery <Guid, PaymentTransaction, PaymentReadModel>(message.Id, message.Principal); var result = await Mediator.Send(command, cancellationToken).ConfigureAwait(false); if (result == null) { throw new DomainException(422, $"Payment Id '{message.Id}' not found."); } if (result.Status != message.PaymentStatus) { var commandCourse = new EntityIdentifierQuery <Guid, TrainingCourse, CourseReadModel>(result.CourseId.Value, message.Principal); var resultCourse = await Mediator.Send(commandCourse, cancellationToken).ConfigureAwait(false); if (resultCourse == null) { throw new DomainException(422, $"Course Id '{result.CourseId}' not found."); } if (resultCourse.MaxAttendee == resultCourse.NoAttendee) { throw new DomainException(422, $"Course '{resultCourse.Title}' is already full."); } var mapCourse = _mapper.Map <CourseUpdateModel>(resultCourse); mapCourse.MaxAttendee = mapCourse.MaxAttendee - 1; mapCourse.NoAttendee = mapCourse.NoAttendee + 1; var updateCourse = new EntityUpdateCommand <Guid, TrainingCourse, CourseUpdateModel, CourseReadModel>(resultCourse.Id, mapCourse, message.Principal); var mediatorCourse = await Mediator.Send(updateCourse, cancellationToken) .ConfigureAwait(false); result.Status = message.PaymentStatus; var map = _mapper.Map <PaymentUpdateModel>(result); var update = new EntityUpdateCommand <Guid, PaymentTransaction, PaymentUpdateModel, PaymentReadModel>(message.Id, map, message.Principal); result = await Mediator.Send(update, cancellationToken).ConfigureAwait(false); var history = _mapper.Map <PaymentTransactionHistory>(result); var dbSetHistory = _dataContext.Set <Data.Entities.PaymentTransactionHistory>(); await dbSetHistory.AddAsync(history, cancellationToken).ConfigureAwait(false); await _dataContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); var mapAttendee = _mapper.Map <TrainingBuildCoursesAttendeeCreatedModel>(result); var courseAttendee = new EntityCreateCommand <Core.Data.Entities.TrainingBuildCourseAttendee, TrainingBuildCoursesAttendeeCreatedModel, TrainingBuildCoursesAttendeeReadModel>(mapAttendee, message.Principal); var resultCourseAttendee = await Mediator.Send(courseAttendee, cancellationToken).ConfigureAwait(false); } return(result); }
protected override async Task <TrainingVideoReadModel> ProcessAsync(TrainingVideoCommand <TrainingVideoCreateModel> message, CancellationToken cancellationToken) { var records = new TrainingVideoReadModel(); var current = _mapper.Map <TrainingVideo>(message.Model); var result = _mapper.Map <CreateResult>(_dataContext.FindFolder(new TrainingVideoPic() { Name = typeof(TrainingVideoPic).Name })); if (result == null) { result = _dataContext.CreateDir(new TrainingVideoPic() { Name = typeof(TrainingVideoPic).Name, ParentPath = null }); } if (message.Model.File != null) { if (message.Model.File.Length > 0) { var file = message.Model.File; var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); using (var ms = new MemoryStream()) { file.CopyTo(ms); var now = DateTimeOffset.Now; result = _dataContext.CreateFile(new TrainingVideoPic() { Name = Guid.NewGuid() + "_" + message.Model.File.FileName, File_stream = ms.ToArray(), ParentPath = result.Path, Creation_time = now, Last_access_time = now, Last_write_time = now }); _mapper.Map(result, current); } } } if (result != null) { var dbSet = _dataContext.Set <TrainingVideo>(); await dbSet.AddAsync(current, cancellationToken).ConfigureAwait(false); var status = await _dataContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); current = await _dataContext.TrainingVideo .Where(x => x.Id == current.Id).SingleOrDefaultAsync().ConfigureAwait(false); if (status != 0) { _mapper.Map(current, records); var videofile = _dataContext.TrainingVideoPicFiletable.GetLevelVidoePicId(current.FileId); _mapper.Map(videofile.FirstOrDefault(), records); return(records); } else { Logger.LogWarning($"Unable to insert training video, please try again later or contact administrator."); throw new DomainException(422, $"Unable to insert training video, please try again later or contact administrator."); } } else { Logger.LogWarning($"Unable to insert training video, please try again later or contact administrator."); throw new DomainException(422, $"Unable to insert training video, please try again later or contact administrator."); } }
protected override async Task <TrainingVideoReadModel> ProcessAsync(TrainingVideoUpdateCommand <TrainingVideoUpdateModel, Guid> message, CancellationToken cancellationToken) { if (message.Id == null) { throw new DomainException(422, $"Id is null , Please try again."); } var current = await _dataContext.TrainingVideo.GetByKeyAsync (message.Id); current = current == null?_mapper.Map <Data.Entities.TrainingVideo>(message.TrainingVideo) : _mapper.Map(message.TrainingVideo, current); CreateResult result = new CreateResult(); result = _mapper.Map <CreateResult>(_dataContext.FindFolder(new PaymentTransactionPic() { Name = typeof(PaymentTransactionPic).Name })); if (result == null) { result = _dataContext.CreateDir(new PaymentTransactionPic() { Name = typeof(PaymentTransactionPic).Name, ParentPath = null }); } if (message.TrainingVideo.File != null) { if (message.TrainingVideo.File.Length > 0) { var file = message.TrainingVideo.File; var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); using (var ms = new MemoryStream()) { file.CopyTo(ms); var now = DateTimeOffset.Now; var payment = new PaymentTransactionPic() { Name = Guid.NewGuid() + "_" + message.TrainingVideo.File.FileName, File_stream = ms.ToArray(), ParentPath = result.Path, Creation_time = now, Last_access_time = now, Last_write_time = now, Stream_id = current == null?Guid.NewGuid() : current.FileId }; result = _dataContext.UpdateFile(payment); } _mapper.Map(result, current); } } if (result != null) { if (current.Id == Guid.Empty) { var dbSet = _dataContext.Set <Data.Entities.TrainingVideo>(); await dbSet.AddAsync(current, cancellationToken).ConfigureAwait(false); } var status = await _dataContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); if (status != 0) { return(_mapper.Map <TrainingVideoReadModel>(current)); } else { Logger.LogWarning($"Unable to update training video, please try again later or contact administrator."); throw new DomainException(422, $"Unable to update training video, please try again later or contact administrator."); } } else { Logger.LogWarning($"Unable to update training video, please try again later or contact administrator."); throw new DomainException(422, $"Unable to update training video, please try again later or contact administrator."); } }
protected override async Task <PaymentReadModel> ProcessAsync(PaymentCommand message, CancellationToken cancellationToken) { CreateResult result = new CreateResult(); if (message.Payment.UserProfileId == null) { throw new DomainException(422, $"User Profile Id is null , Please try again."); } var current = _dataContext.PaymentTransactions .GetByUserProfileId(message.Payment.UserProfileId.Value).Result; if (!message.Payment.CourseId.HasValue) { throw new DomainException(422, $"Course is required for training registration and payments."); } var course = _dataContext.TrainingCourse.GetByKey(message.Payment.CourseId.Value); if (course == null) { throw new DomainException(422, $"Course is not exists."); } if (course.NoAttendee == course.MaxAttendee) { throw new DomainException(422, $"Course is already full., Please select other course or contact System Administrator."); } if (current != null) { throw new DomainException(422, $"You already register to training, Please contact System Administrator."); } current = current == null?_mapper.Map <Data.Entities.PaymentTransaction>(message.Payment) : _mapper.Map(message.Payment, current); result = _mapper.Map <CreateResult>(_dataContext.FindFolder(new PaymentTransactionPic() { Name = typeof(PaymentTransactionPic).Name })); if (result == null) { result = _dataContext.CreateDir(new PaymentTransactionPic() { Name = typeof(PaymentTransactionPic).Name, ParentPath = null }); } if (message.Payment.File != null) { if (message.Payment.File.Length > 0) { var file = message.Payment.File; var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); using (var ms = new MemoryStream()) { file.CopyTo(ms); var now = DateTimeOffset.Now; var payment = new PaymentTransactionPic() { Name = Guid.NewGuid() + "_" + message.Payment.File.FileName, File_stream = ms.ToArray(), ParentPath = result.Path, Creation_time = now, Last_access_time = now, Last_write_time = now, Stream_id = current.FileId ?? Guid.NewGuid() }; result = _dataContext.CreateFile(payment); } _mapper.Map(result, current); } } if (result != null) { if (current.Id == Guid.Empty) { var dbSet = _dataContext.Set <Data.Entities.PaymentTransaction>(); await dbSet .AddAsync(current, cancellationToken) .ConfigureAwait(false); } var status = await _dataContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); if (status != 0) { var msg = string.Format(@"Request for TOT payment approval. Id: {0}", current.Id); await _sMSTemplateService.ForApproval(_smsConfiguration.Value.Admin, msg) .ConfigureAwait(false); var history = _mapper.Map <Data.Entities.PaymentTransactionHistory>(current); var dbSetHistory = _dataContext.Set <Data.Entities.PaymentTransactionHistory>(); await dbSetHistory.AddAsync(history, cancellationToken).ConfigureAwait(false); await _dataContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); return(_mapper.Map <PaymentReadModel>(current)); } else { Logger.LogWarning($"Unable to update user your payment, please try again later or contact administrator."); throw new DomainException(422, $"Unable to update user your payment, please try again later or contact administrator."); } } else { Logger.LogWarning($"Unable to update user your payment, please try again later or contact administrator."); throw new DomainException(422, $"Unable to update user your payment, please try again later or contact administrator."); } }