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

Feature/unfollowing #53

Merged
merged 2 commits into from
Nov 16, 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IsString } from 'class-validator';

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

Expand Down
21 changes: 16 additions & 5 deletions server/apps/api/src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Body, Controller, Get, Inject, LoggerService, Param, Post } from '@nestjs/common';
import {Body, Controller, Delete, Get, Inject, LoggerService, Param, Post} from '@nestjs/common';
import { UserService } from './user.service';
import { AddFollowingDto } from '@user/dto/add-following.dto';
import { followerDto } from '@user/dto/follower.dto';
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('api/user')
Expand All @@ -25,7 +23,7 @@ export class UserController {
try {
const myId = '63734e98384f478a32c3a1cc';
// TODO: Request Headerμ—μ„œ access token으둜 ν˜„μž¬ μ‚¬μš©μž μ•Œμ•„λ‚΄κΈ°
const addFollowingDto: AddFollowingDto = { myId, followId: id };
const addFollowingDto: followerDto = { myId, followId: id };
await this.userService.addFollowing(addFollowingDto);
return responseForm(200, {});
} catch (error) {
Expand All @@ -34,6 +32,19 @@ export class UserController {
}
}

@Delete('following/:id')
async unFollowing(@Param('id') id: string) {
try {
const myId = '63734e98384f478a32c3a1cc';
// TODO: Request Headerμ—μ„œ access token으둜 ν˜„μž¬ μ‚¬μš©μž μ•Œμ•„λ‚΄κΈ°
const unFollowingDto: followerDto = { myId, followId: id };
await this.userService.unFollowing(unFollowingDto);
return responseForm(200, {});
} catch (error) {
this.logger.error(JSON.stringify(error.response));
return error.response;
}
}

// @Post()
// createUser(@Body() createUserDto: CreateUserDto) {
Expand Down
40 changes: 35 additions & 5 deletions server/apps/api/src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BadRequestException, ConflictException, Injectable } from '@nestjs/common';
import { AddFollowingDto } from '@user/dto/add-following.dto';
import { CreateUserDto } from '@user/dto/create-user.dto';
import { followerDto } from '@user/dto/follower.dto';
import { UserRepository } from '@repository/user.repository';

@Injectable()
Expand All @@ -11,7 +10,7 @@ export class UserService {
// this.userRepository.create(createUserDto);
// }

async addFollowing(addFollowingDto: AddFollowingDto) {
async addFollowing(addFollowingDto: followerDto) {
const user = await this.userRepository.findById(addFollowingDto.myId);
const otherUser = await this.userRepository.findById(addFollowingDto.followId);
if (!user || !otherUser) {
Expand All @@ -21,7 +20,38 @@ export class UserService {
} else if (otherUser.followers.includes(addFollowingDto.myId)) {
throw new ConflictException('μƒλŒ€λ°©μ˜ νŒ”λ‘œμ›Œ λͺ©λ‘μ— 이미 μžˆμŠ΅λ‹ˆλ‹€.');
}
this.userRepository.appendFollowing(addFollowingDto);
this.userRepository.appendFollwer(addFollowingDto);

this.userRepository.appendElementAtArr(
{ _id: addFollowingDto.myId },
{ followings: addFollowingDto.followId },
);
this.userRepository.appendElementAtArr(
{ _id: addFollowingDto.followId },
{ followers: addFollowingDto.myId },
);
}

async unFollowing(unFollowingDto: followerDto) {
const user = await this.userRepository.findById(unFollowingDto.myId);
const otherUser = await this.userRepository.findById(unFollowingDto.followId);
if (!user || !otherUser) {
throw new BadRequestException('ν•΄λ‹Ήν•˜λŠ” μ‚¬μš©μžμ˜ _idκ°€ μ˜¬λ°”λ₯΄μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.');
} else if (!user.followings.includes(unFollowingDto.followId)) {
throw new BadRequestException(
`νŒ”λ‘œμš° μš”μ²­ν•œ μ‚¬μš©μž ${user.nickname}은 ${otherUser.nickname}을 νŒ”λ‘œμš°ν•˜κ³  μžˆμ§€ μ•ŠμŠ΅λ‹ˆλ‹€.`,
);
} else if (!otherUser.followers.includes(unFollowingDto.myId)) {
throw new ConflictException(
`${otherUser.nickname}의 νŒ”λ‘œμ›Œ λͺ©λ‘μ— ${user.nickname}κ°€ μ—†μŠ΅λ‹ˆλ‹€.`,
);
}
this.userRepository.deleteElementAtArr(
{ _id: unFollowingDto.myId },
{ followings: [unFollowingDto.followId] },
);
this.userRepository.deleteElementAtArr(
{ _id: unFollowingDto.followId },
{ followers: [unFollowingDto.myId] },
);
}
}
26 changes: 6 additions & 20 deletions server/dao/repository/user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { User, UserDocument } from '@schemas/user.schema';
import { Model } from 'mongoose';
import { AddFollowingDto } from '@user/dto/add-following.dto';
import mongoose, { Model, Schema, Types } from 'mongoose';
import { followerDto } from '@user/dto/follower.dto';
import { SignUpDto } from '@api/src/auth/dto';

@Injectable()
Expand All @@ -22,25 +22,11 @@ export class UserRepository {
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;
},
);
async appendElementAtArr(filter, appendElement) {
await this.userModel.updateOne(filter, { $push: appendElement });
}

appendFollwer(addFollowingDto: AddFollowingDto) {
this.userModel.updateOne(
{ _id: addFollowingDto.followId },
{
$push: { followers: addFollowingDto.myId },
},
(err, res) => {
if (err) throw err;
},
);
async deleteElementAtArr(filter, removeElement) {
await this.userModel.updateOne(filter, { $pullAll: removeElement });
}
}