Skip to content

Commit

Permalink
refactor(DataService): Convert to abstract class and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokiterashima committed Dec 18, 2024
1 parent 0494fd8 commit a403863
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 40 deletions.
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
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

0 comments on commit a403863

Please sign in to comment.