mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-12 22:03:03 -05:00
setup project
This commit is contained in:
68
apps/projection/src/app/data-access/fake-http.service.ts
Normal file
68
apps/projection/src/app/data-access/fake-http.service.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
incrementalNumber,
|
||||
rand,
|
||||
randCity,
|
||||
randCountry,
|
||||
randFirstName,
|
||||
randLastName,
|
||||
randNumber,
|
||||
randWord,
|
||||
} from '@ngneat/falso';
|
||||
import { map, timer } from 'rxjs';
|
||||
import { City } from '../model/city.model';
|
||||
import { Student } from '../model/student.model';
|
||||
import { subject, Teacher } from '../model/teacher.model';
|
||||
|
||||
const factoryTeacher = incrementalNumber();
|
||||
|
||||
export const randTeacher = () => ({
|
||||
id: factoryTeacher(),
|
||||
firstname: randFirstName(),
|
||||
lastname: randLastName(),
|
||||
subject: rand(subject),
|
||||
});
|
||||
|
||||
const teachers: Teacher[] = [
|
||||
randTeacher(),
|
||||
randTeacher(),
|
||||
randTeacher(),
|
||||
randTeacher(),
|
||||
];
|
||||
|
||||
const factoryStudent = incrementalNumber();
|
||||
|
||||
export const randStudent = (): Student => ({
|
||||
id: factoryStudent(),
|
||||
firstname: randFirstName(),
|
||||
lastname: randLastName(),
|
||||
mainTeacher: teachers[randNumber({ max: teachers.length })],
|
||||
school: randWord(),
|
||||
});
|
||||
|
||||
const students: Student[] = [
|
||||
randStudent(),
|
||||
randStudent(),
|
||||
randStudent(),
|
||||
randStudent(),
|
||||
randStudent(),
|
||||
];
|
||||
|
||||
const factoryCity = incrementalNumber();
|
||||
|
||||
export const randomCity = (): City => ({
|
||||
id: factoryCity(),
|
||||
name: randCity(),
|
||||
country: randCountry(),
|
||||
});
|
||||
|
||||
const cities = [randomCity(), randomCity(), randomCity()];
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class FakeHttpService {
|
||||
fetchTeachers$ = timer(500).pipe(map(() => teachers));
|
||||
fetchStudents$ = timer(500).pipe(map(() => students));
|
||||
fetchCities$ = timer(500).pipe(map(() => cities));
|
||||
}
|
||||
23
apps/projection/src/app/data-access/student.store.ts
Normal file
23
apps/projection/src/app/data-access/student.store.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
import { Student } from '../model/student.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class StudentStore {
|
||||
private students = new BehaviorSubject<Student[]>([]);
|
||||
students$ = this.students.asObservable();
|
||||
|
||||
addAll(students: Student[]) {
|
||||
this.students.next(students);
|
||||
}
|
||||
|
||||
addOne(student: Student) {
|
||||
this.students.next([...this.students.value, student]);
|
||||
}
|
||||
|
||||
deleteOne(id: number) {
|
||||
this.students.next(this.students.value.filter((s) => s.id !== id));
|
||||
}
|
||||
}
|
||||
23
apps/projection/src/app/data-access/teacher.store.ts
Normal file
23
apps/projection/src/app/data-access/teacher.store.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
import { Teacher } from '../model/teacher.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class TeacherStore {
|
||||
private teachers = new BehaviorSubject<Teacher[]>([]);
|
||||
teachers$ = this.teachers.asObservable();
|
||||
|
||||
addAll(teachers: Teacher[]) {
|
||||
this.teachers.next(teachers);
|
||||
}
|
||||
|
||||
addOne(teacher: Teacher) {
|
||||
this.teachers.next([...this.teachers.value, teacher]);
|
||||
}
|
||||
|
||||
deleteOne(id: number) {
|
||||
this.teachers.next(this.teachers.value.filter((t) => t.id !== id));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user