Skip to content

Commit 3182e8b

Browse files
committed
Country inputs.
1 parent 8c4b532 commit 3182e8b

File tree

9 files changed

+76
-35
lines changed

9 files changed

+76
-35
lines changed

src/app/domain/Jwt.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
export class Jwt {
2+
public constructor(init?:Partial<Jwt>) {
3+
Object.assign(this, init);
4+
}
5+
26
Token: String;
37
Timeout: Date;
48
Username: String;

src/app/domain/User.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
export class User {
2+
public constructor(init?:Partial<User>) {
3+
Object.assign(this, init);
4+
}
5+
26
Id: Number;
37
Name: String;
48
Email: String;

src/app/domain/card.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
export class Card {
2+
public constructor(init?:Partial<Card>) {
3+
Object.assign(this, init);
4+
}
5+
26
Title: String;
37
Description: String;
48
Link: String;

src/app/domain/country.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class Country {
2+
public constructor(init?:Partial<Country>) {
3+
Object.assign(this, init);
4+
}
5+
6+
Id: Number;
7+
Name: String;
8+
}
9+

src/app/domain/pagination.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
export class Pagination<T> {
2+
public constructor(init?:Partial<Pagination<T>>) {
3+
Object.assign(this, init);
4+
}
5+
26
Items: Array<T>;
37
Offset: Number;
48
Limit: Number;

src/app/modules/core/http-backend.interceptor.ts

+30-22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Jwt } from '../../domain/jwt';
66
import { Card } from './../../domain/card';
77
import { User } from '../../domain/user';
88
import { Pagination } from 'src/app/domain/pagination';
9+
import { Country } from 'src/app/domain/country';
910

1011
@Injectable()
1112
export class HttpBackendInterceptor implements HttpInterceptor {
@@ -17,28 +18,26 @@ export class HttpBackendInterceptor implements HttpInterceptor {
1718
var data = new Date();
1819
data.setDate(data.getDate() + 1);
1920

20-
let jwt: Jwt = {
21+
let jwt = new Jwt({
2122
Token: "eaea23424asdfaefwr52asdfasdf32s",
2223
Timeout: data,
2324
Username: req.body.Email.split("@")[0],
2425
Name: req.body.Email.split("@")[0],
2526
Email: req.body.Email
26-
};
27+
});
2728

2829
return of(new HttpResponse({ status: 200, body: jwt }));
2930
}
3031

3132
if (req.url.includes('cards') && req.method === 'GET') {
3233
let cards: Card[] = [];
3334

34-
var card: Card = {
35-
Title: 'Card title',
36-
Description: 'Some quick example text to build on the card title and make up the bulk of the card\'s content.',
37-
Link: 'Dapibus ac facilisis in'
38-
}
39-
4035
for (let i = 0; i < 9; i++) {
41-
cards.push(card);
36+
cards.push(new Card({
37+
Title: 'Card title',
38+
Description: 'Some quick example text to build on the card title and make up the bulk of the card\'s content.',
39+
Link: 'Dapibus ac facilisis in'
40+
}));
4241
}
4342

4443
return of(new HttpResponse({ status: 200, body: cards }));
@@ -48,20 +47,18 @@ export class HttpBackendInterceptor implements HttpInterceptor {
4847
let users: User[] = [];
4948

5049
var data = new Date();
51-
52-
var user: User = {
53-
Id: 1,
54-
Name: 'Gabriel Lucena',
55-
56-
Document: '023.437.673-27',
57-
Birthdate: new Date('1991-04-28T12:00:00'),
58-
Country: 'Brasil',
59-
Profile: 'Administrator',
60-
Active: true
61-
}
62-
50+
6351
for (let i = 0; i < 10; i++) {
64-
users.push(user);
52+
users.push(new User({
53+
Id: 1,
54+
Name: 'Gabriel Lucena',
55+
56+
Document: '023.437.673-27',
57+
Birthdate: new Date('1991-04-28T12:00:00'),
58+
Country: 'Brasil',
59+
Profile: 'Administrator',
60+
Active: true
61+
}));
6562
}
6663

6764
let pagination: Pagination<User> = {
@@ -89,6 +86,17 @@ export class HttpBackendInterceptor implements HttpInterceptor {
8986
return of(new HttpResponse({ status: 200, body: user }));
9087
}
9188

89+
if (req.url.includes('countries') && req.method === 'GET') {
90+
let countries: Country[] = [];
91+
92+
countries.push(new Country({ Id: 1, Name: 'Brazil' }));
93+
countries.push(new Country({ Id: 1, Name: 'United States of America' }));
94+
countries.push(new Country({ Id: 1, Name: 'Thailand' }));
95+
countries.push(new Country({ Id: 1, Name: 'Greece' }));
96+
97+
return of(new HttpResponse({ status: 200, body: countries }));
98+
}
99+
92100
if (req.url.includes('error') && req.method === 'GET') {
93101
return throwError({ error: { message: 'Error thrown.' } });
94102
}

src/app/routes/users/users-list/users-list.component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class UsersListComponent {
2323
public faEdit: IconDefinition = faEdit;
2424
public pagination: Observable<Pagination<User>>;
2525

26-
constructor(public httpService: HttpService<User>) { }
26+
constructor(public userService: HttpService<User>) { }
2727

2828
edit(user: User): void {
2929
this.editEvent.emit(user);
@@ -34,7 +34,7 @@ export class UsersListComponent {
3434
}
3535

3636
list(offset: Number, limit: Number, filters: FormGroup): Observable<Pagination<User>> {
37-
this.pagination = this.httpService.list('users', offset, limit, filters);
37+
this.pagination = this.userService.paginate('users', offset, limit, filters);
3838

3939
return this.pagination;
4040
}

src/app/routes/users/users-page/users-page.component.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ConfirmationModalComponent } from 'src/app/modules/modals/confirmation-
99
import { Router } from '@angular/router';
1010
import { HttpService } from 'src/app/services/http.service';
1111
import { forkJoin } from 'rxjs';
12+
import { Country } from 'src/app/domain/country';
1213

1314
@Component({
1415
selector: 'users-page',
@@ -25,20 +26,23 @@ export class UsersPageComponent implements OnInit {
2526

2627
constructor(
2728
private router: Router,
28-
private httpService: HttpService<User>) { }
29+
private userService: HttpService<User>,
30+
private countryService: HttpService<Country>,) { }
2931

3032
ngOnInit(): void {
31-
if (this.router.url.includes('new')) {
32-
this.userAdd.user.next(new User());
33-
} else if (this.router.url.includes('users/')) {
34-
let get = this.httpService.get('users', 1);
35-
let list = this.userList.list(0, 10, null);
33+
let list = this.userList.list(0, 10, null);
34+
let countries = this.countryService.list('countries');
3635

37-
forkJoin([get, list]).subscribe(results => {
38-
this.userEdit.user.next(results[0]);
36+
if (this.router.url.includes('new')) {
37+
forkJoin([list, countries]).subscribe(results => {
38+
this.userAdd.user.next(new User());
39+
});
40+
} else if (this.router.url.includes('users/')) {
41+
let get = this.userService.get('users', 1);
42+
43+
forkJoin([list, countries, get]).subscribe(results => {
44+
this.userEdit.user.next(results[2]);
3945
});
40-
} else {
41-
this.userList.list(0, 10, null);
4246
}
4347
}
4448

src/app/services/http.service.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export class HttpService<T> {
2121
return this.http.get<T>(environment.server + '/' + route + '/' + id);
2222
}
2323

24-
list(route: String, offset: Number, limit: Number, filters: FormGroup): Observable<Pagination<T>> {
24+
list(route: String): Observable<T[]>{
25+
return this.http.get<T[]>(environment.server + '/' + route);
26+
}
27+
28+
paginate(route: String, offset: Number, limit: Number, filters: FormGroup): Observable<Pagination<T>> {
2529
let query = '';
2630

2731
if (filters) {

0 commit comments

Comments
 (0)