コード例 #1
0
        public async Task <Unit> Handle(
            UpdateStudentCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    var student = await _dbContext.Students.SingleOrDefaultAsync(
                        x => x.Id == command.Id,
                        cancellationToken
                        );

                    if (student == null)
                    {
                        throw new NotFoundException(nameof(student), command.Id);
                    }

                    // TODO: Check if GroupId exists
                    student.FirstName  = command.FirstName;
                    student.LastName   = command.LastName;
                    student.MiddleName = command.MiddleName;
                    student.Email      = command.Email;
                    student.GroupId    = command.GroupId;

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    if (command.Avatar != null)
                    {
                        var avatarUri = await _blobStorageUploader.UploadImageAsync(
                            command.Avatar,
                            cancellationToken
                            );

                        student.AvatarPath = avatarUri;

                        await _dbContext.SaveChangesAsync(cancellationToken);
                    }

                    transaction.Commit();

                    return(Unit.Value);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #2
0
        public async Task <Unit> Handle(
            RemoveGroupCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    var group =
                        await _dbContext.Groups.SingleOrDefaultAsync(x => x.Id == command.Id, cancellationToken);

                    if (group == null)
                    {
                        throw new NotFoundException(nameof(group), command.Id);
                    }

                    _dbContext.Remove(group);

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    return(Unit.Value);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #3
0
        public async Task <int> Handle(
            CreateUserCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    // TODO check wether PersonId exists
                    var user = new User
                    {
                        Login        = command.Login,
                        PasswordHash = _passwordHasher.HashPassword(command.Password),
                        PersonId     = command.PersonId
                    };

                    _dbContext.Users.Add(user);

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    return(user.Id);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #4
0
        public async Task <int> Handle(
            CreateGroupCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    // TODO: check FacultyId exists
                    var group = new Group
                    {
                        Name         = command.Name,
                        FacultyId    = command.FacultyId,
                        CourseNumber = command.CourseNumber
                    };

                    _dbContext.Groups.Add(group);

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    return(group.Id);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #5
0
        public async Task <int> Handle(
            CreateUniversityCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    var university = new University
                    {
                        Name        = command.Name,
                        ShortName   = command.ShortName,
                        Description = command.Description
                    };

                    _dbContext.Universities.Add(university);

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    return(university.Id);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #6
0
        public async Task <int> Handle(
            CreateSubjectCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    // TODO: Check if GroupId exists
                    var subject = new Subject
                    {
                        Name    = command.Name,
                        GroupId = command.GroupId
                    };

                    _dbContext.Subjects.Add(subject);

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    return(subject.Id);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #7
0
        public async Task <int> Handle(
            CreateStudentCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    // TODO: Check if GroupId exists
                    var student = new Student
                    {
                        FirstName  = command.FirstName,
                        LastName   = command.LastName,
                        MiddleName = command.MiddleName,
                        Email      = command.Email,
                        GroupId    = command.GroupId
                    };

                    _dbContext.Students.Add(student);

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    if (command.Avatar != null)
                    {
                        var avatarUri = await _blobStorageUploader.UploadImageAsync(
                            command.Avatar,
                            cancellationToken
                            );

                        student.AvatarPath = avatarUri;
                    }

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    return(student.Id);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #8
0
        public async Task <int> Handle(
            CreateTeacherCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    // TODO: Check if FacultyId exists
                    var teacher = new Teacher
                    {
                        FirstName  = command.FirstName,
                        LastName   = command.LastName,
                        MiddleName = command.MiddleName,
                        Email      = command.Email,
                        FacultyId  = command.FacultyId
                    };

                    _dbContext.Teachers.Add(teacher);

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    if (command.Avatar != null)
                    {
                        var avatarUri = await _blobStorageUploader.UploadImageAsync(
                            command.Avatar,
                            cancellationToken
                            );

                        teacher.AvatarPath = avatarUri;

                        await _dbContext.SaveChangesAsync(cancellationToken);
                    }

                    transaction.Commit();

                    return(teacher.Id);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #9
0
        public async Task <Unit> Handle(
            UpdateScheduleCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    var schedule = await _dbContext.Schedules.SingleOrDefaultAsync(
                        x => x.Id == command.Id,
                        cancellationToken
                        );

                    if (schedule == null)
                    {
                        throw new NotFoundException(nameof(schedule), command.Id);
                    }

                    // TODO: check SubjectId & TeacherId exists
                    schedule.SubjectId      = command.SubjectId;
                    schedule.TeacherId      = command.TeacherId;
                    schedule.StartTime      = command.StartTime;
                    schedule.Duration       = command.Duration;
                    schedule.LessonType     = command.LessonType;
                    schedule.AudienceNumber = command.AudienceNumber;

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    return(Unit.Value);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #10
0
        public async Task <Unit> Handle(
            UpdateFacultyCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    var faculty = await _dbContext.Faculties.SingleOrDefaultAsync(
                        x => x.Id == command.Id,
                        cancellationToken
                        );

                    if (faculty == null)
                    {
                        throw new NotFoundException(nameof(faculty), command.Id);
                    }

                    // TODO: check UniversityId exists
                    faculty.UniversityId = command.UniversityId;
                    faculty.Description  = command.Description;
                    faculty.ShortName    = command.ShortName;
                    faculty.Name         = command.Name;

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    return(Unit.Value);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #11
0
        public async Task <Unit> Handle(
            UpdateGroupCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    var group = await _dbContext.Groups.SingleOrDefaultAsync(
                        x => x.Id == command.Id,
                        cancellationToken
                        );

                    if (group == null)
                    {
                        throw new NotFoundException(nameof(group), command.Id);
                    }

                    // TODO: check FacultyId exists
                    group.Name         = command.Name;
                    group.FacultyId    = command.FacultyId;
                    group.CourseNumber = command.CourseNumber;

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    return(Unit.Value);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #12
0
        public async Task <Unit> Handle(
            UpdateUserCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    var university = await _dbContext.Users.SingleOrDefaultAsync(
                        x => x.Id == command.Id,
                        cancellationToken
                        );

                    if (university == null)
                    {
                        throw new NotFoundException(nameof(university), command.Id);
                    }

                    university.Login        = command.Login;
                    university.PersonId     = command.PersonId;
                    university.PasswordHash = command.Password;

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    return(Unit.Value);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #13
0
        public async Task <Unit> Handle(
            UpdateSubjectCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    var subject = await _dbContext.Subjects.SingleOrDefaultAsync(
                        x => x.Id == command.Id,
                        cancellationToken
                        );

                    if (subject == null)
                    {
                        throw new NotFoundException(nameof(subject), command.Id);
                    }

                    // TODO: Check if GroupId exists
                    subject.Name    = command.Name;
                    subject.GroupId = command.GroupId;

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    return(Unit.Value);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
コード例 #14
0
        public async Task <int> Handle(
            CreateScheduleCommand command,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    // TODO: check SubjectId & TeacherId exists
                    var schedule = new Schedule
                    {
                        SubjectId      = command.SubjectId,
                        TeacherId      = command.TeacherId,
                        StartTime      = command.StartTime,
                        Duration       = command.Duration,
                        LessonType     = command.LessonType,
                        AudienceNumber = command.AudienceNumber
                    };

                    _dbContext.Schedules.Add(schedule);

                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    return(schedule.Id);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }