mirror of
https://github.com/sakaljurgis/kodi-api-server.git
synced 2026-07-08 20:37:41 +00:00
add recent searches module
This commit is contained in:
@@ -14,6 +14,7 @@ export default class ResponseItem {
|
||||
thumb: string;
|
||||
contextMenus: Array<ContextMenu>;
|
||||
date: string;
|
||||
searchFor: string;
|
||||
|
||||
constructor() {
|
||||
this.setToPlayable();
|
||||
@@ -44,13 +45,17 @@ export default class ResponseItem {
|
||||
return this;
|
||||
}
|
||||
|
||||
setActionSearch() {
|
||||
setActionSearch(searchFor: string | null = null) {
|
||||
this.action = 'search';
|
||||
delete this.isPlayable;
|
||||
delete this.IsPlayable;
|
||||
delete this.mediatype;
|
||||
this.isFolder = false;
|
||||
|
||||
if (searchFor) {
|
||||
this.searchFor = searchFor;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,14 @@ import { Controller, Get, Param, Query, Req } from '@nestjs/common';
|
||||
import { Request } from 'express';
|
||||
import { LrtService } from './lrt.service';
|
||||
import ApiResponse from '../Dto/api-response.dto';
|
||||
import { RecentSearchesService } from '../RecentSearches/recent-searches.service';
|
||||
|
||||
@Controller('api/lrt')
|
||||
export class LrtController {
|
||||
constructor(private readonly lrtService: LrtService) {}
|
||||
constructor(
|
||||
private readonly lrtService: LrtService,
|
||||
private readonly recentSearchesService: RecentSearchesService,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
getLrtMenu(): ApiResponse {
|
||||
@@ -14,9 +18,15 @@ export class LrtController {
|
||||
|
||||
@Get('search')
|
||||
getSearch(@Query('search') search: string): Promise<ApiResponse> {
|
||||
this.recentSearchesService.addRecentSearch('lrt', search).then();
|
||||
return this.lrtService.searchCategories(search);
|
||||
}
|
||||
|
||||
@Get('recent')
|
||||
getRecent(): Promise<ApiResponse> {
|
||||
return this.lrtService.getRecentSearches();
|
||||
}
|
||||
|
||||
@Get('cat/:id')
|
||||
async getCategory(@Param('id') id: string): Promise<ApiResponse> {
|
||||
return this.lrtService.getCategory(id);
|
||||
|
||||
@@ -3,10 +3,11 @@ import { LrtController } from './lrt.controller';
|
||||
import { LrtService } from './lrt.service';
|
||||
import { KodiApiResponseFactory } from '../kodi-api-response.factory';
|
||||
import { LrtApiClientModule } from './LrtApiClient/lrt-api-client.module';
|
||||
import { RecentSearchesService } from '../RecentSearches/recent-searches.service';
|
||||
|
||||
@Module({
|
||||
imports: [LrtApiClientModule],
|
||||
controllers: [LrtController],
|
||||
providers: [KodiApiResponseFactory, LrtService],
|
||||
providers: [KodiApiResponseFactory, LrtService, RecentSearchesService],
|
||||
})
|
||||
export class LrtModule {}
|
||||
|
||||
@@ -2,12 +2,14 @@ import { Injectable } from '@nestjs/common';
|
||||
import ApiResponse from '../Dto/api-response.dto';
|
||||
import { KodiApiResponseFactory } from '../kodi-api-response.factory';
|
||||
import { LrtApiClient } from './LrtApiClient/lrt-api.client';
|
||||
import { RecentSearchesService } from '../RecentSearches/recent-searches.service';
|
||||
|
||||
@Injectable()
|
||||
export class LrtService {
|
||||
constructor(
|
||||
private readonly kodiApiResponseFactory: KodiApiResponseFactory,
|
||||
private readonly lrtApiClient: LrtApiClient,
|
||||
private readonly recentSearchesService: RecentSearchesService
|
||||
) {}
|
||||
|
||||
getMainMenu(): ApiResponse {
|
||||
@@ -19,7 +21,9 @@ export class LrtService {
|
||||
}
|
||||
|
||||
private createMainMenu(apiResponse: ApiResponse): void {
|
||||
//todo - refactor to 5 most recent searches + viskas?
|
||||
const items = {
|
||||
'lrt/recent': 'neseniai ieskoti',
|
||||
'lrt/tema/vaikams': 'vaikams',
|
||||
'lrt/tema/sportas': 'sportas',
|
||||
'lrt/tema/kultura': 'kultura',
|
||||
@@ -82,4 +86,8 @@ export class LrtService {
|
||||
|
||||
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,14 +1,11 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { EntityManager } from 'typeorm';
|
||||
import { configService } from '../config/config.service';
|
||||
|
||||
@Controller('try')
|
||||
export class TryController {
|
||||
constructor(private orm: EntityManager) {}
|
||||
@Get('')
|
||||
async main() {
|
||||
//return await this.orm.query('select * from aliases');
|
||||
//return { hello: 'world' };
|
||||
return configService.getStaticFolder();
|
||||
return await this.orm.query('select * from aliases');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { TryController } from './try.cotroller';
|
||||
import { configService } from '../config/config.service';
|
||||
|
||||
/**
|
||||
* Playground module for playing around
|
||||
*/
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forRoot(configService.getTypeOrmConfig())],
|
||||
imports: [],
|
||||
controllers: [TryController],
|
||||
providers: [],
|
||||
})
|
||||
|
||||
@@ -5,7 +5,7 @@ import { join } from 'path';
|
||||
|
||||
env.config();
|
||||
|
||||
class ConfigService {
|
||||
export class ConfigService {
|
||||
public getEnv(key: string): any {
|
||||
return process.env[key];
|
||||
}
|
||||
@@ -32,6 +32,10 @@ class ConfigService {
|
||||
return join(this.getRootPath(), this.getEnv('STATIC_SERVE_FOLDER'));
|
||||
}
|
||||
|
||||
public getRecentSearchesFolder(): string {
|
||||
return join(this.getRootPath(), this.getEnv('STATIC_SERVE_FOLDER'));
|
||||
}
|
||||
|
||||
private getRootPath(): string {
|
||||
return join(__dirname, '../..');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user