mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-13 22:33:03 -05:00
feat(challenge7): add all mecanism
This commit is contained in:
67
libs/ngrx-notification/backend/src/lib/fake-db.service.ts
Normal file
67
libs/ngrx-notification/backend/src/lib/fake-db.service.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import {
|
||||
School,
|
||||
Student,
|
||||
Teacher,
|
||||
} from '@angular-challenges/ngrx-notification/model';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { randNumber } from '@ngneat/falso';
|
||||
import { ComponentStore } from '@ngrx/component-store';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class FakeDBService extends ComponentStore<{
|
||||
teachers: Teacher[];
|
||||
students: Student[];
|
||||
schools: School[];
|
||||
}> {
|
||||
readonly teachers$ = this.select((state) => state.teachers);
|
||||
readonly randomTeacher$ = this.select(
|
||||
this.teachers$,
|
||||
(teachers) => teachers[randNumber({ max: teachers.length - 1 })]
|
||||
);
|
||||
|
||||
readonly students$ = this.select((state) => state.students);
|
||||
readonly randomStudents$ = this.select(
|
||||
this.students$,
|
||||
(students) => students[randNumber({ max: students.length - 1 })]
|
||||
);
|
||||
|
||||
readonly schools$ = this.select((state) => state.schools);
|
||||
readonly randomSchool$ = this.select(
|
||||
this.schools$,
|
||||
(schools) => schools[randNumber({ max: schools.length - 1 })]
|
||||
);
|
||||
|
||||
constructor() {
|
||||
super({ teachers: [], students: [], schools: [] });
|
||||
}
|
||||
|
||||
addTeacher = this.updater((state, teacher: Teacher) => ({
|
||||
...state,
|
||||
teachers: [...state.teachers, teacher],
|
||||
}));
|
||||
|
||||
updateTeacher = this.updater((state, teacher: Teacher) => ({
|
||||
...state,
|
||||
teachers: state.teachers.map((t) => (t.id === teacher.id ? teacher : t)),
|
||||
}));
|
||||
|
||||
addStudent = this.updater((state, student: Student) => ({
|
||||
...state,
|
||||
students: [...state.students, student],
|
||||
}));
|
||||
|
||||
updateSudent = this.updater((state, student: Student) => ({
|
||||
...state,
|
||||
students: state.students.map((t) => (t.id === student.id ? student : t)),
|
||||
}));
|
||||
|
||||
addSchool = this.updater((state, school: School) => ({
|
||||
...state,
|
||||
schools: [...state.schools, school],
|
||||
}));
|
||||
|
||||
updateSchool = this.updater((state, school: School) => ({
|
||||
...state,
|
||||
schools: state.schools.map((t) => (t.id === school.id ? school : t)),
|
||||
}));
|
||||
}
|
||||
Reference in New Issue
Block a user