Skip to content

Commit

Permalink
✨ add all features
Browse files Browse the repository at this point in the history
  • Loading branch information
KMUlee committed Apr 25, 2024
1 parent 21ac6f4 commit 1db828e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/likes/entities/likes.entity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ApiProperty } from '@nestjs/swagger';
import { Exclude } from 'class-transformer';
import { User } from 'src/user/entities/user.entity';
import {
Expand All @@ -12,12 +13,21 @@ import {
@Entity()
export class Likes {
@PrimaryGeneratedColumn()
@Exclude()
id: number;

@ManyToOne(() => User, (user) => user.likesList)
@ApiProperty({
type: () => User,
})
user: User;

@Column()
@ApiProperty({
example: 1,
description: 'Profile ID',
type: 'number',
})
profileId: number;

@CreateDateColumn({ type: 'timestamptz' })
Expand Down
31 changes: 29 additions & 2 deletions src/likes/likes.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
import { Controller } from '@nestjs/common';
import { Controller, Param, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiParam, ApiResponse } from '@nestjs/swagger';
import { JwtAuthGuard } from 'src/user/user.guard';
import { LikesService } from './likes.service';
import { GetUser } from 'src/user/decorators/GetUser.decorator';
import { Payload } from 'src/user/dto/jwt-payload.dto';
import { Likes } from './entities/likes.entity';

@Controller('likes')
export class LikesController {}
@UseGuards(JwtAuthGuard)
@ApiBearerAuth('access-token')
export class LikesController {
constructor(private readonly likesService: LikesService) {}
@Post('/:profileId')
@ApiParam({
name: 'profileId',
type: 'number',
description: 'Profile ID',
})
@ApiResponse({
status: 201,
description: 'Like a profile',
type: Likes,
})
async likeProfile(
@Param('profileId') profileId: number,
@GetUser() user: Payload,
): Promise<Likes> {
return this.likesService.likeProfile(profileId, user.userId);
}
}
7 changes: 5 additions & 2 deletions src/likes/likes.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { LikesController } from './likes.controller';
import { LikesService } from './likes.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Likes } from './entities/likes.entity';
import { JwtService } from '@nestjs/jwt';
import { JwtStrategy } from 'src/user/strategies/jwt.strategy';
import { UserModule } from 'src/user/user.module';

@Module({
imports: [TypeOrmModule.forFeature([Likes])],
imports: [TypeOrmModule.forFeature([Likes]), UserModule],
controllers: [LikesController],
providers: [LikesService],
providers: [LikesService, JwtService, JwtStrategy],
exports: [LikesService],
})
export class LikesModule {}
18 changes: 17 additions & 1 deletion src/likes/likes.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Likes } from './entities/likes.entity';
import { Repository } from 'typeorm';
import { UserService } from 'src/user/user.service';
import { User } from 'src/user/entities/user.entity';

@Injectable()
export class LikesService {
constructor(
@InjectRepository(Likes)
private readonly likesRepository: Repository<Likes>,
private readonly userService: UserService,
) {}
async getLikedUserIdList(profileId: number): Promise<number[] | []> {
const likesList = await this.likesRepository.find({
Expand All @@ -16,4 +19,17 @@ export class LikesService {
});
return likesList.map((like) => like.user.id);
}

async likeProfile(profileId: number, userId: number): Promise<Likes> {
const user: User = await this.userService.findOneById(userId);
const newLike: Likes = this.likesRepository.create({
profileId,
user,
});
try {
return await this.likesRepository.save(newLike);
} catch (error) {
throw new HttpException('Error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

0 comments on commit 1db828e

Please sign in to comment.