Skip to content

Commit

Permalink
Merge pull request #290 from boostcampwm2023/server/feature/287
Browse files Browse the repository at this point in the history
Recent_Played 테이블 생성 + API 수정
  • Loading branch information
khw3754 authored Dec 6, 2023
2 parents a54d582 + 15f99b4 commit 518ccca
Show file tree
Hide file tree
Showing 19 changed files with 264 additions and 214 deletions.
7 changes: 3 additions & 4 deletions server/src/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { getRepositoryToken } from '@nestjs/typeorm';
import { User } from 'src/entity/user.entity';
import { Repository } from 'typeorm';
import { JwtModule, JwtService } from '@nestjs/jwt';
import { PlaylistService } from 'src/playlist/playlist.service';
import { Playlist } from 'src/entity/playlist.entity';
import { Music } from 'src/entity/music.entity';
import { Music_Playlist } from 'src/entity/music_playlist.entity';
import { Logger, LoggerService } from '@nestjs/common';
import { Logger } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';

describe('AuthController', () => {
let controller: AuthController;
Expand All @@ -19,7 +19,7 @@ describe('AuthController', () => {

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [JwtModule],
imports: [PassportModule.register({ defaultStrategy: 'jwt' }), JwtModule],
controllers: [AuthController],
providers: [
Logger,
Expand All @@ -40,7 +40,6 @@ describe('AuthController', () => {
provide: getRepositoryToken(Music_Playlist),
useClass: Repository,
},
PlaylistService,
],
}).compile();

Expand Down
8 changes: 2 additions & 6 deletions server/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from 'src/entity/user.entity';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { PlaylistService } from 'src/playlist/playlist.service';
import { Playlist } from 'src/entity/playlist.entity';
import { Music } from 'src/entity/music.entity';
import { Music_Playlist } from 'src/entity/music_playlist.entity';
import { Logger } from 'winston';

@Module({
Expand All @@ -24,9 +20,9 @@ import { Logger } from 'winston';
}),
inject: [ConfigService],
}),
TypeOrmModule.forFeature([User, Playlist, Music, Music_Playlist]),
TypeOrmModule.forFeature([User]),
],
providers: [JwtStrategy, AuthService, PlaylistService, Logger],
providers: [JwtStrategy, AuthService, Logger],
exports: [JwtStrategy, PassportModule],
controllers: [AuthController],
})
Expand Down
3 changes: 2 additions & 1 deletion server/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { PlaylistService } from 'src/playlist/playlist.service';
import { Playlist } from 'src/entity/playlist.entity';
import { Music } from 'src/entity/music.entity';
import { Music_Playlist } from 'src/entity/music_playlist.entity';
import { PassportModule } from '@nestjs/passport';

describe('AuthService', () => {
let service: AuthService;
Expand All @@ -17,7 +18,7 @@ describe('AuthService', () => {

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [JwtModule],
imports: [PassportModule.register({ defaultStrategy: 'jwt' }), JwtModule],
providers: [
AuthService,
{
Expand Down
6 changes: 0 additions & 6 deletions server/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { JwtService } from '@nestjs/jwt';
import { InjectRepository } from '@nestjs/typeorm';
import { CatchyException } from 'src/config/catchyException';
import { ERROR_CODE } from 'src/config/errorCode.enum';
import { RECENT_PLAYLIST_NAME } from 'src/constants';
import { UserCreateDto } from 'src/dto/userCreate.dto';
import { User } from 'src/entity/user.entity';
import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum';
import { PlaylistService } from 'src/playlist/playlist.service';
import { Repository } from 'typeorm';
import { v4 as uuid } from 'uuid';

Expand All @@ -17,7 +15,6 @@ export class AuthService {
constructor(
@InjectRepository(User) private userRepository: Repository<User>,
private jwtService: JwtService,
private readonly playlistService: PlaylistService,
) {}

async login(email: string): Promise<{ accessToken: string }> {
Expand Down Expand Up @@ -62,9 +59,6 @@ export class AuthService {
});
await this.userRepository.save(newUser);

this.playlistService.createPlaylist(newUser.user_id, {
title: RECENT_PLAYLIST_NAME,
});
return this.login(email);
}
this.logger.error(`auth.service - signup : WRONG_TOKEN`);
Expand Down
1 change: 0 additions & 1 deletion server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export enum Genres {
'etc' = 'etc',
}

export const RECENT_PLAYLIST_NAME = '최근 재생 목록';
export const keyFlags = ['user', 'cover'];

export const contentTypeHandler: Record<string, string> = {
Expand Down
4 changes: 4 additions & 0 deletions server/src/entity/music.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { User } from './user.entity';
import { Genres } from 'src/constants';
import { Music_Playlist } from './music_playlist.entity';
import { Recent_Played } from './recent_played.entity';

@Entity({ name: 'music' })
export class Music extends BaseEntity {
Expand Down Expand Up @@ -43,6 +44,9 @@ export class Music extends BaseEntity {
@OneToMany(() => Music_Playlist, (music_playlist) => music_playlist.music)
music_playlist: Music_Playlist[];

@OneToMany(() => Recent_Played, (recent_played) => recent_played.music)
recent_played: Recent_Played[];

static async getMusicListByUserId(
userId: string,
count: number,
Expand Down
39 changes: 3 additions & 36 deletions server/src/entity/music_playlist.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from 'typeorm';
import { Music } from './music.entity';
import { Playlist } from './playlist.entity';
import { RECENT_PLAYLIST_NAME } from 'src/constants';

@Entity({ name: 'music_playlist' })
export class Music_Playlist extends BaseEntity {
Expand All @@ -24,7 +23,7 @@ export class Music_Playlist extends BaseEntity {
playlist: Playlist;

@Column()
updated_at: Date;
created_at: Date;

static async getMusicListByPlaylistId(playlistId: number): Promise<Music[]> {
return this.find({
Expand All @@ -46,43 +45,11 @@ export class Music_Playlist extends BaseEntity {
music_playlist_id: false,
},
order: {
updated_at: 'DESC',
created_at: 'DESC',
},
}).then((a: Music_Playlist[]) => a.map((b) => b.music));
}

static async getRecentPlayedMusicByUserId(userId: string): Promise<Music[]> {
return await this.find({
relations: {
music: true,
},
where: {
playlist: {
playlist_title: RECENT_PLAYLIST_NAME,
},
music: {
user: {
user_id: userId,
},
},
},
select: {
music_playlist_id: false,
music: {
music_id: true,
title: true,
music_file: true,
cover: true,
genre: true,
},
},
order: {
updated_at: 'DESC',
},
take: 10,
}).then((a: Music_Playlist[]) => a.map((b) => b.music));
}

static async getMusicCountByPlaylistId(playlist_id: number): Promise<number> {
return this.count({ where: { playlist: { playlist_id } } });
}
Expand All @@ -94,7 +61,7 @@ export class Music_Playlist extends BaseEntity {
relations: { music: true },
select: { music: { cover: true } },
where: { playlist: { playlist_id } },
order: { updated_at: 'DESC' },
order: { created_at: 'DESC' },
});
}
}
61 changes: 61 additions & 0 deletions server/src/entity/recent_played.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
BaseEntity,
Column,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Music } from './music.entity';
import { User } from './user.entity';

@Entity({ name: 'recent_played' })
export class Recent_Played extends BaseEntity {
@PrimaryGeneratedColumn()
recent_played_id: number;

@ManyToOne(() => Music, (music) => music.recent_played)
@JoinColumn({ name: 'music_id' })
music: Music;

@ManyToOne(() => User, (user) => user.recent_played)
@JoinColumn({ name: 'user_id' })
user: User;

@Column()
played_at: Date;

static async getRecentPlayedMusicByUserId(
user_id: string,
count: number,
): Promise<Music[]> {
return await this.find({
relations: {
music: { user: true },
user: true,
},
where: {
user: {
user_id,
},
},
select: {
recent_played_id: false,
music: {
music_id: true,
title: true,
music_file: true,
cover: true,
genre: true,
user: { user_id: true, nickname: true },
},
},
order: {
played_at: 'DESC',
},
take: count,
}).then((recent_played: Recent_Played[]) =>
recent_played.map((recent) => recent.music),
);
}
}
4 changes: 4 additions & 0 deletions server/src/entity/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from 'typeorm';
import { Playlist } from './playlist.entity';
import { Music } from './music.entity';
import { Recent_Played } from './recent_played.entity';

@Entity({ name: 'user' })
export class User extends BaseEntity {
Expand All @@ -33,6 +34,9 @@ export class User extends BaseEntity {
@OneToMany(() => Playlist, (playlist) => playlist.user)
playlists: Playlist[];

@OneToMany(() => Recent_Played, (recent_played) => recent_played.user)
recent_played: Recent_Played[];

static async getCertainUserByNickname(keyword: string): Promise<User[]> {
return this.find({
relations: {
Expand Down
16 changes: 1 addition & 15 deletions server/src/music/music.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ import { ConfigService } from '@nestjs/config';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Music } from 'src/entity/music.entity';
import { realMusicCreateInfo, user } from 'test/constants/music.mockData';
import { user } from 'test/constants/music.mockData';
import { MusicController } from './music.controller';
import * as request from 'supertest';
import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum';
import { User } from 'src/entity/user.entity';
import { AuthService } from 'src/auth/auth.service';
import { JwtService } from '@nestjs/jwt';
import { PlaylistService } from 'src/playlist/playlist.service';
import { Playlist } from 'src/entity/playlist.entity';
import { Music_Playlist } from 'src/entity/music_playlist.entity';
import { AuthGuard } from '@nestjs/passport';
import { GreenEyeService } from 'src/config/greenEye.service';

Expand All @@ -40,7 +35,6 @@ describe('UploadController', () => {
ConfigService,
GreenEyeService,
AuthService,
PlaylistService,
JwtService,
{
provide: getRepositoryToken(Music),
Expand All @@ -50,14 +44,6 @@ describe('UploadController', () => {
provide: getRepositoryToken(User),
useClass: Repository,
},
{
provide: getRepositoryToken(Playlist),
useClass: Repository,
},
{
provide: getRepositoryToken(Music_Playlist),
useClass: Repository,
},
],
})
.overrideGuard(AuthGuard)
Expand Down
5 changes: 5 additions & 0 deletions server/src/music/music.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
newMusicData,
} from 'test/constants/music.mockData';
import { GreenEyeService } from 'src/config/greenEye.service';
import { Recent_Played } from 'src/entity/recent_played.entity';

describe('UploadController', () => {
let app: INestApplication;
Expand All @@ -38,6 +39,10 @@ describe('UploadController', () => {
provide: getRepositoryToken(Music),
useClass: Repository,
},
{
provide: getRepositoryToken(Recent_Played),
useClass: Repository,
},
],
}).compile();

Expand Down
37 changes: 2 additions & 35 deletions server/src/playlist/playlist.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
HttpCode,
Logger,
Param,
Patch,
Post,
Put,
Req,
UseGuards,
UsePipes,
Expand All @@ -22,9 +22,7 @@ import { Music } from 'src/entity/music.entity';
@Controller('playlists')
export class PlaylistController {
private readonly logger = new Logger('Playlist');
constructor(
private playlistService: PlaylistService,
) {}
constructor(private playlistService: PlaylistService) {}

@Post()
@UseGuards(AuthGuard())
Expand Down Expand Up @@ -90,35 +88,4 @@ export class PlaylistController {
const userId: string = req.user.user_id;
return await this.playlistService.getPlaylistMusics(userId, playlistId);
}

@Patch('recent-played')
@UseGuards(AuthGuard())
@HttpCode(HTTP_STATUS_CODE.SUCCESS)
async updateRecentPlayMusic(
@Req() req,
@Body('musicId') music_id: string,
): Promise<{ music_playlist_id: number }> {
this.logger.log(
`PATCH /playlists/recent-played - nickname=${req.user.nickname}, music_id=${music_id}`,
);
const user_id: string = req.user.user_id;
const recentPlaylist: Playlist =
await this.playlistService.getRecentPlaylist(user_id);
const recentPlaylistId: number = recentPlaylist.playlist_id;
if (await this.playlistService.isAlreadyAdded(recentPlaylistId, music_id)) {
return {
music_playlist_id: await this.playlistService.updateRecentMusic(
music_id,
recentPlaylistId,
),
};
}
return {
music_playlist_id: await this.playlistService.addMusicToPlaylist(
user_id,
recentPlaylistId,
music_id,
),
};
}
}
Loading

0 comments on commit 518ccca

Please sign in to comment.