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

memeWatch 컬렉션 생성 #7

Merged
merged 5 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 13 additions & 12 deletions src/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,50 @@ const createUser = async (req: Request, res: Response, next: NextFunction) => {
}
};

const updateLastSeenMeme = async (req: CustomRequest, res: Response, next: NextFunction) => {
const createMemeReaction = 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 });
const updatedMeme = await UserService.createMemeReaction(user.deviceId, meme._id as string);
return res.json({ ...updatedMeme });
} catch (err) {
return next(new CustomError(err.message, err.status));
}
};

const createMemeReaction = async (req: CustomRequest, res: Response, next: NextFunction) => {
const createMemeSave = 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);
return res.json({ ...updatedMeme });
const ret = await UserService.createMemeSave(user.deviceId, meme._id as string);
return res.json({ ret });
} catch (err) {
return next(new CustomError(err.message, err.status));
}
};

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

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

const createMemeShare = 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.createMemeShare(user.deviceId, meme._id as string);
return res.json({ ret });
await UserService.createMemeWatch(user.deviceId, meme._id as string);
const updatedUser = await UserService.updateLastSeenMeme(user.deviceId, meme._id as string);
return res.json({ ...updatedUser });
seohyun0120 marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
return next(new CustomError(err.message, err.status));
}
Expand Down Expand Up @@ -114,10 +115,10 @@ const getSavedMeme = async (req: CustomRequest, res: Response, next: NextFunctio

export {
createUser,
updateLastSeenMeme,
createMemeReaction,
createMemeSave,
createMemeShare,
createMemeWatch,
deleteMemeReaction,
deleteMemeSave,
getLastSeenMeme,
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);
7 changes: 1 addition & 6 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,6 +14,7 @@ router.post(
); // user의 reaction 생성
router.post('/save', getRequestedMemeInfo, getRequestedUserInfo, UserController.createMemeSave); // user의 save 생성
router.post('/share', getRequestedMemeInfo, getRequestedUserInfo, UserController.createMemeShare); // user의 share 생성
router.post('/watch', getRequestedMemeInfo, getRequestedUserInfo, UserController.createMemeWatch); // user의 watch 생성

router.delete(
'/reaction',
Expand Down
41 changes: 41 additions & 0 deletions src/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IMeme, MemeModel } from '../model/meme';
import { MemeReactionModel } from '../model/memeReaction';
import { MemeSaveModel } from '../model/memeSave';
import { MemeShareModel } from '../model/memeShare';
import { MemeWatchModel } from '../model/memeWatch';

async function getUser(deviceId: string): Promise<IUserDocument | null> {
try {
Expand Down Expand Up @@ -187,6 +188,45 @@ async function createMemeShare(deviceId: string, memeId: string): Promise<boolea
}
}

async function createMemeWatch(deviceId: string, memeId: string): Promise<boolean> {
try {
const meme = await MemeModel.findOne({ memeId, isDeleted: false });
const user = await UserModel.findOne({ deviceId, isDeleted: false });

if (_.isNull(meme)) {
throw new CustomError(`Failed to get Meme - memeId(${memeId})`, HttpCode.NOT_FOUND);
}
if (_.isNull(user)) {
throw new CustomError(`Failed to get User - deviceId(${deviceId})`, HttpCode.NOT_FOUND);
}
seohyun0120 marked this conversation as resolved.
Show resolved Hide resolved

const memeWatch = await MemeWatchModel.findOne({ deviceId, memeId, isDeleted: false });
if (!_.isNull(memeWatch)) {
logger.info(`Already watch meme - deviceId(${deviceId}), memeId(${memeId}`);
return true;
}
const newMemeWatch = await MemeWatchModel.create({ memeId, deviceId });
await newMemeWatch.save();

const newWatchCount = meme.watch + 1;
await MemeModel.findOneAndUpdate(
{ memeId },
{
watch: newWatchCount,
},
{
projection: { _id: 0, createdAt: 0, updatedAt: 0 },
returnDocument: 'after',
},
).lean();
seohyun0120 marked this conversation as resolved.
Show resolved Hide resolved

return true;
} catch (err) {
logger.error(`Failed create memeSave`, err.message);
throw new CustomError(`Failed create memeSave(${err.message})`, HttpCode.INTERNAL_SERVER_ERROR);
}
}

async function deleteMemeReaction(deviceId: string, memeId: string): Promise<IMeme> {
try {
const meme = await MemeModel.findOne({ memeId, isDeleted: false });
Expand Down Expand Up @@ -315,6 +355,7 @@ export {
createMemeReaction,
createMemeSave,
createMemeShare,
createMemeWatch,
deleteMemeReaction,
deleteMemeSave,
getLastSeenMeme,
Expand Down
Loading