Exemplo n.º 1
0
        public async Task <Unit> Handle(CreateStudentEnrollment request, CancellationToken cancellationToken)
        {
            var student = new Student
            {
                FirstName = request.FirstName, LastName = request.LastName, IndexNumber = request.IndexNumber,
                BirthDate = request.BirthDate
            };
            var studies = await FindStudies(request, cancellationToken);

            var        firstSemesterEnrollments = studies.Enrollment.Where(e => e.Semester == 1).ToList();
            Enrollment enrollment;

            if (!firstSemesterEnrollments.Any())
            {
                enrollment = new Enrollment
                {
                    Semester          = 1,
                    IdStudyNavigation = studies,
                    StartDate         = DateTime.UtcNow
                };
                await _context.Enrollment.AddAsync(enrollment, cancellationToken);
            }
            else
            {
                enrollment = firstSemesterEnrollments.First();
            }

            student.IdEnrollementNavigation = enrollment;
            await _context.Student.AddAsync(student, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemplo n.º 2
0
        public async Task <Unit> Handle(DeleteStudent request, CancellationToken cancellationToken)
        {
            var student = await _context.Student.FirstOrDefaultAsync(s => s.IndexNumber == request.Index,
                                                                     cancellationToken : cancellationToken);

            if (student is null)
            {
                throw new Exception("Student not found");
            }

            _context.Student.Remove(student);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemplo n.º 3
0
        public async Task <Unit> Handle(EditStudent request, CancellationToken cancellationToken)
        {
            var student = await _context.Student.FirstOrDefaultAsync(s => s.IndexNumber == request.Index, cancellationToken : cancellationToken);

            if (student is null)
            {
                throw new Exception("Student not found");
            }

            if (!string.IsNullOrEmpty(request.FirstName))
            {
                student.FirstName = request.FirstName;
            }
            if (!string.IsNullOrEmpty(request.LastName))
            {
                student.LastName = request.LastName;
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }