Format back

This commit is contained in:
2023-12-07 17:11:08 +01:00
parent 70b109e78b
commit 80329e240e
13 changed files with 79 additions and 82 deletions

View File

@@ -2,7 +2,7 @@ import { Module } from "@nestjs/common";
import { PrismaModule } from "src/prisma/prisma.module"; import { PrismaModule } from "src/prisma/prisma.module";
import { ArtistController } from "./artist.controller"; import { ArtistController } from "./artist.controller";
import { ArtistService } from "./artist.service"; import { ArtistService } from "./artist.service";
import { SearchModule } from 'src/search/search.module'; import { SearchModule } from "src/search/search.module";
@Module({ @Module({
imports: [PrismaModule, SearchModule], imports: [PrismaModule, SearchModule],

View File

@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from "@nestjs/common";
import { Prisma, Artist } from '@prisma/client'; import { Prisma, Artist } from "@prisma/client";
import { PrismaService } from 'src/prisma/prisma.service'; import { PrismaService } from "src/prisma/prisma.service";
import { MeiliService } from 'src/search/meilisearch.service'; import { MeiliService } from "src/search/meilisearch.service";
@Injectable() @Injectable()
export class ArtistService { export class ArtistService {
@@ -14,7 +14,7 @@ export class ArtistService {
const ret = await this.prisma.artist.create({ const ret = await this.prisma.artist.create({
data, data,
}); });
await this.search.index('artists').addDocuments([ret]); await this.search.index("artists").addDocuments([ret]);
return ret; return ret;
} }
@@ -51,7 +51,7 @@ export class ArtistService {
const ret = await this.prisma.artist.delete({ const ret = await this.prisma.artist.delete({
where, where,
}); });
await this.search.index('artists').deleteDocument(ret.id); await this.search.index("artists").deleteDocument(ret.id);
return ret return ret;
} }
} }

View File

@@ -2,4 +2,4 @@ import { Injectable } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport"; import { AuthGuard } from "@nestjs/passport";
@Injectable() @Injectable()
export class ApiKeyAuthGuard extends AuthGuard('api-key') {} export class ApiKeyAuthGuard extends AuthGuard("api-key") {}

View File

@@ -1,28 +1,31 @@
import { Injectable, UnauthorizedException } from "@nestjs/common";
import { Injectable, UnauthorizedException } from '@nestjs/common'; import { AuthService } from "./auth.service";
import { AuthService } from './auth.service'; import { PassportStrategy } from "@nestjs/passport";
import { PassportStrategy } from '@nestjs/passport'; import Strategy from "passport-headerapikey";
import Strategy from 'passport-headerapikey'; import { ConfigService } from "@nestjs/config";
import { ConfigService } from '@nestjs/config';
@Injectable() @Injectable()
export class HeaderApiKeyStrategy extends PassportStrategy(Strategy, 'api-key') { export class HeaderApiKeyStrategy extends PassportStrategy(
constructor( Strategy,
private readonly configService: ConfigService "api-key",
) { ) {
super({ header: 'Authorization', prefix: 'API Key ' }, constructor(private readonly configService: ConfigService) {
true, super(
async (apiKey, done) => { { header: "Authorization", prefix: "API Key " },
return this.validate(apiKey, done); true,
}); async (apiKey, done) => {
} return this.validate(apiKey, done);
},
);
}
public validate = (apiKey: string, done: (error: Error, data) => {}) => { public validate = (apiKey: string, done: (error: Error, data) => {}) => {
if (this.configService.get<string>('API_KEYS')?.split(',').includes(apiKey)) { if (
//@ts-expect-error this.configService.get<string>("API_KEYS")?.split(",").includes(apiKey)
done(null, true); ) {
} //@ts-expect-error
done(new UnauthorizedException(), null); done(null, true);
} }
} done(new UnauthorizedException(), null);
};
}

View File

@@ -15,9 +15,8 @@ export class AuthService {
validateApiKey(apikey: string): boolean { validateApiKey(apikey: string): boolean {
if (process.env.API_KEYS == null) return false; if (process.env.API_KEYS == null) return false;
const keys = process.env.API_KEYS.split(','); const keys = process.env.API_KEYS.split(",");
return keys.includes(apikey); return keys.includes(apikey);
} }
async validateUser( async validateUser(

View File

@@ -11,6 +11,6 @@ export class User {
isGuest: boolean; isGuest: boolean;
@ApiProperty() @ApiProperty()
partyPlayed: number; partyPlayed: number;
@ApiProperty() @ApiProperty()
totalScore: number; totalScore: number;
} }

View File

@@ -1,7 +1,7 @@
import { Module } from '@nestjs/common'; import { Module } from "@nestjs/common";
import { ScoresService } from './scores.service'; import { ScoresService } from "./scores.service";
import { ScoresController } from './scores.controller'; import { ScoresController } from "./scores.controller";
import { PrismaModule } from 'src/prisma/prisma.module'; import { PrismaModule } from "src/prisma/prisma.module";
@Module({ @Module({
imports: [PrismaModule], imports: [PrismaModule],

View File

@@ -1,19 +1,17 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from "@nestjs/common";
import { User } from '@prisma/client'; import { User } from "@prisma/client";
import { PrismaService } from 'src/prisma/prisma.service'; import { PrismaService } from "src/prisma/prisma.service";
@Injectable() @Injectable()
export class ScoresService { export class ScoresService {
constructor( constructor(private prisma: PrismaService) {}
private prisma: PrismaService,
) {}
async topTwenty(): Promise<User[]> { async topTwenty(): Promise<User[]> {
return this.prisma.user.findMany({ return this.prisma.user.findMany({
orderBy: { orderBy: {
totalScore: 'desc', totalScore: "desc",
}, },
take: 20, take: 20,
}); });
} }
} }

View File

@@ -1,29 +1,29 @@
import { Injectable, OnModuleInit } from '@nestjs/common'; import { Injectable, OnModuleInit } from "@nestjs/common";
import MeiliSearch, { DocumentOptions, Settings } from 'meilisearch'; import MeiliSearch, { DocumentOptions, Settings } from "meilisearch";
@Injectable() @Injectable()
export class MeiliService extends MeiliSearch implements OnModuleInit { export class MeiliService extends MeiliSearch implements OnModuleInit {
constructor() { constructor() {
super({ super({
host: process.env.MEILI_ADDR || 'http://meilisearch:7700', host: process.env.MEILI_ADDR || "http://meilisearch:7700",
apiKey: process.env.MEILI_MASTER_KEY, apiKey: process.env.MEILI_MASTER_KEY,
}); });
} }
async definedIndex(uid: string, opts: Settings) { async definedIndex(uid: string, opts: Settings) {
let task = await this.createIndex(uid, { primaryKey: 'id' }); let task = await this.createIndex(uid, { primaryKey: "id" });
await this.waitForTask(task.taskUid); await this.waitForTask(task.taskUid);
task = await this.index(uid).updateSettings(opts); task = await this.index(uid).updateSettings(opts);
await this.waitForTask(task.taskUid); await this.waitForTask(task.taskUid);
} }
async onModuleInit() { async onModuleInit() {
await this.definedIndex('songs', { await this.definedIndex("songs", {
searchableAttributes: ['name', 'artist'], searchableAttributes: ["name", "artist"],
filterableAttributes: ['artistId', 'genreId'], filterableAttributes: ["artistId", "genreId"],
}); });
await this.definedIndex('artists', { await this.definedIndex("artists", {
searchableAttributes: ['name'], searchableAttributes: ["name"],
}); });
} }
} }

View File

@@ -39,12 +39,12 @@ export class SearchController {
@ApiUnauthorizedResponse({ description: "Invalid token" }) @ApiUnauthorizedResponse({ description: "Invalid token" })
async searchSong( async searchSong(
@Request() req: any, @Request() req: any,
@Param('query') query: string, @Param("query") query: string,
@Query('artistId') artistId: number, @Query("artistId") artistId: number,
@Query('genreId') genreId: number, @Query("genreId") genreId: number,
@Query('include') include: string, @Query("include") include: string,
@Query('skip', new DefaultValuePipe(0), ParseIntPipe) skip: number, @Query("skip", new DefaultValuePipe(0), ParseIntPipe) skip: number,
@Query('take', new DefaultValuePipe(20), ParseIntPipe) take: number, @Query("take", new DefaultValuePipe(20), ParseIntPipe) take: number,
): Promise<Song[] | null> { ): Promise<Song[] | null> {
return await this.searchService.searchSong( return await this.searchService.searchSong(
query, query,

View File

@@ -38,7 +38,7 @@ export class SearchService {
filter: [ filter: [
...(artistId ? [`artistId = ${artistId}`] : []), ...(artistId ? [`artistId = ${artistId}`] : []),
...(genreId ? [`genreId = ${genreId}`] : []), ...(genreId ? [`genreId = ${genreId}`] : []),
].join(' AND '), ].join(" AND "),
}) })
).hits.map((x) => x.id); ).hits.map((x) => x.id);

View File

@@ -1,9 +1,9 @@
import { Module } from '@nestjs/common'; import { Module } from "@nestjs/common";
import { SongService } from './song.service'; import { SongService } from "./song.service";
import { SongController } from './song.controller'; import { SongController } from "./song.controller";
import { PrismaModule } from 'src/prisma/prisma.module'; import { PrismaModule } from "src/prisma/prisma.module";
import { HistoryModule } from 'src/history/history.module'; import { HistoryModule } from "src/history/history.module";
import { SearchModule } from 'src/search/search.module'; import { SearchModule } from "src/search/search.module";
@Module({ @Module({
imports: [PrismaModule, HistoryModule, SearchModule], imports: [PrismaModule, HistoryModule, SearchModule],

View File

@@ -118,17 +118,14 @@ export class UsersService {
}); });
} }
async addScore( async addScore(where: number, score: number) {
where: number,
score: number,
) {
return this.prisma.user.update({ return this.prisma.user.update({
where: { id: where }, where: { id: where },
data: { data: {
partyPlayed: { partyPlayed: {
increment: score, increment: score,
},
}, },
}); },
});
} }
} }