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

[Server] #20 팔로잉 요청 수행 #44

Merged
merged 4 commits into from
Nov 15, 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
6 changes: 5 additions & 1 deletion server/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"endOfLine": "lf",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"trailingComma": "all"
"trailingComma": "all",
"printWidth": 100
}
2 changes: 1 addition & 1 deletion server/apps/api/src/api.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Controller, Get } from '@nestjs/common';
import { ApiService } from './api.service';

@Controller()
@Controller('api')
export class ApiController {
constructor(private readonly apiService: ApiService) {}

Expand Down
15 changes: 15 additions & 0 deletions server/apps/api/src/api.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { ConfigModule } from '@nestjs/config';
import { ChannelModule } from './channel/channel.module';
import { CommunityModule } from './community/community.module';
import { UserModule } from './user/user.module';
import * as winston from 'winston';
import { utilities as nestWinstonModuleUtilities, WinstonModule } from 'nest-winston';
import { AuthModule } from './auth/auth.module';

@Module({
Expand All @@ -15,6 +17,19 @@ import { AuthModule } from './auth/auth.module';
envFilePath: `config/${process.env.NODE_ENV}.env`,
}),
MongooseModule.forRoot(process.env.MONGODB_URL),
WinstonModule.forRoot({
transports: [
new winston.transports.Console({
level: process.env.NODE_ENV === 'production' ? 'info' : 'silly',
format: winston.format.combine(
winston.format.colorize({ all: true }),
winston.format.simple(),
winston.format.timestamp(),
nestWinstonModuleUtilities.format.nestLike('Asnity', { prettyPrint: true }),
),
}),
],
}),
UserModule,
ChannelModule,
CommunityModule,
Expand Down
2 changes: 2 additions & 0 deletions server/apps/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { NestFactory } from '@nestjs/core';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { ApiModule } from './api.module';

async function bootstrap() {
const app = await NestFactory.create(ApiModule);
app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));
await app.listen(3000);
}
bootstrap();
9 changes: 9 additions & 0 deletions server/apps/api/src/user/dto/add-following.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IsString } from 'class-validator';

export class AddFollowingDto {
@IsString()
myId: string;

@IsString()
followId: string;
}
46 changes: 32 additions & 14 deletions server/apps/api/src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { User } from '@schemas/user.schema';
import { Body, Controller, Get, Inject, LoggerService, Param, Post } from '@nestjs/common';
import { UserService } from './user.service';
import { AddFollowingDto } from '@user/dto/add-following.dto';
import { SucessRes } from '@utils/def';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { User } from '@schemas/user.schema';
import { CreateUserDto } from '@user/dto/create-user.dto';
import { responseForm } from '@utils/responseForm';

@Controller('user')
@Controller('api/user')
export class UserController {
constructor(private userService: UserService) {}
constructor(
@Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: LoggerService,
private userService: UserService,
) {}

@Get()
getUsers() {
const createUserDto: CreateUserDto = { id: 'mj', pw: 'mjpw' };
this.userService.createUser(createUserDto);
return 'hello user';
}

@Post('following/:id')
async addFollowing(@Param('id') id: string) {
try {
const myId = '63734e98384f478a32c3a1cc';
// TODO: Request Header에서 access token으로 현재 사용자 알아내기
const addFollowingDto: AddFollowingDto = { myId, followId: id };
await this.userService.addFollowing(addFollowingDto);
return responseForm(200, {});
} catch (error) {
this.logger.error(JSON.stringify(error.response));
return error.response;
}
}

// @Get()
// getUsers() {
// const createUserDto: CreateUserDto = { id: 'ny', pw: 'nypw' };
// this.userService.createUser(createUserDto);
// return 'hello user';
// }
// @Get('followers/:id')
// getFollowers(@Param('id') id: string): User[] {
// return this.usersService.getAllFollowers();
// }

// @Post()
// createUser(@Body() createUserDto: CreateUserDto) {
Expand Down
6 changes: 2 additions & 4 deletions server/apps/api/src/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import { Module } from '@nestjs/common';
import { UserController } from '@user/user.controller';
import { UserService } from '@user/user.service';
import { MongooseModule } from '@nestjs/mongoose';
import { WinstonModule } from 'nest-winston';
import { User, UserSchema } from '@schemas/user.schema';
import { AuthModule } from '../auth/auth.module';
import { UserRepository } from '@repository/user.repository';
import { AuthController } from '../auth/auth.controller';

@Module({
imports: [
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]),
AuthModule,
],
imports: [MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), AuthModule],
controllers: [UserController],
providers: [UserService, UserRepository],
})
Expand Down
24 changes: 19 additions & 5 deletions server/apps/api/src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BadRequestException, ConflictException, Injectable } from '@nestjs/common';
import { AddFollowingDto } from '@user/dto/add-following.dto';
import { Injectable } from '@nestjs/common';
import { CreateUserDto } from '@user/dto/create-user.dto';
import { UserRepository } from '@repository/user.repository';
Expand All @@ -6,9 +8,21 @@ import { UserRepository } from '@repository/user.repository';
export class UserService {
constructor(private readonly userRepository: UserRepository) {}

// getAllFollowers() {}
//
// createUser(createUserDto: CreateUserDto) {
// this.userRepository.create(createUserDto);
// }
createUser(createUserDto: CreateUserDto) {
this.userRepository.create(createUserDto);
}

async addFollowing(addFollowingDto: AddFollowingDto) {
const user = await this.userRepository.findById(addFollowingDto.myId);
const otherUser = await this.userRepository.findById(addFollowingDto.followId);
if (!user || !otherUser) {
throw new BadRequestException('해당하는 사용자의 _id가 올바르지 않습니다.');
} else if (user.followings.includes(addFollowingDto.followId)) {
throw new BadRequestException('팔로우 요청한 사용자는 이미 팔로우되어있습니다.');
} else if (otherUser.followers.includes(addFollowingDto.myId)) {
throw new ConflictException('상대방의 팔로워 목록에 이미 있습니다.');
}
this.userRepository.appendFollowing(addFollowingDto);
this.userRepository.appendFollwer(addFollowingDto);
}
}
29 changes: 28 additions & 1 deletion server/dao/repository/user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { User, UserDocument } from '@schemas/user.schema';
import { Model } from 'mongoose';
import mongoose, { Model, Schema } from 'mongoose';
import { AddFollowingDto } from '@user/dto/add-following.dto';
import { SignUpDto } from '@api/src/auth/dto';

@Injectable()
Expand All @@ -12,4 +13,30 @@ export class UserRepository {
await this.userModel.create(createUserDto);
return null;
}

async findById(id: string) {
return await this.userModel.findById(id);
}

appendFollowing(addFollowingDto: AddFollowingDto) {
this.userModel.updateOne(
{ _id: addFollowingDto.myId },
{ $push: { followings: addFollowingDto.followId } },
(err, res) => {
if (err) throw err;
},
);
}

appendFollwer(addFollowingDto: AddFollowingDto) {
this.userModel.updateOne(
{ _id: addFollowingDto.followId },
{
$push: { followers: addFollowingDto.myId },
},
(err, res) => {
if (err) throw err;
},
);
}
}
4 changes: 3 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@
"eslint-plugin-prettier": "^4.0.0",
"jest": "28.1.3",
"mongoose": "^6.7.2",
"nest-winston": "^1.8.0",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "28.0.8",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.1.0",
"typescript": "^4.7.4"
"typescript": "^4.7.4",
"winston": "^3.8.2"
},
"jest": {
"moduleFileExtensions": [
Expand Down