public async Task UpdateCourseAsync(CourseDto courseDto) { Course courseEntity = await this._dbContext.FindAsync <Course>(courseDto.Id); //Tags IList <RelCourseTag> relTagEntities = await _dbContext.RelCourseTags .Where(x => x.IdCourse == courseEntity.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 <RelCourseTagDto> relCourseTagDtos = new List <RelCourseTagDto>(); IList <TagDto> tagDtos = courseDto.Tags.GroupBy(x => x.Id).Select(x => x.First()).ToList(); foreach (TagDto tagDto in tagDtos) { RelCourseTagDto relCourseTag = new RelCourseTagDto(); relCourseTag.IdTag = tagDto.Id; relCourseTag.IdCourse = courseDto.Id; if (relIdByTagId.ContainsKey(tagDto.Id)) { relCourseTag.Id = relIdByTagId[tagDto.Id]; } relCourseTagDtos.Add(relCourseTag); } _dbContext.UpdateRelation(relCourseTagDtos, relTagEntities, CopyDataRelCourse); CopyDataCourse(courseDto, courseEntity); await _dbContext.SaveChangesAsync(); }
private void CopyDataRelCourse(RelCourseTagDto dto, RelCourseTag entity) { RelCourseTagDto dtoSource = dto; RelCourseTag entityDestination = entity; entityDestination.IdCourse = dtoSource.IdCourse; entityDestination.IdTag = dtoSource.IdTag; }
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(); } }