Skip to content

Commit

Permalink
fix popular tags api
Browse files Browse the repository at this point in the history
  • Loading branch information
nhaancs committed Mar 30, 2021
1 parent e1fc220 commit 82b870f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export class ArticleApiHandlersController {

@Post('articles')
async create(@Req() req, @Body() data: Partial<INewArticle>): Promise<IResponse<IArticle>> {
console.log('create article', req.user, data)
delete (data as any)?.id

let article: Partial<Article> = {
Expand Down Expand Up @@ -207,8 +206,9 @@ export class ArticleApiHandlersController {

@SkipAuth()
@Get('tags')
async findAllTags(@Req() req, @Query() query): Promise<IResponse<string>> {
let tags = await this.tagService.findAll()
async findAllTags(@Query() query): Promise<IResponse<string>> {
const options = mapQueriesToFindManyOptions(query)
let tags = await this.tagService.findAll(options)

return new ListSuccessResponse<string>({
listData: tags.map(t => t.name),
Expand All @@ -218,15 +218,16 @@ export class ArticleApiHandlersController {

// Helper functions

private updateTags(tagList: string[]) {
tagList.forEach(async t => {
private async updateTags(tagList: string[]) {
for (let t of tagList) {
t = t.trim()
const tagExists = !!(await this.tagService.findOne({name: t}))
if (!tagExists) {
this.tagService.insert({name: t})
const tag = await this.tagService.findOne({name: t})
if (!tag) {
await this.tagService.insert({name: t, count: 1})
} else {
await this.tagService.update({id: tag.id}, {count: ++tag.count})
}

})
}
}

private async didUserFavoriteThisArticle(userId: string, slug: string): Promise<boolean> {
Expand Down
2 changes: 2 additions & 0 deletions libs/article/api/shared/src/lib/tag.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ import { BaseEntity } from '@realworld/shared/api/foundation';
export abstract class Tag extends BaseEntity {
@Column({ unique: true })
name: string;
@Column()
count: number;
}
3 changes: 0 additions & 3 deletions libs/article/feature/src/lib/home/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,20 @@ <h1 class="logo-font">conduit</h1>
<li class="nav-item" *ngIf="userService?.isAuth">
<a
class="nav-link"
[class.disabled]="feedType!=='personal'"
[class.active]="feedType==='personal'"
(click)="toggleFeed('personal')"
>Your Feed</a>
</li>
<li class="nav-item">
<a
class="nav-link"
[class.disabled]="feedType!=='global'"
[class.active]="feedType==='global'"
(click)="toggleFeed('global')"
>Global Feed</a>
</li>
<li class="nav-item" *ngIf="selectedTag && feedType==='tag'">
<a
class="nav-link"
[class.disabled]="feedType!=='tag'"
[class.active]="feedType==='tag'"
(click)="toogleFeed('tag')"
>#{{selectedTag}}</a>
Expand Down
6 changes: 5 additions & 1 deletion libs/article/feature/src/lib/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export class HomeComponent implements OnInit {

ngOnInit() {
this.toggleFeed('global')
this.tags$ = this.tagService.getAll(null, null).pipe(map(res => res.data))
this.tags$ = this.tagService.getAll({
limit: 10,
pageIndex: 0,
order: {orderBy: 'count' as any, orderType: 'desc'}
}, null).pipe(map(res => res.data))
}

toggleFeed(feedType: 'global'|'personal'|'tag', tag?: string) {
Expand Down
4 changes: 4 additions & 0 deletions migrations/1616818613984-InitTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class InitTag1616818613984 implements MigrationInterface {
type: 'varchar',
length: '255'
},
{
name: 'count',
type: 'integer',
},
],
indices: [{name: 'id-index', columnNames: ['id']}],
engine: DB_ENGINE.MYISAM
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
todo:
- guards
- rendor markdown
- set title for each page

> ### [Nx monorepo](https://nx.dev) with [Nestjs](https://nestjs.com) and [Angular](https://angular.io) codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the [RealWorld](https://github.com/gothinkster/realworld) spec and API.
Expand Down

0 comments on commit 82b870f

Please sign in to comment.