Skip to content

Commit 8b65b57

Browse files
committed
Working on fix bugs.
1 parent 0621c9c commit 8b65b57

File tree

4 files changed

+79
-69
lines changed

4 files changed

+79
-69
lines changed

src/app/home/services/book.service.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import {
2222
providedIn: 'root'
2323
})
2424
export class BookService {
25-
private _list: Array<Book>;
26-
2725
private _filter: IFilter = {
2826
displayRecycled: false,
2927
isOpened: false,
@@ -37,31 +35,24 @@ export class BookService {
3735
private writer: WriterService,
3836
private cate: CateService
3937
) {
40-
this.crud.getItems({table: 'Book'})
38+
}
39+
40+
getList = async () => {
41+
let list: Array<Book>;
42+
43+
await this.crud.getItems({table: 'Book'})
4144
.subscribe((res: IQueryResult) => {
4245
this.opMessage.newMsg(res.message);
4346
const books = res.data as Book[];
44-
this._list = books.slice();
47+
list = books.slice();
4548
});
46-
}
4749

48-
get list () {
49-
return this._list;
50+
return list;
5051
}
5152

52-
bookUpdated = (book: Book) => {
53-
const index = this._list.findIndex(b => b.id === book.id);
54-
this._list.splice(index, 1);
55-
this._list.push(book);
56-
}
57-
58-
getList = (filter: IFilter) => {
59-
return this.list.filter(b => filterFn(b, filter));
60-
}
61-
62-
bookDeleted = (book: Book) => {
63-
const index = this._list.findIndex(b => b.id === book.id);
64-
this._list.splice(index, 1);
53+
getFilterdList = async (filter: IFilter) => {
54+
const list = await this.getList();
55+
return list.filter(b => filterFn(b, filter));
6556
}
6657

6758
save = async (res: IAddBookDialogResData) => {
@@ -77,16 +68,14 @@ export class BookService {
7768

7869
newBook.website = await this.website.newWebsit(site);
7970
newBook.writer = await this.writer.newWriter(writerName, newBook.website);
80-
newBook.cateList = await this.cate.saveList(res.cateList).slice();
71+
newBook.cateList = await this.cate.saveList(res.cateList);
8172

8273
const query: IQuery = {
8374
table: 'Book',
8475
item: newBook
8576
}
8677
this.crud.addItem(query).subscribe((res: IQueryResult) => {
8778
this.opMessage.newMsg(res.message);
88-
89-
this._list.push(res.data as Book);
9079
});
9180
}
9281

@@ -102,22 +91,20 @@ export class BookService {
10291

10392
this.crud.updateItem(query).subscribe((queryRes: IQueryResult) => {
10493
this.opMessage.newMsg(queryRes.message);
105-
this.bookUpdated(queryRes.data as Book);
10694
});
10795
}
10896

10997
update = async (book: Book) => {
11098
let query: IQuery;
11199

112-
book.cateList = await this.cate.saveList(book.cateList).slice();
100+
book.cateList = await this.cate.saveList(book.cateList);
113101

114102
query = {
115103
table: 'Book',
116104
item: book
117105
}
118106
this.crud.updateItem(query).subscribe((queryRes: IQueryResult) => {
119107
this.opMessage.newMsg(queryRes.message);
120-
this.bookUpdated(queryRes.data as Book);
121108
});
122109
}
123110

@@ -132,7 +119,6 @@ export class BookService {
132119

133120
await this.crud.deleteItem(query).subscribe((queryRes: IQueryResult) => {
134121
this.opMessage.newMsg(queryRes.message);
135-
this.bookDeleted(res.book);
136122
});
137123

138124
return 0;
@@ -153,7 +139,6 @@ export class BookService {
153139

154140
this.crud.updateItem(query).subscribe((queryRes: IQueryResult) => {
155141
this.opMessage.newMsg(queryRes.message);
156-
this.bookUpdated(queryRes.data as Book);
157142
});
158143
}
159144
}

src/app/home/services/cate.service.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,31 @@ import {
1313
providedIn: 'root'
1414
})
1515
export class CateService {
16-
private _list: Array<Category>;
17-
1816
constructor(
1917
private crud: CrudService,
2018
private opMessage: OpMessageService,
2119
) {
22-
this.crud.getItems({table: 'Category'})
20+
}
21+
22+
getList = async () => {
23+
let list: Array<Category>;
24+
25+
await this.crud.getItems({table: 'Category'})
2326
.subscribe((res: IQueryResult) => {
2427
this.opMessage.newMsg(res.message);
2528
const cates = res.data as Category[];
26-
this._list = cates.slice();
29+
list = cates.slice();
2730
});
28-
}
2931

30-
get list () {
31-
return this._list;
32+
return list;
3233
}
3334

34-
saveList = (cateList: Array<Category>) => {
35+
saveList = async (cateList: Array<Category>) => {
3536
const tempList: Array<Category> = [];
36-
cateList.map(c => {
37-
const cate = this._list.find(cate => cate.name === c.name);
37+
const list = await this.getList();
38+
39+
await cateList.map(c => {
40+
const cate = list.find(cate => cate.name === c.name);
3841
if (cate) tempList.push(cate);
3942
else {
4043
const _cate = new Category();

src/app/home/services/website.service.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,30 @@ import {
1313
providedIn: 'root'
1414
})
1515
export class WebsiteService {
16-
private _websiteList: Array<Website>;
17-
1816
constructor(
1917
private crud: CrudService,
2018
private opMessage: OpMessageService,
2119
) {
22-
this.crud.getItems({table: 'Website'})
20+
}
21+
22+
getList = async () => {
23+
let list: Array<Website>;
24+
25+
await this.crud.getItems({table: 'Website'})
2326
.subscribe((res: IQueryResult) => {
2427
this.opMessage.newMsg(res.message);
2528
const websites = res.data as Website[];
26-
this._websiteList = websites.slice();
29+
list = websites.slice();
2730
});
31+
32+
return list;
33+
2834
}
2935

30-
newWebsit = (uri: string) => {
31-
const website = this._websiteList.find(w => w.uri === uri);
36+
newWebsit = async (uri: string) => {
37+
const list = await this.getList();
38+
39+
const website = list.find(w => w.uri === uri);
3240
if (website){
3341
return website;
3442
}
@@ -40,13 +48,15 @@ export class WebsiteService {
4048
table: "Website",
4149
item: _website
4250
}
43-
this.crud.addItem(query).subscribe((res: IQueryResult) => {
51+
52+
let w: Website;
53+
await this.crud.addItem(query).subscribe((res: IQueryResult) => {
4454
this.opMessage.newMsg(res.message);
4555

46-
const website = res.data as Website;
47-
this._websiteList.push(website);
48-
return website;
56+
w = res.data as Website;
4957
});
58+
59+
return w;
5060
}
5161
}
5262
}

src/app/home/services/writer.service.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,32 @@ import {
1313
providedIn: 'root'
1414
})
1515
export class WriterService {
16-
private _writerList: Array<Writer>
17-
18-
constructor(
19-
private crud: CrudService,
20-
private opMessage: OpMessageService,
21-
) {
22-
this.crud.getItems({table: 'Writer'})
23-
.subscribe((res: IQueryResult) => {
24-
this.opMessage.newMsg(res.message);
25-
const writers = res.data as Writer[];
26-
this._writerList = writers.slice();
27-
});
28-
}
16+
constructor(
17+
private crud: CrudService,
18+
private opMessage: OpMessageService,
19+
) {}
20+
21+
getList = async () => {
22+
let list: Array<Writer>;
23+
24+
await this.crud.getItems({table: 'Writer'})
25+
.subscribe((res: IQueryResult) => {
26+
this.opMessage.newMsg(res.message);
27+
const writers = res.data as Writer[];
28+
list = writers.slice();
29+
});
2930

31+
return list;
32+
}
33+
34+
newWriter = async (writerName: string, website: Website) => {
35+
const list = await this.getList();
3036

31-
newWriter = (writerName: string, website: Website) => {
32-
const writer = this._writerList.find(w => w.name === writerName);
37+
const writer = list.find(w => w.name === writerName);
3338
if (writer){
3439
// 查看 website 是否在 writer 的
3540
if(writer.websiteList === undefined) writer.websiteList = [];
41+
3642
const index = writer.websiteList.findIndex(w => w.uri === website.uri);
3743
if(index < 0) {
3844
writer.websiteList.push(website);
@@ -41,10 +47,14 @@ export class WriterService {
4147
table: 'Writer',
4248
item: writer,
4349
}
44-
this.crud.updateItem(query).subscribe((res: IQueryResult) => {
50+
51+
let w: Writer;
52+
await this.crud.updateItem(query).subscribe((res: IQueryResult) => {
4553
this.opMessage.newMsg(res.message);
46-
return res.data as Writer;
54+
w = res.data as Writer;
4755
});
56+
57+
return w;
4858
}
4959
else return writer;
5060
}
@@ -60,13 +70,15 @@ export class WriterService {
6070
table: "Writer",
6171
item: _writer
6272
}
63-
this.crud.addItem(query).subscribe((res: IQueryResult) => {
73+
74+
let w: Writer;
75+
await this.crud.addItem(query).subscribe((res: IQueryResult) => {
6476
this.opMessage.newMsg(res.message);
6577

66-
const writer = res.data as Writer;
67-
this._writerList.push(writer);
68-
return writer;
78+
w = res.data as Writer;
6979
});
80+
81+
return w;
7082
}
7183
}
7284
}

0 commit comments

Comments
 (0)