public async Task <List <QuestionsDto> > Handle(GetQuestionsByInquiry.GetQuestionsByInquiryQuery request, CancellationToken cancellationToken) { var questions = await _context.Questions .Include(x => x.InputType) .Include(x => x.QuestionCategory) .Include(x => x.Inquiry) .Include(x => x.QuestionOptions) .Where(x => x.Inquiry.Id == request.InquiryId) .ToListAsync(); var questionsDto = new List <QuestionsDto>(); foreach (var question in questions) { questionsDto.Add(new QuestionsDto { Id = question.Id, Inquiry = question.Inquiry, InputType = question.InputType, QuestionCategory = question.QuestionCategory, QuestionOptions = question.QuestionOptions.ToList(), Title = question.Title, IsRequired = question.IsRequired, Images = await _photosUrl.GetImagesPath(question.Id) }); } return(questionsDto); }
public async Task <QuestionsDto> Handle(GetQuestionById.GetQuestionByIdQuery request, CancellationToken cancellationToken) { var question = await _context.Questions .Include(x => x.InputType) .Include(x => x.QuestionCategory) .Include(x => x.Inquiry) .Include(x => x.QuestionOptions) .Where(x => x.Id == request.QuestionId) .FirstOrDefaultAsync(); if (question == null) { return(null); } return(new QuestionsDto { Id = question.Id, Inquiry = question.Inquiry, InputType = question.InputType, QuestionCategory = question.QuestionCategory, QuestionOptions = question.QuestionOptions.ToList(), Title = question.Title, IsRequired = question.IsRequired, Images = await _photosUrl.GetImagesPath(question.Id) }); }
public async Task <InquiryDto> Handle(GetInquiryByIdQuery request, CancellationToken cancellationToken) { var inquiry = await _context.Inquiries .Include(x => x.Questions) .OrderBy(x => x.CreationDate) .FirstOrDefaultAsync(x => x.Id == request.InquiryId); var questions = await _context.Questions .Include(x => x.InputType) .Include(x => x.QuestionCategory) .Include(x => x.Inquiry) .Include(x => x.QuestionOptions) .Where(x => x.Inquiry.Id == inquiry.Id) .ToListAsync(); var questionsDto = new List <QuestionsDto>(); foreach (var question in questions) { questionsDto.Add(new QuestionsDto { Id = question.Id, Inquiry = question.Inquiry, InputType = question.InputType, QuestionCategory = question.QuestionCategory, QuestionOptions = question.QuestionOptions.ToList(), Title = question.Title, IsRequired = question.IsRequired, Images = await _photosUrl.GetImagesPath(question.Id) }); } return(new InquiryDto { Id = inquiry.Id, Description = inquiry.Description, Submitted = inquiry.Submitted, CreationDate = inquiry.CreationDate.Day + "-" + inquiry.CreationDate.Month + "-" + inquiry.CreationDate.Year, QuestionsDtos = questionsDto }); }
public async Task <QuestionsDto> Handle(CreateQuestionWithoutPhoto.CreateQuestionWithoutPhotoCommand request, CancellationToken cancellationToken) { var inputType = await _context.InputTypes .FirstOrDefaultAsync(x => x.Id == request.InputTypeId); if (inputType == null) { throw new NotFoundException("Input type not found"); } var questionCategory = await _context.QuestionCategories .FirstOrDefaultAsync(x => x.Id == request.QuestionCategoryId); if (questionCategory == null) { throw new NotFoundException("Question Category not not found"); } var inquiry = await _context.Inquiries .FirstOrDefaultAsync(x => x.Id == request.InquiryId); if (inquiry == null) { throw new NotFoundException("Inquiry not not found"); } using (var transaction = _context.Database.BeginTransaction()) { try { var question = new Question { Title = request.Title, Inquiry = inquiry, InputType = inputType, QuestionCategory = questionCategory, IsRequired = request.IsRequired }; _context.Questions.Add(question); if (await _context.SaveChangesAsync() < 1) { transaction.Rollback(); throw new Exception("Fail while saving Question"); } if (request.QuestionOptions.Count > 0) { foreach (var option in request.QuestionOptions) { var questionPotion = new QuestionOption { Question = question, Description = option }; _context.QuestionOptions.Add(questionPotion); } if (await _context.SaveChangesAsync() < 1) { transaction.Rollback(); throw new Exception("Fail to save option"); } } transaction.Commit(); return(new QuestionsDto { Id = question.Id, Inquiry = question.Inquiry, InputType = question.InputType, QuestionCategory = question.QuestionCategory, Title = question.Title, IsRequired = question.IsRequired, Images = await _photosUrl.GetImagesPath(question.Id) }); } catch (Exception e) { transaction.Rollback(); throw new RestException(HttpStatusCode.NotFound, e.ToString()); } } }
public async Task <QuestionsDto> Handle(CreateQuestion.CreateQuestionCommand request, CancellationToken cancellationToken) { var inputType = await _context.InputTypes .FirstOrDefaultAsync(x => x.Id == request.InputTypeId); if (inputType == null) { throw new NotFoundException("Input type not found"); } var questionCategory = await _context.QuestionCategories .FirstOrDefaultAsync(x => x.Id == request.QuestionCategoryId); if (questionCategory == null) { throw new NotFoundException("Question Category not not found"); } var inquiry = await _context.Inquiries .FirstOrDefaultAsync(x => x.Id == request.InquiryId); if (inquiry == null) { throw new NotFoundException("Inquiry not not found"); } using (var transaction = _context.Database.BeginTransaction()) { try { var question = new Question { Title = request.Title, Inquiry = inquiry, InputType = inputType, QuestionCategory = questionCategory, IsRequired = request.IsRequired }; _context.Questions.Add(question); if (await _context.SaveChangesAsync() < 1) { transaction.Rollback(); throw new Exception("Fail while saving Question"); } if (request.QuestionOptions.Count > 0) { foreach (var option in request.QuestionOptions) { var questionPotion = new QuestionOption { Question = question, Description = option }; _context.QuestionOptions.Add(questionPotion); } if (await _context.SaveChangesAsync() < 1) { transaction.Rollback(); throw new Exception("Fail to save option"); } } foreach (var file in request.Files) { var uploadDir = _configuration["UploadDir"]; var root = "/"; if (uploadDir[0] != '/') { root = _environment.ContentRootPath; } var finalUploadDir = Path.Combine(root, uploadDir); var ext = Path.GetExtension(file.FileName); var fileToken = $"{GenerateSecureString(20)}{ext}"; if (file.Length > 0) { using (var fileStream = new FileStream(Path.Combine(finalUploadDir, fileToken), FileMode.Create)) { await file.CopyToAsync(fileStream); var image = new Image { Name = file.FileName, Url = fileToken, Question = question }; _context.Images.Add(image); await _context.SaveChangesAsync(); } } } transaction.Commit(); return(new QuestionsDto { Id = question.Id, Inquiry = question.Inquiry, InputType = question.InputType, QuestionCategory = question.QuestionCategory, Title = question.Title, IsRequired = question.IsRequired, Images = await _photosUrl.GetImagesPath(question.Id) }); } catch (Exception e) { transaction.Rollback(); throw new RestException(HttpStatusCode.NotFound, e.ToString()); } } }
public async Task <QuestionsDto> Handle(UpdateQuestionCommand request, CancellationToken cancellationToken) { var question = await _context.Questions .Include(x => x.InputType) .Include(x => x.QuestionCategory) .Include(x => x.QuestionOptions) .Include(x => x.QuestionCategory) .FirstOrDefaultAsync(x => x.Id == request.QuestionId); if (question == null) { throw new NotFoundException("Question Not Found"); } var inputType = await _context.InputTypes .FirstOrDefaultAsync(x => x.Id == request.InputTypeId); if (inputType == null) { throw new NotFoundException("Input type not found"); } var questionCategory = await _context.QuestionCategories .FirstOrDefaultAsync(x => x.Id == request.QuestionCategoryId); if (questionCategory == null) { throw new NotFoundException("Question Category not found"); } using (var transaction = _context.Database.BeginTransaction()) { try { question.Title = request.Title ?? request.Title; question.InputType = inputType; question.IsRequired = request.IsRequired; question.QuestionCategory = questionCategory; _context.Entry(question).State = EntityState.Modified; if (await _context.SaveChangesAsync() > 1) { transaction.Rollback(); throw new Exception("Fail while editing Question"); } transaction.Commit(); return(new QuestionsDto { Id = question.Id, Inquiry = question.Inquiry, InputType = question.InputType, QuestionCategory = question.QuestionCategory, QuestionOptions = question.QuestionOptions.ToList(), Title = question.Title, IsRequired = question.IsRequired, Images = await _photosUrl.GetImagesPath(question.Id) }); } catch (Exception e) { transaction.Rollback(); throw new RestException(HttpStatusCode.NotFound, e.ToString()); } } }