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 { 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],

View File

@@ -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;
}
}

View File

@@ -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") {}

View File

@@ -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 ' },
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)) {
if (
this.configService.get<string>("API_KEYS")?.split(",").includes(apiKey)
) {
//@ts-expect-error
done(null, true);
}
done(new UnauthorizedException(), null);
}
};
}

View File

@@ -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(

View File

@@ -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],

View File

@@ -1,17 +1,15 @@
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,
});

View File

@@ -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"],
});
}
}

View File

@@ -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,

View File

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

View File

@@ -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],

View File

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