Skip to content

Commit 6bedc74

Browse files
committed
libraries modified
1 parent 6108ec5 commit 6bedc74

File tree

7 files changed

+192
-33
lines changed

7 files changed

+192
-33
lines changed

src/app/components/authors/authors.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import {FormBuilder, FormGroup} from '@angular/forms';
33
import {DataService} from '../../services/data.service';
4-
import {Author} from '../../models/user.model';
4+
import {Author} from '../../models/models.model';
55

66
@Component({
77
selector: 'app-authors',

src/app/components/books/books.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import {FormBuilder, FormGroup} from '@angular/forms';
33
import {DataService} from '../../services/data.service';
4-
import {Author, Book} from '../../models/user.model';
4+
import {Author, Book} from '../../models/models.model';
55

66
@Component({
77
selector: 'app-books',
Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,88 @@
1-
<p>libraries works!</p>
1+
2+
<div style="text-align: center" *ngIf="msgText !== null">
3+
<app-msg [msgText]="msgText"></app-msg>
4+
</div>
5+
6+
<h1>Libraries</h1>
7+
8+
<div class="col-lg-12 row justify-content-center align-items-center container">
9+
10+
<div class="col-lg-6">
11+
<form [formGroup]="libraryForm" (ngSubmit)="onSubmit(libraryForm.value)">
12+
13+
<div class="form-group row">
14+
<label for="inputId3" class="col-sm-2 col-form-label">id</label>
15+
<div class="col-sm-8">
16+
<input type="number" class="form-control" id="inputId3" formControlName="id" placeholder="Library id">
17+
</div>
18+
</div>
19+
20+
<div class="form-group row">
21+
<label for="inputName" class="col-sm-2 col-form-label">Name</label>
22+
<div class="col-sm-8">
23+
<input type="text" class="form-control" id="inputName" formControlName="name" placeholder="Library Name">
24+
</div>
25+
</div>
26+
27+
<div class="form-group row">
28+
<label for="inputAddress" class="col-sm-2 col-form-label">Address</label>
29+
<div class="col-sm-8">
30+
<input type="text" class="form-control" id="inputAddress" formControlName="address" placeholder="Library Address">
31+
</div>
32+
</div>
33+
34+
<!--
35+
<div class="form-group row">
36+
<label for="inputAuthor3" class="col-sm-2 col-form-label">Author</label>
37+
<div class="col-sm-8">
38+
<input type="text" class="form-control" id="inputAuthor3" formControlName="author" placeholder="Author Id">
39+
</div>
40+
</div>
41+
-->
42+
43+
<div class="form-group row">
44+
<div class="col-sm-12">
45+
<button type="submit" class="btn btn-primary">{{option}}</button>
46+
</div>
47+
</div>
48+
</form>
49+
</div>
50+
51+
<div *ngIf="card.title" class="col-lg-2 card" style="width: 18rem;">
52+
<img src="http://via.placeholder.com/100" class="card-img-top" alt="...">
53+
<div class="card-body">
54+
<h5 class="card-title">{{card.title}}</h5>
55+
<p>{{card.desc}}</p>
56+
</div>
57+
</div>
58+
59+
</div>
60+
61+
62+
63+
<div class="col-lg-12">
64+
<table class="table">
65+
<thead>
66+
<tr>
67+
<th scope="col">Id</th>
68+
<th>Name</th>
69+
<th>Address</th>
70+
<th>Books</th>
71+
<th></th>
72+
<th></th>
73+
</tr>
74+
</thead>
75+
<tbody>
76+
<tr *ngFor="let library of libraries">
77+
<td scope="row">{{library.id}}</td>
78+
<td>{{library.name}}</td>
79+
<td>{{library.address}}</td>
80+
<td>{{library.books}}</td>
81+
<td><button type="button" class="btn btn-primary" (click)="showLibrary(library)">Show</button></td>
82+
<td><button type="button" class="btn btn-danger" (click)="deleteLibrary(library)">Delete</button></td>
83+
</tr>
84+
</tbody>
85+
</table>
86+
</div>
87+
88+

src/app/components/libraries/libraries.component.spec.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
2+
import {FormBuilder, FormGroup} from '@angular/forms';
3+
import {DataService} from '../../services/data.service';
4+
import {Library} from '../../models/models.model';
25

36
@Component({
47
selector: 'app-libraries',
@@ -7,9 +10,86 @@ import { Component, OnInit } from '@angular/core';
710
})
811
export class LibrariesComponent implements OnInit {
912

10-
constructor() { }
13+
msgText: string;
14+
15+
libraries: object = [];
16+
17+
libraryForm: FormGroup;
18+
19+
option = 'Send';
20+
21+
card = {
22+
title: '',
23+
desc: '',
24+
};
25+
26+
27+
constructor(protected service: DataService, protected fb: FormBuilder) {
28+
29+
this.libraryForm = this.fb.group({
30+
id: null,
31+
name: '',
32+
address: '',
33+
books: []
34+
});
35+
36+
}
1137

1238
ngOnInit() {
39+
40+
this.service.getLibraries().subscribe((data) => {
41+
console.log(data);
42+
this.libraries = data as Library[];
43+
}, (error) => {
44+
this.msgText = error.message;
45+
} );
46+
}
47+
48+
showLibrary(library){
49+
console.log(library.id, library.name);
50+
this.libraryForm = this.fb.group({
51+
id: library.id,
52+
name: library.name,
53+
address: library.address,
54+
books: library.books
55+
});
56+
this.card.title = library.name;
57+
this.card.desc = library.address;
58+
this.option = 'Edit';
59+
}
60+
61+
deleteLibrary(library){
62+
console.log(library.id, library.name);
63+
this.service.deleteLibraries(library.id).subscribe((data) => {
64+
console.log(data);
65+
}, (error) => {this.msgText = error.message;}, () => {
66+
this.reloadData();
67+
});
68+
69+
}
70+
71+
onSubmit(data){
72+
console.log(data);
73+
if (this.option == 'Send'){
74+
this.service.postLibraries(data).subscribe((data) => {
75+
}, (error) => {this.msgText = error.message; console.log(error)}, () => {
76+
this.reloadData();
77+
});
78+
}
79+
80+
if (this.option == 'Edit'){
81+
this.service.putLibraries(data.id, data).subscribe((data) => {},
82+
(error) => {this.msgText = error.message;},
83+
() => {this.reloadData()});
84+
}
85+
86+
87+
this.option = 'Send';
88+
}
89+
90+
reloadData(){
91+
this.libraries = [];
92+
this.service.getLibraries().subscribe((data) => {this.libraries = data as Library[]}, (error) => {this.msgText = error.message;});
1393
}
1494

1595
}

src/app/services/data.service.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,38 @@ export class DataService {
4848
}
4949

5050
// LIBRARIES
51-
getLibrearies(){
51+
getLibraries(){
5252
return this.http.get(this.urlLibraries);
5353
}
5454

55-
postLibrearies(params){
55+
postLibraries(params){
5656
return this.http.post(this.urlLibraries, params);
5757
}
5858

59-
putLibrearies(libraryId, params){
59+
putLibraries(libraryId, params){
6060
return this.http.put(this.urlLibraries + '/' + libraryId, params);
6161
}
6262

63-
deleteLibrearies(libraryId){
63+
deleteLibraries(libraryId){
6464
return this.http.delete(this.urlLibraries + '/' + libraryId);
6565
}
6666

67+
// GENERICS
68+
get(url: string){
69+
return this.http.get(url);
70+
}
71+
72+
post(url, params){
73+
return this.http.post(url, params);
74+
}
75+
76+
put(url, params){
77+
return this.http.put(url, params);
78+
}
79+
80+
delete(url){
81+
return this.http.delete(url);
82+
}
83+
6784

6885
}

0 commit comments

Comments
 (0)