Skip to content

Commit

Permalink
feat: add IKeywordCategoryDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
pjw5521 committed Jun 29, 2024
1 parent fc8892a commit 1b1e07b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
1 change: 0 additions & 1 deletion src/controller/keywordCategory.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { HttpCode } from '../errors/HttpCode';
import * as KeywordCategoryService from '../service/keywordCategory.service';
import { logger } from '../util/logger';
import { IKeywordCategoryUpdatePayload } from '../model/keywordCategory';
import mongoose from 'mongoose';
import { CustomRequest } from '../middleware/requestedInfo';

const createKeywordCategory = async (req: Request, res: Response, next: NextFunction) => {
Expand Down
6 changes: 3 additions & 3 deletions src/middleware/requestedInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { getKeywordCategory } from '../service/keywordCategory.service';
import { IKeyword } from 'src/model/keyword';
import { getUser } from '../service/user.service';
import { IUserDocument } from '../model/user';
import { IKeywordCategory } from '../model/keywordCategory';
import { IKeywordCategoryDocument } from '../model/keywordCategory';

export interface CustomRequest extends Request {
requestedMeme?: IMemeDocument;
requestedUser?: IUserDocument;
requestedKeyword?: IKeyword;
requestedKeywordCategory?: IKeywordCategory;
requestedKeywordCategory?: IKeywordCategoryDocument;
}

export const getRequestedMemeInfo = async (
Expand Down Expand Up @@ -119,7 +119,7 @@ export const getRequestedKeywordCategoryInfo = async (
if (_.isNull(categoryName)) {
return next(new CustomError(`'categoryId' should be provided`, HttpCode.BAD_REQUEST));
}
const category = await getKeywordCategory(categoryName);
const category: IKeywordCategoryDocument = await getKeywordCategory(categoryName);
if (_.isNull(category)) {
return next(
new CustomError(`KeywordCategory(${categoryName}) does not exist`, HttpCode.NOT_FOUND),
Expand Down
13 changes: 10 additions & 3 deletions src/model/keywordCategory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mongoose, { Schema } from 'mongoose';
import mongoose, { Schema, Types } from 'mongoose';
export interface IKeywordCategoryCreatePayload {
name: string;
isRecommend: boolean;
Expand All @@ -10,9 +10,16 @@ export interface IKeywordCategoryUpdatePayload {
}

export interface IKeywordCategory {
_id: string;
name: string;
isRecommend: boolean;
}

export interface IKeywordCategoryDocument extends Document {
_id: Types.ObjectId;
name: string;
isRecommend: boolean;
createdAt: Date;
updatedAt: Date;
isDeleted: boolean;
}

Expand All @@ -29,7 +36,7 @@ const KeywordCategorySchema: Schema = new Schema(
},
);

export const KeywordCategoryModel = mongoose.model<IKeywordCategory>(
export const KeywordCategoryModel = mongoose.model<IKeywordCategoryDocument>(
'KeywordCategory',
KeywordCategorySchema,
);
37 changes: 20 additions & 17 deletions src/service/keywordCategory.service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import {
KeywordCategoryModel,
IKeywordCategoryCreatePayload,
IKeywordCategory,
IKeywordCategoryDocument,
} from '../model/keywordCategory';
import CustomError from '../errors/CustomError';
import { HttpCode } from '../errors/HttpCode';
import { logger } from '../util/logger';

async function createKeywordCategory(
info: IKeywordCategoryCreatePayload,
): Promise<IKeywordCategory> {
): Promise<IKeywordCategoryDocument> {
try {
const newCategory = new KeywordCategoryModel({
const newCategory = await KeywordCategoryModel.create({
...info,
});
await newCategory.save();
logger.info(`Created new keyword category: ${JSON.stringify(newCategory)}`);
return newCategory.toObject();
const newCategoryObj = newCategory.toObject();

logger.info(`Created new keyword category: ${JSON.stringify(newCategoryObj)}`);
return newCategoryObj;
} catch (err) {
logger.error(`Failed to create category ${info.name}: ${err.message}`);
throw new CustomError('Failed to create category', HttpCode.INTERNAL_SERVER_ERROR);
Expand All @@ -25,19 +27,20 @@ async function createKeywordCategory(

async function updateKeywordCategory(
categoryName: string,
updateInfos: Partial<IKeywordCategory>,
): Promise<IKeywordCategory> {
updateInfo: any,
): Promise<IKeywordCategoryDocument> {
const updatedCategory = await KeywordCategoryModel.findOneAndUpdate(
{ name: categoryName },
updateInfos,
{
new: true,
},
).lean();
{ name: categoryName, isDeleted: false },
{ $set: updateInfo },
{ new: true },
);

if (!updatedCategory) {
throw new CustomError(`Category with ID ${updatedCategory} not found`, HttpCode.NOT_FOUND);
}
return updatedCategory;
logger.info(`Update keyword category - category(${categoryName})`);

return updatedCategory.toObject();
}

async function deleteKeywordCategory(categoryName: string): Promise<boolean> {
Expand All @@ -50,13 +53,13 @@ async function deleteKeywordCategory(categoryName: string): Promise<boolean> {
return true;
}

async function getKeywordCategory(categoryName: string): Promise<IKeywordCategory> {
async function getKeywordCategory(categoryName: string): Promise<IKeywordCategoryDocument> {
try {
const keywordCategory = await KeywordCategoryModel.findOne({
name: categoryName,
isDeleted: false,
}).lean();
return keywordCategory;
});
return keywordCategory.toObject();
} catch (err) {
logger.info(`Failed to get a KeywordCategory Info By id (${categoryName})`);
}
Expand Down

0 comments on commit 1b1e07b

Please sign in to comment.