Commit fb491b03 authored by Giorgio de Lodi's avatar Giorgio de Lodi

First Commit

parent 8cd7c0d3
......@@ -17,4 +17,7 @@
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
</li>
</ul>
<ul>
<li *ngFor="let u of users">{{u.username}}</li>
</ul>
import { Component } from '@angular/core';
import {UserService} from '../services/user.service';
import {User} from '../models/user.model';
import {Observable} from 'rxjs/Observable';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
......@@ -7,4 +9,27 @@ import { Component } from '@angular/core';
})
export class AppComponent {
title = 'app';
username:string = "giorgio";
password:string = "giorgio";
currentUser:User = null;
users:Array<User>;
constructor(private _service:UserService){
this.listAll();
}
doLogin(){
let self = this;
this._service.login(this.username,this.password).subscribe(data => {
self.currentUser = data;
});
}
listAll(){
let self = this;
this._service.getAll().subscribe((data:any) => {
self.users = data;
},error => {
console.log(error);
});
}
}
......@@ -5,6 +5,9 @@ import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import {UserService} from '../services/user.service';
@NgModule({
declarations: [
AppComponent
......@@ -13,7 +16,7 @@ import { HttpClientModule } from '@angular/common/http';
BrowserModule,
HttpClientModule
],
providers: [],
providers: [UserService],
bootstrap: [AppComponent]
})
export class AppModule { }
export class User{
public id:number;
public name:string;
public username:string;
public password:string;
public email:string;
}
\ No newline at end of file
......@@ -4,18 +4,26 @@ import {Injectable} from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {User} from '../models/user.model';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class UserService {
private url = "http://localhost:8080/training";
constructor(private http:HttpClient) {}
// Uses http.get() to load data from a single API endpoint
getFoods() {
return this.http.get('/api/food');
getAll(){
return this.http.get<Array<User>>(this.url+"/users/all");
}
login(username:string, password:string){
return this.http.post<User>(this.url+"/users/login",JSON.stringify({username: username,password:password}),httpOptions);
}
add(user:User){
return this.http.post<User>(this.url+"/users/add",JSON.stringify(user),httpOptions);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment