public async Task <CourseEnrollment> GetCourseEnrollmentAsync(string courseTitle) { Guard.NotNullOrEmpty(courseTitle, nameof(courseTitle)); var filter = Builders <BsonDocument> .Filter .Eq(Fields.CourseTitle, courseTitle); var document = await _context.Courses .Find(filter) .SingleOrDefaultAsync(); if (document == null) { return(null); } BsonDocument summaryDoc = FromBson.GetSummaryDocument(document); return(new CourseEnrollment( courseId: FromBson.GetId(document), courseVersion: FromBson.GetVersion(document), courseTitle: FromBson.GetCourseTitle(document), capacity: FromBson.GetCourseCapacity(document), students: FromBson.GetCourseStudents(document), summary: new CourseSummary( ageMin: FromBson.GetSummaryMin(summaryDoc), ageMax: FromBson.GetSummaryMax(summaryDoc), ageSum: FromBson.GetSummarySum(summaryDoc), ageAvg: FromBson.GetSummaryAvg(summaryDoc), studentCount: FromBson.GetSummaryStudentCount(summaryDoc) ) )); }
public async Task <IEnumerable <CourseSummaryListModel> > Handle(CourseSummaryListQuery query, CancellationToken ct) { var filter = Builders <BsonDocument> .Filter.Empty; var project = Builders <BsonDocument> .Projection .Include(Fields.CourseTitle) .Include(Fields.CourseCapacity) .Include(Fields.CourseSummary); var documents = await _context.Courses .Find(filter) .Project(project) .ToListAsync(); return(documents .Select(document => { var summaryDoc = FromBson.GetSummaryDocument(document); return new CourseSummaryListModel { Title = FromBson.GetCourseTitle(document), Capacity = FromBson.GetCourseCapacity(document), StudentCount = FromBson.GetSummaryStudentCount(summaryDoc), MinAge = FromBson.GetSummaryMin(summaryDoc), MaxAge = FromBson.GetSummaryMax(summaryDoc), AverageAge = FromBson.GetSummaryAvg(summaryDoc) }; }) .ToList()); }
public async Task <CourseSummaryDetailModel> Handle(CourseSummaryDetailQuery query, CancellationToken ct) { var filter = Builders <BsonDocument> .Filter .Eq(Fields.CourseTitle, query.CourseTitle); var project = Builders <BsonDocument> .Projection .Include(Fields.CourseTitle) .Include(Fields.CourseTeacher) .Include(Fields.CourseCapacity) .Include(Fields.CourseStudents) .Include(Fields.CourseSummary); var document = await _context.Courses .Find(filter) .Project(project) .SingleOrDefaultAsync(); if (document == null) { return(null); } var studentsId = FromBson.GetCourseStudents(document) .Select(id => ObjectId.Parse(id)) .ToList(); var studentsFilter = Builders <BsonDocument> .Filter .In(Fields.Id, studentsId); var studentsProject = Builders <BsonDocument> .Projection .Include(Fields.StudentEmail) .Include(Fields.StudentName); var studentDocuments = await _context.Students .Find(studentsFilter) .Project(studentsProject) .ToListAsync(); var students = studentDocuments.Select(studentDoc => { return(new StudentSummaryModel { Email = FromBson.GetStudentEmail(studentDoc), Name = FromBson.GetStudentName(studentDoc) }); }) .ToList(); var summaryDoc = FromBson.GetSummaryDocument(document); return(new CourseSummaryDetailModel { Title = FromBson.GetCourseTitle(document), Teacher = document[Fields.CourseTeacher].ToString(), Capacity = FromBson.GetCourseCapacity(document), StudentCount = FromBson.GetSummaryStudentCount(summaryDoc), MinAge = FromBson.GetSummaryMin(summaryDoc), MaxAge = FromBson.GetSummaryMax(summaryDoc), AverageAge = FromBson.GetSummaryAvg(summaryDoc), Students = students }); }
public async Task <Course> GetCourseAsync(string courseTitle) { Guard.NotNullOrEmpty(courseTitle, nameof(courseTitle)); var filter = Builders <BsonDocument> .Filter .Eq(Fields.CourseTitle, courseTitle); var document = await _context.Courses .Find(filter) .SingleOrDefaultAsync(); if (document == null) { return(null); } return(new Course( id: FromBson.GetId(document), version: FromBson.GetVersion(document), title: FromBson.GetCourseTitle(document), teacher: FromBson.GetCourseTeacher(document), capacity: FromBson.GetCourseCapacity(document) )); }