forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
284 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class LikesDto {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,19 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Likes } from './entities/likes.entity'; | ||
import { Repository } from 'typeorm'; | ||
|
||
@Injectable() | ||
export class LikesService {} | ||
export class LikesService { | ||
constructor( | ||
@InjectRepository(Likes) | ||
private readonly likesRepository: Repository<Likes>, | ||
) {} | ||
async getLikedUserIdList(profileId: number): Promise<number[] | []> { | ||
const likesList = await this.likesRepository.find({ | ||
where: { profileId }, | ||
relations: ['user'], | ||
}); | ||
return likesList.map((like) => like.user.id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Type } from 'class-transformer'; | ||
import { IsNotEmpty, IsString } from 'class-validator'; | ||
|
||
export class ProfileDto { | ||
@IsNotEmpty() | ||
@IsString() | ||
readonly name: string; | ||
|
||
@IsNotEmpty() | ||
@IsString() | ||
readonly title: string; | ||
|
||
@IsNotEmpty() | ||
@IsString() | ||
readonly description: string; | ||
|
||
@IsNotEmpty() | ||
@IsString() | ||
readonly githubLink: string; | ||
} | ||
|
||
export class GetProfileDto extends ProfileDto { | ||
@Type(() => ProfileDto) | ||
likedByUsers: ProfileDto[] | []; | ||
|
||
@Type(() => ProfileDto) | ||
likedProjects: ProfileDto[] | []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,41 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; | ||
import { Exclude } from 'class-transformer'; | ||
import { User } from 'src/user/entities/user.entity'; | ||
import { | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
JoinColumn, | ||
OneToOne, | ||
PrimaryGeneratedColumn, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
|
||
@Entity() | ||
class ProfileEntity { | ||
export class Profile { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@OneToOne(() => User, (user) => user.profile) | ||
@JoinColumn({ name: 'userId' }) | ||
user: User; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@Column() | ||
description: string; | ||
|
||
@Column() | ||
githubLink: string; | ||
|
||
@CreateDateColumn({ type: 'timestamptz' }) | ||
@Exclude() | ||
createdAt: Date; | ||
|
||
@UpdateDateColumn({ type: 'timestamptz' }) | ||
@Exclude() | ||
updatedAt: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,59 @@ | ||
import { Controller } from '@nestjs/common'; | ||
import { | ||
Body, | ||
Controller, | ||
ForbiddenException, | ||
Get, | ||
Param, | ||
Post, | ||
Put, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { ProfileService } from './profile.service'; | ||
import { JwtAuthGuard } from 'src/user/user.guard'; | ||
import { GetUser } from 'src/user/decorators/GetUser.decorator'; | ||
import { Payload } from 'src/user/dto/jwt-payload.dto'; | ||
import { ApiBearerAuth } from '@nestjs/swagger'; | ||
import { GetProfileDto, ProfileDto } from './dto/profile.dto'; | ||
|
||
@Controller('profile') | ||
export class ProfileController {} | ||
@UseGuards(JwtAuthGuard) | ||
@ApiBearerAuth('access-token') | ||
export class ProfileController { | ||
constructor(private readonly profileService: ProfileService) {} | ||
|
||
@Get('/:id') | ||
async getProfile( | ||
@Param('id') id: number, | ||
@GetUser() user: Payload, | ||
): Promise<{ profile: GetProfileDto | null }> { | ||
if (id !== user.userId) { | ||
throw new ForbiddenException( | ||
'You do not have permission to access this profile', | ||
); | ||
} | ||
return { profile: await this.profileService.getProfile(id) }; | ||
} | ||
|
||
@Post('/:id') | ||
async createProfile( | ||
@Param('id') id: number, | ||
@GetUser() user: Payload, | ||
@Body() newProfile: ProfileDto, | ||
): Promise<ProfileDto> { | ||
if (id !== user.userId) { | ||
throw new ForbiddenException( | ||
'You do not have permission to create a profile for this user', | ||
); | ||
} | ||
return await this.profileService.createProfile(id, newProfile); | ||
} | ||
|
||
@Put('/:id') | ||
async updateProfile( | ||
@Param('id') id: number, | ||
@GetUser() user: Payload, | ||
@Body() profile: ProfileDto, | ||
) { | ||
return await this.profileService.updateProfile(id, profile); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ProfileController } from './profile.controller'; | ||
import { ProfileService } from './profile.service'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Profile } from './entities/profile.entity'; | ||
import { JwtStrategy } from 'src/user/strategies/jwt.strategy'; | ||
import { UserModule } from 'src/user/user.module'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { LikesModule } from 'src/likes/likes.module'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Profile]), UserModule, LikesModule], | ||
controllers: [ProfileController], | ||
providers: [ProfileService], | ||
providers: [ProfileService, JwtService, ConfigService, JwtStrategy], | ||
}) | ||
export class ProfileModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,99 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Profile } from './entities/profile.entity'; | ||
import { Repository } from 'typeorm'; | ||
import { GetProfileDto, ProfileDto } from './dto/profile.dto'; | ||
import { UserService } from 'src/user/user.service'; | ||
import { LikesService } from 'src/likes/likes.service'; | ||
import { User } from 'src/user/entities/user.entity'; | ||
|
||
@Injectable() | ||
export class ProfileService {} | ||
export class ProfileService { | ||
constructor( | ||
@InjectRepository(Profile) | ||
private readonly profileRepository: Repository<Profile>, | ||
private readonly userService: UserService, | ||
private readonly likesService: LikesService, | ||
) {} | ||
|
||
async getProfile(userId: number): Promise<GetProfileDto | null> { | ||
const profile = await this.profileRepository.findOne({ | ||
where: { | ||
user: { | ||
id: userId, | ||
}, | ||
}, | ||
relations: ['user'], | ||
}); | ||
|
||
if (!profile) { | ||
return null; | ||
} | ||
|
||
const likedProjects: ProfileDto[] = []; | ||
const likedList = await this.userService.getLikedList(userId); | ||
likedList.forEach(async (id) => { | ||
const likedProfile = await this.profileRepository.findOneBy({ id }); | ||
likedProjects.push(likedProfile); | ||
}); | ||
const likedByUsers: ProfileDto[] = []; | ||
const likedUserIdList = await this.likesService.getLikedUserIdList( | ||
profile.id, | ||
); | ||
likedUserIdList.forEach(async (id) => { | ||
const likedProfile = await this.profileRepository.findOne({ | ||
where: { | ||
user: { | ||
id, | ||
}, | ||
}, | ||
relations: ['user'], | ||
}); | ||
likedByUsers.push(likedProfile); | ||
}); | ||
const profileData: GetProfileDto = { | ||
...profile, | ||
likedProjects, | ||
likedByUsers, | ||
}; | ||
return profileData; | ||
} | ||
|
||
async createProfile( | ||
userId: number, | ||
profileData: ProfileDto, | ||
): Promise<ProfileDto> { | ||
const user: User = await this.userService.findOneById(userId); | ||
try { | ||
const profile = this.profileRepository.create({ | ||
...profileData, | ||
user, | ||
}); | ||
return await this.profileRepository.save(profile); | ||
} catch (error) { | ||
throw new HttpException( | ||
'Failed to create profile. Please try again later.', | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
); | ||
} | ||
} | ||
|
||
async updateProfile(userId: number, profileData: ProfileDto) { | ||
const profile = await this.profileRepository.findOne({ | ||
where: { | ||
user: { | ||
id: userId, | ||
}, | ||
}, | ||
}); | ||
const updatedProfile: Profile = { ...profile, ...profileData }; | ||
try { | ||
return await this.profileRepository.save(updatedProfile); | ||
} catch (error) { | ||
throw new HttpException( | ||
'Failed to update profile. Please try again later.', | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ExecutionContext, createParamDecorator } from '@nestjs/common'; | ||
import { Payload } from '../dto/jwt-payload.dto'; | ||
|
||
export const GetUser = createParamDecorator( | ||
(data: unknown, ctx: ExecutionContext): Payload => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
return request.user; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('jwt') {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.