private void CopyDataRelImage(RelQuestionImageDto dto, RelQuestionImage entity) { RelQuestionImageDto dtoSource = dto; RelQuestionImage entityDestination = entity; entityDestination.IdQuestion = dtoSource.IdQuestion; entityDestination.IdImage = dtoSource.IdImage; }
protected virtual void CopyData(object dto, CranEntity entity) { if (dto is QuestionOptionDto && entity is QuestionOption) { QuestionOptionDto dtoSource = (QuestionOptionDto)dto; QuestionOption entityDestination = (QuestionOption)entity; entityDestination.IsTrue = dtoSource.IsTrue; entityDestination.Text = dtoSource.Text ?? string.Empty; entityDestination.IdQuestion = dtoSource.IdQuestion; } else if (dto is QuestionDto && entity is Question) { QuestionDto dtoSource = (QuestionDto )dto; Question entityDestination = (Question)entity; entityDestination.Title = dtoSource.Title; entityDestination.Text = dtoSource.Text ?? string.Empty; entityDestination.Explanation = dtoSource.Explanation; entityDestination.QuestionType = dtoSource.QuestionType; entityDestination.Language = Enum.Parse <Language>(dtoSource.Language); } else if (dto is RelQuestionTagDto && entity is RelQuestionTag) { RelQuestionTagDto dtoSource = (RelQuestionTagDto)dto; RelQuestionTag entityDestination = (RelQuestionTag)entity; entityDestination.IdQuestion = dtoSource.IdQuestion; entityDestination.IdTag = dtoSource.IdTag; } else if (dto is RelQuestionImageDto && entity is RelQuestionImage) { RelQuestionImageDto dtoSource = (RelQuestionImageDto)dto; RelQuestionImage entityDestination = (RelQuestionImage)entity; entityDestination.IdQuestion = dtoSource.IdQuestion; entityDestination.IdImage = dtoSource.IdImage; } else if (dto is ImageDto && entity is Image) { ImageDto dtoSource = (ImageDto)dto; Image entityDestination = (Image)entity; entityDestination.Width = dtoSource.Width; entityDestination.Height = dtoSource.Height; entityDestination.Full = dtoSource.Full; } else if (dto is CourseDto && entity is Course) { CourseDto dtoSource = (CourseDto)dto; Course entityDestination = (Course)entity; entityDestination.Title = dtoSource.Title; entityDestination.Language = Enum.Parse <Language>(dtoSource.Language); entityDestination.NumQuestionsToAsk = dtoSource.NumQuestionsToAsk; entityDestination.Description = dtoSource.Description; } else if (dto is RelCourseTagDto && entity is RelCourseTag) { RelCourseTagDto dtoSource = (RelCourseTagDto)dto; RelCourseTag entityDestination = (RelCourseTag)entity; entityDestination.IdCourse = dtoSource.IdCourse; entityDestination.IdTag = dtoSource.IdTag; } else { throw new NotImplementedException(); } }
public async Task UpdateQuestionAsync(QuestionDto questionDto) { await CheckWriteAccessToQuestion(questionDto.Id); //set the parent id foreach (var optionDto in questionDto.Options) { optionDto.IdQuestion = questionDto.Id; } Question questionEntity = await _context.FindAsync <Question>(questionDto.Id); //Options IList <QuestionOption> questionOptionEntities = await _context.QuestionOptions.Where(x => x.IdQuestion == questionEntity.Id).ToListAsync(); UpdateRelation(questionDto.Options, questionOptionEntities); //QuestionType questionEntity.QuestionType = questionDto.Options.Count(x => x.IsTrue) == 1 ? QuestionType.SingleChoice : QuestionType.MultipleChoice; //Tags IList <RelQuestionTag> relTagEntities = await _context.RelQuestionTags .Where(x => x.IdQuestion == questionEntity.Id).ToListAsync(); relTagEntities = relTagEntities.GroupBy(x => x.IdTag).Select(x => x.First()).ToList(); IDictionary <int, int> relIdByTagId = relTagEntities.ToDictionary(x => x.IdTag, x => x.Id); IList <RelQuestionTagDto> relQuestionTagDtos = new List <RelQuestionTagDto>(); IList <TagDto> tagDtos = questionDto.Tags.Where(x => x.IdTagType == (int)TagType.Standard) .GroupBy(x => x.Id).Select(x => x.First()).ToList(); foreach (TagDto tagDto in tagDtos) { RelQuestionTagDto relQuestionTagDto = new RelQuestionTagDto(); relQuestionTagDto.IdTag = tagDto.Id; relQuestionTagDto.IdQuestion = questionDto.Id; if (relIdByTagId.ContainsKey(tagDto.Id)) { relQuestionTagDto.Id = relIdByTagId[tagDto.Id]; } relQuestionTagDtos.Add(relQuestionTagDto); } UpdateRelation(relQuestionTagDtos, relTagEntities); //Image Relation IList <RelQuestionImage> relImages = await _context.RelQuestionImages.Where(x => x.IdQuestion == questionEntity.Id).ToListAsync(); IDictionary <int, int> relIdByImageId = relImages.ToDictionary(x => x.IdImage, x => x.Id); IList <RelQuestionImageDto> relImagesDtos = new List <RelQuestionImageDto>(); IList <int> binaryIds = questionDto.Images.Select(x => x.IdBinary).ToList(); IList <Image> images = await _context.Images.Where(x => binaryIds.Contains(x.IdBinary)).ToListAsync(); IDictionary <int, Image> imageByBinaryId = images.ToDictionary(x => x.IdBinary, x => x); foreach (ImageDto image in questionDto.Images) { RelQuestionImageDto relQuestionImageDto = new RelQuestionImageDto(); relQuestionImageDto.IdQuestion = questionDto.Id; relQuestionImageDto.IdImage = imageByBinaryId[image.IdBinary].Id; if (relIdByImageId.ContainsKey(relQuestionImageDto.IdImage)) { relQuestionImageDto.Id = relIdByImageId[relQuestionImageDto.IdImage]; } relImagesDtos.Add(relQuestionImageDto); } UpdateRelation(relImagesDtos, relImages); //Image Data foreach (ImageDto imageDto in questionDto.Images) { Image image = imageByBinaryId[imageDto.IdBinary]; CopyData(imageDto, image); } CopyData(questionDto, questionEntity); await SaveChangesAsync(); }