Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": ["**/node_modules/**"]
"exclude": [
"**/node_modules/**"
]
}
},
"e2e": {
Expand Down Expand Up @@ -174,7 +176,8 @@
}
},
"cli": {
"defaultCollection": "@ionic/angular-toolkit"
"defaultCollection": "@ionic/angular-toolkit",
"analytics": "367aa371-c6ed-45b9-924a-d2e136c6ebee"
},
"schematics": {
"@ionic/angular-toolkit:component": {
Expand All @@ -184,4 +187,4 @@
"styleext": "scss"
}
}
}
}
Empty file added data/data.json
Empty file.
20 changes: 15 additions & 5 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ import { PreloadAllModules, RouterModule, Routes } from '@angular/router';

const routes: Routes = [
{
path: 'home',
loadChildren: () => import('./home/home.module').then( m => m.HomePageModule)
path: '', redirectTo: 'student-list', pathMatch: 'full'
},
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
path: 'student-create',
loadChildren: () => import('./student-create/student-create.module').then( m => m.StudentCreatePageModule)
},
{
path: 'student-edit',
loadChildren: () => import('./student-edit/student-edit.module').then( m => m.StudentEditPageModule)
},
{
path: 'student-list',
loadChildren: () => import('./student-list/student-list.module').then( m => m.StudentListPageModule)
},
{
path: 'student-detail',
loadChildren: () => import('./student-detail/student-detail.module').then( m => m.StudentDetailPageModule)
},
];

Expand Down
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';

import { HttpClientModule } from '@angular/common/http'

@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
imports: [HttpClientModule, BrowserModule, IonicModule.forRoot(), AppRoutingModule],
providers: [
StatusBar,
SplashScreen,
Expand Down
7 changes: 7 additions & 0 deletions src/app/models/student.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Student } from './student';

describe('Student', () => {
it('should create an instance', () => {
expect(new Student()).toBeTruthy();
});
});
6 changes: 6 additions & 0 deletions src/app/models/student.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class Student {
id: number;
name: string;
age: string;
address: string;
}
16 changes: 16 additions & 0 deletions src/app/services/api.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { ApiService } from './api.service';

describe('ApiService', () => {
let service: ApiService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ApiService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
91 changes: 91 additions & 0 deletions src/app/services/api.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Student } from '../models/student';
import { Observable, throwError } from 'rxjs';
import { retry, catchError } from 'rxjs/operators'

@Injectable({
providedIn: 'root'
})
export class ApiService {

// API path
base_path = 'http://localhost:3000/students';

constructor(private http: HttpClient) { }

// Http Options
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}

// Handle API errors
handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
console.error('An error occurred:', error.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.error(
`Backend returned code ${error.status}, ` +
`body was: ${error.error}`);
}
// return an observable with a user-facing error message
return throwError(
'Something bad happened; please try again later.');
};

// Create a new item
createItem(item): Observable<Student> {
return this.http
.post<Student>(this.base_path, JSON.stringify(item), this.httpOptions)
.pipe(
retry(2),
catchError(this.handleError)
)
}

// Get single student data by ID
getItem(id): Observable<Student> {
return this.http
.get<Student>(this.base_path + '/' + id)
.pipe(
retry(2),
catchError(this.handleError)
)
}

// Get students data
getList(): Observable<Student> {
return this.http
.get<Student>(this.base_path)
.pipe(
retry(2),
catchError(this.handleError)
)
}

// Update item by id
updateItem(id, item): Observable<Student> {
return this.http
.put<Student>(this.base_path + '/' + id, JSON.stringify(item), this.httpOptions)
.pipe(
retry(2),
catchError(this.handleError)
)
}

// Delete item by id
deleteItem(id) {
return this.http
.delete<Student>(this.base_path + '/' + id, this.httpOptions)
.pipe(
retry(2),
catchError(this.handleError)
)
}

}
17 changes: 17 additions & 0 deletions src/app/student-create/student-create-routing.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { StudentCreatePage } from './student-create.page';

const routes: Routes = [
{
path: '',
component: StudentCreatePage
}
];

@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class StudentCreatePageRoutingModule {}
20 changes: 20 additions & 0 deletions src/app/student-create/student-create.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { IonicModule } from '@ionic/angular';

import { StudentCreatePageRoutingModule } from './student-create-routing.module';

import { StudentCreatePage } from './student-create.page';

@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
StudentCreatePageRoutingModule
],
declarations: [StudentCreatePage]
})
export class StudentCreatePageModule {}
25 changes: 25 additions & 0 deletions src/app/student-create/student-create.page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- student-create.html -->
<ion-header>
<ion-toolbar color="tertiary">
<ion-title>Create Student</ion-title>
</ion-toolbar>
</ion-header>

<ion-content class="ion-padding">

<ion-item>
<ion-label>Name</ion-label>
<ion-input [(ngModel)]="data.name" placeholder="Enter Name"></ion-input>
</ion-item>

<ion-item>
<ion-label>Age</ion-label>
<ion-input [(ngModel)]="data.age" placeholder="Enter Age"></ion-input>
</ion-item>

<ion-item>
<ion-button (click)="submitForm()">Add
</ion-button>
</ion-item>

</ion-content>
Empty file.
24 changes: 24 additions & 0 deletions src/app/student-create/student-create.page.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';

import { StudentCreatePage } from './student-create.page';

describe('StudentCreatePage', () => {
let component: StudentCreatePage;
let fixture: ComponentFixture<StudentCreatePage>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ StudentCreatePage ],
imports: [IonicModule.forRoot()]
}).compileComponents();

fixture = TestBed.createComponent(StudentCreatePage);
component = fixture.componentInstance;
fixture.detectChanges();
}));

it('should create', () => {
expect(component).toBeTruthy();
});
});
30 changes: 30 additions & 0 deletions src/app/student-create/student-create.page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Component, OnInit } from '@angular/core';
import { Student } from '../models/student';
import { ApiService } from '../services/api.service';
import { Router } from '@angular/router';

@Component({
selector: 'app-student-create',
templateUrl: './student-create.page.html',
styleUrls: ['./student-create.page.scss'],
})
export class StudentCreatePage implements OnInit {

data: Student

constructor(
public apiService: ApiService,
public router: Router
) {
this.data = new Student();
}

ngOnInit() {
}

submitForm() {
this.apiService.createItem(this.data).subscribe((response) => {
this.router.navigate(['student-list']);
});
}
}
17 changes: 17 additions & 0 deletions src/app/student-detail/student-detail-routing.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { StudentDetailPage } from './student-detail.page';

const routes: Routes = [
{
path: '',
component: StudentDetailPage
}
];

@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class StudentDetailPageRoutingModule {}
20 changes: 20 additions & 0 deletions src/app/student-detail/student-detail.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { IonicModule } from '@ionic/angular';

import { StudentDetailPageRoutingModule } from './student-detail-routing.module';

import { StudentDetailPage } from './student-detail.page';

@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
StudentDetailPageRoutingModule
],
declarations: [StudentDetailPage]
})
export class StudentDetailPageModule {}
9 changes: 9 additions & 0 deletions src/app/student-detail/student-detail.page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<ion-header>
<ion-toolbar>
<ion-title>student-detail</ion-title>
</ion-toolbar>
</ion-header>

<ion-content>

</ion-content>
Empty file.
24 changes: 24 additions & 0 deletions src/app/student-detail/student-detail.page.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';

import { StudentDetailPage } from './student-detail.page';

describe('StudentDetailPage', () => {
let component: StudentDetailPage;
let fixture: ComponentFixture<StudentDetailPage>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ StudentDetailPage ],
imports: [IonicModule.forRoot()]
}).compileComponents();

fixture = TestBed.createComponent(StudentDetailPage);
component = fixture.componentInstance;
fixture.detectChanges();
}));

it('should create', () => {
expect(component).toBeTruthy();
});
});
Loading