├── .browserslistrc ├── .editorconfig ├── .gitignore ├── README.md ├── angular-13-example-crud-app.png ├── angular.json ├── karma.conf.js ├── package-lock.json ├── package.json ├── src ├── app │ ├── app-routing.module.ts │ ├── app.component.css │ ├── app.component.html │ ├── app.component.spec.ts │ ├── app.component.ts │ ├── app.module.ts │ ├── components │ │ ├── add-tutorial │ │ │ ├── add-tutorial.component.css │ │ │ ├── add-tutorial.component.html │ │ │ ├── add-tutorial.component.spec.ts │ │ │ └── add-tutorial.component.ts │ │ ├── tutorial-details │ │ │ ├── tutorial-details.component.css │ │ │ ├── tutorial-details.component.html │ │ │ ├── tutorial-details.component.spec.ts │ │ │ └── tutorial-details.component.ts │ │ └── tutorials-list │ │ │ ├── tutorials-list.component.css │ │ │ ├── tutorials-list.component.html │ │ │ ├── tutorials-list.component.spec.ts │ │ │ └── tutorials-list.component.ts │ ├── models │ │ ├── tutorial.model.spec.ts │ │ └── tutorial.model.ts │ └── services │ │ ├── tutorial.service.spec.ts │ │ └── tutorial.service.ts ├── assets │ └── .gitkeep ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── main.ts ├── polyfills.ts ├── styles.css └── 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 | # 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 | 16 | # IDEs and editors 17 | /.idea 18 | .project 19 | .classpath 20 | .c9/ 21 | *.launch 22 | .settings/ 23 | *.sublime-workspace 24 | 25 | # IDE - VSCode 26 | .vscode/* 27 | !.vscode/settings.json 28 | !.vscode/tasks.json 29 | !.vscode/launch.json 30 | !.vscode/extensions.json 31 | .history/* 32 | 33 | # misc 34 | /.angular/cache 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular example project: CRUD with Rest API 2 | 3 | Build an Angular CRUD App example to consume Rest APIs, display, modify & search data. 4 | 5 | Tutorial Application in that: 6 | - Each Tutorial has id, title, description, published status. 7 | - We can create, retrieve, update, delete Tutorials. 8 | - There is a Search bar for finding Tutorials by title. 9 | 10 | ![angular-13-example-crud-app](angular-13-example-crud-app.png) 11 | 12 | Run `ng serve --port 8081` for a dev server. Navigate to `http://localhost:8081/`. The app will automatically reload if you change any of the source files. 13 | 14 | For instruction, please visit: 15 | > [Angular example: CRUD Application with Rest API](https://www.bezkoder.com/angular-13-crud-example/) 16 | 17 | More Practice: 18 | > [Angular Pagination example | ngx-pagination](https://www.bezkoder.com/angular-13-pagination-ngx/) 19 | 20 | > [Angular JWT Authentication & Authorization with Web API](https://www.bezkoder.com/angular-13-jwt-auth/) 21 | 22 | > [Angular File upload example with Progress bar](https://www.bezkoder.com/angular-13-file-upload/) 23 | 24 | > [Angular Multiple Files upload example with Progress Bar](https://www.bezkoder.com/angular-13-multiple-file-upload/) 25 | 26 | > [Angular Form Validation example (Reactive Forms)](https://www.bezkoder.com/angular-13-form-validation/) 27 | 28 | Fullstack with Node: 29 | 30 | > [Angular + Node Express + MySQL example](https://www.bezkoder.com/angular-13-node-js-express-mysql/) 31 | 32 | > [Angular + Node Express + PostgreSQL example](https://www.bezkoder.com/angular-13-node-js-express-postgresql/) 33 | 34 | > [Angular + Node Express + MongoDB example](https://www.bezkoder.com/mean-stack-crud-example-angular-13/) 35 | 36 | > [Angular + Node Express: File upload example](https://www.bezkoder.com/angular-13-node-express-file-upload/) 37 | 38 | Fullstack with Spring Boot: 39 | 40 | > [Angular + Spring Boot + H2 Embedded Database example](https://www.bezkoder.com/spring-boot-angular-13-crud/) 41 | 42 | > [Angular + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-angular-13-mysql/) 43 | 44 | > [Angular + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-13-postgresql/) 45 | 46 | > [Angular + Spring Boot + MongoDB example](https://www.bezkoder.com/angular-13-spring-boot-mongodb/) 47 | 48 | > [Angular + Spring Boot: File upload example](https://www.bezkoder.com/angular-13-spring-boot-file-upload/) 49 | 50 | Fullstack with Django: 51 | > [Angular + Django example](https://bezkoder.com/django-angular-13-crud-rest-framework/) 52 | 53 | Security: 54 | > [Angular + Spring Boot: JWT Authentication and Authorization example](https://www.bezkoder.com/angular-12-spring-boot-jwt-auth/) 55 | 56 | > [Angular + Node.js Express: JWT Authentication and Authorization example](https://www.bezkoder.com/node-js-angular-12-jwt-auth/) 57 | 58 | Serverless with Firebase: 59 | > [Angular Firebase CRUD with Realtime DataBase | AngularFireDatabase](https://www.bezkoder.com/angular-13-firebase-crud/) 60 | 61 | > [Angular Firestore CRUD example with AngularFireStore](https://www.bezkoder.com/angular-13-firestore-crud-angularfirestore/) 62 | 63 | > [Angular Firebase Storage: File Upload/Display/Delete example](https://www.bezkoder.com/angular-13-firebase-storage/) 64 | 65 | Integration (run back-end & front-end on same server/port) 66 | > [How to integrate Angular with Node Restful Services](https://bezkoder.com/integrate-angular-12-node-js/) 67 | 68 | > [How to Integrate Angular with Spring Boot Rest API](https://bezkoder.com/integrate-angular-12-spring-boot/) 69 | -------------------------------------------------------------------------------- /angular-13-example-crud-app.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/angular-13-crud-example/848637590f4b4d982b773b3abc5f74e4cc21f088/angular-13-example-crud-app.png -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "Angular13Crud": { 7 | "projectType": "application", 8 | "schematics": { 9 | "@schematics/angular:application": { 10 | "strict": true 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/Angular13Crud", 21 | "index": "src/index.html", 22 | "main": "src/main.ts", 23 | "polyfills": "src/polyfills.ts", 24 | "tsConfig": "tsconfig.app.json", 25 | "assets": [ 26 | "src/favicon.ico", 27 | "src/assets" 28 | ], 29 | "styles": [ 30 | "src/styles.css" 31 | ], 32 | "scripts": [] 33 | }, 34 | "configurations": { 35 | "production": { 36 | "budgets": [ 37 | { 38 | "type": "initial", 39 | "maximumWarning": "500kb", 40 | "maximumError": "1mb" 41 | }, 42 | { 43 | "type": "anyComponentStyle", 44 | "maximumWarning": "2kb", 45 | "maximumError": "4kb" 46 | } 47 | ], 48 | "fileReplacements": [ 49 | { 50 | "replace": "src/environments/environment.ts", 51 | "with": "src/environments/environment.prod.ts" 52 | } 53 | ], 54 | "outputHashing": "all" 55 | }, 56 | "development": { 57 | "buildOptimizer": false, 58 | "optimization": false, 59 | "vendorChunk": true, 60 | "extractLicenses": false, 61 | "sourceMap": true, 62 | "namedChunks": true 63 | } 64 | }, 65 | "defaultConfiguration": "production" 66 | }, 67 | "serve": { 68 | "builder": "@angular-devkit/build-angular:dev-server", 69 | "configurations": { 70 | "production": { 71 | "browserTarget": "Angular13Crud:build:production" 72 | }, 73 | "development": { 74 | "browserTarget": "Angular13Crud:build:development" 75 | } 76 | }, 77 | "defaultConfiguration": "development" 78 | }, 79 | "extract-i18n": { 80 | "builder": "@angular-devkit/build-angular:extract-i18n", 81 | "options": { 82 | "browserTarget": "Angular13Crud:build" 83 | } 84 | }, 85 | "test": { 86 | "builder": "@angular-devkit/build-angular:karma", 87 | "options": { 88 | "main": "src/test.ts", 89 | "polyfills": "src/polyfills.ts", 90 | "tsConfig": "tsconfig.spec.json", 91 | "karmaConfig": "karma.conf.js", 92 | "assets": [ 93 | "src/favicon.ico", 94 | "src/assets" 95 | ], 96 | "styles": [ 97 | "src/styles.css" 98 | ], 99 | "scripts": [] 100 | } 101 | } 102 | } 103 | } 104 | }, 105 | "defaultProject": "Angular13Crud" 106 | } 107 | -------------------------------------------------------------------------------- /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/Angular13Crud'), 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": "angular13-crud", 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": "~13.0.0", 14 | "@angular/common": "~13.0.0", 15 | "@angular/compiler": "~13.0.0", 16 | "@angular/core": "~13.0.0", 17 | "@angular/forms": "~13.0.0", 18 | "@angular/platform-browser": "~13.0.0", 19 | "@angular/platform-browser-dynamic": "~13.0.0", 20 | "@angular/router": "~13.0.0", 21 | "rxjs": "~7.4.0", 22 | "tslib": "^2.3.0", 23 | "zone.js": "~0.11.4" 24 | }, 25 | "devDependencies": { 26 | "@angular-devkit/build-angular": "~13.0.1", 27 | "@angular/cli": "~13.0.1", 28 | "@angular/compiler-cli": "~13.0.0", 29 | "@types/jasmine": "~3.10.0", 30 | "@types/node": "^12.11.1", 31 | "jasmine-core": "~3.10.0", 32 | "karma": "~6.3.0", 33 | "karma-chrome-launcher": "~3.1.0", 34 | "karma-coverage": "~2.0.3", 35 | "karma-jasmine": "~4.0.0", 36 | "karma-jasmine-html-reporter": "~1.7.0", 37 | "typescript": "~4.4.3" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/app/app-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { RouterModule, Routes } from '@angular/router'; 3 | import { TutorialsListComponent } from './components/tutorials-list/tutorials-list.component'; 4 | import { TutorialDetailsComponent } from './components/tutorial-details/tutorial-details.component'; 5 | import { AddTutorialComponent } from './components/add-tutorial/add-tutorial.component'; 6 | 7 | const routes: Routes = [ 8 | { path: '', redirectTo: 'tutorials', pathMatch: 'full' }, 9 | { path: 'tutorials', component: TutorialsListComponent }, 10 | { path: 'tutorials/:id', component: TutorialDetailsComponent }, 11 | { path: 'add', component: AddTutorialComponent } 12 | ]; 13 | 14 | @NgModule({ 15 | imports: [RouterModule.forRoot(routes)], 16 | exports: [RouterModule] 17 | }) 18 | export class AppRoutingModule { } 19 | -------------------------------------------------------------------------------- /src/app/app.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/angular-13-crud-example/848637590f4b4d982b773b3abc5f74e4cc21f088/src/app/app.component.css -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 |
2 | 13 | 14 |
15 | 16 |
17 |
-------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | import { RouterTestingModule } from '@angular/router/testing'; 3 | import { AppComponent } from './app.component'; 4 | 5 | describe('AppComponent', () => { 6 | beforeEach(async () => { 7 | await TestBed.configureTestingModule({ 8 | imports: [ 9 | RouterTestingModule 10 | ], 11 | declarations: [ 12 | AppComponent 13 | ], 14 | }).compileComponents(); 15 | }); 16 | 17 | it('should create the app', () => { 18 | const fixture = TestBed.createComponent(AppComponent); 19 | const app = fixture.componentInstance; 20 | expect(app).toBeTruthy(); 21 | }); 22 | 23 | it(`should have as title 'Angular13Crud'`, () => { 24 | const fixture = TestBed.createComponent(AppComponent); 25 | const app = fixture.componentInstance; 26 | expect(app.title).toEqual('Angular13Crud'); 27 | }); 28 | 29 | it('should render title', () => { 30 | const fixture = TestBed.createComponent(AppComponent); 31 | fixture.detectChanges(); 32 | const compiled = fixture.nativeElement as HTMLElement; 33 | expect(compiled.querySelector('.content span')?.textContent).toContain('Angular13Crud app is running!'); 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /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 | export class AppComponent { 9 | title = 'Angular 13 CRUD example'; 10 | } 11 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { BrowserModule } from '@angular/platform-browser'; 3 | import { FormsModule } from '@angular/forms'; 4 | import { HttpClientModule } from '@angular/common/http'; 5 | 6 | import { AppRoutingModule } from './app-routing.module'; 7 | import { AppComponent } from './app.component'; 8 | import { AddTutorialComponent } from './components/add-tutorial/add-tutorial.component'; 9 | import { TutorialDetailsComponent } from './components/tutorial-details/tutorial-details.component'; 10 | import { TutorialsListComponent } from './components/tutorials-list/tutorials-list.component'; 11 | 12 | @NgModule({ 13 | declarations: [ 14 | AppComponent, 15 | AddTutorialComponent, 16 | TutorialDetailsComponent, 17 | TutorialsListComponent 18 | ], 19 | imports: [ 20 | BrowserModule, 21 | AppRoutingModule, 22 | FormsModule, 23 | HttpClientModule 24 | ], 25 | providers: [], 26 | bootstrap: [AppComponent] 27 | }) 28 | export class AppModule { } 29 | -------------------------------------------------------------------------------- /src/app/components/add-tutorial/add-tutorial.component.css: -------------------------------------------------------------------------------- 1 | .submit-form { 2 | max-width: 400px; 3 | margin: auto; 4 | } -------------------------------------------------------------------------------- /src/app/components/add-tutorial/add-tutorial.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |
5 | 6 | 14 |
15 | 16 |
17 | 18 | 25 |
26 | 27 | 28 |
29 | 30 |
31 |

Tutorial was submitted successfully!

32 | 33 |
34 |
35 |
36 | -------------------------------------------------------------------------------- /src/app/components/add-tutorial/add-tutorial.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { AddTutorialComponent } from './add-tutorial.component'; 4 | 5 | describe('AddTutorialComponent', () => { 6 | let component: AddTutorialComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async () => { 10 | await TestBed.configureTestingModule({ 11 | declarations: [ AddTutorialComponent ] 12 | }) 13 | .compileComponents(); 14 | }); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(AddTutorialComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/components/add-tutorial/add-tutorial.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { Tutorial } from 'src/app/models/tutorial.model'; 3 | import { TutorialService } from 'src/app/services/tutorial.service'; 4 | 5 | @Component({ 6 | selector: 'app-add-tutorial', 7 | templateUrl: './add-tutorial.component.html', 8 | styleUrls: ['./add-tutorial.component.css'] 9 | }) 10 | export class AddTutorialComponent implements OnInit { 11 | 12 | tutorial: Tutorial = { 13 | title: '', 14 | description: '', 15 | published: false 16 | }; 17 | submitted = false; 18 | 19 | constructor(private tutorialService: TutorialService) { } 20 | 21 | ngOnInit(): void { 22 | } 23 | 24 | saveTutorial(): void { 25 | const data = { 26 | title: this.tutorial.title, 27 | description: this.tutorial.description 28 | }; 29 | 30 | this.tutorialService.create(data) 31 | .subscribe({ 32 | next: (res) => { 33 | console.log(res); 34 | this.submitted = true; 35 | }, 36 | error: (e) => console.error(e) 37 | }); 38 | } 39 | 40 | newTutorial(): void { 41 | this.submitted = false; 42 | this.tutorial = { 43 | title: '', 44 | description: '', 45 | published: false 46 | }; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/app/components/tutorial-details/tutorial-details.component.css: -------------------------------------------------------------------------------- 1 | .edit-form { 2 | max-width: 400px; 3 | margin: auto; 4 | } -------------------------------------------------------------------------------- /src/app/components/tutorial-details/tutorial-details.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |

Tutorial

4 |
5 | {{ currentTutorial.title }} 6 |
7 |
8 | 9 | {{ currentTutorial.description }} 10 |
11 |
12 | 13 | {{ currentTutorial.published ? "Published" : "Pending" }} 14 |
15 | 16 | 20 | Edit 21 | 22 |
23 | 24 |
25 |
26 |

Please click on a Tutorial...

27 |
28 |
29 | 30 | 31 |
32 |

Tutorial

33 |
34 |
35 | 36 | 43 |
44 |
45 | 46 | 53 |
54 | 55 |
56 | 57 | {{ currentTutorial.published ? "Published" : "Pending" }} 58 |
59 |
60 | 61 | 68 | 75 | 76 | 79 | 80 | 87 |

{{ message }}

88 |
89 | 90 |
91 |
92 |

Cannot access this Tutorial...

93 |
94 |
95 | -------------------------------------------------------------------------------- /src/app/components/tutorial-details/tutorial-details.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { TutorialDetailsComponent } from './tutorial-details.component'; 4 | 5 | describe('TutorialDetailsComponent', () => { 6 | let component: TutorialDetailsComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async () => { 10 | await TestBed.configureTestingModule({ 11 | declarations: [ TutorialDetailsComponent ] 12 | }) 13 | .compileComponents(); 14 | }); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(TutorialDetailsComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/components/tutorial-details/tutorial-details.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, Input, OnInit } from '@angular/core'; 2 | import { TutorialService } from 'src/app/services/tutorial.service'; 3 | import { ActivatedRoute, Router } from '@angular/router'; 4 | import { Tutorial } from 'src/app/models/tutorial.model'; 5 | 6 | @Component({ 7 | selector: 'app-tutorial-details', 8 | templateUrl: './tutorial-details.component.html', 9 | styleUrls: ['./tutorial-details.component.css'] 10 | }) 11 | export class TutorialDetailsComponent implements OnInit { 12 | 13 | @Input() viewMode = false; 14 | 15 | @Input() currentTutorial: Tutorial = { 16 | title: '', 17 | description: '', 18 | published: false 19 | }; 20 | 21 | message = ''; 22 | 23 | constructor( 24 | private tutorialService: TutorialService, 25 | private route: ActivatedRoute, 26 | private router: Router) { } 27 | 28 | ngOnInit(): void { 29 | if (!this.viewMode) { 30 | this.message = ''; 31 | this.getTutorial(this.route.snapshot.params["id"]); 32 | } 33 | } 34 | 35 | getTutorial(id: string): void { 36 | this.tutorialService.get(id) 37 | .subscribe({ 38 | next: (data) => { 39 | this.currentTutorial = data; 40 | console.log(data); 41 | }, 42 | error: (e) => console.error(e) 43 | }); 44 | } 45 | 46 | updatePublished(status: boolean): void { 47 | const data = { 48 | title: this.currentTutorial.title, 49 | description: this.currentTutorial.description, 50 | published: status 51 | }; 52 | 53 | this.message = ''; 54 | 55 | this.tutorialService.update(this.currentTutorial.id, data) 56 | .subscribe({ 57 | next: (res) => { 58 | console.log(res); 59 | this.currentTutorial.published = status; 60 | this.message = res.message ? res.message : 'The status was updated successfully!'; 61 | }, 62 | error: (e) => console.error(e) 63 | }); 64 | } 65 | 66 | updateTutorial(): void { 67 | this.message = ''; 68 | 69 | this.tutorialService.update(this.currentTutorial.id, this.currentTutorial) 70 | .subscribe({ 71 | next: (res) => { 72 | console.log(res); 73 | this.message = res.message ? res.message : 'This tutorial was updated successfully!'; 74 | }, 75 | error: (e) => console.error(e) 76 | }); 77 | } 78 | 79 | deleteTutorial(): void { 80 | this.tutorialService.delete(this.currentTutorial.id) 81 | .subscribe({ 82 | next: (res) => { 83 | console.log(res); 84 | this.router.navigate(['/tutorials']); 85 | }, 86 | error: (e) => console.error(e) 87 | }); 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /src/app/components/tutorials-list/tutorials-list.component.css: -------------------------------------------------------------------------------- 1 | .list { 2 | text-align: left; 3 | max-width: 750px; 4 | margin: auto; 5 | } -------------------------------------------------------------------------------- /src/app/components/tutorials-list/tutorials-list.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 | 10 |
11 | 18 |
19 |
20 |
21 |
22 |

Tutorials List

23 |
    24 |
  • 30 | {{ tutorial.title }} 31 |
  • 32 |
33 | 34 | 37 |
38 |
39 | 43 |
44 |
45 | -------------------------------------------------------------------------------- /src/app/components/tutorials-list/tutorials-list.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { TutorialsListComponent } from './tutorials-list.component'; 4 | 5 | describe('TutorialsListComponent', () => { 6 | let component: TutorialsListComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async () => { 10 | await TestBed.configureTestingModule({ 11 | declarations: [ TutorialsListComponent ] 12 | }) 13 | .compileComponents(); 14 | }); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(TutorialsListComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/components/tutorials-list/tutorials-list.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { Tutorial } from 'src/app/models/tutorial.model'; 3 | import { TutorialService } from 'src/app/services/tutorial.service'; 4 | 5 | @Component({ 6 | selector: 'app-tutorials-list', 7 | templateUrl: './tutorials-list.component.html', 8 | styleUrls: ['./tutorials-list.component.css'] 9 | }) 10 | export class TutorialsListComponent implements OnInit { 11 | 12 | tutorials?: Tutorial[]; 13 | currentTutorial: Tutorial = {}; 14 | currentIndex = -1; 15 | title = ''; 16 | 17 | constructor(private tutorialService: TutorialService) { } 18 | 19 | ngOnInit(): void { 20 | this.retrieveTutorials(); 21 | } 22 | 23 | retrieveTutorials(): void { 24 | this.tutorialService.getAll() 25 | .subscribe({ 26 | next: (data) => { 27 | this.tutorials = data; 28 | console.log(data); 29 | }, 30 | error: (e) => console.error(e) 31 | }); 32 | } 33 | 34 | refreshList(): void { 35 | this.retrieveTutorials(); 36 | this.currentTutorial = {}; 37 | this.currentIndex = -1; 38 | } 39 | 40 | setActiveTutorial(tutorial: Tutorial, index: number): void { 41 | this.currentTutorial = tutorial; 42 | this.currentIndex = index; 43 | } 44 | 45 | removeAllTutorials(): void { 46 | this.tutorialService.deleteAll() 47 | .subscribe({ 48 | next: (res) => { 49 | console.log(res); 50 | this.refreshList(); 51 | }, 52 | error: (e) => console.error(e) 53 | }); 54 | } 55 | 56 | searchTitle(): void { 57 | this.currentTutorial = {}; 58 | this.currentIndex = -1; 59 | 60 | this.tutorialService.findByTitle(this.title) 61 | .subscribe({ 62 | next: (data) => { 63 | this.tutorials = data; 64 | console.log(data); 65 | }, 66 | error: (e) => console.error(e) 67 | }); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/app/models/tutorial.model.spec.ts: -------------------------------------------------------------------------------- 1 | import { Tutorial } from './tutorial.model'; 2 | 3 | describe('Tutorial', () => { 4 | it('should create an instance', () => { 5 | expect(new Tutorial()).toBeTruthy(); 6 | }); 7 | }); 8 | -------------------------------------------------------------------------------- /src/app/models/tutorial.model.ts: -------------------------------------------------------------------------------- 1 | export class Tutorial { 2 | id?: any; 3 | title?: string; 4 | description?: string; 5 | published?: boolean; 6 | } 7 | -------------------------------------------------------------------------------- /src/app/services/tutorial.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | 3 | import { TutorialService } from './tutorial.service'; 4 | 5 | describe('TutorialService', () => { 6 | let service: TutorialService; 7 | 8 | beforeEach(() => { 9 | TestBed.configureTestingModule({}); 10 | service = TestBed.inject(TutorialService); 11 | }); 12 | 13 | it('should be created', () => { 14 | expect(service).toBeTruthy(); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /src/app/services/tutorial.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { HttpClient } from '@angular/common/http'; 3 | import { Observable } from 'rxjs'; 4 | import { Tutorial } from '../models/tutorial.model'; 5 | 6 | const baseUrl = 'http://localhost:8080/api/tutorials'; 7 | 8 | @Injectable({ 9 | providedIn: 'root' 10 | }) 11 | export class TutorialService { 12 | 13 | constructor(private http: HttpClient) { } 14 | 15 | getAll(): Observable { 16 | return this.http.get(baseUrl); 17 | } 18 | 19 | get(id: any): Observable { 20 | return this.http.get(`${baseUrl}/${id}`); 21 | } 22 | 23 | create(data: any): Observable { 24 | return this.http.post(baseUrl, data); 25 | } 26 | 27 | update(id: any, data: any): Observable { 28 | return this.http.put(`${baseUrl}/${id}`, data); 29 | } 30 | 31 | delete(id: any): Observable { 32 | return this.http.delete(`${baseUrl}/${id}`); 33 | } 34 | 35 | deleteAll(): Observable { 36 | return this.http.delete(baseUrl); 37 | } 38 | 39 | findByTitle(title: any): Observable { 40 | return this.http.get(`${baseUrl}?title=${title}`); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/angular-13-crud-example/848637590f4b4d982b773b3abc5f74e4cc21f088/src/assets/.gitkeep -------------------------------------------------------------------------------- /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/bezkoder/angular-13-crud-example/848637590f4b4d982b773b3abc5f74e4cc21f088/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Angular13Crud 6 | 7 | 8 | 9 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /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.css: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | -------------------------------------------------------------------------------- /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 | keys(): string[]; 13 | (id: string): T; 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().map(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": "es2017", 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 | --------------------------------------------------------------------------------