Esempio n. 1
0
        public override async Task <IReadOnlyCollection <StudentReadModel> > ExecuteAsync(
            GetUserStudentsQuery query,
            CancellationToken cancellationToken = default)
        {
            List <StudentReadModel> students;

            await using (var connection = new NpgsqlConnection(_connectionString))
            {
                students = (await connection.QueryAsync <StudentReadModel>(
                                Sql,
                                new { userId = query.UserId })).ToList();
            }

            var contacts = await _queryProcessor.ExecuteAsync(new GetUserContactsQuery(query.UserId), cancellationToken);

            foreach (var student in students)
            {
                student.Contacts = contacts.Where(c => c.StudentId == student.Id)
                                   .ToList();
            }

            return(students);
        }
Esempio n. 2
0
        private async Task EnsureStudentDoesNotExists(
            CreateStudentCommand command,
            CancellationToken cancellationToken)
        {
            var getUserStudentsQuery = new GetUserStudentsQuery(command.UserId);
            var studentList          = await _queryProcessor.ExecuteAsync(getUserStudentsQuery, cancellationToken);

            var existingStudent = studentList.FirstOrDefault(x => x.StudentNumber == command.StudentNumber);

            if (existingStudent != null)
            {
                var getUserClassroomsQuery = new GetUserClassroomsQuery(command.UserId);
                var classroomList          = await _queryProcessor.ExecuteAsync(getUserClassroomsQuery, cancellationToken);

                var newStudentGrade = classroomList.First(c => c.Id == command.ClassroomId).Grade;

                if ((Grade.CheckIfHighSchool(existingStudent.ClassroomGrade) &&
                     Grade.CheckIfHighSchool(newStudentGrade)) ||
                    (Grade.CheckIfSecondarySchool(existingStudent.ClassroomGrade) && Grade.CheckIfSecondarySchool(newStudentGrade)))
                {
                    throw new ValidationException(ErrorCodes.StudentExists);
                }
            }
        }