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; } } }
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; } } }
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; } } }