-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff2cbe9
commit f8d58a6
Showing
15 changed files
with
238 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface RosterUser { | ||
username: string; | ||
url: string; | ||
articlesCount: number; | ||
totalFavorites: number; | ||
firstArticleDate?: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { RosterService } from './roster.service'; | ||
|
||
@Controller('roster') | ||
export class RosterController { | ||
constructor(private readonly rosterService: RosterService) {} | ||
|
||
@Get() | ||
async getRoster() { | ||
return this.rosterService.getRoster(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { RosterController } from './roster.controller'; | ||
import { RosterService } from './roster.service'; | ||
import { MikroOrmModule } from '@mikro-orm/nestjs'; | ||
import { User } from '../user/user.entity'; | ||
|
||
@Module({ | ||
controllers: [RosterController], | ||
imports: [MikroOrmModule.forFeature({ entities: [User] })], | ||
providers: [RosterService], | ||
}) | ||
export class RosterModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { User } from '../user/user.entity'; | ||
import { UserRepository } from '../user/user.repository'; | ||
import { Article } from '../article/article.entity'; | ||
|
||
export interface RosterUser { | ||
username: string; | ||
url: string; | ||
articlesCount: number; | ||
totalFavorites: number; | ||
firstArticleDate?: Date; | ||
} | ||
|
||
@Injectable() | ||
export class RosterService { | ||
constructor(private userRepository: UserRepository) {} | ||
|
||
async getRoster(): Promise<RosterUser[]> { | ||
const users = await this.userRepository.findAll({ | ||
populate: ['articles'], | ||
}); | ||
|
||
users.sort((a, b) => { | ||
return ( | ||
b.articles.getItems().reduce((sum: number, article: Article) => { | ||
return sum + article.favoritesCount; | ||
}, 0) - | ||
a.articles.getItems().reduce((sum: number, article: Article) => { | ||
return sum + article.favoritesCount; | ||
}, 0) | ||
); | ||
}); | ||
|
||
return users.map((user) => { | ||
return { | ||
username: user.username, | ||
url: `/profile/${user.username}`, | ||
articlesCount: user.articles.length, | ||
totalFavorites: user.articles.getItems().reduce((sum, article) => { | ||
return sum + article.favoritesCount; | ||
}, 0), | ||
firstArticleDate: user.articles[0]?.createdAt, | ||
}; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
.roster-page { | ||
padding: 20px; | ||
} | ||
|
||
.roster-title { | ||
font-size: 24px; | ||
font-weight: bold; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.roster-table { | ||
width: 100%; | ||
border-collapse: collapse; | ||
} | ||
|
||
.roster-table th, | ||
.roster-table td { | ||
padding: 10px; | ||
border: 1px solid #ddd; | ||
text-align: left; | ||
} | ||
|
||
.roster-table th { | ||
background-color: #f2f2f2; | ||
font-weight: bold; | ||
} | ||
|
||
.roster-table tbody tr:nth-child(even) { | ||
background-color: #f5f5f5; | ||
} | ||
|
||
.roster-table a { | ||
color: #0077cc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<div class="roster-page"> | ||
<h1 class="roster-title">Conduit Roster</h1> | ||
|
||
<table class="roster-table"> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Total Articles</th> | ||
<th>Total Favourites</th> | ||
<th>First Article Date</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr *ngFor="let user of users | async"> | ||
<td> | ||
<a [routerLink]="['/profile', user.username]"> | ||
{{ user.username }} | ||
</a> | ||
</td> | ||
<td>{{ user.articlesCount }}</td> | ||
<td>{{ user.totalFavorites }}</td> | ||
<td> | ||
{{ user.firstArticleDate | date }} | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { | ||
Component, | ||
Input, | ||
Output, | ||
EventEmitter, | ||
ChangeDetectionStrategy, | ||
OnInit, | ||
OnChanges, | ||
SimpleChanges, | ||
} from '@angular/core'; | ||
import { RosterService } from './roster.service'; | ||
import { RosterUser } from './roster.type'; | ||
import { Observable, Subject, Subscription } from 'rxjs'; | ||
import { RouterModule } from '@angular/router'; | ||
|
||
@Component({ | ||
selector: 'roster', | ||
standalone: true, | ||
templateUrl: './roster.component.html', | ||
styleUrls: ['./roster.component.css'], | ||
imports: [CommonModule, RouterModule], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class RosterListComponent implements OnInit { | ||
users = new Subject<RosterUser[]>(); | ||
|
||
constructor(private rosterService: RosterService) {} | ||
|
||
ngOnInit() { | ||
this.rosterService.getRoster().subscribe((roster) => { | ||
console.log(roster); | ||
this.users.next(roster); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// roster.service.ts | ||
|
||
import { Injectable } from '@angular/core'; | ||
import { ApiService } from '@realworld/core/http-client'; | ||
import { Observable } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class RosterService { | ||
constructor(private apiService: ApiService) {} | ||
|
||
getRoster(): Observable<any> { | ||
return this.apiService.get('/roster'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface RosterUser { | ||
username: string; | ||
url: string; | ||
articlesCount: number; | ||
totalFavorites: number; | ||
firstArticleDate?: Date; | ||
} |