Building a web application with Spring Boot and Angular (#6496)
* Initial Commit * Delete angularclient folder * Add spring-boot-angular module to root pom.xml * Update pom.xml * Update root pom.xml * Update root pom.xml
This commit is contained in:
committed by
KevinGilmore
parent
6cd8c51b4d
commit
b10782f89e
@@ -0,0 +1,15 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { Routes, RouterModule } from '@angular/router';
|
||||
import { UserListComponent } from './user-list/user-list.component';
|
||||
import { UserFormComponent } from './user-form/user-form.component';
|
||||
|
||||
const routes: Routes = [
|
||||
{ path: 'users', component: UserListComponent },
|
||||
{ path: 'adduser', component: UserFormComponent }
|
||||
];
|
||||
|
||||
@NgModule({
|
||||
imports: [RouterModule.forRoot(routes)],
|
||||
exports: [RouterModule]
|
||||
})
|
||||
export class AppRoutingModule { }
|
||||
@@ -0,0 +1,16 @@
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="card bg-dark my-5">
|
||||
<div class="card-body">
|
||||
<h2 class="card-title text-center text-white py-3">{{ title }}</h2>
|
||||
<ul class="text-center list-inline py-3">
|
||||
<li class="list-inline-item"><a routerLink="/users" class="btn btn-info"> List Users</a></li>
|
||||
<li class="list-inline-item"><a routerLink="/adduser" class="btn btn-info">Add User</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<router-outlet></router-outlet>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,31 @@
|
||||
import { TestBed, async } from '@angular/core/testing';
|
||||
import { RouterTestingModule } from '@angular/router/testing';
|
||||
import { AppComponent } from './app.component';
|
||||
describe('AppComponent', () => {
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
RouterTestingModule
|
||||
],
|
||||
declarations: [
|
||||
AppComponent
|
||||
],
|
||||
}).compileComponents();
|
||||
}));
|
||||
it('should create the app', async(() => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
const app = fixture.debugElement.componentInstance;
|
||||
expect(app).toBeTruthy();
|
||||
}));
|
||||
it(`should have as title 'app'`, async(() => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
const app = fixture.debugElement.componentInstance;
|
||||
expect(app.title).toEqual('app');
|
||||
}));
|
||||
it('should render title in a h1 tag', async(() => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
fixture.detectChanges();
|
||||
const compiled = fixture.debugElement.nativeElement;
|
||||
expect(compiled.querySelector('h1').textContent).toContain('Welcome to app!');
|
||||
}));
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Component } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
templateUrl: './app.component.html',
|
||||
styleUrls: ['./app.component.css']
|
||||
})
|
||||
export class AppComponent {
|
||||
|
||||
title: string;
|
||||
|
||||
constructor() {
|
||||
this.title = 'Spring Boot - Angular Application';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { BrowserModule } from '@angular/platform-browser';
|
||||
import { NgModule } from '@angular/core';
|
||||
import { AppRoutingModule } from './app-routing.module';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { HttpClientModule } from '@angular/common/http';
|
||||
import { AppComponent } from './app.component';
|
||||
import { UserListComponent } from './user-list/user-list.component';
|
||||
import { UserFormComponent } from './user-form/user-form.component';
|
||||
import { UserService } from './service/user.service';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
AppComponent,
|
||||
UserListComponent,
|
||||
UserFormComponent
|
||||
],
|
||||
imports: [
|
||||
BrowserModule,
|
||||
AppRoutingModule,
|
||||
HttpClientModule,
|
||||
FormsModule
|
||||
],
|
||||
providers: [UserService],
|
||||
bootstrap: [AppComponent]
|
||||
})
|
||||
export class AppModule { }
|
||||
@@ -0,0 +1,5 @@
|
||||
export class User {
|
||||
id: string;
|
||||
name: string;
|
||||
email: string;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { TestBed, inject } from '@angular/core/testing';
|
||||
|
||||
import { UserService } from './user.service';
|
||||
|
||||
describe('UserService', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [UserService]
|
||||
});
|
||||
});
|
||||
|
||||
it('should be created', inject([UserService], (service: UserService) => {
|
||||
expect(service).toBeTruthy();
|
||||
}));
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { User } from '../model/user';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
|
||||
private usersUrl: string;
|
||||
|
||||
constructor(private http: HttpClient) {
|
||||
this.usersUrl = 'http://localhost:8080/users';
|
||||
}
|
||||
|
||||
public findAll(): Observable<User[]> {
|
||||
return this.http.get<User[]>(this.usersUrl);
|
||||
}
|
||||
|
||||
public save(user: User) {
|
||||
return this.http.post<User>(this.usersUrl, user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<div class="card my-5">
|
||||
<div class="card-body">
|
||||
<form (ngSubmit)="onSubmit()" #userForm="ngForm">
|
||||
<div class="form-group">
|
||||
<label for="name">Name</label>
|
||||
<input type="text" [(ngModel)]="user.name" class="form-control" id="name" name="name" placeholder="Enter your name"
|
||||
required #name="ngModel">
|
||||
</div>
|
||||
<div [hidden]="!name.pristine" class="alert alert-danger">Name is required</div>
|
||||
<div class="form-group">
|
||||
<label for="email">Email</label>
|
||||
<input type="text" [(ngModel)]="user.email" class="form-control" id="email" name="email" placeholder="Enter your email address"
|
||||
required #email="ngModel">
|
||||
<div [hidden]="!email.pristine" class="alert alert-danger">Email is required</div>
|
||||
</div>
|
||||
<button type="submit" [disabled]="!userForm.form.valid" class="btn btn-info">Submit</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,25 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { UserFormComponent } from './user-form.component';
|
||||
|
||||
describe('UserFormComponent', () => {
|
||||
let component: UserFormComponent;
|
||||
let fixture: ComponentFixture<UserFormComponent>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ UserFormComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(UserFormComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Component } from '@angular/core';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { UserService } from '../service/user.service';
|
||||
import { User } from '../model/user';
|
||||
|
||||
@Component({
|
||||
selector: 'app-user-form',
|
||||
templateUrl: './user-form.component.html',
|
||||
styleUrls: ['./user-form.component.css']
|
||||
})
|
||||
export class UserFormComponent {
|
||||
|
||||
user: User;
|
||||
|
||||
constructor(private route: ActivatedRoute, private router: Router, private userService: UserService) {
|
||||
this.user = new User();
|
||||
}
|
||||
|
||||
onSubmit() {
|
||||
this.userService.save(this.user).subscribe(result => this.gotoUserList());
|
||||
}
|
||||
|
||||
gotoUserList() {
|
||||
this.router.navigate(['/users']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<div class="card my-5">
|
||||
<div class="card-body">
|
||||
<table class="table table-bordered table-striped">
|
||||
<thead class="thead-dark">
|
||||
<tr>
|
||||
<th scope="col">#</th>
|
||||
<th scope="col">Name</th>
|
||||
<th scope="col">Email</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr *ngFor="let user of users">
|
||||
<td>{{ user.id }}</td>
|
||||
<td>{{ user.name }}</td>
|
||||
<td><a href="mailto:{{ user.email }}">{{ user.email }}</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,25 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { UserListComponent } from './user-list.component';
|
||||
|
||||
describe('UserListComponent', () => {
|
||||
let component: UserListComponent;
|
||||
let fixture: ComponentFixture<UserListComponent>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ UserListComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(UserListComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { User } from '../model/user';
|
||||
import { UserService } from '../service/user.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-user-list',
|
||||
templateUrl: './user-list.component.html',
|
||||
styleUrls: ['./user-list.component.css']
|
||||
})
|
||||
export class UserListComponent implements OnInit {
|
||||
|
||||
users: User[];
|
||||
|
||||
constructor(private userService: UserService) {
|
||||
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.userService.findAll().subscribe(data => {
|
||||
this.users = data;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user