Skip to content

Commit

Permalink
Merge pull request #104 from boostcampwm2023/server/feature/homeMusic
Browse files Browse the repository at this point in the history
홈화면 가장 최근 업로드 된 음악 불러오기
  • Loading branch information
sk000801 authored Nov 16, 2023
2 parents 43decfc + 412f175 commit b05d69c
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 8 deletions.
11 changes: 11 additions & 0 deletions server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@ export const fileSize: Record<string, number> = {
MUSIC_FILE_LIMIT_SIZE: 1024 * 1024 * 50,
IMAGE_FILE_LIMIT_SIZE: 1024 * 1024 * 5,
};

export enum Genres {
'hip-hop' = 'hip-hop',
'acoustic' = 'acoustic',
'ballade' = 'ballade',
'r&b' = 'r&b',
'jazz' = 'jazz',
'rock' = 'rock',
'dance' = 'dance',
'etc' = 'etc',
}
8 changes: 7 additions & 1 deletion server/src/dto/musicCreate.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { IsNotEmpty, IsString } from 'class-validator';
import { IsNotEmpty, IsString, MaxLength } from 'class-validator';
import { Genres } from 'src/constants';

export class MusicCreateDto {
@IsNotEmpty()
@IsString()
@MaxLength(50, { message: '글자 수가 50을 넘어갔습니다.' })
title: string;

@IsNotEmpty()
Expand All @@ -12,4 +14,8 @@ export class MusicCreateDto {
@IsNotEmpty()
@IsString()
file: string;

@IsNotEmpty()
@IsString()
genre: Genres;
}
8 changes: 6 additions & 2 deletions server/src/entity/music.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {
Entity,
CreateDateColumn,
BaseEntity,
OneToOne,
JoinColumn,
PrimaryGeneratedColumn,
ManyToOne,
} from 'typeorm';
import { User } from './user.entity';
import { Genres } from 'src/constants';

@Entity({ name: 'music' })
export class Music extends BaseEntity {
Expand All @@ -26,10 +27,13 @@ export class Music extends BaseEntity {
@Column()
musicFile: string;

@Column()
genre: Genres;

@CreateDateColumn()
created_at: Date;

@OneToOne(() => User)
@ManyToOne(() => User)
@JoinColumn({ name: 'user_id' })
user_id: string;
}
1 change: 1 addition & 0 deletions server/src/httpStatusCode.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export enum HTTP_STATUS_CODE {
'DUPLICATED_NICKNAME' = 409,
'WRONG TOKEN' = 401,
'SERVER_ERROR' = 500,
'BAD_REQUEST' = 400,
}
24 changes: 23 additions & 1 deletion server/src/music/music.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
HttpCode,
HttpException,
Post,
Get,
UseGuards,
UsePipes,
ValidationPipe,
Expand All @@ -13,6 +14,8 @@ import { MusicService } from './music.service';
import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum';
import { MusicCreateDto } from 'src/dto/musicCreate.dto';
import { AuthGuard } from '@nestjs/passport';
import { Genres } from 'src/constants';
import { Music } from 'src/entity/music.entity';

@Controller('musics')
export class MusicController {
Expand All @@ -22,7 +25,10 @@ export class MusicController {
@UsePipes(ValidationPipe)
@UseGuards(AuthGuard())
@HttpCode(HTTP_STATUS_CODE.SUCCESS)
async upload(@Body() musicCreateDto: MusicCreateDto, @Req() req) {
async upload(
@Body() musicCreateDto: MusicCreateDto,
@Req() req,
): Promise<{ userId: string }> {
try {
const userId = req.user.user_id;

Expand All @@ -36,4 +42,20 @@ export class MusicController {
throw new HttpException('WRONG TOKEN', HTTP_STATUS_CODE['WRONG TOKEN']);
}
}

@Get('recent-uploads')
@HttpCode(HTTP_STATUS_CODE.SUCCESS)
async getRecentMusics(): Promise<{ musics: Music[] }> {
const musics = await this.musicService.getRecentMusic();

return { musics };
}

@Get('genres')
@HttpCode(HTTP_STATUS_CODE.SUCCESS)
getGenres(): { genres: string[] } {
const genreName: string[] = Object.keys(Genres);

return { genres: genreName };
}
}
41 changes: 39 additions & 2 deletions server/src/music/music.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { MusicCreateDto } from 'src/dto/musicCreate.dto';
import { Music } from 'src/entity/music.entity';
import { Genres } from 'src/constants';

@Injectable()
export class MusicService {
Expand All @@ -12,18 +13,54 @@ export class MusicService {
@InjectRepository(Music) private musicRepository: Repository<Music>,
) {}

createMusic(musicCreateDto: MusicCreateDto, user_id: string) {
isValidGenre(genre: string): boolean {
if (Object.values(Genres).includes(genre as Genres)) {
return true;
}

return false;
}

createMusic(musicCreateDto: MusicCreateDto, user_id: string): void {
try {
const { title, cover, file: musicFile } = musicCreateDto;
const { title, cover, file: musicFile, genre } = musicCreateDto;

if (!this.isValidGenre(genre)) {
throw new HttpException(
'NOT_EXIST_GENRE',
HTTP_STATUS_CODE.BAD_REQUEST,
);
}

const newMusic: Music = this.musicRepository.create({
title,
cover,
musicFile,
created_at: new Date(),
genre,
user_id,
});

this.musicRepository.save(newMusic);
} catch (err) {
if (err instanceof HttpException) {
throw err;
}

throw new HttpException('SERVER ERROR', HTTP_STATUS_CODE.SERVER_ERROR);
}
}

async getRecentMusic(): Promise<Music[]> {
try {
const musics = await this.musicRepository.find({
order: {
created_at: 'DESC',
},
take: 10,
});

return musics;
} catch {
throw new HttpException('SERVER ERROR', HTTP_STATUS_CODE.SERVER_ERROR);
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/upload/upload.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class UploadController {
}),
)
file: Express.Multer.File,
) {
): Promise<{ url: string }> {
const { url } = await this.uploadService.uploadMusic(file);
return { url };
}
Expand All @@ -45,7 +45,7 @@ export class UploadController {
}),
)
file: Express.Multer.File,
) {
): Promise<{ url: string }> {
const { url } = await this.uploadService.uploadImage(file);
return { url };
}
Expand Down

0 comments on commit b05d69c

Please sign in to comment.