Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Music player #72 #31 #30 #73

Merged
merged 2 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ services:
- grafana-storage:/var/lib/grafana
ports:
- '3009:3000'
depends_on:
- prometheus
env_file:
- docker.env

Expand All @@ -95,6 +97,8 @@ services:
- elasticsearch-data:/usr/share/elasticsearch/data
ports:
- 9200:9200
depends_on:
- dev
env_file:
- docker.env
kibana:
Expand Down
2 changes: 2 additions & 0 deletions src/comments/comments.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Resource } from '../resources/schemas/resource.schema';
import { UserSearchServiceMock } from '../test-utils/mocks/users-search.service.test';
import { MinioServiceMock } from '../test-utils/mocks/minio.service.test';
import { PaymentServiceMock } from '../test-utils/mocks/payment.service.test';
import { TrackSearchServiceMock } from '../test-utils/mocks/tracks-search.service.test';

const owner = data.users.abdou;

Expand Down Expand Up @@ -111,6 +112,7 @@ describe('CommentsController', () => {
useValue: new RepoMockModel(data.resources),
},
UserSearchServiceMock,
TrackSearchServiceMock,
],
})
.overrideGuard(JwtAuthGuard)
Expand Down
6 changes: 4 additions & 2 deletions src/comments/comments.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { CommentSchema, Comment } from './schemas/comment.schema';
import { Track, TrackSchema } from '../tracks/schemas/track.schema';
import { Resource, ResourceSchema } from '../resources/schemas/resource.schema';
import { User, UserSchema } from '../users/schemas/user.schema';
import { TracksService } from '../tracks/tracks.service';
import { ResourcesService } from '../resources/resources.service';
import { MinioClientService } from '../minio-client/minio-client.service';
import { FilesService } from '../files/files.service';
import { PaymentsService } from '../payments/payments.service';
import UsersModule from '../users/users.module';
import { TracksModule } from '../tracks/tracks.module';
import { SearchModule } from '../search/search.module';

@Module({
imports: [
Expand All @@ -22,11 +23,12 @@ import UsersModule from '../users/users.module';
{ name: User.name, schema: UserSchema },
]),
UsersModule,
TracksModule,
SearchModule,
],
controllers: [CommentsController],
providers: [
CommentsService,
TracksService,
ResourcesService,
MinioClientService,
FilesService,
Expand Down
8 changes: 5 additions & 3 deletions src/comments/comments.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
closeInMongodConnection,
rootMongooseTestModule,
} from '../test-utils/in-memory/mongoose.helper.test';
import { ICreateTrackResponse } from '../tracks/interfaces/track-create-response.interface';
import { Track, TrackSchema } from '../tracks/schemas/track.schema';
import { TracksService } from '../tracks/tracks.service';
import { User, UserDocument, UserSchema } from '../users/schemas/user.schema';
Expand All @@ -21,6 +20,8 @@ import { FileMimeType } from '../files/dto/simple-create-file.dto';
import { UserSearchServiceMock } from '../test-utils/mocks/users-search.service.test';
import { MinioServiceMock } from '../test-utils/mocks/minio.service.test';
import { PaymentServiceMock } from '../test-utils/mocks/payment.service.test';
import { ITrackResponse } from '../tracks/interfaces/track-response.interface';
import { TrackSearchServiceMock } from '../test-utils/mocks/tracks-search.service.test';

const abdou = data.users.abdou;
const jayz = data.users.jayz;
Expand All @@ -36,8 +37,8 @@ const commentThreeResult = data.comments.comment_3;

let user: UserDocument;
let artist: UserDocument;
let encore: ICreateTrackResponse;
let threat: ICreateTrackResponse;
let encore: ITrackResponse;
let threat: ITrackResponse;
let resourceOne: ICreateResourceResponse;
let commentId: string;
describe('CommentsService', () => {
Expand Down Expand Up @@ -79,6 +80,7 @@ describe('CommentsService', () => {
MinioServiceMock,
PaymentServiceMock,
UserSearchServiceMock,
TrackSearchServiceMock,
],
}).compile();

Expand Down
6 changes: 3 additions & 3 deletions src/files/files.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export class FilesService {
return `This action returns all files`;
}

findFileById(id: string) {
this.logger.log(`Finding file ${id}`);
return `This action returns a #${id} file`;
async findFileByName(fileName: string, bucketName: BucketName) {
this.logger.log(`Finding file ${fileName}`);
return await this.minioClient.getFile(fileName, bucketName);
}

updateFile(id: string, updateFileDto: UpdateFileDto) {
Expand Down
16 changes: 9 additions & 7 deletions src/minio-client/minio-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SimpleCreateFileDto } from '../files/dto/simple-create-file.dto';
import * as crypto from 'crypto';
import * as Minio from 'minio';
import { ConfigService } from '@nestjs/config';
import { Readable } from 'stream';

export enum BucketName {
Resources = 'resources',
Expand Down Expand Up @@ -67,13 +68,14 @@ export class MinioClientService {
return fileName;
}

async getFile(originalFileName: string, bucketName: BucketName) {
this.logger.log(`Getting file ${originalFileName}`);
return this.client.getObject(bucketName, originalFileName, (err, data) => {
if (err)
throw new BadRequestException('An error occured when getting file!');
data;
});
async getFile(fileName: string, bucketName: BucketName): Promise<Readable> {
this.logger.log(`Getting file ${fileName}`);
try {
const file = await this.client.getObject(bucketName, fileName);
return file;
} catch (error) {
throw new BadRequestException('An error occured when getting file!');
}
}

async delete(objetName: string, bucketName: BucketName) {
Expand Down
6 changes: 6 additions & 0 deletions src/playlists/interfaces/playlist-search-body.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface IReleaseSearchBody {
id: string;
title: string;
}

export default IReleaseSearchBody;
12 changes: 12 additions & 0 deletions src/playlists/interfaces/playlist-search-result.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import IPlaylistSearchBody from '../../releases/interfaces/release-search-body.interface';

interface IPlaylistSearchResult {
hits: {
total: number;
hits: Array<{
_source: IPlaylistSearchBody;
}>;
};
}

export default IPlaylistSearchResult;
98 changes: 98 additions & 0 deletions src/playlists/playlists-search.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { Injectable, Logger } from '@nestjs/common';
import { ElasticsearchService } from '@nestjs/elasticsearch';
import IPlaylistSearchBody from '../releases/interfaces/release-search-body.interface';
import { ISearchService } from '../search/interfaces/search.service.interface';
import { PlaylistDocument } from './schemas/playlist.schema';

@Injectable()
export default class PlaylistsSearchService
implements ISearchService<PlaylistDocument>
{
index = 'playlist';
private readonly logger: Logger = new Logger(PlaylistsSearchService.name);

constructor(private readonly elasticsearchService: ElasticsearchService) {}

async insertIndex(playlist: PlaylistDocument): Promise<any> {
this.logger.log(`Inserting user ID "${playlist._id}"`);
return await this.elasticsearchService.index<IPlaylistSearchBody>({
index: this.index,
body: {
id: playlist._id,
title: playlist.title,
},
});
}
async searchIndex(text: string): Promise<any[]> {
this.logger.log(`Searching "${text}"`);

const { hits } =
await this.elasticsearchService.search<IPlaylistSearchBody>({
index: this.index,
body: {
query: {
bool: {
should: [
{
match: {
title: {
query: text,
fuzziness: 'AUTO',
},
},
},
{
match_phrase_prefix: {
title: {
query: text,
},
},
},
],
},
},
},
});
return hits.hits.map((item) => item._source);
}
updateIndex(playlist: PlaylistDocument): Promise<any> {
this.logger.log(`Searching "${playlist._id}"`);

const newBody: IPlaylistSearchBody = {
id: playlist._id,
title: playlist.title,
};

const script: string = Object.entries(newBody).reduce(
(result, [key, value]) => {
return `${result} ctx._source.${key}='${value}';`;
},
'',
);

return this.elasticsearchService.updateByQuery({
index: this.index,
body: {
query: {
match: {
id: playlist._id,
},
},
script: script,
},
});
}
deleteIndex(id: string): void {
this.logger.log(`Deleting user "${id}" index `);
this.elasticsearchService.deleteByQuery({
index: this.index,
body: {
query: {
match: {
id: id,
},
},
},
});
}
}
36 changes: 6 additions & 30 deletions src/playlists/playlists.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { FilesService } from '../files/files.service';
import RepoMockModel, {
data2list,
} from '../test-utils/mocks/standard-mock.service.test';
import { PlaylistsService } from './playlists.service';
import { TracksService } from '../tracks/tracks.service';
import { getModelToken } from '@nestjs/mongoose';
import { User } from '../users/schemas/user.schema';
Expand All @@ -24,6 +23,9 @@ import { Track } from '../tracks/schemas/track.schema';
import { MinioServiceMock } from '../test-utils/mocks/minio.service.test';
import { UserSearchServiceMock } from '../test-utils/mocks/users-search.service.test';
import { PaymentServiceMock } from '../test-utils/mocks/payment.service.test';
import { TrackSearchServiceMock } from '../test-utils/mocks/tracks-search.service.test';
import { PlaylistsSearchServiceMock } from '../test-utils/mocks/playlists-search.service.test';
import { PlaylistsServiceMock } from '../test-utils/mocks/playlists.service.test';

const playlists = data2list(data.playlists);

Expand Down Expand Up @@ -64,35 +66,7 @@ describe('PlaylistsController', () => {
FilesService,
UsersService,
ConfigService,
{
provide: PlaylistsService,
useValue: {
createPlaylist: jest.fn(() => {
return {
...create_expected,
};
}),
findAllPlaylists: jest.fn(() => {
return playlists;
}),
findPlaylistById: jest.fn(() => {
return {
...playlist1,
};
}),
updatePlaylist: jest.fn(() => {
return {};
}),
removePlaylist: jest.fn(() => {
return {
...delete_expected,
};
}),
find: jest.fn(() => {
return playlists;
}),
},
},
PlaylistsServiceMock,
MinioServiceMock,
PaymentServiceMock,
{
Expand All @@ -104,6 +78,8 @@ describe('PlaylistsController', () => {
useValue: new TracksRepoMockModel(data.tracks),
},
UserSearchServiceMock,
TrackSearchServiceMock,
PlaylistsSearchServiceMock,
],
})
.overrideGuard(JwtAuthGuard)
Expand Down
9 changes: 9 additions & 0 deletions src/playlists/playlists.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ export class PlaylistsController {
return this.playlistsService.find(title);
}

@Get('/search')
@UseGuards(JwtAuthGuard)
@ApiCookieAuth('Set-Cookie')
@ApiOperation({ summary: 'Search user' })
searchUsers(@Query('search') search: string) {
if (search) return this.playlistsService.searchPlaylist(search);
return [];
}

@Get(':id')
@UseGuards(JwtAuthGuard)
@ApiCookieAuth('Set-Cookie')
Expand Down
8 changes: 5 additions & 3 deletions src/playlists/playlists.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { PlaylistsService } from './playlists.service';
import { PlaylistsController } from './playlists.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { Playlist, PlaylistSchema } from './schemas/playlist.schema';
import { TracksService } from '../tracks/tracks.service';
import { Track, TrackSchema } from '../tracks/schemas/track.schema';
import { User, UserSchema } from '../users/schemas/user.schema';
import { TracksModule } from '../tracks/tracks.module';
import { FilesService } from '../files/files.service';
import { MinioClientService } from '../minio-client/minio-client.service';
import { PaymentsService } from '../payments/payments.service';
import UsersModule from '../users/users.module';
import { SearchModule } from '../search/search.module';
import PlaylistsSearchService from './playlists-search.service';
@Module({
imports: [
MongooseModule.forFeature([
Expand All @@ -20,15 +21,16 @@ import UsersModule from '../users/users.module';
]),
TracksModule,
UsersModule,
SearchModule,
],
controllers: [PlaylistsController],
providers: [
PlaylistsService,
TracksService,
PlaylistsSearchService,
MinioClientService,
FilesService,
PaymentsService,
],
exports: [PlaylistsService],
exports: [PlaylistsService, PlaylistsSearchService],
})
export class PlaylistsModule {}
Loading