├── .gitignore ├── client └── angularclient │ ├── .editorconfig │ ├── .gitignore │ ├── README.md │ ├── angular.json │ ├── e2e │ ├── protractor.conf.js │ ├── src │ │ ├── app.e2e-spec.ts │ │ └── app.po.ts │ └── tsconfig.e2e.json │ ├── package-lock.json │ ├── package.json │ ├── src │ ├── app │ │ ├── app.component.css │ │ ├── app.component.html │ │ ├── app.component.spec.ts │ │ ├── app.component.ts │ │ ├── app.module.ts │ │ ├── app.routing.ts │ │ ├── core │ │ │ ├── core.module.ts │ │ │ └── services │ │ │ │ ├── customer-data.service.ts │ │ │ │ ├── http-base.service.ts │ │ │ │ └── pagination.service.ts │ │ ├── customer │ │ │ ├── customer.module.ts │ │ │ ├── customer.routing.ts │ │ │ ├── details │ │ │ │ ├── details.component.html │ │ │ │ └── details.component.ts │ │ │ ├── list │ │ │ │ ├── list.component.html │ │ │ │ └── list.component.ts │ │ │ └── overview │ │ │ │ ├── overview.component.html │ │ │ │ └── overview.component.ts │ │ └── models │ │ │ ├── customer.model.ts │ │ │ └── pagination.model.ts │ ├── assets │ │ └── .gitkeep │ ├── browserslist │ ├── environments │ │ ├── environment.prod.ts │ │ └── environment.ts │ ├── favicon.ico │ ├── index.html │ ├── karma.conf.js │ ├── main.ts │ ├── polyfills.ts │ ├── styles.css │ ├── test.ts │ ├── tsconfig.app.json │ ├── tsconfig.spec.json │ └── tslint.json │ ├── tsconfig.json │ └── tslint.json ├── readme.md └── server └── aspnetcore ├── .vs └── ASPNETCoreWebAPI │ ├── DesignTimeBuild │ └── .dtbcache │ ├── config │ └── applicationhost.config │ └── v16 │ ├── .suo │ └── Server │ └── sqlite3 │ ├── db.lock │ └── storage.ide ├── ASPNETCoreWebAPI.sln └── ASPNETCoreWebAPI ├── ASPNETCoreWebAPI.csproj ├── Controllers └── CustomersController.cs ├── Dtos ├── CustomerCreateDto.cs ├── CustomerDto.cs └── CustomerUpdateDto.cs ├── Entities └── Customer.cs ├── Helpers ├── DynamicExtensions.cs └── QueryParametersExtensions.cs ├── Models ├── LinkDto.cs └── QueryParameters.cs ├── Program.cs ├── Properties └── launchSettings.json ├── Repositories ├── CustomerDbContext.cs ├── CustomerRepository.cs └── ICustomerRepository.cs ├── Services ├── ISeedDataService.cs └── SeedDataService.cs ├── Startup.cs ├── appsettings.Development.json └── appsettings.json /.gitignore: -------------------------------------------------------------------------------- 1 | server/.vs/ 2 | server/src/ASPNETCoreWebAPI/obj 3 | server/src/ASPNETCoreWebAPI/bin 4 | /server/aspnetcore/ASPNETCoreWebAPI/bin 5 | /server/aspnetcore/ASPNETCoreWebAPI/obj 6 | -------------------------------------------------------------------------------- /client/angularclient/.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 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /client/angularclient/.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 | # Only exists if Bazel was run 8 | /bazel-out 9 | 10 | # dependencies 11 | /node_modules 12 | 13 | # profiling files 14 | chrome-profiler-events.json 15 | speed-measure-plugin.json 16 | 17 | # IDEs and editors 18 | /.idea 19 | .project 20 | .classpath 21 | .c9/ 22 | *.launch 23 | .settings/ 24 | *.sublime-workspace 25 | 26 | # IDE - VSCode 27 | .vscode/* 28 | !.vscode/settings.json 29 | !.vscode/tasks.json 30 | !.vscode/launch.json 31 | !.vscode/extensions.json 32 | .history/* 33 | 34 | # misc 35 | /.sass-cache 36 | /connect.lock 37 | /coverage 38 | /libpeerconnection.log 39 | npm-debug.log 40 | yarn-error.log 41 | testem.log 42 | /typings 43 | 44 | # System Files 45 | .DS_Store 46 | Thumbs.db 47 | -------------------------------------------------------------------------------- /client/angularclient/README.md: -------------------------------------------------------------------------------- 1 | # Angularclient 2 | 3 | This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.3.9. 4 | 5 | ## Development server 6 | 7 | Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app 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. Use the `--prod` flag for a production build. 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 [Protractor](http://www.protractortest.org/). 24 | 25 | ## Further help 26 | 27 | To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). 28 | -------------------------------------------------------------------------------- /client/angularclient/angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "angularclient": { 7 | "root": "", 8 | "sourceRoot": "src", 9 | "projectType": "application", 10 | "prefix": "app", 11 | "schematics": {}, 12 | "architect": { 13 | "build": { 14 | "builder": "@angular-devkit/build-angular:browser", 15 | "options": { 16 | "outputPath": "dist/angularclient", 17 | "index": "src/index.html", 18 | "main": "src/main.ts", 19 | "polyfills": "src/polyfills.ts", 20 | "tsConfig": "src/tsconfig.app.json", 21 | "assets": [ 22 | "src/favicon.ico", 23 | "src/assets" 24 | ], 25 | "styles": [ 26 | "src/styles.css" 27 | ], 28 | "scripts": [], 29 | "es5BrowserSupport": true 30 | }, 31 | "configurations": { 32 | "production": { 33 | "fileReplacements": [ 34 | { 35 | "replace": "src/environments/environment.ts", 36 | "with": "src/environments/environment.prod.ts" 37 | } 38 | ], 39 | "optimization": true, 40 | "outputHashing": "all", 41 | "sourceMap": false, 42 | "extractCss": true, 43 | "namedChunks": false, 44 | "aot": true, 45 | "extractLicenses": true, 46 | "vendorChunk": false, 47 | "buildOptimizer": true, 48 | "budgets": [ 49 | { 50 | "type": "initial", 51 | "maximumWarning": "2mb", 52 | "maximumError": "5mb" 53 | } 54 | ] 55 | } 56 | } 57 | }, 58 | "serve": { 59 | "builder": "@angular-devkit/build-angular:dev-server", 60 | "options": { 61 | "browserTarget": "angularclient:build" 62 | }, 63 | "configurations": { 64 | "production": { 65 | "browserTarget": "angularclient:build:production" 66 | } 67 | } 68 | }, 69 | "extract-i18n": { 70 | "builder": "@angular-devkit/build-angular:extract-i18n", 71 | "options": { 72 | "browserTarget": "angularclient:build" 73 | } 74 | }, 75 | "test": { 76 | "builder": "@angular-devkit/build-angular:karma", 77 | "options": { 78 | "main": "src/test.ts", 79 | "polyfills": "src/polyfills.ts", 80 | "tsConfig": "src/tsconfig.spec.json", 81 | "karmaConfig": "src/karma.conf.js", 82 | "styles": [ 83 | "src/styles.css" 84 | ], 85 | "scripts": [], 86 | "assets": [ 87 | "src/favicon.ico", 88 | "src/assets" 89 | ] 90 | } 91 | }, 92 | "lint": { 93 | "builder": "@angular-devkit/build-angular:tslint", 94 | "options": { 95 | "tsConfig": [ 96 | "src/tsconfig.app.json", 97 | "src/tsconfig.spec.json" 98 | ], 99 | "exclude": [ 100 | "**/node_modules/**" 101 | ] 102 | } 103 | } 104 | } 105 | }, 106 | "angularclient-e2e": { 107 | "root": "e2e/", 108 | "projectType": "application", 109 | "prefix": "", 110 | "architect": { 111 | "e2e": { 112 | "builder": "@angular-devkit/build-angular:protractor", 113 | "options": { 114 | "protractorConfig": "e2e/protractor.conf.js", 115 | "devServerTarget": "angularclient:serve" 116 | }, 117 | "configurations": { 118 | "production": { 119 | "devServerTarget": "angularclient:serve:production" 120 | } 121 | } 122 | }, 123 | "lint": { 124 | "builder": "@angular-devkit/build-angular:tslint", 125 | "options": { 126 | "tsConfig": "e2e/tsconfig.e2e.json", 127 | "exclude": [ 128 | "**/node_modules/**" 129 | ] 130 | } 131 | } 132 | } 133 | } 134 | }, 135 | "defaultProject": "angularclient" 136 | } -------------------------------------------------------------------------------- /client/angularclient/e2e/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // Protractor configuration file, see link for more information 2 | // https://github.com/angular/protractor/blob/master/lib/config.ts 3 | 4 | const { SpecReporter } = require('jasmine-spec-reporter'); 5 | 6 | exports.config = { 7 | allScriptsTimeout: 11000, 8 | specs: [ 9 | './src/**/*.e2e-spec.ts' 10 | ], 11 | capabilities: { 12 | 'browserName': 'chrome' 13 | }, 14 | directConnect: true, 15 | baseUrl: 'http://localhost:4200/', 16 | framework: 'jasmine', 17 | jasmineNodeOpts: { 18 | showColors: true, 19 | defaultTimeoutInterval: 30000, 20 | print: function() {} 21 | }, 22 | onPrepare() { 23 | require('ts-node').register({ 24 | project: require('path').join(__dirname, './tsconfig.e2e.json') 25 | }); 26 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 27 | } 28 | }; -------------------------------------------------------------------------------- /client/angularclient/e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | import { browser, logging } from 'protractor'; 3 | 4 | describe('workspace-project App', () => { 5 | let page: AppPage; 6 | 7 | beforeEach(() => { 8 | page = new AppPage(); 9 | }); 10 | 11 | it('should display welcome message', () => { 12 | page.navigateTo(); 13 | expect(page.getTitleText()).toEqual('Welcome to angularclient!'); 14 | }); 15 | 16 | afterEach(async () => { 17 | // Assert that there are no errors emitted from the browser 18 | const logs = await browser.manage().logs().get(logging.Type.BROWSER); 19 | expect(logs).not.toContain(jasmine.objectContaining({ 20 | level: logging.Level.SEVERE, 21 | } as logging.Entry)); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /client/angularclient/e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get(browser.baseUrl) as Promise; 6 | } 7 | 8 | getTitleText() { 9 | return element(by.css('app-root h1')).getText() as Promise; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /client/angularclient/e2e/tsconfig.e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "types": [ 8 | "jasmine", 9 | "jasminewd2", 10 | "node" 11 | ] 12 | } 13 | } -------------------------------------------------------------------------------- /client/angularclient/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angularclient", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "test": "ng test", 9 | "lint": "ng lint", 10 | "e2e": "ng e2e" 11 | }, 12 | "private": true, 13 | "dependencies": { 14 | "@angular/animations": "~7.2.0", 15 | "@angular/common": "~7.2.0", 16 | "@angular/compiler": "~7.2.0", 17 | "@angular/core": "~7.2.0", 18 | "@angular/forms": "~7.2.0", 19 | "@angular/platform-browser": "~7.2.0", 20 | "@angular/platform-browser-dynamic": "~7.2.0", 21 | "@angular/router": "~7.2.0", 22 | "@angular/material": "~7.2.0", 23 | "@angular/cdk": "~7.2.0", 24 | "core-js": "^2.5.4", 25 | "rxjs": "~6.3.3", 26 | "tslib": "^1.9.0", 27 | "zone.js": "~0.8.26" 28 | }, 29 | "devDependencies": { 30 | "@angular-devkit/build-angular": "~0.13.0", 31 | "@angular/cli": "~7.3.9", 32 | "@angular/compiler-cli": "~7.2.0", 33 | "@angular/language-service": "~7.2.0", 34 | "@types/node": "~8.9.4", 35 | "@types/jasmine": "~2.8.8", 36 | "@types/jasminewd2": "~2.0.3", 37 | "codelyzer": "~4.5.0", 38 | "jasmine-core": "~2.99.1", 39 | "jasmine-spec-reporter": "~4.2.1", 40 | "karma": "~4.0.0", 41 | "karma-chrome-launcher": "~2.2.0", 42 | "karma-coverage-istanbul-reporter": "~2.0.1", 43 | "karma-jasmine": "~1.1.2", 44 | "karma-jasmine-html-reporter": "^0.2.2", 45 | "protractor": "~5.4.0", 46 | "ts-node": "~7.0.0", 47 | "tslint": "~5.11.0", 48 | "typescript": "~3.2.2" 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /client/angularclient/src/app/app.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FabianGosebrink/ASPNETCore-Angular-Material-HATEOAS-Paging/bd5d7995b9def6c2d2207f9872159931ce5fd14f/client/angularclient/src/app/app.component.css -------------------------------------------------------------------------------- /client/angularclient/src/app/app.component.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /client/angularclient/src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, async } from '@angular/core/testing'; 2 | import { AppComponent } from './app.component'; 3 | describe('AppComponent', () => { 4 | beforeEach(async(() => { 5 | TestBed.configureTestingModule({ 6 | declarations: [ 7 | AppComponent 8 | ], 9 | }).compileComponents(); 10 | })); 11 | it('should create the app', async(() => { 12 | const fixture = TestBed.createComponent(AppComponent); 13 | const app = fixture.debugElement.componentInstance; 14 | expect(app).toBeTruthy(); 15 | })); 16 | it(`should have as title 'app'`, async(() => { 17 | const fixture = TestBed.createComponent(AppComponent); 18 | const app = fixture.debugElement.componentInstance; 19 | expect(app.title).toEqual('app'); 20 | })); 21 | it('should render title in a h1 tag', async(() => { 22 | const fixture = TestBed.createComponent(AppComponent); 23 | fixture.detectChanges(); 24 | const compiled = fixture.debugElement.nativeElement; 25 | expect(compiled.querySelector('h1').textContent).toContain('Welcome to app!'); 26 | })); 27 | }); 28 | -------------------------------------------------------------------------------- /client/angularclient/src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-root', 5 | templateUrl: './app.component.html', 6 | styleUrls: ['./app.component.css'] 7 | }) 8 | 9 | export class AppComponent { } 10 | -------------------------------------------------------------------------------- /client/angularclient/src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { BrowserModule } from '@angular/platform-browser'; 3 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 4 | import { RouterModule } from '@angular/router'; 5 | 6 | import { AppComponent } from './app.component'; 7 | import { AppRoutes } from './app.routing'; 8 | import { CoreModule } from './core/core.module'; 9 | import { CustomerModule } from './customer/customer.module'; 10 | 11 | @NgModule({ 12 | declarations: [ 13 | AppComponent 14 | ], 15 | 16 | imports: [ 17 | BrowserModule, 18 | CustomerModule, 19 | BrowserAnimationsModule, 20 | 21 | RouterModule.forRoot(AppRoutes), 22 | CoreModule 23 | ], 24 | 25 | providers: [], 26 | 27 | bootstrap: [ 28 | AppComponent 29 | ] 30 | 31 | }) 32 | export class AppModule { } 33 | -------------------------------------------------------------------------------- /client/angularclient/src/app/app.routing.ts: -------------------------------------------------------------------------------- 1 | import { Routes } from '@angular/router'; 2 | 3 | export const AppRoutes: Routes = [ 4 | { path: '', redirectTo: 'all', pathMatch: 'full' }, 5 | { 6 | path: '**', 7 | redirectTo: 'all' 8 | } 9 | ]; 10 | -------------------------------------------------------------------------------- /client/angularclient/src/app/core/core.module.ts: -------------------------------------------------------------------------------- 1 | import { HttpClientModule } from '@angular/common/http'; 2 | import { NgModule } from '@angular/core'; 3 | 4 | import { CustomerDataService } from './services/customer-data.service'; 5 | import { PaginationService } from './services/pagination.service'; 6 | 7 | @NgModule({ 8 | imports: [HttpClientModule], 9 | exports: [], 10 | declarations: [], 11 | providers: [ 12 | CustomerDataService, 13 | PaginationService 14 | ], 15 | }) 16 | 17 | export class CoreModule { } 18 | -------------------------------------------------------------------------------- /client/angularclient/src/app/core/services/customer-data.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | 3 | import { Customer } from '../../models/customer.model'; 4 | import { HttpBaseService } from './http-base.service'; 5 | 6 | @Injectable() 7 | export class CustomerDataService extends HttpBaseService { 8 | 9 | fireRequest(customer: Customer, method: string) { 10 | 11 | const links = customer.links 12 | ? customer.links.find(x => x.method === method) 13 | : null; 14 | 15 | switch (method) { 16 | case 'DELETE': { 17 | return super.delete(links.href); 18 | } 19 | case 'POST': { 20 | return super.add(customer); 21 | } 22 | case 'PUT': { 23 | return super.update(links.href, customer); 24 | } 25 | default: { 26 | console.log(`${links.method} not found!!!`); 27 | break; 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /client/angularclient/src/app/core/services/http-base.service.ts: -------------------------------------------------------------------------------- 1 | import { HttpClient, HttpHeaders } from '@angular/common/http'; 2 | import { Injectable } from '@angular/core'; 3 | import { PaginationService } from './pagination.service'; 4 | 5 | @Injectable() 6 | export class HttpBaseService { 7 | private headers = new HttpHeaders(); 8 | private endpoint = `https://localhost:44380/api/customers/`; 9 | 10 | constructor( 11 | private httpClient: HttpClient, 12 | private paginationService: PaginationService 13 | ) { 14 | this.headers = this.headers.set('Content-Type', 'application/json'); 15 | this.headers = this.headers.set('Accept', 'application/json'); 16 | } 17 | 18 | getAll() { 19 | const mergedUrl = 20 | `${this.endpoint}` + 21 | `?page=${this.paginationService.page}&pageCount=${ 22 | this.paginationService.pageCount 23 | }`; 24 | 25 | return this.httpClient.get(mergedUrl, { observe: 'response' }); 26 | } 27 | 28 | getSingle(id: number) { 29 | return this.httpClient.get(`${this.endpoint}${id}`); 30 | } 31 | 32 | add(toAdd: T) { 33 | return this.httpClient.post(this.endpoint, toAdd, { 34 | headers: this.headers 35 | }); 36 | } 37 | 38 | update(url: string, toUpdate: T) { 39 | return this.httpClient.put(url, toUpdate, { headers: this.headers }); 40 | } 41 | 42 | delete(url: string) { 43 | return this.httpClient.delete(url); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /client/angularclient/src/app/core/services/pagination.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { PageEvent } from '@angular/material/paginator'; 3 | 4 | import { PaginationModel } from '../../models/pagination.model'; 5 | 6 | @Injectable() 7 | export class PaginationService { 8 | private paginationModel: PaginationModel; 9 | 10 | get page(): number { 11 | return this.paginationModel.pageIndex; 12 | } 13 | 14 | get selectItemsPerPage(): number[] { 15 | return this.paginationModel.selectItemsPerPage; 16 | } 17 | 18 | get pageCount(): number { 19 | return this.paginationModel.pageSize; 20 | } 21 | 22 | constructor() { 23 | this.paginationModel = new PaginationModel(); 24 | } 25 | 26 | change(pageEvent: PageEvent) { 27 | this.paginationModel.pageIndex = pageEvent.pageIndex + 1; 28 | this.paginationModel.pageSize = pageEvent.pageSize; 29 | this.paginationModel.allItemsLength = pageEvent.length; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /client/angularclient/src/app/customer/customer.module.ts: -------------------------------------------------------------------------------- 1 | import { ListComponent } from './list/list.component'; 2 | import { CommonModule } from '@angular/common'; 3 | import { NgModule } from '@angular/core'; 4 | import { FormsModule } from '@angular/forms'; 5 | import { MatButtonModule } from '@angular/material/button'; 6 | import { MatIconModule } from '@angular/material/icon'; 7 | import { MatInputModule } from '@angular/material/input'; 8 | import { MatPaginatorModule } from '@angular/material/paginator'; 9 | import { MatTableModule } from '@angular/material/table'; 10 | import { RouterModule } from '@angular/router'; 11 | 12 | import { CustomerRoutes } from './customer.routing'; 13 | import { DetailsComponent } from './details/details.component'; 14 | import { OverviewComponent } from './overview/overview.component'; 15 | 16 | @NgModule({ 17 | imports: [ 18 | CommonModule, 19 | RouterModule.forChild(CustomerRoutes), 20 | MatPaginatorModule, 21 | MatButtonModule, 22 | MatTableModule, 23 | MatIconModule, 24 | MatInputModule, 25 | FormsModule 26 | ], 27 | exports: [], 28 | declarations: [ 29 | OverviewComponent, 30 | DetailsComponent, 31 | ListComponent 32 | ], 33 | providers: [], 34 | }) 35 | export class CustomerModule { } 36 | -------------------------------------------------------------------------------- /client/angularclient/src/app/customer/customer.routing.ts: -------------------------------------------------------------------------------- 1 | import { Routes } from '@angular/router'; 2 | 3 | import { DetailsComponent } from './details/details.component'; 4 | import { OverviewComponent } from './overview/overview.component'; 5 | 6 | export const CustomerRoutes: Routes = [ 7 | { path: 'all', component: OverviewComponent }, 8 | { path: 'details', component: DetailsComponent }, 9 | { path: 'details/:id', component: DetailsComponent } 10 | ]; 11 | -------------------------------------------------------------------------------- /client/angularclient/src/app/customer/details/details.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | 5 |
6 | 7 | Back 8 |
9 | -------------------------------------------------------------------------------- /client/angularclient/src/app/customer/details/details.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { ActivatedRoute, Router } from '@angular/router'; 3 | import { CustomerDataService } from '../../core/services/customer-data.service'; 4 | import { Customer } from '../../models/customer.model'; 5 | 6 | @Component({ 7 | selector: 'app-details', 8 | templateUrl: 'details.component.html' 9 | }) 10 | export class DetailsComponent implements OnInit { 11 | customer = new Customer(); 12 | 13 | constructor( 14 | private customerDataService: CustomerDataService, 15 | private route: ActivatedRoute, 16 | private router: Router 17 | ) {} 18 | 19 | ngOnInit() { 20 | const id = this.route.snapshot.paramMap.get('id'); 21 | if (id) { 22 | this.customerDataService 23 | .getSingle(+id) 24 | .subscribe(customer => (this.customer = customer)); 25 | } 26 | } 27 | 28 | save() { 29 | const method = this.customer.id ? 'PUT' : 'POST'; 30 | 31 | this.customerDataService 32 | .fireRequest(this.customer, method) 33 | .subscribe(() => this.router.navigate(['all'])); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /client/angularclient/src/app/customer/list/list.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | 5 | No. 6 | {{element.id}} 7 | 8 | 9 | 10 | Name 11 | {{element.name}} 12 | 13 | 14 | 15 | Created 16 | {{element.created | date}} 17 | 18 | 19 | 20 | Actions 21 | 22 | 23 | edit 24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 | 32 | 33 | 37 | -------------------------------------------------------------------------------- /client/angularclient/src/app/customer/list/list.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, EventEmitter, Input, Output } from '@angular/core'; 2 | import { MatTableDataSource } from '@angular/material/table'; 3 | 4 | import { PaginationService } from '../../core/services/pagination.service'; 5 | import { Customer } from '../../models/customer.model'; 6 | 7 | @Component({ 8 | selector: 'app-list', 9 | templateUrl: 'list.component.html' 10 | }) 11 | 12 | export class ListComponent { 13 | 14 | dataSource = new MatTableDataSource(); 15 | displayedColumns = ['id', 'name', 'created', 'actions']; 16 | 17 | @Input('dataSource') 18 | set dataSourceForTable(value: Customer[]) { 19 | this.dataSource = new MatTableDataSource(value); 20 | } 21 | 22 | @Input() totalCount: number; 23 | @Output() onDeleteCustomer = new EventEmitter(); 24 | @Output() onPageSwitch = new EventEmitter(); 25 | 26 | constructor(public paginationService: PaginationService) { } 27 | } 28 | -------------------------------------------------------------------------------- /client/angularclient/src/app/customer/overview/overview.component.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | Add new Customer -------------------------------------------------------------------------------- /client/angularclient/src/app/customer/overview/overview.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { PageEvent } from '@angular/material/paginator'; 3 | import { CustomerDataService } from '../../core/services/customer-data.service'; 4 | import { PaginationService } from '../../core/services/pagination.service'; 5 | import { Customer } from '../../models/customer.model'; 6 | 7 | 8 | @Component({ 9 | selector: 'app-overview', 10 | templateUrl: 'overview.component.html' 11 | }) 12 | 13 | export class OverviewComponent implements OnInit { 14 | 15 | dataSource: Customer[]; 16 | totalCount: number; 17 | 18 | constructor( 19 | private customerDataService: CustomerDataService, 20 | private paginationService: PaginationService) { } 21 | 22 | ngOnInit(): void { 23 | this.getAllCustomers(); 24 | } 25 | 26 | switchPage(event: PageEvent) { 27 | this.paginationService.change(event); 28 | this.getAllCustomers(); 29 | } 30 | 31 | delete(customer: Customer) { 32 | this.customerDataService.fireRequest(customer, 'DELETE') 33 | .subscribe(() => { 34 | this.dataSource = this.dataSource.filter(x => x.id !== customer.id); 35 | }); 36 | } 37 | 38 | getAllCustomers() { 39 | this.customerDataService.getAll() 40 | .subscribe((result: any) => { 41 | this.totalCount = JSON.parse(result.headers.get('X-Pagination')).totalCount; 42 | this.dataSource = result.body.value; 43 | }); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /client/angularclient/src/app/models/customer.model.ts: -------------------------------------------------------------------------------- 1 | export class Customer { 2 | id: number; 3 | name: string; 4 | created: Date; 5 | links: Link[]; 6 | } 7 | 8 | export class Link { 9 | href: string; 10 | rel: string; 11 | method: string; 12 | } 13 | -------------------------------------------------------------------------------- /client/angularclient/src/app/models/pagination.model.ts: -------------------------------------------------------------------------------- 1 | export class PaginationModel { 2 | selectItemsPerPage: number[] = [5, 10, 25, 100]; 3 | pageSize = this.selectItemsPerPage[0]; 4 | pageIndex = 1; 5 | allItemsLength = 0; 6 | } 7 | -------------------------------------------------------------------------------- /client/angularclient/src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FabianGosebrink/ASPNETCore-Angular-Material-HATEOAS-Paging/bd5d7995b9def6c2d2207f9872159931ce5fd14f/client/angularclient/src/assets/.gitkeep -------------------------------------------------------------------------------- /client/angularclient/src/browserslist: -------------------------------------------------------------------------------- 1 | # This file is currently used by autoprefixer to adjust CSS to support the below specified browsers 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | # 5 | # For IE 9-11 support, please remove 'not' from the last line of the file and adjust as needed 6 | 7 | > 0.5% 8 | last 2 versions 9 | Firefox ESR 10 | not dead 11 | not IE 9-11 -------------------------------------------------------------------------------- /client/angularclient/src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /client/angularclient/src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build --prod` 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/dist/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /client/angularclient/src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FabianGosebrink/ASPNETCore-Angular-Material-HATEOAS-Paging/bd5d7995b9def6c2d2207f9872159931ce5fd14f/client/angularclient/src/favicon.ico -------------------------------------------------------------------------------- /client/angularclient/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Angularclient 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /client/angularclient/src/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-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, '../coverage/angularclient'), 20 | reports: ['html', 'lcovonly', 'text-summary'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | singleRun: false, 30 | restartOnFileChange: true 31 | }); 32 | }; 33 | -------------------------------------------------------------------------------- /client/angularclient/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 | -------------------------------------------------------------------------------- /client/angularclient/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 Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 22 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 23 | 24 | /** 25 | * Web Animations `@angular/platform-browser/animations` 26 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 27 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 28 | */ 29 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 30 | 31 | /** 32 | * By default, zone.js will patch all possible macroTask and DomEvents 33 | * user can disable parts of macroTask/DomEvents patch by setting following flags 34 | * because those flags need to be set before `zone.js` being loaded, and webpack 35 | * will put import in the top of bundle, so user need to create a separate file 36 | * in this directory (for example: zone-flags.ts), and put the following flags 37 | * into that file, and then add the following code before importing zone.js. 38 | * import './zone-flags.ts'; 39 | * 40 | * The flags allowed in zone-flags.ts are listed here. 41 | * 42 | * The following flags will work for all browsers. 43 | * 44 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 45 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 46 | * (window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 47 | * 48 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 49 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 50 | * 51 | * (window as any).__Zone_enable_cross_context_check = true; 52 | * 53 | */ 54 | 55 | /*************************************************************************************************** 56 | * Zone JS is required by default for Angular itself. 57 | */ 58 | import 'zone.js/dist/zone'; // Included with Angular CLI. 59 | 60 | 61 | /*************************************************************************************************** 62 | * APPLICATION IMPORTS 63 | */ 64 | -------------------------------------------------------------------------------- /client/angularclient/src/styles.css: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | @import '@angular/material/prebuilt-themes/deeppurple-amber.css'; 3 | -------------------------------------------------------------------------------- /client/angularclient/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/dist/zone-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: any; 11 | 12 | // First, initialize the Angular testing environment. 13 | getTestBed().initTestEnvironment( 14 | BrowserDynamicTestingModule, 15 | platformBrowserDynamicTesting() 16 | ); 17 | // Then we find all the tests. 18 | const context = require.context('./', true, /\.spec\.ts$/); 19 | // And load the modules. 20 | context.keys().map(context); 21 | -------------------------------------------------------------------------------- /client/angularclient/src/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "types": [] 6 | }, 7 | "exclude": [ 8 | "test.ts", 9 | "**/*.spec.ts" 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /client/angularclient/src/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/spec", 5 | "types": [ 6 | "jasmine", 7 | "node" 8 | ] 9 | }, 10 | "files": [ 11 | "test.ts", 12 | "polyfills.ts" 13 | ], 14 | "include": [ 15 | "**/*.spec.ts", 16 | "**/*.d.ts" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /client/angularclient/src/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tslint.json", 3 | "rules": { 4 | "directive-selector": [ 5 | true, 6 | "attribute", 7 | "app", 8 | "camelCase" 9 | ], 10 | "component-selector": [ 11 | true, 12 | "element", 13 | "app", 14 | "kebab-case" 15 | ] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /client/angularclient/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "./dist/out-tsc", 6 | "sourceMap": true, 7 | "declaration": false, 8 | "module": "es2015", 9 | "moduleResolution": "node", 10 | "emitDecoratorMetadata": true, 11 | "experimentalDecorators": true, 12 | "importHelpers": true, 13 | "target": "es5", 14 | "typeRoots": [ 15 | "node_modules/@types" 16 | ], 17 | "lib": [ 18 | "es2018", 19 | "dom" 20 | ] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /client/angularclient/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint:recommended", 3 | "rulesDirectory": [ 4 | "codelyzer" 5 | ], 6 | "rules": { 7 | "array-type": false, 8 | "arrow-parens": false, 9 | "deprecation": { 10 | "severity": "warn" 11 | }, 12 | "import-blacklist": [ 13 | true, 14 | "rxjs/Rx" 15 | ], 16 | "interface-name": false, 17 | "max-classes-per-file": false, 18 | "max-line-length": [ 19 | true, 20 | 140 21 | ], 22 | "member-access": false, 23 | "member-ordering": [ 24 | true, 25 | { 26 | "order": [ 27 | "static-field", 28 | "instance-field", 29 | "static-method", 30 | "instance-method" 31 | ] 32 | } 33 | ], 34 | "no-consecutive-blank-lines": false, 35 | "no-console": [ 36 | true, 37 | "debug", 38 | "info", 39 | "time", 40 | "timeEnd", 41 | "trace" 42 | ], 43 | "no-empty": false, 44 | "no-inferrable-types": [ 45 | true, 46 | "ignore-params" 47 | ], 48 | "no-non-null-assertion": true, 49 | "no-redundant-jsdoc": true, 50 | "no-switch-case-fall-through": true, 51 | "no-use-before-declare": true, 52 | "no-var-requires": false, 53 | "object-literal-key-quotes": [ 54 | true, 55 | "as-needed" 56 | ], 57 | "object-literal-sort-keys": false, 58 | "ordered-imports": false, 59 | "quotemark": [ 60 | true, 61 | "single" 62 | ], 63 | "trailing-comma": false, 64 | "no-output-on-prefix": true, 65 | "use-input-property-decorator": true, 66 | "use-output-property-decorator": true, 67 | "use-host-property-decorator": true, 68 | "no-input-rename": true, 69 | "no-output-rename": true, 70 | "use-life-cycle-interface": true, 71 | "use-pipe-transform-interface": true, 72 | "component-class-suffix": true, 73 | "directive-class-suffix": true 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ### ASP.NET Core - Angular Material - Paging 2 | 3 | Text tdb... -------------------------------------------------------------------------------- /server/aspnetcore/.vs/ASPNETCoreWebAPI/DesignTimeBuild/.dtbcache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FabianGosebrink/ASPNETCore-Angular-Material-HATEOAS-Paging/bd5d7995b9def6c2d2207f9872159931ce5fd14f/server/aspnetcore/.vs/ASPNETCoreWebAPI/DesignTimeBuild/.dtbcache -------------------------------------------------------------------------------- /server/aspnetcore/.vs/ASPNETCoreWebAPI/config/applicationhost.config: -------------------------------------------------------------------------------- 1 |  2 | 20 | 21 | 48 | 49 | 50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | 59 | 60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | 79 |
80 |
81 | 82 |
83 |
84 |
85 |
86 |
87 |
88 | 89 |
90 |
91 |
92 |
93 |
94 | 95 |
96 |
97 |
98 | 99 |
100 |
101 | 102 |
103 |
104 | 105 |
106 |
107 |
108 | 109 | 110 |
111 |
112 |
113 |
114 |
115 |
116 | 117 |
118 |
119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 976 | 977 | 978 | 979 | 980 | 981 | 982 | 983 | 984 | 985 | 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | -------------------------------------------------------------------------------- /server/aspnetcore/.vs/ASPNETCoreWebAPI/v16/.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FabianGosebrink/ASPNETCore-Angular-Material-HATEOAS-Paging/bd5d7995b9def6c2d2207f9872159931ce5fd14f/server/aspnetcore/.vs/ASPNETCoreWebAPI/v16/.suo -------------------------------------------------------------------------------- /server/aspnetcore/.vs/ASPNETCoreWebAPI/v16/Server/sqlite3/db.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FabianGosebrink/ASPNETCore-Angular-Material-HATEOAS-Paging/bd5d7995b9def6c2d2207f9872159931ce5fd14f/server/aspnetcore/.vs/ASPNETCoreWebAPI/v16/Server/sqlite3/db.lock -------------------------------------------------------------------------------- /server/aspnetcore/.vs/ASPNETCoreWebAPI/v16/Server/sqlite3/storage.ide: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FabianGosebrink/ASPNETCore-Angular-Material-HATEOAS-Paging/bd5d7995b9def6c2d2207f9872159931ce5fd14f/server/aspnetcore/.vs/ASPNETCoreWebAPI/v16/Server/sqlite3/storage.ide -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.28803.202 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASPNETCoreWebAPI", "ASPNETCoreWebAPI\ASPNETCoreWebAPI.csproj", "{EC80D2E0-6932-477B-8173-F511E4E5BE12}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {EC80D2E0-6932-477B-8173-F511E4E5BE12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {EC80D2E0-6932-477B-8173-F511E4E5BE12}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {EC80D2E0-6932-477B-8173-F511E4E5BE12}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {EC80D2E0-6932-477B-8173-F511E4E5BE12}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {41C9516E-C237-487B-923E-09EAAA9160F1} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/ASPNETCoreWebAPI.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.2 5 | InProcess 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Controllers/CustomersController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using AutoMapper; 4 | using ASPNETCoreWebAPI.Dtos; 5 | using Microsoft.AspNetCore.Mvc; 6 | using ASPNETCoreWebAPI.Repositories; 7 | using System.Collections.Generic; 8 | using ASPNETCoreWebAPI.Entities; 9 | using ASPNETCoreWebAPI.Models; 10 | using ASPNETCoreWebAPI.Helpers; 11 | using Newtonsoft.Json; 12 | 13 | namespace ASPNETCoreWebAPI.Controllers 14 | { 15 | [ApiController] 16 | [Route("api/[controller]")] 17 | public class CustomersController : ControllerBase 18 | { 19 | private readonly ICustomerRepository _customerRepository; 20 | private readonly IUrlHelper _urlHelper; 21 | 22 | public CustomersController(IUrlHelper urlHelper, ICustomerRepository customerRepository) 23 | { 24 | _customerRepository = customerRepository; 25 | _urlHelper = urlHelper; 26 | } 27 | 28 | [HttpGet(Name = nameof(GetAll))] 29 | public IActionResult GetAll([FromQuery] QueryParameters queryParameters) 30 | { 31 | List allCustomers = _customerRepository.GetAll(queryParameters).ToList(); 32 | 33 | var allItemCount = _customerRepository.Count(); 34 | 35 | var paginationMetadata = new 36 | { 37 | totalCount = allItemCount, 38 | pageSize = queryParameters.PageCount, 39 | currentPage = queryParameters.Page, 40 | totalPages = queryParameters.GetTotalPages(allItemCount) 41 | }; 42 | 43 | Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); 44 | 45 | var links = CreateLinksForCollection(queryParameters, allItemCount); 46 | 47 | var toReturn = allCustomers.Select(x => ExpandSingleItem(x)); 48 | 49 | return Ok(new 50 | { 51 | value = toReturn, 52 | links = links 53 | }); 54 | } 55 | 56 | [HttpGet] 57 | [Route("{id:int}", Name = nameof(GetSingle))] 58 | public IActionResult GetSingle(int id) 59 | { 60 | Customer customer = _customerRepository.GetSingle(id); 61 | 62 | if (customer == null) 63 | { 64 | return NotFound(); 65 | } 66 | 67 | return Ok(ExpandSingleItem(customer)); 68 | } 69 | 70 | [HttpPost(Name = nameof(Add))] 71 | public IActionResult Add([FromBody] CustomerCreateDto customerCreateDto) 72 | { 73 | if (customerCreateDto == null) 74 | { 75 | return BadRequest(); 76 | } 77 | 78 | Customer toAdd = Mapper.Map(customerCreateDto); 79 | 80 | toAdd.Created = DateTime.Now; 81 | _customerRepository.Add(toAdd); 82 | 83 | if (!_customerRepository.Save()) 84 | { 85 | throw new Exception("Creating an item failed on save."); 86 | } 87 | 88 | Customer newItem = _customerRepository.GetSingle(toAdd.Id); 89 | 90 | return CreatedAtRoute(nameof(GetSingle), new { id = newItem.Id }, 91 | Mapper.Map(newItem)); 92 | } 93 | 94 | [HttpDelete] 95 | [Route("{id:int}", Name = nameof(Delete))] 96 | public IActionResult Delete(int id) 97 | { 98 | Customer customer = _customerRepository.GetSingle(id); 99 | 100 | if (customer == null) 101 | { 102 | return NotFound(); 103 | } 104 | 105 | _customerRepository.Delete(id); 106 | 107 | if (!_customerRepository.Save()) 108 | { 109 | throw new Exception("Deleting an item failed on save."); 110 | } 111 | 112 | return NoContent(); 113 | } 114 | 115 | [HttpPut] 116 | [Route("{id:int}", Name = nameof(Update))] 117 | public IActionResult Update(int id, [FromBody]CustomerUpdateDto updateDto) 118 | { 119 | if (updateDto == null) 120 | { 121 | return BadRequest(); 122 | } 123 | 124 | var existingCustomer = _customerRepository.GetSingle(id); 125 | 126 | if (existingCustomer == null) 127 | { 128 | return NotFound(); 129 | } 130 | 131 | Mapper.Map(updateDto, existingCustomer); 132 | 133 | _customerRepository.Update(id, existingCustomer); 134 | 135 | if (!_customerRepository.Save()) 136 | { 137 | throw new Exception("Updating an item failed on save."); 138 | } 139 | 140 | return Ok(ExpandSingleItem(existingCustomer)); 141 | } 142 | 143 | private List CreateLinksForCollection(QueryParameters queryParameters, int totalCount) 144 | { 145 | var links = new List(); 146 | 147 | links.Add( 148 | new LinkDto(_urlHelper.Link(nameof(Add), null), "create", "POST")); 149 | 150 | // self 151 | links.Add( 152 | new LinkDto(_urlHelper.Link(nameof(GetAll), new 153 | { 154 | pagecount = queryParameters.PageCount, 155 | page = queryParameters.Page, 156 | orderby = queryParameters.OrderBy 157 | }), "self", "GET")); 158 | 159 | links.Add(new LinkDto(_urlHelper.Link(nameof(GetAll), new 160 | { 161 | pagecount = queryParameters.PageCount, 162 | page = 1, 163 | orderby = queryParameters.OrderBy 164 | }), "first", "GET")); 165 | 166 | links.Add(new LinkDto(_urlHelper.Link(nameof(GetAll), new 167 | { 168 | pagecount = queryParameters.PageCount, 169 | page = queryParameters.GetTotalPages(totalCount), 170 | orderby = queryParameters.OrderBy 171 | }), "last", "GET")); 172 | 173 | if (queryParameters.HasNext(totalCount)) 174 | { 175 | links.Add(new LinkDto(_urlHelper.Link(nameof(GetAll), new 176 | { 177 | pagecount = queryParameters.PageCount, 178 | page = queryParameters.Page + 1, 179 | orderby = queryParameters.OrderBy 180 | }), "next", "GET")); 181 | } 182 | 183 | if (queryParameters.HasPrevious()) 184 | { 185 | links.Add(new LinkDto(_urlHelper.Link(nameof(GetAll), new 186 | { 187 | pagecount = queryParameters.PageCount, 188 | page = queryParameters.Page - 1, 189 | orderby = queryParameters.OrderBy 190 | }), "previous", "GET")); 191 | } 192 | 193 | return links; 194 | } 195 | 196 | private dynamic ExpandSingleItem(Customer customer) 197 | { 198 | var links = GetLinks(customer.Id); 199 | CustomerDto item = Mapper.Map(customer); 200 | 201 | var resourceToReturn = item.ToDynamic() as IDictionary; 202 | resourceToReturn.Add("links", links); 203 | 204 | return resourceToReturn; 205 | } 206 | 207 | private IEnumerable GetLinks(int id) 208 | { 209 | var links = new List(); 210 | 211 | links.Add( 212 | new LinkDto(_urlHelper.Link(nameof(GetSingle), new { id = id }), 213 | "self", 214 | "GET")); 215 | 216 | links.Add( 217 | new LinkDto(_urlHelper.Link(nameof(Delete), new { id = id }), 218 | "delete", 219 | "DELETE")); 220 | 221 | links.Add( 222 | new LinkDto(_urlHelper.Link(nameof(Add), null), 223 | "create", 224 | "POST")); 225 | 226 | links.Add( 227 | new LinkDto(_urlHelper.Link(nameof(Update), new { id = id }), 228 | "update", 229 | "PUT")); 230 | 231 | return links; 232 | } 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Dtos/CustomerCreateDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | 7 | namespace ASPNETCoreWebAPI.Dtos 8 | { 9 | public class CustomerCreateDto 10 | { 11 | [Required] 12 | public string Name { get; set; } 13 | public DateTime Created { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Dtos/CustomerDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ASPNETCoreWebAPI.Dtos 4 | { 5 | public class CustomerDto 6 | { 7 | public int Id { get; set; } 8 | public string Name { get; set; } 9 | public DateTime Created { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Dtos/CustomerUpdateDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace ASPNETCoreWebAPI.Dtos 7 | { 8 | public class CustomerUpdateDto 9 | { 10 | public string Name { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Entities/Customer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ASPNETCoreWebAPI.Entities 4 | { 5 | public class Customer 6 | { 7 | public int Id { get; set; } 8 | public string Name { get; set; } 9 | public DateTime Created { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Helpers/DynamicExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.ComponentModel; 3 | using System.Dynamic; 4 | 5 | namespace ASPNETCoreWebAPI.Models 6 | { 7 | public static class DynamicExtensions 8 | { 9 | public static dynamic ToDynamic(this object value) 10 | { 11 | IDictionary expando = new ExpandoObject(); 12 | 13 | foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType())) 14 | expando.Add(property.Name, property.GetValue(value)); 15 | 16 | return expando as ExpandoObject; 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Helpers/QueryParametersExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using ASPNETCoreWebAPI.Models; 4 | 5 | namespace ASPNETCoreWebAPI.Helpers 6 | { 7 | public static class QueryParametersExtensions 8 | { 9 | public static bool HasPrevious(this QueryParameters queryParameters) 10 | { 11 | return (queryParameters.Page > 1); 12 | } 13 | 14 | public static bool HasNext(this QueryParameters queryParameters, int totalCount) 15 | { 16 | return (queryParameters.Page < (int)GetTotalPages(queryParameters, totalCount)); 17 | } 18 | 19 | public static double GetTotalPages(this QueryParameters queryParameters, int totalCount) 20 | { 21 | return Math.Ceiling(totalCount / (double)queryParameters.PageCount); 22 | } 23 | 24 | public static bool HasQuery(this QueryParameters queryParameters) 25 | { 26 | return !String.IsNullOrEmpty(queryParameters.Query); 27 | } 28 | 29 | public static bool IsDescending(this QueryParameters queryParameters) 30 | { 31 | if (!String.IsNullOrEmpty(queryParameters.OrderBy)) 32 | { 33 | return queryParameters.OrderBy.Split(' ').Last().ToLowerInvariant().StartsWith("desc"); 34 | } 35 | return false; 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Models/LinkDto.cs: -------------------------------------------------------------------------------- 1 | namespace ASPNETCoreWebAPI.Models 2 | { 3 | public class LinkDto 4 | { 5 | public string Href { get; set; } 6 | public string Rel { get; set; } 7 | public string Method { get; set; } 8 | 9 | public LinkDto(string href, string rel, string method) 10 | { 11 | Href = href; 12 | Rel = rel; 13 | Method = method; 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Models/QueryParameters.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace ASPNETCoreWebAPI.Models 5 | { 6 | public class QueryParameters 7 | { 8 | private const int maxPageCount = 50; 9 | public int Page { get; set; } = 1; 10 | 11 | private int _pageCount = maxPageCount; 12 | public int PageCount 13 | { 14 | get { return _pageCount; } 15 | set { _pageCount = (value > maxPageCount) ? maxPageCount : value; } 16 | } 17 | 18 | public string Query { get; set; } 19 | 20 | public string OrderBy { get; set; } = "Name"; 21 | } 22 | } -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ASPNETCoreWebAPI.Repositories; 3 | using ASPNETCoreWebAPI.Services; 4 | using Microsoft.AspNetCore; 5 | using Microsoft.AspNetCore.Hosting; 6 | using Microsoft.Extensions.Configuration; 7 | using Microsoft.Extensions.DependencyInjection; 8 | using Microsoft.Extensions.Logging; 9 | 10 | namespace ASPNETCoreWebAPI 11 | { 12 | public class Program 13 | { 14 | public static void Main(string[] args) 15 | { 16 | var host = BuildWebHost(args); 17 | 18 | using (var scope = host.Services.CreateScope()) 19 | { 20 | var services = scope.ServiceProvider; 21 | try 22 | { 23 | var context = services.GetRequiredService(); 24 | var dbInitializer = services.GetRequiredService(); 25 | dbInitializer.Initialize(context).GetAwaiter().GetResult(); 26 | } 27 | catch (Exception ex) 28 | { 29 | var logger = services.GetRequiredService>(); 30 | logger.LogError(ex, "An error occurred while seeding the database."); 31 | } 32 | } 33 | 34 | host.Run(); 35 | } 36 | 37 | public static IWebHost BuildWebHost(string[] args) => 38 | WebHost.CreateDefaultBuilder(args) 39 | .ConfigureAppConfiguration((context, config) => 40 | { 41 | var env = context.HostingEnvironment; 42 | 43 | config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 44 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); 45 | }) 46 | .ConfigureLogging((hostingContext, logging) => 47 | { 48 | logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); 49 | logging.AddConsole(); 50 | logging.AddDebug(); 51 | }) 52 | .UseStartup() 53 | .Build(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:65515", 8 | "sslPort": 44380 9 | } 10 | }, 11 | "profiles": { 12 | "IIS Express": { 13 | "commandName": "IISExpress", 14 | "launchBrowser": true, 15 | "launchUrl": "swagger", 16 | "environmentVariables": { 17 | "ASPNETCORE_ENVIRONMENT": "Development" 18 | } 19 | }, 20 | "ASPNETCoreWebAPI": { 21 | "commandName": "Project", 22 | "launchBrowser": true, 23 | "launchUrl": "api/values", 24 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 25 | "environmentVariables": { 26 | "ASPNETCORE_ENVIRONMENT": "Development" 27 | } 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Repositories/CustomerDbContext.cs: -------------------------------------------------------------------------------- 1 | using ASPNETCoreWebAPI.Entities; 2 | using Microsoft.EntityFrameworkCore; 3 | 4 | namespace ASPNETCoreWebAPI.Repositories 5 | { 6 | public class CustomerDbContext : DbContext 7 | { 8 | public CustomerDbContext(DbContextOptions options) 9 | : base(options) 10 | { 11 | 12 | } 13 | 14 | public DbSet Customers { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Repositories/CustomerRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using ASPNETCoreWebAPI.Entities; 6 | using ASPNETCoreWebAPI.Models; 7 | using System.Linq.Dynamic.Core; 8 | using ASPNETCoreWebAPI.Helpers; 9 | 10 | namespace ASPNETCoreWebAPI.Repositories 11 | { 12 | public class CustomerRepository : ICustomerRepository 13 | { 14 | private readonly CustomerDbContext _context; 15 | 16 | public CustomerRepository(CustomerDbContext context) 17 | { 18 | _context = context; 19 | } 20 | 21 | public Customer GetSingle(int id) 22 | { 23 | return _context.Customers.FirstOrDefault(x => x.Id == id); 24 | } 25 | 26 | public void Add(Customer item) 27 | { 28 | _context.Customers.Add(item); 29 | } 30 | 31 | public void Delete(int id) 32 | { 33 | Customer Customer = GetSingle(id); 34 | _context.Customers.Remove(Customer); 35 | } 36 | 37 | public Customer Update(int id, Customer item) 38 | { 39 | _context.Customers.Update(item); 40 | return item; 41 | } 42 | 43 | public IQueryable GetAll(QueryParameters queryParameters) 44 | { 45 | IQueryable _allItems = _context.Customers.OrderBy(queryParameters.OrderBy, 46 | queryParameters.IsDescending()); 47 | 48 | if (queryParameters.HasQuery()) 49 | { 50 | _allItems = _allItems 51 | .Where(x => x.Name.ToString().Contains(queryParameters.Query.ToLowerInvariant())); 52 | } 53 | 54 | return _allItems 55 | .Skip(queryParameters.PageCount * (queryParameters.Page - 1)) 56 | .Take(queryParameters.PageCount); 57 | } 58 | 59 | public int Count() 60 | { 61 | return _context.Customers.Count(); 62 | } 63 | 64 | public bool Save() 65 | { 66 | return (_context.SaveChanges() >= 0); 67 | } 68 | } 69 | } -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Repositories/ICustomerRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using ASPNETCoreWebAPI.Entities; 4 | using ASPNETCoreWebAPI.Models; 5 | 6 | namespace ASPNETCoreWebAPI.Repositories 7 | { 8 | public interface ICustomerRepository 9 | { 10 | Customer GetSingle(int id); 11 | void Add(Customer item); 12 | void Delete(int id); 13 | Customer Update(int id, Customer item); 14 | IQueryable GetAll(QueryParameters queryParameters); 15 | 16 | int Count(); 17 | 18 | bool Save(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Services/ISeedDataService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using ASPNETCoreWebAPI.Repositories; 3 | 4 | namespace ASPNETCoreWebAPI.Services 5 | { 6 | public interface ISeedDataService 7 | { 8 | Task Initialize(CustomerDbContext context); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Services/SeedDataService.cs: -------------------------------------------------------------------------------- 1 | using ASPNETCoreWebAPI.Entities; 2 | using ASPNETCoreWebAPI.Repositories; 3 | using System; 4 | using System.Threading.Tasks; 5 | 6 | namespace ASPNETCoreWebAPI.Services 7 | { 8 | public class SeedDataService : ISeedDataService 9 | { 10 | public async Task Initialize(CustomerDbContext context) 11 | { 12 | context.Customers.Add(new Customer() { Name = "Phil Collins", Created = DateTime.Now }); 13 | context.Customers.Add(new Customer() { Name = "Tony Banks", Created = DateTime.Now }); 14 | context.Customers.Add(new Customer() { Name = "Mike Rutherford", Created = DateTime.Now }); 15 | context.Customers.Add(new Customer() { Name = "Chester Thompson", Created = DateTime.Now }); 16 | context.Customers.Add(new Customer() { Name = "Daryl Stuermer", Created = DateTime.Now }); 17 | context.Customers.Add(new Customer() { Name = "Mark Knopfler", Created = DateTime.Now }); 18 | 19 | context.Customers.Add(new Customer() { Name = "David Knopfler", Created = DateTime.Now }); 20 | context.Customers.Add(new Customer() { Name = "John Illsley", Created = DateTime.Now }); 21 | context.Customers.Add(new Customer() { Name = "Pick Withers", Created = DateTime.Now }); 22 | 23 | await context.SaveChangesAsync(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using ASPNETCoreWebAPI.Dtos; 4 | using ASPNETCoreWebAPI.Entities; 5 | using ASPNETCoreWebAPI.Repositories; 6 | using ASPNETCoreWebAPI.Services; 7 | using Microsoft.AspNetCore.Builder; 8 | using Microsoft.AspNetCore.Diagnostics; 9 | using Microsoft.AspNetCore.Hosting; 10 | using Microsoft.AspNetCore.Http; 11 | using Microsoft.AspNetCore.Mvc; 12 | using Microsoft.AspNetCore.Mvc.ApiExplorer; 13 | using Microsoft.AspNetCore.Mvc.Infrastructure; 14 | using Microsoft.AspNetCore.Mvc.Routing; 15 | using Microsoft.EntityFrameworkCore; 16 | using Microsoft.Extensions.Configuration; 17 | using Microsoft.Extensions.DependencyInjection; 18 | using Microsoft.Extensions.Logging; 19 | using Newtonsoft.Json.Serialization; 20 | using Swashbuckle.AspNetCore.Swagger; 21 | 22 | namespace ASPNETCoreWebAPI 23 | { 24 | public class Startup 25 | { 26 | public Startup(IConfiguration configuration) 27 | { 28 | Configuration = configuration; 29 | } 30 | 31 | public IConfiguration Configuration { get; } 32 | 33 | // This method gets called by the runtime. Use this method to add services to the container. 34 | public void ConfigureServices(IServiceCollection services) 35 | { 36 | services.AddCors(options => 37 | { 38 | options.AddPolicy("AllowAllOrigins", 39 | builder => builder.AllowAnyOrigin() 40 | .AllowAnyMethod() 41 | .AllowAnyHeader() 42 | .AllowCredentials() 43 | .WithExposedHeaders("X-Pagination")); 44 | }); 45 | 46 | services.AddDbContext(opt => opt.UseInMemoryDatabase("CustomerDatabase")); 47 | 48 | services.AddSingleton(); 49 | services.AddScoped(); 50 | services.AddRouting(options => options.LowercaseUrls = true); 51 | services.AddSingleton(); 52 | services.AddScoped(x => 53 | { 54 | var actionContext = x.GetRequiredService().ActionContext; 55 | var factory = x.GetRequiredService(); 56 | return factory.GetUrlHelper(actionContext); 57 | }); 58 | 59 | services.AddSwaggerGen(c => 60 | { 61 | c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); 62 | }); 63 | 64 | services.AddMvc().AddJsonOptions(options => 65 | { 66 | options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 67 | }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); 68 | } 69 | 70 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 71 | public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, 72 | IHostingEnvironment env) 73 | { 74 | app.Use(async (ctx, next) => 75 | { 76 | await next(); 77 | if (ctx.Response.StatusCode == 204) 78 | { 79 | ctx.Response.ContentLength = 0; 80 | } 81 | }); 82 | 83 | if (env.IsDevelopment()) 84 | { 85 | app.UseDeveloperExceptionPage(); 86 | } 87 | else 88 | { 89 | app.UseHsts(); 90 | app.UseExceptionHandler(errorApp => 91 | { 92 | errorApp.Run(async context => 93 | { 94 | context.Response.StatusCode = 500; 95 | context.Response.ContentType = "text/plain"; 96 | var errorFeature = context.Features.Get(); 97 | if (errorFeature != null) 98 | { 99 | var logger = loggerFactory.CreateLogger("Global exception logger"); 100 | logger.LogError(500, errorFeature.Error, errorFeature.Error.Message); 101 | } 102 | 103 | await context.Response.WriteAsync("There was an error"); 104 | }); 105 | }); 106 | } 107 | 108 | app.UseHttpsRedirection(); 109 | 110 | app.UseCors("AllowAllOrigins"); 111 | AutoMapper.Mapper.Initialize(mapper => 112 | { 113 | mapper.CreateMap().ReverseMap(); 114 | mapper.CreateMap().ReverseMap(); 115 | mapper.CreateMap().ReverseMap(); 116 | }); 117 | 118 | app.UseSwagger(); 119 | app.UseSwaggerUI(c => 120 | { 121 | c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); 122 | }); 123 | 124 | 125 | app.UseMvc(); 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /server/aspnetcore/ASPNETCoreWebAPI/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Warning" 5 | } 6 | }, 7 | "AllowedHosts": "*" 8 | } 9 | --------------------------------------------------------------------------------