Format back
This commit is contained in:
@@ -2,7 +2,7 @@ import { Module } from "@nestjs/common";
|
||||
import { PrismaModule } from "src/prisma/prisma.module";
|
||||
import { ArtistController } from "./artist.controller";
|
||||
import { ArtistService } from "./artist.service";
|
||||
import { SearchModule } from 'src/search/search.module';
|
||||
import { SearchModule } from "src/search/search.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, SearchModule],
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Prisma, Artist } from '@prisma/client';
|
||||
import { PrismaService } from 'src/prisma/prisma.service';
|
||||
import { MeiliService } from 'src/search/meilisearch.service';
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { Prisma, Artist } from "@prisma/client";
|
||||
import { PrismaService } from "src/prisma/prisma.service";
|
||||
import { MeiliService } from "src/search/meilisearch.service";
|
||||
|
||||
@Injectable()
|
||||
export class ArtistService {
|
||||
@@ -14,7 +14,7 @@ export class ArtistService {
|
||||
const ret = await this.prisma.artist.create({
|
||||
data,
|
||||
});
|
||||
await this.search.index('artists').addDocuments([ret]);
|
||||
await this.search.index("artists").addDocuments([ret]);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ export class ArtistService {
|
||||
const ret = await this.prisma.artist.delete({
|
||||
where,
|
||||
});
|
||||
await this.search.index('artists').deleteDocument(ret.id);
|
||||
return ret
|
||||
await this.search.index("artists").deleteDocument(ret.id);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,4 +2,4 @@ import { Injectable } from "@nestjs/common";
|
||||
import { AuthGuard } from "@nestjs/passport";
|
||||
|
||||
@Injectable()
|
||||
export class ApiKeyAuthGuard extends AuthGuard('api-key') {}
|
||||
export class ApiKeyAuthGuard extends AuthGuard("api-key") {}
|
||||
|
||||
@@ -1,28 +1,31 @@
|
||||
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import Strategy from 'passport-headerapikey';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
||||
import { AuthService } from "./auth.service";
|
||||
import { PassportStrategy } from "@nestjs/passport";
|
||||
import Strategy from "passport-headerapikey";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
|
||||
@Injectable()
|
||||
export class HeaderApiKeyStrategy extends PassportStrategy(Strategy, 'api-key') {
|
||||
constructor(
|
||||
private readonly configService: ConfigService
|
||||
) {
|
||||
super({ header: 'Authorization', prefix: 'API Key ' },
|
||||
true,
|
||||
async (apiKey, done) => {
|
||||
return this.validate(apiKey, done);
|
||||
});
|
||||
}
|
||||
export class HeaderApiKeyStrategy extends PassportStrategy(
|
||||
Strategy,
|
||||
"api-key",
|
||||
) {
|
||||
constructor(private readonly configService: ConfigService) {
|
||||
super(
|
||||
{ header: "Authorization", prefix: "API Key " },
|
||||
true,
|
||||
async (apiKey, done) => {
|
||||
return this.validate(apiKey, done);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
public validate = (apiKey: string, done: (error: Error, data) => {}) => {
|
||||
if (this.configService.get<string>('API_KEYS')?.split(',').includes(apiKey)) {
|
||||
//@ts-expect-error
|
||||
done(null, true);
|
||||
}
|
||||
done(new UnauthorizedException(), null);
|
||||
}
|
||||
}
|
||||
public validate = (apiKey: string, done: (error: Error, data) => {}) => {
|
||||
if (
|
||||
this.configService.get<string>("API_KEYS")?.split(",").includes(apiKey)
|
||||
) {
|
||||
//@ts-expect-error
|
||||
done(null, true);
|
||||
}
|
||||
done(new UnauthorizedException(), null);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -15,9 +15,8 @@ export class AuthService {
|
||||
|
||||
validateApiKey(apikey: string): boolean {
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
async validateUser(
|
||||
|
||||
@@ -11,6 +11,6 @@ export class User {
|
||||
isGuest: boolean;
|
||||
@ApiProperty()
|
||||
partyPlayed: number;
|
||||
@ApiProperty()
|
||||
totalScore: number;
|
||||
@ApiProperty()
|
||||
totalScore: number;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ScoresService } from './scores.service';
|
||||
import { ScoresController } from './scores.controller';
|
||||
import { PrismaModule } from 'src/prisma/prisma.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { ScoresService } from "./scores.service";
|
||||
import { ScoresController } from "./scores.controller";
|
||||
import { PrismaModule } from "src/prisma/prisma.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { User } from '@prisma/client';
|
||||
import { PrismaService } from 'src/prisma/prisma.service';
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { User } from "@prisma/client";
|
||||
import { PrismaService } from "src/prisma/prisma.service";
|
||||
|
||||
@Injectable()
|
||||
export class ScoresService {
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
) {}
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async topTwenty(): Promise<User[]> {
|
||||
return this.prisma.user.findMany({
|
||||
orderBy: {
|
||||
totalScore: 'desc',
|
||||
totalScore: "desc",
|
||||
},
|
||||
take: 20,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,29 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import MeiliSearch, { DocumentOptions, Settings } from 'meilisearch';
|
||||
import { Injectable, OnModuleInit } from "@nestjs/common";
|
||||
import MeiliSearch, { DocumentOptions, Settings } from "meilisearch";
|
||||
|
||||
@Injectable()
|
||||
export class MeiliService extends MeiliSearch implements OnModuleInit {
|
||||
constructor() {
|
||||
super({
|
||||
host: process.env.MEILI_ADDR || 'http://meilisearch:7700',
|
||||
host: process.env.MEILI_ADDR || "http://meilisearch:7700",
|
||||
apiKey: process.env.MEILI_MASTER_KEY,
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
task = await this.index(uid).updateSettings(opts);
|
||||
await this.waitForTask(task.taskUid);
|
||||
}
|
||||
|
||||
async onModuleInit() {
|
||||
await this.definedIndex('songs', {
|
||||
searchableAttributes: ['name', 'artist'],
|
||||
filterableAttributes: ['artistId', 'genreId'],
|
||||
await this.definedIndex("songs", {
|
||||
searchableAttributes: ["name", "artist"],
|
||||
filterableAttributes: ["artistId", "genreId"],
|
||||
});
|
||||
await this.definedIndex('artists', {
|
||||
searchableAttributes: ['name'],
|
||||
await this.definedIndex("artists", {
|
||||
searchableAttributes: ["name"],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,12 +39,12 @@ export class SearchController {
|
||||
@ApiUnauthorizedResponse({ description: "Invalid token" })
|
||||
async searchSong(
|
||||
@Request() req: any,
|
||||
@Param('query') query: string,
|
||||
@Query('artistId') artistId: number,
|
||||
@Query('genreId') genreId: number,
|
||||
@Query('include') include: string,
|
||||
@Query('skip', new DefaultValuePipe(0), ParseIntPipe) skip: number,
|
||||
@Query('take', new DefaultValuePipe(20), ParseIntPipe) take: number,
|
||||
@Param("query") query: string,
|
||||
@Query("artistId") artistId: number,
|
||||
@Query("genreId") genreId: number,
|
||||
@Query("include") include: string,
|
||||
@Query("skip", new DefaultValuePipe(0), ParseIntPipe) skip: number,
|
||||
@Query("take", new DefaultValuePipe(20), ParseIntPipe) take: number,
|
||||
): Promise<Song[] | null> {
|
||||
return await this.searchService.searchSong(
|
||||
query,
|
||||
|
||||
@@ -38,7 +38,7 @@ export class SearchService {
|
||||
filter: [
|
||||
...(artistId ? [`artistId = ${artistId}`] : []),
|
||||
...(genreId ? [`genreId = ${genreId}`] : []),
|
||||
].join(' AND '),
|
||||
].join(" AND "),
|
||||
})
|
||||
).hits.map((x) => x.id);
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { SongService } from './song.service';
|
||||
import { SongController } from './song.controller';
|
||||
import { PrismaModule } from 'src/prisma/prisma.module';
|
||||
import { HistoryModule } from 'src/history/history.module';
|
||||
import { SearchModule } from 'src/search/search.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { SongService } from "./song.service";
|
||||
import { SongController } from "./song.controller";
|
||||
import { PrismaModule } from "src/prisma/prisma.module";
|
||||
import { HistoryModule } from "src/history/history.module";
|
||||
import { SearchModule } from "src/search/search.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, HistoryModule, SearchModule],
|
||||
|
||||
@@ -118,17 +118,14 @@ export class UsersService {
|
||||
});
|
||||
}
|
||||
|
||||
async addScore(
|
||||
where: number,
|
||||
score: number,
|
||||
) {
|
||||
async addScore(where: number, score: number) {
|
||||
return this.prisma.user.update({
|
||||
where: { id: where },
|
||||
data: {
|
||||
partyPlayed: {
|
||||
increment: score,
|
||||
},
|
||||
where: { id: where },
|
||||
data: {
|
||||
partyPlayed: {
|
||||
increment: score,
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user