add recent searches module

This commit is contained in:
Jurgis Sakalauskas
2022-12-09 18:09:04 +02:00
parent 7e5fa1fb2a
commit cd4ba620de
9 changed files with 94 additions and 11 deletions
+6 -1
View File
@@ -14,6 +14,7 @@ export default class ResponseItem {
thumb: string; thumb: string;
contextMenus: Array<ContextMenu>; contextMenus: Array<ContextMenu>;
date: string; date: string;
searchFor: string;
constructor() { constructor() {
this.setToPlayable(); this.setToPlayable();
@@ -44,13 +45,17 @@ export default class ResponseItem {
return this; return this;
} }
setActionSearch() { setActionSearch(searchFor: string | null = null) {
this.action = 'search'; this.action = 'search';
delete this.isPlayable; delete this.isPlayable;
delete this.IsPlayable; delete this.IsPlayable;
delete this.mediatype; delete this.mediatype;
this.isFolder = false; this.isFolder = false;
if (searchFor) {
this.searchFor = searchFor;
}
return this; return this;
} }
+11 -1
View File
@@ -2,10 +2,14 @@ import { Controller, Get, Param, Query, Req } from '@nestjs/common';
import { Request } from 'express'; import { Request } from 'express';
import { LrtService } from './lrt.service'; import { LrtService } from './lrt.service';
import ApiResponse from '../Dto/api-response.dto'; import ApiResponse from '../Dto/api-response.dto';
import { RecentSearchesService } from '../RecentSearches/recent-searches.service';
@Controller('api/lrt') @Controller('api/lrt')
export class LrtController { export class LrtController {
constructor(private readonly lrtService: LrtService) {} constructor(
private readonly lrtService: LrtService,
private readonly recentSearchesService: RecentSearchesService,
) {}
@Get() @Get()
getLrtMenu(): ApiResponse { getLrtMenu(): ApiResponse {
@@ -14,9 +18,15 @@ export class LrtController {
@Get('search') @Get('search')
getSearch(@Query('search') search: string): Promise<ApiResponse> { getSearch(@Query('search') search: string): Promise<ApiResponse> {
this.recentSearchesService.addRecentSearch('lrt', search).then();
return this.lrtService.searchCategories(search); return this.lrtService.searchCategories(search);
} }
@Get('recent')
getRecent(): Promise<ApiResponse> {
return this.lrtService.getRecentSearches();
}
@Get('cat/:id') @Get('cat/:id')
async getCategory(@Param('id') id: string): Promise<ApiResponse> { async getCategory(@Param('id') id: string): Promise<ApiResponse> {
return this.lrtService.getCategory(id); return this.lrtService.getCategory(id);
+2 -1
View File
@@ -3,10 +3,11 @@ import { LrtController } from './lrt.controller';
import { LrtService } from './lrt.service'; import { LrtService } from './lrt.service';
import { KodiApiResponseFactory } from '../kodi-api-response.factory'; import { KodiApiResponseFactory } from '../kodi-api-response.factory';
import { LrtApiClientModule } from './LrtApiClient/lrt-api-client.module'; import { LrtApiClientModule } from './LrtApiClient/lrt-api-client.module';
import { RecentSearchesService } from '../RecentSearches/recent-searches.service';
@Module({ @Module({
imports: [LrtApiClientModule], imports: [LrtApiClientModule],
controllers: [LrtController], controllers: [LrtController],
providers: [KodiApiResponseFactory, LrtService], providers: [KodiApiResponseFactory, LrtService, RecentSearchesService],
}) })
export class LrtModule {} export class LrtModule {}
+8
View File
@@ -2,12 +2,14 @@ import { Injectable } from '@nestjs/common';
import ApiResponse from '../Dto/api-response.dto'; import ApiResponse from '../Dto/api-response.dto';
import { KodiApiResponseFactory } from '../kodi-api-response.factory'; import { KodiApiResponseFactory } from '../kodi-api-response.factory';
import { LrtApiClient } from './LrtApiClient/lrt-api.client'; import { LrtApiClient } from './LrtApiClient/lrt-api.client';
import { RecentSearchesService } from '../RecentSearches/recent-searches.service';
@Injectable() @Injectable()
export class LrtService { export class LrtService {
constructor( constructor(
private readonly kodiApiResponseFactory: KodiApiResponseFactory, private readonly kodiApiResponseFactory: KodiApiResponseFactory,
private readonly lrtApiClient: LrtApiClient, private readonly lrtApiClient: LrtApiClient,
private readonly recentSearchesService: RecentSearchesService
) {} ) {}
getMainMenu(): ApiResponse { getMainMenu(): ApiResponse {
@@ -19,7 +21,9 @@ export class LrtService {
} }
private createMainMenu(apiResponse: ApiResponse): void { private createMainMenu(apiResponse: ApiResponse): void {
//todo - refactor to 5 most recent searches + viskas?
const items = { const items = {
'lrt/recent': 'neseniai ieskoti',
'lrt/tema/vaikams': 'vaikams', 'lrt/tema/vaikams': 'vaikams',
'lrt/tema/sportas': 'sportas', 'lrt/tema/sportas': 'sportas',
'lrt/tema/kultura': 'kultura', 'lrt/tema/kultura': 'kultura',
@@ -82,4 +86,8 @@ export class LrtService {
return apiResponse; return apiResponse;
} }
async getRecentSearches(): Promise<ApiResponse> {
return this.recentSearchesService.getRecentSearches('lrt', 'lrt/search');
}
} }
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { KodiApiResponseFactory } from '../kodi-api-response.factory';
import { RecentSearchesService } from './recent-searches.service';
@Module({
imports: [],
controllers: [],
providers: [RecentSearchesService, KodiApiResponseFactory],
exports: [RecentSearchesService],
})
export class RecentSearchesModule {}
@@ -0,0 +1,49 @@
import { Injectable } from '@nestjs/common';
import ApiResponse from '../Dto/api-response.dto';
import { configService, ConfigService } from '../../config/config.service';
import { KodiApiResponseFactory } from '../kodi-api-response.factory';
import { join } from 'path';
import { readFile, writeFile } from 'fs/promises';
@Injectable()
export class RecentSearchesService {
private readonly configService: ConfigService;
constructor(
private readonly kodiApiResponseFactory: KodiApiResponseFactory,
) {
this.configService = configService;
}
async getRecentSearches(module: string, path: string): Promise<ApiResponse> {
const data = await this.getRecentSearchesData(module);
const response = this.kodiApiResponseFactory.createApiResponse();
for (const recentSearch of data) {
response
.createItem()
.setLabel(recentSearch)
.setActionSearch(recentSearch)
.setPath(path);
}
return response;
}
async addRecentSearch(module: string, term: string): Promise<void> {
const data = await this.getRecentSearchesData(module);
const index = data.indexOf(term);
if (index > -1) {
data.splice(index, 1);
}
data.push(term);
const rawData = JSON.stringify(data);
const filePath = join(this.configService.getRecentSearchesFolder(), module);
await writeFile(filePath, rawData);
}
private async getRecentSearchesData(module: string): Promise<Array<string>> {
const filePath = join(this.configService.getRecentSearchesFolder(), module);
const rawData = '' + (await readFile(filePath).catch(() => '[]'));
return JSON.parse(rawData);
}
}
+1 -4
View File
@@ -1,14 +1,11 @@
import { Controller, Get } from '@nestjs/common'; import { Controller, Get } from '@nestjs/common';
import { EntityManager } from 'typeorm'; import { EntityManager } from 'typeorm';
import { configService } from '../config/config.service';
@Controller('try') @Controller('try')
export class TryController { export class TryController {
constructor(private orm: EntityManager) {} constructor(private orm: EntityManager) {}
@Get('') @Get('')
async main() { async main() {
//return await this.orm.query('select * from aliases'); return await this.orm.query('select * from aliases');
//return { hello: 'world' };
return configService.getStaticFolder();
} }
} }
+1 -3
View File
@@ -1,14 +1,12 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TryController } from './try.cotroller'; import { TryController } from './try.cotroller';
import { configService } from '../config/config.service';
/** /**
* Playground module for playing around * Playground module for playing around
*/ */
@Module({ @Module({
imports: [TypeOrmModule.forRoot(configService.getTypeOrmConfig())], imports: [],
controllers: [TryController], controllers: [TryController],
providers: [], providers: [],
}) })
+5 -1
View File
@@ -5,7 +5,7 @@ import { join } from 'path';
env.config(); env.config();
class ConfigService { export class ConfigService {
public getEnv(key: string): any { public getEnv(key: string): any {
return process.env[key]; return process.env[key];
} }
@@ -32,6 +32,10 @@ class ConfigService {
return join(this.getRootPath(), this.getEnv('STATIC_SERVE_FOLDER')); return join(this.getRootPath(), this.getEnv('STATIC_SERVE_FOLDER'));
} }
public getRecentSearchesFolder(): string {
return join(this.getRootPath(), this.getEnv('STATIC_SERVE_FOLDER'));
}
private getRootPath(): string { private getRootPath(): string {
return join(__dirname, '../..'); return join(__dirname, '../..');
} }