Skip to content

Commit 336ab99

Browse files
committed
Updated.
1 parent d4a388a commit 336ab99

File tree

12 files changed

+126
-129
lines changed

12 files changed

+126
-129
lines changed

electron/src/models.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class Writer extends Meta{
6161
fullName: string;
6262

6363
@Column({default: ''})
64-
avatar_url: string;
64+
avatarUrl: string;
6565

6666
@Column({default: ''})
6767
location: string;
@@ -76,6 +76,9 @@ export class Writer extends Meta{
7676

7777
@Entity()
7878
export class Book extends Meta{
79+
@Column({default: false})
80+
isFromMainstreamPlatform: boolean;
81+
7982
@Column({default: ''})
8083
name: string;
8184

src/app/home/components/book-list.component.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
<!-- 作者和来源 -->
1414
<mat-card-header>
15-
<div mat-card-avatar style="background-image: url('assets/images/avatar.png'); background-size: cover;">
16-
</div>
17-
<mat-card-title>{{book.writer.name}}</mat-card-title>
15+
<img mat-card-avatar [src]="book.isFromMainstreamPlatform ? book.writer.avatarUrl : 'assets/images/avatar.png'" />
16+
<mat-card-title>{{book.writer.fullName}}</mat-card-title>
1817
<mat-card-subtitle>{{book.website.uri}}</mat-card-subtitle>
1918
</mat-card-header>
2019

2120
<mat-card-content fxLayout="row" fxLayoutGap="5px" class="cate-list">
21+
<mat-label>{{book.desc}}</mat-label>
2222
<mat-chip-list>
2323
<mat-chip class="cate-chip" *ngFor="let cate of book.cateList" (click)="listBooksUnderCate(cate)">
2424
<ion-label>{{cate.name}}</ion-label>
@@ -35,7 +35,7 @@
3535
(click)="openReadingRecordDialog(book)">
3636
<ion-icon name="list-outline"></ion-icon>阅读记录
3737
</button>
38-
<button mat-button (click)="openReadmeDialog(book)">
38+
<button mat-button *ngIf="book.isFromMainstreamPlatform || book.downloaded" (click)="openReadmeDialog(book)">
3939
<ion-icon name="compass-outline"></ion-icon>README
4040
</button>
4141
<button mat-button *ngIf="book.downloaded" (click)="openBook(book)">

src/app/home/components/book-list.component.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class BookListComponent implements OnInit, OnChanges {
4343
@Input() displayRecycled: boolean = false;
4444
@Input() beenOpened: boolean = true;
4545
@Input() _bookList: Array<Book>;
46+
4647
filter: IFilter = {
4748
displayRecycled: this.displayRecycled,
4849
beenOpened: this.beenOpened,
@@ -60,17 +61,21 @@ export class BookListComponent implements OnInit, OnChanges {
6061

6162
get bookList () {
6263
const bookList = this._bookList.filter(b => filterFn(b, this.filter));
63-
return /\:/.test(this.sortBy) ? sortBy(bookList, this.sortBy.split(':')[0], this.sortBy.split(':')[1])
64+
return /\:/.test(this.sortBy)
65+
? sortBy(bookList, this.sortBy.split(':')[0], this.sortBy.split(':')[1])
6466
: sortBy(bookList, this.sortBy);
6567
}
6668

67-
ngOnInit() {}
69+
ngOnInit() {
70+
console.log(this._bookList)
71+
}
6872

6973
ngOnChanges (changes: SimpleChanges) {
7074
if ('sortBy' in changes) {
7175
if(changes.displayRecycled) this.filter.displayRecycled = changes.displayRecycled.currentValue;
7276
if(changes.beenOpened) this.filter.beenOpened = changes.beenOpened.currentValue;
7377
}
78+
7479
}
7580

7681
changeFilter = (filterAction: IFilterAction) => {
@@ -96,6 +101,7 @@ export class BookListComponent implements OnInit, OnChanges {
96101
break;
97102
}
98103
}
104+
99105
startDownload = (book: Book) => {
100106
this.crud.ipcRenderer.send('download-book', book);
101107
document

src/app/home/components/readme-dialog.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export class ReadmeDialog implements OnInit{
4242
? join(this.data.website.uri, this.data.writer.name, this.data.name, 'README.md')
4343
: join('https://gitlab.com', this.data.writer.name, this.data.name, '-', 'raw', this.data.defaultBranch, 'README.md');
4444
}
45+
46+
return join(this.data.website.uri, this.data.writer.name, this.data.name, 'README.md');
4547
}
4648

4749
ngOnInit() {}

src/app/home/components/readme.dialog.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div mat-dialog-content>
2-
<docspa-md-include [isPageContent]="true" [path]="data.desc" [isPreview]="true"></docspa-md-include>
2+
<docspa-md-include [isPageContent]="true" [path]="readmeUrl" [isPreview]="true"></docspa-md-include>
33
</div>
44
<div mat-dialog-actions>
55
<button mat-button matDialogClose>关闭</button>

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

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class BookService {
3737
private cate: CateService
3838
) {
3939
this.crud.getItems({table: 'Book'})
40-
.subscribe((res: IQueryResult) => {
40+
.then((res: IQueryResult) => {
4141
this.opMessage.newMsg(res.message);
4242
const books = res.data as Book[];
4343
this.list = books.slice();
@@ -72,51 +72,50 @@ export class BookService {
7272

7373
const re = new RegExp(/\.git$/)
7474
newBook.name = re.test(name) ? name.replace(re, '') : name;
75+
newBook.isFromMainstreamPlatform = /github/.test(site) || /gitee/.test(site) || /gitlab/.test(site);
7576

7677
newBook.website = await this.website.newWebsit(site);
77-
78+
79+
let rawRepo: object;
7880
if(/gitee/.test(site)){
79-
await this.fetchService.getRepoProfile(site, newBook.name, writerName).subscribe(async (res) => {
80-
newBook.writer = await this.writer.newWriter(writerName, newBook.website, res['owner']['login']);
81-
});
81+
rawRepo = await this.fetchService.getRepoProfile(site, newBook.name, writerName);
82+
newBook.writer = await this.writer.newWriter(writerName, newBook, rawRepo['owner']['login']);
8283
}
8384
else {
84-
newBook.writer = await this.writer.newWriter(writerName, newBook.website);
85+
newBook.writer = await this.writer.newWriter(writerName, newBook);
8586
}
8687

8788
newBook.cateList = await this.cate.saveList(res.cateList);
8889
newBook.recordList = [];
8990

9091
// 这里要获取到 Repository 的更多信息
9192
if(/gitlab/.test(site)) {
92-
await this.fetchService.getRepoProfile(site, newBook.name, writerName, newBook.writer.platformId)
93-
.subscribe(res => {
94-
const repo = (res as object[]).find(repo => repo['path'] === newBook.name)
93+
const rawRepoList = await this.fetchService.getRepoProfile(site, newBook.name, writerName, newBook.writer.platformId);
9594

96-
newBook.desc = repo['description'];
97-
newBook.defaultBranch = repo['default_branch'];
98-
});
95+
rawRepo = (rawRepoList as object[]).find(repo => repo['path'] === newBook.name);
96+
97+
newBook.desc = rawRepo['description'];
98+
newBook.defaultBranch = rawRepo['default_branch'];
9999
}
100-
else {
101-
await this.fetchService.getRepoProfile(site, newBook.name, writerName)
102-
.subscribe(res => {
103-
newBook.desc = res['description'];
104-
newBook.defaultBranch = res['default_branch'];
105-
});
100+
101+
if(newBook.isFromMainstreamPlatform && !(/gitlab/.test(site))){
102+
rawRepo = rawRepo ? rawRepo : await this.fetchService.getRepoProfile(site, newBook.name, writerName);
103+
newBook.desc = rawRepo['description'];
104+
newBook.defaultBranch = rawRepo['default_branch'];
106105
}
107106

107+
console.log(newBook)
108108
const query: IQuery = {
109109
table: 'Book',
110110
item: newBook
111111
}
112112

113-
this.crud.addItem(query).subscribe((res: IQueryResult) => {
114-
this.opMessage.newMsg(res.message);
115-
this.listUpdated(res.data as Book);
116-
});
113+
const queryRes:IQueryResult = await this.crud.addItem(query);
114+
this.opMessage.newMsg(queryRes.message);
115+
this.listUpdated(queryRes.data as Book);
117116
}
118117

119-
open = (book: Book) => {
118+
open = async (book: Book) => {
120119
this.crud.ipcRenderer.send('open-book', book);
121120
book.recycled = false;
122121

@@ -125,10 +124,9 @@ export class BookService {
125124
item: book
126125
}
127126

128-
this.crud.updateItem(query).subscribe((queryRes: IQueryResult) => {
129-
this.opMessage.newMsg(queryRes.message);
130-
this.listUpdated(queryRes.data as Book);
131-
});
127+
const res: IQueryResult = await this.crud.updateItem(query);
128+
this.opMessage.newMsg(res.message);
129+
this.listUpdated(res.data as Book);
132130
}
133131

134132
update = async (book: Book) => {
@@ -140,10 +138,9 @@ export class BookService {
140138
table: 'Book',
141139
item: book
142140
}
143-
this.crud.updateItem(query).subscribe((queryRes: IQueryResult) => {
144-
this.opMessage.newMsg(queryRes.message);
145-
this.listUpdated(queryRes.data as Book);
146-
});
141+
const queryRes: IQueryResult = await this.crud.updateItem(query);
142+
this.opMessage.newMsg(queryRes.message);
143+
this.listUpdated(queryRes.data as Book);
147144
}
148145

149146
recycleRecoverDelete = async (res: IDeleteBookDialogResData) => {
@@ -155,9 +152,8 @@ export class BookService {
155152
item: res.book
156153
}
157154

158-
await this.crud.deleteItem(query).subscribe((res: IQueryResult) => {
159-
this.opMessage.newMsg(res.message);
160-
});
155+
const queryRes: IQueryResult = await this.crud.deleteItem(query);
156+
this.opMessage.newMsg(queryRes.message);
161157
}
162158

163159
if (!res.recycled && res.remove){
@@ -166,10 +162,9 @@ export class BookService {
166162
item: res.book
167163
}
168164

169-
await this.crud.deleteItem(query).subscribe((queryRes: IQueryResult) => {
170-
this.opMessage.newMsg(queryRes.message);
171-
this.listUpdated(res.book, true);
172-
});
165+
const queryRes:IQueryResult = await this.crud.deleteItem(query);
166+
this.opMessage.newMsg(queryRes.message);
167+
this.listUpdated(res.book, true);
173168

174169
return;
175170
}
@@ -183,9 +178,8 @@ export class BookService {
183178
item: res.book
184179
}
185180

186-
this.crud.updateItem(query).subscribe((queryRes: IQueryResult) => {
187-
this.opMessage.newMsg(queryRes.message);
188-
this.listUpdated(queryRes.data as Book);
189-
});
181+
const queryRes:IQueryResult = await this.crud.updateItem(query);
182+
this.opMessage.newMsg(queryRes.message);
183+
this.listUpdated(queryRes.data as Book);
190184
}
191185
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class CateService {
2020
private opMessage: OpMessageService,
2121
) {
2222
this.crud.getItems({table: 'Category'})
23-
.subscribe((res: IQueryResult) => {
23+
.then((res: IQueryResult) => {
2424
this.opMessage.newMsg(res.message);
2525
const cates = res.data as Category[];
2626
this.list = cates.slice();
@@ -30,7 +30,7 @@ export class CateService {
3030
saveList = async (cateList: Array<Category>) => {
3131
const tempList: Array<Category> = [];
3232

33-
await cateList.map(c => {
33+
await cateList.map(async (c: Category) => {
3434
const cate = this.list.find(cate => cate.name === c.name);
3535

3636
if (cate) tempList.push(cate);
@@ -42,13 +42,13 @@ export class CateService {
4242
table: "Category",
4343
item: _cate
4444
}
45-
this.crud.addItem(query).subscribe((res: IQueryResult) => {
46-
this.opMessage.newMsg(res.message);
4745

48-
const cate = res.data as Category;
49-
this.list.push(cate);
50-
tempList.push(cate);
51-
});
46+
const res: IQueryResult = await this.crud.addItem(query);
47+
48+
this.opMessage.newMsg(res.message);
49+
const cate = res.data as Category;
50+
this.list.push(cate);
51+
tempList.push(cate);
5252
}
5353
});
5454

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

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ export class FetchService {
3030
}
3131

3232
const headers = new HttpHeaders(header);
33-
const obs: Observable<JSON> = this.http
33+
const obs: Observable<object|object[]> = this.http
3434
.get(url, {headers: headers, responseType: 'json'})
3535
.pipe(
3636
catchError((e) => {
3737
return of(e);
3838
}),
39-
map((data: JSON) => {
39+
map((data: object | object[]) => {
4040
this.inFlight.delete(url);
4141
return data;
4242
}),
@@ -47,55 +47,51 @@ export class FetchService {
4747
return obs;
4848
}
4949

50-
getWriterProfile = (writerName: string, websiteUri: string): Observable<object|object[]> => {
50+
getWriterProfile = (writerName: string, websiteUri: string): Promise<object|object[]> => {
5151
let url: string;
5252
let header: string;
5353

54-
if(/gitlab/.test(websiteUri)) {
55-
url = `https://gitlab.com/api/v4/users?username=${writerName}`;
56-
header = `PRIVATE-TOKEN: ${this.tokens.gitlabToken}`;
57-
58-
return this.get(url, header)[0];
59-
}
60-
6154
if(/github/.test(websiteUri)) {
6255
url = `https://api.github.com/users/${writerName}`;
6356
header = 'Accept: application/vnd.github.v3+json';
6457
}
6558

59+
if(/gitlab/.test(websiteUri)) {
60+
url = `https://gitlab.com/api/v4/users?username=${writerName}`;
61+
header = `PRIVATE-TOKEN: ${this.tokens.gitlabToken}`;
62+
}
63+
6664
if(/gitee/.test(websiteUri)) {
6765
url = `https://gitee.com/api/v5/users/${writerName}?access_token=${this.tokens.giteeToken}`;
6866
header = 'Content-Type: application/json;charset=UTF-8';
6967
}
7068

71-
return this.get(url, header);
69+
return this.get(url, header).toPromise();
7270
}
7371

74-
getRepoProfile = (website: string, repo: string, owner: string, ownerId?: number): Observable<object|object[]> => {
72+
getRepoProfile = (website: string, repo: string, owner: string, ownerId?: number): Promise<object|object[]> => {
7573
let url: string;
7674
let header: string;
7775

78-
if(/gitlab/.test(website) && ownerId) {
79-
url = `https://gitlab.com/api/v4/users/${ownerId}/projects`;
80-
header = `PRIVATE-TOKEN: ${this.tokens.gitlabToken}`;
81-
82-
return this.get(url, header);
83-
}
84-
8576
if(/github/.test(website)) {
8677
url = `https://api.github.com/repos/${owner}/${repo}`;
8778
header = "Accept: application/vnd.github.v3+json";
8879
}
8980

81+
if(/gitlab/.test(website) && ownerId) {
82+
url = `https://gitlab.com/api/v4/users/${ownerId}/projects`;
83+
header = `PRIVATE-TOKEN: ${this.tokens.gitlabToken}`;
84+
}
85+
9086
if(/gitee/.test(website)) {
91-
url = `https://gitee.com/api/v5/repos/${owner}/${repo}?access_token=${this.tokens.gitlabToken}`;
87+
url = `https://gitee.com/api/v5/repos/${owner}/${repo}?access_token=${this.tokens.giteeToken}`;
9288
header = 'Content-Type: application/json;charset=UTF-8';
9389
}
9490

95-
return this.get(url, header);
91+
return this.get(url, header).toPromise();
9692
}
9793

98-
searchBooks = (websiteUri: string, keywords: string): Observable<object|object[]> => {
94+
searchBooks = (websiteUri: string, keywords: string): Promise<object|object[]> => {
9995
let url: string;
10096
let header: string;
10197

@@ -107,7 +103,7 @@ export class FetchService {
107103

108104
if(/gitee/.test(websiteUri)) {
109105
const q = encodeURIComponent(keywords);
110-
url = `https://gitee.com/api/v5/search/repositories?access_token=${this.tokens.gitlabToken}&q=${q}=1&per_page=20&order=desc`;
106+
url = `https://gitee.com/api/v5/search/repositories?access_token=${this.tokens.giteeToken}&q=${q}=1&per_page=20&order=desc`;
111107
header = "Content-Type: application/json;charset=UTF-8";
112108
}
113109

@@ -117,6 +113,6 @@ export class FetchService {
117113
header = `PRIVATE-TOKEN: ${this.tokens.gitlabToken}`;
118114
}
119115

120-
return this.get(url, header);
116+
return this.get(url, header).toPromise();
121117
}
122118
}

0 commit comments

Comments
 (0)