mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 12:53:03 -05:00
feat(challenge7): add all mecanism
This commit is contained in:
@@ -1 +1,2 @@
|
||||
export * from './lib/fake-backend.service';
|
||||
export * from './lib/push.service';
|
||||
|
||||
105
libs/ngrx-notification/backend/src/lib/fake-backend.service.ts
Normal file
105
libs/ngrx-notification/backend/src/lib/fake-backend.service.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import {
|
||||
randSchool,
|
||||
randStudent,
|
||||
randTeacher,
|
||||
} from '@angular-challenges/ngrx-notification/model';
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { randCompanyName, randFirstName } from '@ngneat/falso';
|
||||
import { concatLatestFrom } from '@ngrx/effects';
|
||||
import { map, tap, timer } from 'rxjs';
|
||||
import { FakeDBService } from './fake-db.service';
|
||||
import { PushService } from './push.service';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class FakeBackendService {
|
||||
private fakeDbService = inject(FakeDBService);
|
||||
private pushService = inject(PushService);
|
||||
|
||||
getAllTeachers = () => this.fakeDbService.teachers$;
|
||||
getAllStudents = () => this.fakeDbService.students$;
|
||||
getAllStchools = () => this.fakeDbService.schools$;
|
||||
|
||||
start() {
|
||||
this.fakeAddTeacher();
|
||||
this.fakeUpdateTeacher();
|
||||
this.fakeAddStudent();
|
||||
this.fakeUpdateStudent();
|
||||
this.fakeAddSchool();
|
||||
this.fakeUpdateSchool();
|
||||
}
|
||||
|
||||
private fakeAddTeacher() {
|
||||
timer(0, 4000)
|
||||
.pipe(
|
||||
map(() => randTeacher()),
|
||||
tap((teacher) => this.pushService.pushData(teacher)),
|
||||
tap((teacher) => this.fakeDbService.addTeacher(teacher))
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
private fakeUpdateTeacher() {
|
||||
timer(8000, 5000)
|
||||
.pipe(
|
||||
concatLatestFrom(() => this.fakeDbService.randomTeacher$),
|
||||
map(([, teacher]) => ({
|
||||
...teacher,
|
||||
firstname: randFirstName(),
|
||||
version: teacher.version + 1,
|
||||
})),
|
||||
tap((teacher) => this.pushService.pushData(teacher)),
|
||||
tap((teacher) => this.fakeDbService.updateTeacher(teacher))
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
private fakeAddStudent() {
|
||||
timer(0, 2000)
|
||||
.pipe(
|
||||
map(() => randStudent()),
|
||||
tap((student) => this.pushService.pushData(student)),
|
||||
tap((student) => this.fakeDbService.addStudent(student))
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
private fakeUpdateStudent() {
|
||||
timer(8000, 6000)
|
||||
.pipe(
|
||||
concatLatestFrom(() => this.fakeDbService.randomStudents$),
|
||||
map(([, student]) => ({
|
||||
...student,
|
||||
firstname: randFirstName(),
|
||||
version: student.version + 1,
|
||||
})),
|
||||
tap((student) => this.pushService.pushData(student)),
|
||||
tap((student) => this.fakeDbService.updateSudent(student))
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
private fakeAddSchool() {
|
||||
timer(0, 2000)
|
||||
.pipe(
|
||||
map(() => randSchool()),
|
||||
tap((school) => this.pushService.pushData(school)),
|
||||
tap((school) => this.fakeDbService.addSchool(school))
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
private fakeUpdateSchool() {
|
||||
timer(8000, 4000)
|
||||
.pipe(
|
||||
concatLatestFrom(() => this.fakeDbService.randomSchool$),
|
||||
map(([, school]) => ({
|
||||
...school,
|
||||
name: randCompanyName(),
|
||||
version: school.version + 1,
|
||||
})),
|
||||
tap((school) => this.pushService.pushData(school)),
|
||||
tap((school) => this.fakeDbService.updateSchool(school))
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
}
|
||||
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)),
|
||||
}));
|
||||
}
|
||||
@@ -1,10 +1,6 @@
|
||||
import {
|
||||
Push,
|
||||
randStudent,
|
||||
randTeacher,
|
||||
} from '@angular-challenges/ngrx-notification/model';
|
||||
import { Push } from '@angular-challenges/ngrx-notification/model';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject, tap, timer } from 'rxjs';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class PushService {
|
||||
@@ -13,20 +9,7 @@ export class PushService {
|
||||
);
|
||||
notification$ = this.notificationSubject.asObservable();
|
||||
|
||||
init() {
|
||||
this.startTeacherNotification();
|
||||
this.startStudentNotification();
|
||||
}
|
||||
|
||||
private startTeacherNotification() {
|
||||
timer(0, 4000)
|
||||
.pipe(tap(() => this.notificationSubject.next(randTeacher())))
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
private startStudentNotification() {
|
||||
timer(1000, 3000)
|
||||
.pipe(tap(() => this.notificationSubject.next(randStudent())))
|
||||
.subscribe();
|
||||
pushData(data: Push) {
|
||||
this.notificationSubject.next(data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './lib/push.model';
|
||||
export * from './lib/school.model';
|
||||
export * from './lib/student.model';
|
||||
export * from './lib/teacher.model';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export type PushType = 'teacher' | 'student';
|
||||
export type PushType = 'teacher' | 'student' | 'school';
|
||||
|
||||
export interface Push {
|
||||
type: PushType;
|
||||
|
||||
21
libs/ngrx-notification/model/src/lib/school.model.ts
Normal file
21
libs/ngrx-notification/model/src/lib/school.model.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { incrementalNumber, randCompanyName } from '@ngneat/falso';
|
||||
import { Push } from './push.model';
|
||||
|
||||
export interface School extends Push {
|
||||
id: number;
|
||||
name: string;
|
||||
version: number;
|
||||
}
|
||||
|
||||
const schoolTeacher = incrementalNumber();
|
||||
|
||||
export const randSchool = (): School => ({
|
||||
id: schoolTeacher(),
|
||||
name: randCompanyName(),
|
||||
version: 0,
|
||||
type: 'school',
|
||||
});
|
||||
|
||||
export const isSchool = (notif: Push): notif is School => {
|
||||
return notif.type === 'school';
|
||||
};
|
||||
@@ -1,16 +1,11 @@
|
||||
import {
|
||||
incrementalNumber,
|
||||
randFirstName,
|
||||
randLastName,
|
||||
randWord,
|
||||
} from '@ngneat/falso';
|
||||
import { incrementalNumber, randFirstName, randLastName } from '@ngneat/falso';
|
||||
import { Push } from './push.model';
|
||||
|
||||
export interface Student extends Push {
|
||||
id: number;
|
||||
firstname: string;
|
||||
lastname: string;
|
||||
school: string;
|
||||
version: number;
|
||||
}
|
||||
|
||||
const factoryStudent = incrementalNumber();
|
||||
@@ -19,7 +14,7 @@ export const randStudent = (): Student => ({
|
||||
id: factoryStudent(),
|
||||
firstname: randFirstName(),
|
||||
lastname: randLastName(),
|
||||
school: randWord(),
|
||||
version: 0,
|
||||
type: 'student',
|
||||
});
|
||||
|
||||
|
||||
@@ -1,25 +1,11 @@
|
||||
import {
|
||||
incrementalNumber,
|
||||
rand,
|
||||
randFirstName,
|
||||
randLastName,
|
||||
} from '@ngneat/falso';
|
||||
import { incrementalNumber, randFirstName, randLastName } from '@ngneat/falso';
|
||||
import { Push } from './push.model';
|
||||
|
||||
export const subject = [
|
||||
'Sciences',
|
||||
'History',
|
||||
'English',
|
||||
'Maths',
|
||||
'Sport',
|
||||
] as const;
|
||||
export type Subject = typeof subject[number];
|
||||
|
||||
export interface Teacher extends Push {
|
||||
id: number;
|
||||
firstname: string;
|
||||
lastname: string;
|
||||
subject: Subject;
|
||||
version: number;
|
||||
}
|
||||
|
||||
const factoryTeacher = incrementalNumber();
|
||||
@@ -28,7 +14,7 @@ export const randTeacher = (): Teacher => ({
|
||||
id: factoryTeacher(),
|
||||
firstname: randFirstName(),
|
||||
lastname: randLastName(),
|
||||
subject: rand(subject),
|
||||
version: 0,
|
||||
type: 'teacher',
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user