Add ability to fetch and peek scores
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
||||
import { ApiConsumes, ApiTags } from '@nestjs/swagger';
|
||||
import { BabyNamesService, Cards } from './baby-names.service';
|
||||
import { Request, Response } from 'express';
|
||||
import { map, pipe } from 'ramda';
|
||||
|
||||
@Controller('baby-names')
|
||||
@ApiTags('baby-names')
|
||||
@@ -149,6 +150,51 @@ export class BabyNamesController {
|
||||
});
|
||||
}
|
||||
|
||||
@Get('user/:key.json')
|
||||
async getUserData(@Req() request: Request, @Res() res: Response) {
|
||||
const data = await this.babyNamesService.getUserScores(request.params.key);
|
||||
return res.json(data);
|
||||
}
|
||||
|
||||
@Get('user/:key.csv')
|
||||
async getUserDataCSV(@Req() request: Request, @Res() res: Response) {
|
||||
const data = await this.babyNamesService.getUserScoresArray(
|
||||
request.params.key,
|
||||
);
|
||||
const lines = [
|
||||
['Name', 'Opinion', 'Pronunciation', 'Spelling', 'Comment'],
|
||||
...data.map((item) => [
|
||||
item.name,
|
||||
item.opinion,
|
||||
item.pronunciation,
|
||||
item.spelling,
|
||||
item.comment,
|
||||
]),
|
||||
]
|
||||
.map((row) => row.join(','))
|
||||
.join('\n');
|
||||
res.header('Content-Type', 'text/csv');
|
||||
res.attachment(`${request.params.key}-data.csv`);
|
||||
res.send(lines);
|
||||
}
|
||||
|
||||
@Get('user/:key/:name.json')
|
||||
async getUserScoreForName(@Req() request: Request, @Res() res: Response) {
|
||||
const key = request.params.key;
|
||||
const name = request.params.name;
|
||||
if (!key || !name) {
|
||||
res.status(400).json({ error: "Missing 'key' or 'name' field" });
|
||||
return;
|
||||
}
|
||||
const scores = await this.babyNamesService.getUserScores(key);
|
||||
const score = scores[name];
|
||||
if (!score) {
|
||||
res.status(404).json({ error: 'Name not found' });
|
||||
return;
|
||||
}
|
||||
res.json(score);
|
||||
}
|
||||
|
||||
@Get('cards/:key.json')
|
||||
async getUserCards(@Req() request: Request, @Res() res: Response) {
|
||||
const key = request.params.key;
|
||||
|
@@ -41,10 +41,23 @@ export interface NameSynonymsMap {
|
||||
[key: string]: NameSynonyms;
|
||||
}
|
||||
|
||||
interface UserScoreMap {
|
||||
[key: string]: object;
|
||||
interface UserScore {
|
||||
opinion: number;
|
||||
pronunciation: number;
|
||||
spelling: number;
|
||||
comment: string;
|
||||
}
|
||||
|
||||
interface UserScoreMap {
|
||||
[key: string]: UserScore;
|
||||
}
|
||||
|
||||
type UserScoreArray = Array<
|
||||
{
|
||||
name: string;
|
||||
} & UserScore
|
||||
>;
|
||||
|
||||
@Injectable()
|
||||
export class BabyNamesService {
|
||||
public nameList: NameList = [];
|
||||
@@ -130,6 +143,14 @@ export class BabyNamesService {
|
||||
return JSON.parse(scoresKey);
|
||||
}
|
||||
|
||||
public async getUserScoresArray(userKey: string): Promise<UserScoreArray> {
|
||||
const scores = await this.getUserScores(userKey);
|
||||
return Object.keys(scores).map((name) => ({
|
||||
name,
|
||||
...scores[name],
|
||||
}));
|
||||
}
|
||||
|
||||
public async getUserCardsOrDefault(
|
||||
userKey: string,
|
||||
): Promise<CardsWithHistory> {
|
||||
@@ -174,7 +195,7 @@ export class BabyNamesService {
|
||||
public async addUserScore(
|
||||
userKey: string,
|
||||
name: string,
|
||||
score: object,
|
||||
score: UserScore,
|
||||
): Promise<void> {
|
||||
const scores = await this.getUserScores(userKey);
|
||||
scores[name] = score;
|
||||
|
Reference in New Issue
Block a user