├── .browserslistrc ├── .editorconfig ├── .gitignore ├── .vscode ├── extensions.json ├── launch.json └── tasks.json ├── README.md ├── angular.json ├── karma.conf.js ├── package-lock.json ├── package.json ├── src ├── app │ ├── app.component.html │ ├── app.component.scss │ ├── app.component.spec.ts │ ├── app.component.ts │ ├── app.module.ts │ ├── core │ │ ├── application │ │ │ ├── facade.service.spec.ts │ │ │ └── facade.service.ts │ │ ├── entities │ │ │ ├── pizza.model.ts │ │ │ ├── song.model.ts │ │ │ ├── state-models │ │ │ │ └── user.model.ts │ │ │ └── view-models │ │ │ │ └── user.model.ts │ │ └── infrastructure │ │ │ ├── pizza.service.spec.ts │ │ │ ├── pizza.service.ts │ │ │ ├── song.service.spec.ts │ │ │ ├── song.service.ts │ │ │ ├── user.service.spec.ts │ │ │ └── user.service.ts │ └── primeng-modules.ts ├── assets │ ├── .gitkeep │ ├── pizzas.json │ ├── songs.json │ └── users.json ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── main.ts ├── polyfills.ts ├── styles.scss └── test.ts ├── tsconfig.app.json ├── tsconfig.json └── tsconfig.spec.json /.browserslistrc: -------------------------------------------------------------------------------- 1 | # This file is used by the build system to adjust CSS and JS output to support the specified browsers below. 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | 5 | # For the full list of supported browsers by the Angular framework, please see: 6 | # https://angular.io/guide/browser-support 7 | 8 | # You can see what browsers were selected by your queries by running: 9 | # npx browserslist 10 | 11 | last 1 Chrome version 12 | last 1 Firefox version 13 | last 2 Edge major versions 14 | last 2 Safari major versions 15 | last 2 iOS major versions 16 | Firefox ESR 17 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.ts] 12 | quote_type = single 13 | 14 | [*.md] 15 | max_line_length = off 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # Compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | /bazel-out 8 | 9 | # Node 10 | /node_modules 11 | npm-debug.log 12 | yarn-error.log 13 | 14 | # IDEs and editors 15 | .idea/ 16 | .project 17 | .classpath 18 | .c9/ 19 | *.launch 20 | .settings/ 21 | *.sublime-workspace 22 | 23 | # Visual Studio Code 24 | .vscode/* 25 | !.vscode/settings.json 26 | !.vscode/tasks.json 27 | !.vscode/launch.json 28 | !.vscode/extensions.json 29 | .history/* 30 | 31 | # Miscellaneous 32 | /.angular/cache 33 | .sass-cache/ 34 | /connect.lock 35 | /coverage 36 | /libpeerconnection.log 37 | testem.log 38 | /typings 39 | 40 | # System files 41 | .DS_Store 42 | Thumbs.db 43 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=827846 3 | "recommendations": ["angular.ng-template"] 4 | } 5 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 3 | "version": "0.2.0", 4 | "configurations": [ 5 | { 6 | "name": "ng serve", 7 | "type": "pwa-chrome", 8 | "request": "launch", 9 | "preLaunchTask": "npm: start", 10 | "url": "http://localhost:4200/" 11 | }, 12 | { 13 | "name": "ng test", 14 | "type": "chrome", 15 | "request": "launch", 16 | "preLaunchTask": "npm: test", 17 | "url": "http://localhost:9876/debug.html" 18 | } 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // For more information, visit: https://go.microsoft.com/fwlink/?LinkId=733558 3 | "version": "2.0.0", 4 | "tasks": [ 5 | { 6 | "type": "npm", 7 | "script": "start", 8 | "isBackground": true, 9 | "problemMatcher": { 10 | "owner": "typescript", 11 | "pattern": "$tsc", 12 | "background": { 13 | "activeOnStart": true, 14 | "beginsPattern": { 15 | "regexp": "(.*?)" 16 | }, 17 | "endsPattern": { 18 | "regexp": "bundle generation complete" 19 | } 20 | } 21 | } 22 | }, 23 | { 24 | "type": "npm", 25 | "script": "test", 26 | "isBackground": true, 27 | "problemMatcher": { 28 | "owner": "typescript", 29 | "pattern": "$tsc", 30 | "background": { 31 | "activeOnStart": true, 32 | "beginsPattern": { 33 | "regexp": "(.*?)" 34 | }, 35 | "endsPattern": { 36 | "regexp": "bundle generation complete" 37 | } 38 | } 39 | } 40 | } 41 | ] 42 | } 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FacadePattern 2 | 3 | This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 14.1.3. 4 | 5 | ## Development server 6 | 7 | Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The application will automatically reload if you change any of the source files. 8 | 9 | ## Code scaffolding 10 | 11 | Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`. 12 | 13 | ## Build 14 | 15 | Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. 16 | 17 | ## Running unit tests 18 | 19 | Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). 20 | 21 | ## Running end-to-end tests 22 | 23 | Run `ng e2e` to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities. 24 | 25 | ## Further help 26 | 27 | To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page. 28 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "facade-pattern": { 7 | "projectType": "application", 8 | "schematics": { 9 | "@schematics/angular:component": { 10 | "style": "scss" 11 | } 12 | }, 13 | "root": "", 14 | "sourceRoot": "src", 15 | "prefix": "app", 16 | "architect": { 17 | "build": { 18 | "builder": "@angular-devkit/build-angular:browser", 19 | "options": { 20 | "outputPath": "dist/facade-pattern", 21 | "index": "src/index.html", 22 | "main": "src/main.ts", 23 | "polyfills": "src/polyfills.ts", 24 | "tsConfig": "tsconfig.app.json", 25 | "inlineStyleLanguage": "scss", 26 | "assets": [ 27 | "src/favicon.ico", 28 | "src/assets" 29 | ], 30 | "styles": [ 31 | "src/styles.scss", 32 | "node_modules/primeicons/primeicons.css", 33 | "node_modules/primeng/resources/themes/lara-light-blue/theme.css", 34 | "node_modules/primeng/resources/primeng.min.css" 35 | ], 36 | "scripts": [] 37 | }, 38 | "configurations": { 39 | "production": { 40 | "budgets": [ 41 | { 42 | "type": "initial", 43 | "maximumWarning": "500kb", 44 | "maximumError": "1mb" 45 | }, 46 | { 47 | "type": "anyComponentStyle", 48 | "maximumWarning": "2kb", 49 | "maximumError": "4kb" 50 | } 51 | ], 52 | "fileReplacements": [ 53 | { 54 | "replace": "src/environments/environment.ts", 55 | "with": "src/environments/environment.prod.ts" 56 | } 57 | ], 58 | "outputHashing": "all" 59 | }, 60 | "development": { 61 | "buildOptimizer": false, 62 | "optimization": false, 63 | "vendorChunk": true, 64 | "extractLicenses": false, 65 | "sourceMap": true, 66 | "namedChunks": true 67 | } 68 | }, 69 | "defaultConfiguration": "production" 70 | }, 71 | "serve": { 72 | "builder": "@angular-devkit/build-angular:dev-server", 73 | "configurations": { 74 | "production": { 75 | "browserTarget": "facade-pattern:build:production" 76 | }, 77 | "development": { 78 | "browserTarget": "facade-pattern:build:development" 79 | } 80 | }, 81 | "defaultConfiguration": "development" 82 | }, 83 | "extract-i18n": { 84 | "builder": "@angular-devkit/build-angular:extract-i18n", 85 | "options": { 86 | "browserTarget": "facade-pattern:build" 87 | } 88 | }, 89 | "test": { 90 | "builder": "@angular-devkit/build-angular:karma", 91 | "options": { 92 | "main": "src/test.ts", 93 | "polyfills": "src/polyfills.ts", 94 | "tsConfig": "tsconfig.spec.json", 95 | "karmaConfig": "karma.conf.js", 96 | "inlineStyleLanguage": "scss", 97 | "assets": [ 98 | "src/favicon.ico", 99 | "src/assets" 100 | ], 101 | "styles": [ 102 | "src/styles.scss" 103 | ], 104 | "scripts": [] 105 | } 106 | } 107 | } 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | jasmine: { 17 | // you can add configuration options for Jasmine here 18 | // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html 19 | // for example, you can disable the random execution with `random: false` 20 | // or set a specific seed with `seed: 4321` 21 | }, 22 | clearContext: false // leave Jasmine Spec Runner output visible in browser 23 | }, 24 | jasmineHtmlReporter: { 25 | suppressAll: true // removes the duplicated traces 26 | }, 27 | coverageReporter: { 28 | dir: require('path').join(__dirname, './coverage/facade-pattern'), 29 | subdir: '.', 30 | reporters: [ 31 | { type: 'html' }, 32 | { type: 'text-summary' } 33 | ] 34 | }, 35 | reporters: ['progress', 'kjhtml'], 36 | port: 9876, 37 | colors: true, 38 | logLevel: config.LOG_INFO, 39 | autoWatch: true, 40 | browsers: ['Chrome'], 41 | singleRun: false, 42 | restartOnFileChange: true 43 | }); 44 | }; 45 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "facade-pattern", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "watch": "ng build --watch --configuration development", 9 | "test": "ng test" 10 | }, 11 | "private": true, 12 | "dependencies": { 13 | "@angular/animations": "^14.1.0", 14 | "@angular/common": "^14.1.0", 15 | "@angular/compiler": "^14.1.0", 16 | "@angular/core": "^14.1.0", 17 | "@angular/forms": "^14.1.0", 18 | "@angular/platform-browser": "^14.1.0", 19 | "@angular/platform-browser-dynamic": "^14.1.0", 20 | "@angular/router": "^14.1.0", 21 | "@ngrx/component-store": "^14.1.0", 22 | "primeicons": "^6.0.1", 23 | "primeng": "^14.1.0", 24 | "rxjs": "~7.5.0", 25 | "tslib": "^2.3.0", 26 | "zone.js": "~0.11.4" 27 | }, 28 | "devDependencies": { 29 | "@angular-devkit/build-angular": "^14.1.3", 30 | "@angular/cli": "~14.1.3", 31 | "@angular/compiler-cli": "^14.1.0", 32 | "@types/jasmine": "~4.0.0", 33 | "jasmine-core": "~4.2.0", 34 | "karma": "~6.4.0", 35 | "karma-chrome-launcher": "~3.1.0", 36 | "karma-coverage": "~2.2.0", 37 | "karma-jasmine": "~5.1.0", 38 | "karma-jasmine-html-reporter": "~2.0.0", 39 | "typescript": "~4.7.2" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 | 2 | 0; else notLoaded"> 3 | 8 | 9 | {{ item.email }} 10 | 11 | 12 | {{ option.email }} 13 | 14 | 15 | 16 | 17 | 20 | {{ selectedUser | json }} 21 | 22 | 23 | 24 | 25 | 26 | No user selected 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/app/app.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HaasStefan/facade-pattern/82d46373af9be6441e89d4a97fbbab45ba301af3/src/app/app.component.scss -------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | import { AppComponent } from './app.component'; 3 | 4 | describe('AppComponent', () => { 5 | beforeEach(async () => { 6 | await TestBed.configureTestingModule({ 7 | declarations: [ 8 | AppComponent 9 | ], 10 | }).compileComponents(); 11 | }); 12 | 13 | it('should create the app', () => { 14 | const fixture = TestBed.createComponent(AppComponent); 15 | const app = fixture.componentInstance; 16 | expect(app).toBeTruthy(); 17 | }); 18 | 19 | it(`should have as title 'facade-pattern'`, () => { 20 | const fixture = TestBed.createComponent(AppComponent); 21 | const app = fixture.componentInstance; 22 | expect(app.title).toEqual('facade-pattern'); 23 | }); 24 | 25 | it('should render title', () => { 26 | const fixture = TestBed.createComponent(AppComponent); 27 | fixture.detectChanges(); 28 | const compiled = fixture.nativeElement as HTMLElement; 29 | expect(compiled.querySelector('.content span')?.textContent).toContain('facade-pattern app is running!'); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | import { FacadeService } from './core/application/facade.service'; 3 | import { User } from './core/entities/view-models/user.model'; 4 | 5 | @Component({ 6 | selector: 'app-root', 7 | templateUrl: './app.component.html', 8 | styleUrls: ['./app.component.scss'] 9 | }) 10 | export class AppComponent { 11 | readonly selectedUser$ = this.facade.selectedUser$; 12 | readonly users$ = this.facade.users$; 13 | 14 | constructor( 15 | private readonly facade: FacadeService 16 | ) {} 17 | 18 | selectUser(user: User) { 19 | this.facade.selectUser(user); 20 | } 21 | 22 | load() { 23 | this.facade.loadUsers(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { BrowserModule } from '@angular/platform-browser'; 3 | import {HttpClientModule} from '@angular/common/http'; 4 | import { AppComponent } from './app.component'; 5 | import { PRIMENG_MODULES } from './primeng-modules'; 6 | import { FormsModule } from '@angular/forms'; 7 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 8 | 9 | @NgModule({ 10 | declarations: [ 11 | AppComponent 12 | ], 13 | imports: [ 14 | BrowserModule, 15 | FormsModule, 16 | BrowserAnimationsModule, 17 | HttpClientModule, 18 | ...PRIMENG_MODULES 19 | ], 20 | providers: [], 21 | bootstrap: [AppComponent] 22 | }) 23 | export class AppModule { } 24 | -------------------------------------------------------------------------------- /src/app/core/application/facade.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | 3 | import { FacadeService } from './facade.service'; 4 | 5 | describe('FacadeService', () => { 6 | let service: FacadeService; 7 | 8 | beforeEach(() => { 9 | TestBed.configureTestingModule({}); 10 | service = TestBed.inject(FacadeService); 11 | }); 12 | 13 | it('should be created', () => { 14 | expect(service).toBeTruthy(); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /src/app/core/application/facade.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { ComponentStore, tapResponse } from '@ngrx/component-store'; 3 | import { 4 | combineLatest, 5 | combineLatestAll, 6 | filter, 7 | map, 8 | mergeMap, 9 | Observable, 10 | of, 11 | pipe, 12 | switchMap, 13 | throwError, 14 | } from 'rxjs'; 15 | import { User } from '../entities/view-models/user.model'; 16 | import { User as UserStateModel } from '../entities/state-models/user.model'; 17 | import { PizzaService } from '../infrastructure/pizza.service'; 18 | import { SongService } from '../infrastructure/song.service'; 19 | import { UserService } from '../infrastructure/user.service'; 20 | 21 | interface State { 22 | selectedUser: number; 23 | users: UserStateModel[]; 24 | } 25 | 26 | @Injectable({ 27 | providedIn: 'root', 28 | }) 29 | export class FacadeService extends ComponentStore { 30 | readonly selectedUser$ = this.select(({ users, selectedUser }) => 31 | users.find((u) => u.id === selectedUser) 32 | ).pipe( 33 | filter(user => !!user), 34 | switchMap((user) => 35 | user ? of(user).pipe(this.mapOneToViewModel()) : of(user) 36 | ), 37 | ); 38 | 39 | readonly users$ = this.select(({ users }) => users).pipe( 40 | this.mapArrayToViewModel() 41 | ); 42 | 43 | readonly selectUser = this.updater((state: State, user: User) => ({ 44 | ...state, 45 | selectedUser: user.id, 46 | })); 47 | 48 | readonly loadUsers = this.effect( 49 | pipe( 50 | switchMap(() => 51 | this.userService 52 | .getAll() 53 | .pipe( 54 | tapResponse((users) => this.patchState({ users }), console.error) 55 | ) 56 | ) 57 | ) 58 | ); 59 | 60 | constructor( 61 | private readonly userService: UserService, 62 | private readonly pizzaService: PizzaService, 63 | private readonly songService: SongService 64 | ) { 65 | super({ 66 | selectedUser: -1, 67 | users: [], 68 | }); 69 | } 70 | 71 | private mapArrayToViewModel(): ( 72 | source$: Observable 73 | ) => Observable { 74 | return (source$) => 75 | source$.pipe( 76 | switchMap((users) => 77 | users && users.length > 0 78 | ? of(users).pipe( 79 | mergeMap((users) => users.map((u) => this.getUser(u))), 80 | combineLatestAll() 81 | ) 82 | : of([]) 83 | ) 84 | ); 85 | } 86 | 87 | private mapOneToViewModel(): ( 88 | source$: Observable 89 | ) => Observable { 90 | return (source$) => source$.pipe(switchMap((user) => this.getUser(user))); 91 | } 92 | 93 | private getUser(user: UserStateModel | null): Observable { 94 | if (!user) return throwError(() => new Error('user not found')); 95 | 96 | return combineLatest({ 97 | pizza: this.pizzaService.get(user.favourites.pizzaId), 98 | song: this.songService.get(user.favourites.songId), 99 | }).pipe( 100 | map( 101 | ({ pizza, song }) => 102 | ({ 103 | id: user.id, 104 | email: user.email, 105 | favourites: { 106 | pizza, 107 | song, 108 | }, 109 | } as User) 110 | ) 111 | ); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/app/core/entities/pizza.model.ts: -------------------------------------------------------------------------------- 1 | 2 | export interface Pizza { 3 | id: number, 4 | name: string, 5 | ingredients: string[] 6 | } -------------------------------------------------------------------------------- /src/app/core/entities/song.model.ts: -------------------------------------------------------------------------------- 1 | 2 | export interface Song { 3 | id: number, 4 | title: string, 5 | interpret: string, 6 | releaseDate: Date 7 | } -------------------------------------------------------------------------------- /src/app/core/entities/state-models/user.model.ts: -------------------------------------------------------------------------------- 1 | 2 | export interface User { 3 | id: number, 4 | email: string, 5 | favourites: { 6 | pizzaId: number, 7 | songId: number 8 | } 9 | } -------------------------------------------------------------------------------- /src/app/core/entities/view-models/user.model.ts: -------------------------------------------------------------------------------- 1 | import { Pizza } from "../pizza.model" 2 | import { Song } from "../song.model" 3 | 4 | export interface User { 5 | id: number, 6 | email: string, 7 | favourites: { 8 | pizza: Pizza | null 9 | song: Song | null 10 | } 11 | } -------------------------------------------------------------------------------- /src/app/core/infrastructure/pizza.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | 3 | import { PizzaService } from './pizza.service'; 4 | 5 | describe('PizzaService', () => { 6 | let service: PizzaService; 7 | 8 | beforeEach(() => { 9 | TestBed.configureTestingModule({}); 10 | service = TestBed.inject(PizzaService); 11 | }); 12 | 13 | it('should be created', () => { 14 | expect(service).toBeTruthy(); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /src/app/core/infrastructure/pizza.service.ts: -------------------------------------------------------------------------------- 1 | import { HttpClient } from '@angular/common/http'; 2 | import { Injectable } from '@angular/core'; 3 | import { map, Observable, ReplaySubject, share } from 'rxjs'; 4 | import { Pizza } from '../entities/pizza.model'; 5 | 6 | @Injectable({ 7 | providedIn: 'root', 8 | }) 9 | export class PizzaService { 10 | private readonly pizzas$!: Observable; 11 | 12 | constructor(private readonly http: HttpClient) { 13 | this.pizzas$ = this.http.get('/assets/pizzas.json').pipe( 14 | share({ 15 | connector: () => new ReplaySubject(), 16 | }) 17 | ); 18 | } 19 | 20 | get(id: number): Observable { 21 | return this.pizzas$.pipe( 22 | map(pizzas => { 23 | const pizza = pizzas.find(p => p.id === id); 24 | return pizza ?? null; 25 | }) 26 | ); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/app/core/infrastructure/song.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | 3 | import { SongService } from './song.service'; 4 | 5 | describe('SongService', () => { 6 | let service: SongService; 7 | 8 | beforeEach(() => { 9 | TestBed.configureTestingModule({}); 10 | service = TestBed.inject(SongService); 11 | }); 12 | 13 | it('should be created', () => { 14 | expect(service).toBeTruthy(); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /src/app/core/infrastructure/song.service.ts: -------------------------------------------------------------------------------- 1 | import { HttpClient } from '@angular/common/http'; 2 | import { Injectable } from '@angular/core'; 3 | import { map, Observable, ReplaySubject, share } from 'rxjs'; 4 | import { Song } from '../entities/song.model'; 5 | 6 | @Injectable({ 7 | providedIn: 'root' 8 | }) 9 | export class SongService { 10 | private readonly songs$!: Observable; 11 | 12 | constructor(private readonly http: HttpClient) { 13 | this.songs$ = this.http.get('/assets/songs.json').pipe( 14 | share({ 15 | connector: () => new ReplaySubject(), 16 | }) 17 | ); 18 | } 19 | 20 | get(id: number): Observable { 21 | return this.songs$.pipe( 22 | map(songs => { 23 | const song = songs.find(s => s.id === id); 24 | return song ?? null; 25 | }) 26 | ); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/app/core/infrastructure/user.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | 3 | import { UserService } from './user.service'; 4 | 5 | describe('UserService', () => { 6 | let service: UserService; 7 | 8 | beforeEach(() => { 9 | TestBed.configureTestingModule({}); 10 | service = TestBed.inject(UserService); 11 | }); 12 | 13 | it('should be created', () => { 14 | expect(service).toBeTruthy(); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /src/app/core/infrastructure/user.service.ts: -------------------------------------------------------------------------------- 1 | import { HttpClient } from '@angular/common/http'; 2 | import { Injectable } from '@angular/core'; 3 | import { map, Observable, ReplaySubject, share } from 'rxjs'; 4 | import { User } from '../entities/state-models/user.model'; 5 | 6 | @Injectable({ 7 | providedIn: 'root', 8 | }) 9 | export class UserService { 10 | private readonly users$!: Observable; 11 | 12 | constructor(private readonly http: HttpClient) { 13 | this.users$ = this.http.get('/assets/users.json').pipe( 14 | share({ 15 | connector: () => new ReplaySubject(), 16 | }) 17 | ); 18 | } 19 | 20 | get(id: number): Observable { 21 | return this.users$.pipe( 22 | map(users => { 23 | const user = users.find(u => u.id === id); 24 | return user ?? null; 25 | }) 26 | ); 27 | } 28 | 29 | getAll(): Observable { 30 | return this.users$; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/app/primeng-modules.ts: -------------------------------------------------------------------------------- 1 | import {CardModule} from 'primeng/card'; 2 | import {DropdownModule} from 'primeng/dropdown'; 3 | import {ButtonModule} from 'primeng/button'; 4 | 5 | export const PRIMENG_MODULES = [ 6 | CardModule, 7 | DropdownModule, 8 | ButtonModule 9 | ]; -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HaasStefan/facade-pattern/82d46373af9be6441e89d4a97fbbab45ba301af3/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/assets/pizzas.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Salame", 5 | "ingredients": ["Tomatos", "Cheese", "Salame"] 6 | }, 7 | { 8 | "id": 2, 9 | "name": "Tuna", 10 | "ingredients": ["Tomatos", "Cheese", "Tuna"] 11 | } 12 | ] 13 | -------------------------------------------------------------------------------- /src/assets/songs.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "title": "Black Summer", 5 | "interpret": "Red Hot Chilli Peppers", 6 | "releaseDate": "2022-01-01" 7 | }, 8 | { 9 | "id": 2, 10 | "title": "Californication", 11 | "interpret": "Red Hot Chilli Peppers", 12 | "releaseDate": "2022-01-01" 13 | }, 14 | { 15 | "id": 3, 16 | "title": "Where is my mind", 17 | "interpret": "Pixies", 18 | "releaseDate": "2022-01-01" 19 | }, 20 | { 21 | "id": 4, 22 | "title": "Don't Forget Me", 23 | "interpret": "Red Hot Chilli Peppers", 24 | "releaseDate": "2022-01-01" 25 | } 26 | ] -------------------------------------------------------------------------------- /src/assets/users.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "email": "john@doe.com", 5 | "favourites": { 6 | "pizzaId": 2, 7 | "songId": 3 8 | } 9 | }, 10 | { 11 | "id": 2, 12 | "email": "jane@doe.com", 13 | "favourites": { 14 | "pizzaId": 1, 15 | "songId": 2 16 | } 17 | } 18 | ] -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * For easier debugging in development mode, you can import the following file 11 | * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. 12 | * 13 | * This import should be commented out in production mode because it will have a negative impact 14 | * on performance if an error is thrown. 15 | */ 16 | // import 'zone.js/plugins/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HaasStefan/facade-pattern/82d46373af9be6441e89d4a97fbbab45ba301af3/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | FacadePattern 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.error(err)); 13 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes recent versions of Safari, Chrome (including 12 | * Opera), Edge on the desktop, and iOS and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** 22 | * By default, zone.js will patch all possible macroTask and DomEvents 23 | * user can disable parts of macroTask/DomEvents patch by setting following flags 24 | * because those flags need to be set before `zone.js` being loaded, and webpack 25 | * will put import in the top of bundle, so user need to create a separate file 26 | * in this directory (for example: zone-flags.ts), and put the following flags 27 | * into that file, and then add the following code before importing zone.js. 28 | * import './zone-flags'; 29 | * 30 | * The flags allowed in zone-flags.ts are listed here. 31 | * 32 | * The following flags will work for all browsers. 33 | * 34 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 35 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 36 | * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 37 | * 38 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 39 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 40 | * 41 | * (window as any).__Zone_enable_cross_context_check = true; 42 | * 43 | */ 44 | 45 | /*************************************************************************************************** 46 | * Zone JS is required by default for Angular itself. 47 | */ 48 | import 'zone.js'; // Included with Angular CLI. 49 | 50 | 51 | /*************************************************************************************************** 52 | * APPLICATION IMPORTS 53 | */ 54 | -------------------------------------------------------------------------------- /src/styles.scss: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | 3 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | declare const require: { 11 | context(path: string, deep?: boolean, filter?: RegExp): { 12 | (id: string): T; 13 | keys(): string[]; 14 | }; 15 | }; 16 | 17 | // First, initialize the Angular testing environment. 18 | getTestBed().initTestEnvironment( 19 | BrowserDynamicTestingModule, 20 | platformBrowserDynamicTesting(), 21 | ); 22 | 23 | // Then we find all the tests. 24 | const context = require.context('./', true, /\.spec\.ts$/); 25 | // And load the modules. 26 | context.keys().forEach(context); 27 | -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "./tsconfig.json", 4 | "compilerOptions": { 5 | "outDir": "./out-tsc/app", 6 | "types": [] 7 | }, 8 | "files": [ 9 | "src/main.ts", 10 | "src/polyfills.ts" 11 | ], 12 | "include": [ 13 | "src/**/*.d.ts" 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "compileOnSave": false, 4 | "compilerOptions": { 5 | "baseUrl": "./", 6 | "outDir": "./dist/out-tsc", 7 | "forceConsistentCasingInFileNames": true, 8 | "strict": true, 9 | "noImplicitOverride": true, 10 | "noPropertyAccessFromIndexSignature": true, 11 | "noImplicitReturns": true, 12 | "noFallthroughCasesInSwitch": true, 13 | "sourceMap": true, 14 | "declaration": false, 15 | "downlevelIteration": true, 16 | "experimentalDecorators": true, 17 | "moduleResolution": "node", 18 | "importHelpers": true, 19 | "target": "es2020", 20 | "module": "es2020", 21 | "lib": [ 22 | "es2020", 23 | "dom" 24 | ] 25 | }, 26 | "angularCompilerOptions": { 27 | "enableI18nLegacyMessageIdFormat": false, 28 | "strictInjectionParameters": true, 29 | "strictInputAccessModifiers": true, 30 | "strictTemplates": true 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "./tsconfig.json", 4 | "compilerOptions": { 5 | "outDir": "./out-tsc/spec", 6 | "types": [ 7 | "jasmine" 8 | ] 9 | }, 10 | "files": [ 11 | "src/test.ts", 12 | "src/polyfills.ts" 13 | ], 14 | "include": [ 15 | "src/**/*.spec.ts", 16 | "src/**/*.d.ts" 17 | ] 18 | } 19 | --------------------------------------------------------------------------------
{{ selectedUser | json }}