Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(DataService): make abstract #2025

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/app/services/data.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { ProjectService } from '../../assets/wise5/services/projectService';
import { StudentTeacherCommonServicesModule } from '../student-teacher-common-services.module';

import { DataService } from './data.service';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { StudentDataService } from '../../assets/wise5/services/studentDataService';

let service: DataService;
let projectService: ProjectService;

describe('DataService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [StudentTeacherCommonServicesModule],
providers: [provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting()]
});
service = TestBed.inject(DataService);
imports: [StudentTeacherCommonServicesModule],
providers: [provideHttpClient(withInterceptorsFromDi())]
});
service = TestBed.inject(StudentDataService);
projectService = TestBed.inject(ProjectService);
});

setCurrentNode();
});

Expand All @@ -37,7 +34,6 @@ function setCurrentNode() {
spyOn(projectService, 'isGroupNode').and.callFake(() => {
return false;
});
spyOn(service, 'broadcastCurrentNodeChanged').and.callFake(() => {});
service.setCurrentNode(node2);
expect(service.previousStep).toEqual(node1);
expect(service.currentNode).toEqual(node2);
Expand Down
34 changes: 12 additions & 22 deletions src/app/services/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { ProjectService } from '../../assets/wise5/services/projectService';

@Injectable({
providedIn: 'root'
})
export class DataService {
@Injectable()
export abstract class DataService {
currentNode = null;
previousStep = null;
private currentNodeChangedSource: Subject<any> = new Subject<any>();
public currentNodeChanged$ = this.currentNodeChangedSource.asObservable();
private studentWorkReceivedSource: Subject<any> = new Subject<any>();
public studentWorkReceived$ = this.studentWorkReceivedSource.asObservable();

constructor(protected ProjectService: ProjectService) {}
constructor(protected projectService: ProjectService) {}

getCurrentNode() {
getCurrentNode(): any {
return this.currentNode;
}

getCurrentNodeId() {
getCurrentNodeId(): string {
if (this.currentNode != null) {
return this.currentNode.id;
}
Expand All @@ -30,36 +28,28 @@ export class DataService {
return [];
}

getStackHistory(): any[] {
return [];
}

evaluateCriterias(criteria) {}

// refactor: this should be only in studentDataService
saveVLEEvent(nodeId, componentId, componentType, category, event, eventData) {}

setCurrentNodeByNodeId(nodeId) {
this.setCurrentNode(this.ProjectService.getNodeById(nodeId));
// refactor: replace this with setCurrentNode()
setCurrentNodeByNodeId(nodeId: string): void {
this.setCurrentNode(this.projectService.getNodeById(nodeId));
}

setCurrentNode(node) {
setCurrentNode(node: any): void {
const previousCurrentNode = this.currentNode;
this.currentNode = node;
if (previousCurrentNode !== node) {
if (previousCurrentNode && !this.ProjectService.isGroupNode(previousCurrentNode.id)) {
if (previousCurrentNode && !this.projectService.isGroupNode(previousCurrentNode.id)) {
this.previousStep = previousCurrentNode;
}
this.broadcastCurrentNodeChanged({
this.currentNodeChangedSource.next({
previousNode: previousCurrentNode,
currentNode: this.currentNode
});
}
}

broadcastCurrentNodeChanged(previousAndCurrentNode: any) {
this.currentNodeChangedSource.next(previousAndCurrentNode);
}

broadcastStudentWorkReceived(studentWork: any) {
this.studentWorkReceivedSource.next(studentWork);
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/services/studentNodeService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DataService } from './data.service';
import { ProjectService } from '../../assets/wise5/services/projectService';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { Node } from '../../assets/wise5/common/Node';
import { StudentDataService } from '../../assets/wise5/services/studentDataService';

let dataService: DataService;
let dialog: MatDialog;
Expand All @@ -21,7 +22,7 @@ describe('StudentNodeService', () => {
imports: [MatDialogModule, StudentTeacherCommonServicesModule],
providers: [provideHttpClient(withInterceptorsFromDi())]
});
dataService = TestBed.inject(DataService);
dataService = TestBed.inject(StudentDataService);
dialog = TestBed.inject(MatDialog);
service = TestBed.inject(StudentNodeService);
nodeStatusService = TestBed.inject(NodeStatusService);
Expand Down
18 changes: 11 additions & 7 deletions src/app/services/teacherNodeService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { MatDialogModule } from '@angular/material/dialog';
import { StudentTeacherCommonServicesModule } from '../student-teacher-common-services.module';
import { DataService } from './data.service';
import { ProjectService } from '../../assets/wise5/services/projectService';
import { TeacherNodeService } from '../../assets/wise5/services/teacherNodeService';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { TeacherDataService } from '../../assets/wise5/services/teacherDataService';
import { TeacherProjectService } from '../../assets/wise5/services/teacherProjectService';

let dataService: DataService;
const nodeId1 = 'node1';
const nodeId2 = 'node2';
let projectService: ProjectService;
let service: TeacherNodeService;

describe('TeacherNodeService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [MatDialogModule, StudentTeacherCommonServicesModule],
providers: [TeacherNodeService, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting()]
});
dataService = TestBed.inject(DataService);
projectService = TestBed.inject(ProjectService);
imports: [MatDialogModule, StudentTeacherCommonServicesModule],
providers: [
TeacherNodeService,
TeacherProjectService,
provideHttpClient(withInterceptorsFromDi())
]
});
dataService = TestBed.inject(TeacherDataService);
projectService = TestBed.inject(TeacherProjectService);
service = TestBed.inject(TeacherNodeService);
});
getNextNodeId();
Expand Down
8 changes: 8 additions & 0 deletions src/app/student-teacher-common-services.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ import { CompletionService } from '../assets/wise5/services/completionService';
import { StudentNodeService } from '../assets/wise5/services/studentNodeService';
import { StudentProjectTranslationService } from '../assets/wise5/services/studentProjectTranslationService';
import { AiChatService } from '../assets/wise5/components/aiChat/aiChatService';
import { TeacherNodeService } from '../assets/wise5/services/teacherNodeService';
import { TeacherDataService } from '../assets/wise5/services/teacherDataService';
import { TeacherWebSocketService } from '../assets/wise5/services/teacherWebSocketService';
import { ClassroomStatusService } from '../assets/wise5/services/classroomStatusService';

@NgModule({
providers: [
Expand All @@ -65,6 +69,7 @@ import { AiChatService } from '../assets/wise5/components/aiChat/aiChatService';
AudioOscillatorService,
AudioRecorderService,
BranchService,
ClassroomStatusService,
ClickToSnipImageService,
ConceptMapService,
ConstraintService,
Expand Down Expand Up @@ -111,6 +116,9 @@ import { AiChatService } from '../assets/wise5/components/aiChat/aiChatService';
TableService,
TabulatorDataService,
TagService,
TeacherDataService,
TeacherNodeService,
TeacherWebSocketService,
StudentProjectTranslationService,
VLEProjectService,
WiseLinkService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { NodeService } from '../../services/nodeService';
import { ClassroomStatusService } from '../../services/classroomStatusService';
import { TeacherDataService } from '../../services/teacherDataService';
import { TeacherProjectService } from '../../services/teacherProjectService';
Expand All @@ -9,6 +7,7 @@ import { StepToolsComponent } from './step-tools.component';
import { StudentTeacherCommonServicesModule } from '../../../../app/student-teacher-common-services.module';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { StudentNodeService } from '../../services/studentNodeService';

describe('StepTools', () => {
let component: StepToolsComponent;
Expand All @@ -19,12 +18,11 @@ describe('StepTools', () => {
imports: [NoopAnimationsModule, StepToolsComponent, StudentTeacherCommonServicesModule],
providers: [
ClassroomStatusService,
NodeService,
StudentNodeService,
TeacherDataService,
TeacherProjectService,
TeacherWebSocketService,
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting()
provideHttpClient(withInterceptorsFromDi())
]
});
fixture = TestBed.createComponent(StepToolsComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let fixture: ComponentFixture<EditGraphAdvancedComponent>;
describe('EditGraphAdvancedComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [
declarations: [
EditComponentAddToNotebookButtonComponent,
EditCommonAdvancedComponent,
EditComponentExcludeFromTotalScoreComponent,
Expand All @@ -46,18 +46,25 @@ describe('EditGraphAdvancedComponent', () => {
EditConnectedComponentsAddButtonComponent,
EditConnectedComponentsComponent,
EditGraphAdvancedComponent
],
schemas: [NO_ERRORS_SCHEMA],
imports: [BrowserAnimationsModule,
],
schemas: [NO_ERRORS_SCHEMA],
imports: [
BrowserAnimationsModule,
FormsModule,
MatCheckboxModule,
MatDialogModule,
MatFormFieldModule,
MatIconModule,
MatInputModule,
StudentTeacherCommonServicesModule],
providers: [TeacherNodeService, TeacherProjectService, provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting()]
}).compileComponents();
StudentTeacherCommonServicesModule
],
providers: [
TeacherNodeService,
TeacherProjectService,
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting()
]
}).compileComponents();
});

beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/assets/wise5/services/studentDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export class StudentDataService extends DataService {
return null;
}

getStackHistory() {
getStackHistory(): any[] {
return this.stackHistory;
}

Expand Down
4 changes: 2 additions & 2 deletions src/assets/wise5/services/studentNodeService.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Injectable } from '@angular/core';
import { NodeService } from './nodeService';
import { MatDialog } from '@angular/material/dialog';
import { DataService } from '../../../app/services/data.service';
import { ConfigService } from './configService';
import { ConstraintService } from './constraintService';
import { ProjectService } from './projectService';
import { NodeStatusService } from './nodeStatusService';
import { DialogWithCloseComponent } from '../directives/dialog-with-close/dialog-with-close.component';
import { Constraint } from '../../../app/domain/constraint';
import { TransitionLogic } from '../common/TransitionLogic';
import { StudentDataService } from './studentDataService';

@Injectable()
export class StudentNodeService extends NodeService {
constructor(
protected dataService: DataService,
protected dataService: StudentDataService,
protected dialog: MatDialog,
protected configService: ConfigService,
protected constraintService: ConstraintService,
Expand Down
12 changes: 6 additions & 6 deletions src/assets/wise5/services/studentWebSocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export class StudentWebSocketService {
constructor(
private AnnotationService: AnnotationService,
private configService: ConfigService,
private dataService: StudentDataService,
private nodeService: NodeService,
private notebookService: NotebookService,
private ProjectService: ProjectService,
private stompService: StompService,
private StudentDataService: StudentDataService,
private TagService: TagService
) {}

Expand All @@ -35,7 +35,7 @@ export class StudentWebSocketService {
const body = JSON.parse(message.body);
if (body.type === 'studentWork') {
const studentWork = JSON.parse(body.content);
this.StudentDataService.broadcastStudentWorkReceived(studentWork);
this.dataService.broadcastStudentWorkReceived(studentWork);
} else if (body.type === 'annotation') {
this.AnnotationService.broadcastAnnotationReceived(JSON.parse(body.content));
} else if (body.type === 'goToNode') {
Expand All @@ -60,20 +60,20 @@ export class StudentWebSocketService {
} else if (body.type === 'tagsToWorkgroup') {
const tags = JSON.parse(body.content);
this.TagService.setTags(tags);
this.StudentDataService.updateNodeStatuses();
this.dataService.updateNodeStatuses();
this.nodeService.evaluateTransitionLogic();
} else if (body.type === 'goToNode') {
this.goToStep(body.content);
} else if (body.type === 'goToNextNode') {
this.goToNextStep();
} else if (body.type === 'classmateStudentWork') {
this.StudentDataService.broadcastStudentWorkReceived(JSON.parse(body.content));
this.dataService.broadcastStudentWorkReceived(JSON.parse(body.content));
}
});
}

private handleAnnotationReceived(annotation: Annotation): void {
this.StudentDataService.studentData.annotations.push(annotation);
this.dataService.studentData.annotations.push(annotation);
if (annotation.notebookItemId) {
this.notebookService.broadcastNotebookItemAnnotationReceived(annotation);
} else {
Expand All @@ -95,7 +95,7 @@ export class StudentWebSocketService {
const node = JSON.parse(nodeString);
this.ProjectService.replaceNode(node.id, node);
this.ProjectService.parseProject();
this.StudentDataService.updateNodeStatuses();
this.dataService.updateNodeStatuses();
}

sendStudentWorkToClassmate(workgroupId: number, studentWork: any): void {
Expand Down
15 changes: 15 additions & 0 deletions src/assets/wise5/services/teacherNodeService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Injectable } from '@angular/core';
import { NodeService } from './nodeService';
import { Subject, Observable } from 'rxjs';
import { TeacherDataService } from './teacherDataService';
import { TeacherProjectService } from './teacherProjectService';
import { MatDialog } from '@angular/material/dialog';
import { ConfigService } from './configService';
import { ConstraintService } from './constraintService';

@Injectable()
export class TeacherNodeService extends NodeService {
Expand All @@ -12,6 +17,16 @@ export class TeacherNodeService extends NodeService {
private starterStateResponseSource: Subject<any> = new Subject<any>();
public starterStateResponse$: Observable<any> = this.starterStateResponseSource.asObservable();

constructor(
protected dataService: TeacherDataService,
protected dialog: MatDialog,
protected configService: ConfigService,
protected constraintService: ConstraintService,
protected projectService: TeacherProjectService
) {
super(dataService, dialog, configService, constraintService, projectService);
}

broadcastComponentShowSubmitButtonValueChanged(args: any): void {
this.componentShowSubmitButtonValueChangedSource.next(args);
}
Expand Down
Loading
Loading