Skip to content

Commit

Permalink
Merge pull request #7 from mash-up-kr/feature/makeMemeWatch
Browse files Browse the repository at this point in the history
memeWatch 컬렉션 생성
  • Loading branch information
seohyun0120 authored Jun 27, 2024
2 parents 903ce6b + 26cafa3 commit 4fa5ac8
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 158 deletions.
32 changes: 10 additions & 22 deletions src/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,12 @@ const createUser = async (req: Request, res: Response, next: NextFunction) => {
}
};

const updateLastSeenMeme = async (req: CustomRequest, res: Response, next: NextFunction) => {
const user = req.requestedUser;
const meme = req.requestedMeme;

try {
const updatedUser = await UserService.updateLastSeenMeme(user.deviceId, meme._id as string);
return res.json({ ...updatedUser });
} catch (err) {
return next(new CustomError(err.message, err.status));
}
};

const createMemeReaction = async (req: CustomRequest, res: Response, next: NextFunction) => {
const user = req.requestedUser;
const meme = req.requestedMeme;

try {
const updatedMeme = await UserService.createMemeReaction(user.deviceId, meme._id as string);
const updatedMeme = await UserService.createMemeReaction(user, meme);
return res.json({ ...updatedMeme });
} catch (err) {
return next(new CustomError(err.message, err.status));
Expand All @@ -47,7 +35,7 @@ const createMemeSave = async (req: CustomRequest, res: Response, next: NextFunct
const meme = req.requestedMeme;

try {
const ret = await UserService.createMemeSave(user.deviceId, meme._id as string);
const ret = await UserService.createMemeSave(user, meme);
return res.json({ ret });
} catch (err) {
return next(new CustomError(err.message, err.status));
Expand All @@ -59,19 +47,20 @@ const createMemeShare = async (req: CustomRequest, res: Response, next: NextFunc
const meme = req.requestedMeme;

try {
const ret = await UserService.createMemeShare(user.deviceId, meme._id as string);
const ret = await UserService.createMemeShare(user, meme);
return res.json({ ret });
} catch (err) {
return next(new CustomError(err.message, err.status));
}
};

const deleteMemeReaction = async (req: CustomRequest, res: Response, next: NextFunction) => {
const createMemeWatch = async (req: CustomRequest, res: Response, next: NextFunction) => {
const user = req.requestedUser;
const meme = req.requestedMeme;

try {
const ret = await UserService.deleteMemeReaction(user.deviceId, meme._id as string);
const ret = await UserService.createMemeWatch(user, meme);
await UserService.updateLastSeenMeme(user, meme);
return res.json({ ret });
} catch (err) {
return next(new CustomError(err.message, err.status));
Expand All @@ -83,7 +72,7 @@ const deleteMemeSave = async (req: CustomRequest, res: Response, next: NextFunct
const meme = req.requestedMeme;

try {
const result = await UserService.deleteMemeSave(user.deviceId, meme._id as string);
const result = await UserService.deleteMemeSave(user, meme);
return res.json({ result });
} catch (err) {
return next(new CustomError(err.message, err.status));
Expand All @@ -94,7 +83,7 @@ const getLastSeenMeme = async (req: CustomRequest, res: Response, next: NextFunc
const user = req.requestedUser;

try {
const memeList = await UserService.getLastSeenMeme(user.deviceId);
const memeList = await UserService.getLastSeenMeme(user);
return res.json({ memeList });
} catch (err) {
return next(new CustomError(err.message, err.status));
Expand All @@ -105,7 +94,7 @@ const getSavedMeme = async (req: CustomRequest, res: Response, next: NextFunctio
const user = req.requestedUser;

try {
const memeList = await UserService.getSavedMeme(user.deviceId);
const memeList = await UserService.getSavedMeme(user);
return res.json({ memeList });
} catch (err) {
return next(new CustomError(err.message, err.status));
Expand All @@ -114,11 +103,10 @@ const getSavedMeme = async (req: CustomRequest, res: Response, next: NextFunctio

export {
createUser,
updateLastSeenMeme,
createMemeReaction,
createMemeSave,
createMemeShare,
deleteMemeReaction,
createMemeWatch,
deleteMemeSave,
getLastSeenMeme,
getSavedMeme,
Expand Down
3 changes: 3 additions & 0 deletions src/model/meme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface IMeme {
keywords: string[];
image: string;
reaction: number;
watch: number;
source: string;
isTodayMeme: boolean;
}
Expand All @@ -19,6 +20,7 @@ export interface IMemeDocument extends Document {
keywords: string[];
image: string;
reaction: number;
watch: number;
source: string;
isTodayMeme: boolean;
createdAt: Date;
Expand All @@ -31,6 +33,7 @@ const MemeSchema: Schema = new Schema(
image: { type: String, required: true },
keywords: { type: [String], requried: true, default: [] },
reaction: { type: Number, required: true, default: 0 },
watch: { type: Number, required: true, default: 0 },
source: { type: String, required: true },
isTodayMeme: { type: Boolean },
isDeleted: { type: Boolean, required: true, default: false },
Expand Down
24 changes: 24 additions & 0 deletions src/model/memeWatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import mongoose, { Schema } from 'mongoose';

export interface IMemeWatch {
deviceId: string;
memeId: string;
isDeleted: boolean;
createdAt: Date;
updatedAt: Date;
}

const MemeWatchSchema: Schema = new Schema(
{
deviceId: { type: String, required: true },
memeId: { type: String, required: true },
isDeleted: { type: Boolean, required: true, default: false },
},
{
timestamps: true,
versionKey: false,
collection: 'MemeWatch',
},
);

export const MemeWatchModel = mongoose.model<IMemeWatch>('MemeWatch', MemeWatchSchema);
15 changes: 1 addition & 14 deletions src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ import { getRequestedMemeInfo, getRequestedUserInfo } from '../middleware/reques
const router = express.Router();

router.post('/', UserController.createUser); // user 생성
router.put(
'/lastSeenMeme',
getRequestedMemeInfo,
getRequestedUserInfo,
UserController.updateLastSeenMeme,
); // user가 본 meme 업데이트

router.post(
'/reaction',
Expand All @@ -20,16 +14,9 @@ router.post(
); // user의 reaction 생성
router.post('/save', getRequestedMemeInfo, getRequestedUserInfo, UserController.createMemeSave); // user의 save 생성
router.post('/share', getRequestedMemeInfo, getRequestedUserInfo, UserController.createMemeShare); // user의 share 생성

router.delete(
'/reaction',
getRequestedMemeInfo,
getRequestedUserInfo,
UserController.deleteMemeReaction,
); // user의 reaction 삭제
router.post('/watch', getRequestedMemeInfo, getRequestedUserInfo, UserController.createMemeWatch); // user의 watch 생성
router.delete('/save', getRequestedMemeInfo, getRequestedUserInfo, UserController.deleteMemeSave); // user의 save 삭제

router.get('/:userId/lastSeenMeme', getRequestedUserInfo, UserController.getLastSeenMeme); // user가 본 meme 조회
router.get('/:userId/save', getRequestedUserInfo, UserController.getSavedMeme); // user가 저장한 meme 조회

export default router;
Loading

0 comments on commit 4fa5ac8

Please sign in to comment.