Add additional properties
All checks were successful
Gitea Actions Demo / build (push) Successful in 2m9s
All checks were successful
Gitea Actions Demo / build (push) Successful in 2m9s
This commit is contained in:
@@ -34,11 +34,14 @@ export class BabyNamesController {
|
||||
const currentIndex = await this.babyNamesService.getCurrentNumber(
|
||||
key as string,
|
||||
);
|
||||
const name = this.babyNamesService.nameList[currentIndex];
|
||||
const synonyms = this.babyNamesService.getSynonyms(name);
|
||||
return {
|
||||
key: key,
|
||||
name: this.babyNamesService.nameList[currentIndex],
|
||||
name,
|
||||
index: currentIndex,
|
||||
message: request.query.message || null,
|
||||
synonyms: synonyms.join(', '),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -52,14 +55,23 @@ export class BabyNamesController {
|
||||
name: string;
|
||||
nameindex: string;
|
||||
opinion: string;
|
||||
pronunciation: string;
|
||||
spelling: string;
|
||||
comment: string;
|
||||
},
|
||||
@Res({ passthrough: true }) res: Response,
|
||||
) {
|
||||
const { key, name, opinion, nameindex } = body;
|
||||
const { key, name, opinion, nameindex, pronunciation, spelling, comment } =
|
||||
body;
|
||||
if (!key) {
|
||||
throw new Error("Missing 'key' field");
|
||||
}
|
||||
await this.babyNamesService.addUserScore(key, name, parseInt(opinion, 10));
|
||||
await this.babyNamesService.addUserScore(key, name, {
|
||||
opinion: parseInt(opinion, 10),
|
||||
pronunciation: parseInt(pronunciation, 10),
|
||||
spelling: parseInt(spelling, 10),
|
||||
comment: comment || '',
|
||||
});
|
||||
await this.babyNamesService.writeUserNumber(
|
||||
key,
|
||||
parseInt(nameindex, 10) + 1,
|
||||
|
@@ -20,11 +20,26 @@ export interface NameCountMap {
|
||||
[key: string]: number;
|
||||
}
|
||||
|
||||
interface NameSynonyms {
|
||||
name: string;
|
||||
gender: string;
|
||||
synonyms: string[];
|
||||
}
|
||||
|
||||
export interface NameSynonymsMap {
|
||||
[key: string]: NameSynonyms;
|
||||
}
|
||||
|
||||
interface UserScoreMap {
|
||||
[key: string]: object;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class BabyNamesService {
|
||||
public nameList: NameList = [];
|
||||
public nameCountMap: NameCountMap = {};
|
||||
private readonly kvNamespace = 'baby-names';
|
||||
public nameSynonymsMap: NameSynonymsMap = {};
|
||||
public userScoreMap: UserScoreMap = {};
|
||||
|
||||
constructor(private readonly minioService: MinioService) {
|
||||
this.refreshNames();
|
||||
@@ -37,6 +52,16 @@ export class BabyNamesService {
|
||||
this.nameList = (
|
||||
await axios.get('https://cache.sh/baby-name-data/namecount-list.json')
|
||||
).data;
|
||||
this.nameSynonymsMap = (
|
||||
await axios.get('https://cache.sh/baby-name-data/btn_synonyms.json')
|
||||
).data;
|
||||
this.nameSynonymsMap = Object.keys(this.nameSynonymsMap).reduce(
|
||||
(acc, key) => {
|
||||
acc[key.toLowerCase()] = this.nameSynonymsMap[key];
|
||||
return acc;
|
||||
},
|
||||
{} as NameSynonymsMap,
|
||||
);
|
||||
}
|
||||
|
||||
public async getCurrentNumber(userKey: string): Promise<number> {
|
||||
@@ -63,7 +88,7 @@ export class BabyNamesService {
|
||||
);
|
||||
}
|
||||
|
||||
public async getUserScores(userKey: string): Promise<NameCountMap> {
|
||||
public async getUserScores(userKey: string): Promise<UserScoreMap> {
|
||||
const scoresKey = await this.minioService
|
||||
.getBuffer(
|
||||
this.minioService.defaultBucketName,
|
||||
@@ -79,7 +104,7 @@ export class BabyNamesService {
|
||||
|
||||
public async saveUserScores(
|
||||
userKey: string,
|
||||
scores: NameCountMap,
|
||||
scores: UserScoreMap,
|
||||
): Promise<void> {
|
||||
await this.minioService.uploadBuffer(
|
||||
this.minioService.defaultBucketName,
|
||||
@@ -91,10 +116,18 @@ export class BabyNamesService {
|
||||
public async addUserScore(
|
||||
userKey: string,
|
||||
name: string,
|
||||
score: number,
|
||||
score: object,
|
||||
): Promise<void> {
|
||||
const scores = await this.getUserScores(userKey);
|
||||
scores[name] = score;
|
||||
await this.saveUserScores(userKey, scores);
|
||||
}
|
||||
|
||||
public getSynonyms(name: string): string[] {
|
||||
const entry = this.nameSynonymsMap[name.toLowerCase()];
|
||||
if (entry) {
|
||||
return entry.synonyms;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user