Skip to content

Commit

Permalink
feat/#214 add update api
Browse files Browse the repository at this point in the history
  • Loading branch information
DungNguyen2003 committed Jun 10, 2023
1 parent 65c601c commit f9069be
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/apis/v1/popup/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { Response } from 'express';
import * as service from './service';
import fmt from 'utils/formatter';
import RequestWithUser from 'utils/rest/request';
// import URLParams from 'utils/rest/urlparams';
import { DocumentDto, ParamsPopupDto } from './dto/CreatePopupDto';
import { DocumentDto, ParamsPopupDto, UpdatePopupDto } from './dto/CreatePopupDto';

export const createPopup = async (req: RequestWithUser, res: Response) => {
const input: DocumentDto = req.body;
Expand Down Expand Up @@ -35,3 +34,12 @@ export const getPopupsByDateRange = async (req: RequestWithUser, res: Response)
const result = await service.getPopupsByDateRange();
res.send(fmt.formatResponse(result, Date.now() - req.startTime, 'OK'));
};

export const updatePopup = async (req: RequestWithUser, res: Response) => {
const input: UpdatePopupDto = req.body;
const params: ParamsPopupDto = req.params;

const result = await service.updatePopup(params.id, input);

res.send(fmt.formatResponse(result, Date.now() - req.startTime, 'OK'));
};
22 changes: 22 additions & 0 deletions src/apis/v1/popup/dto/CreatePopupDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,25 @@ export class DateRangePopupDto {
@IsNumber()
end_date: number;
}

export class UpdatePopupDto {
@IsOptional()
@IsNumber()
priority: number;

@IsOptional()
@IsString()
title: string;

@IsOptional()
@IsString()
description: string;

@IsOptional()
@IsString()
cover_image: string;

@IsOptional()
@IsBoolean()
is_revoked: boolean;
}
8 changes: 8 additions & 0 deletions src/apis/v1/popup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ router.get('/', authMiddleware, adminMiddleware, asyncRouteHandler(controller.ge

router.get('/daily', authMiddleware, adminMiddleware, asyncRouteHandler(controller.getPopupsByDateRange));

router.patch(
'/:id',
authMiddleware,
adminMiddleware,
validationMiddleware(ParamsPopupDto, APP_CONSTANTS.params),
asyncRouteHandler(controller.updatePopup)
);

export default router;
21 changes: 18 additions & 3 deletions src/apis/v1/popup/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ErrorCodes, HttpException } from 'exceptions';
import { PopupModel } from 'models';
import { logger } from 'utils/logger';
import { DocumentDto } from './dto/CreatePopupDto';
import { DocumentDto, UpdatePopupDto } from './dto/CreatePopupDto';

export const createPopup = async (input: DocumentDto) => {
try {
Expand Down Expand Up @@ -67,8 +67,23 @@ export const getPopupsByDateRange = async () => {
}).sort({ priority: -1 });

logger.info('Popups fetched successfully');
logger.info(data);
logger.info(currentTimestamp);
return data;
} catch (error) {
logger.error(`Error getting popup: ${error} `);
throw new HttpException(400, ErrorCodes.BAD_REQUEST.MESSAGE, ErrorCodes.BAD_REQUEST.CODE);
}
};

export const updatePopup = async (id: string, input: UpdatePopupDto) => {
try {
const data = await PopupModel.findOneAndUpdate(
{ _id: id },
{
$set: input,
}
);

logger.info('Popups updated successfully');
return data;
} catch (error) {
logger.error(`Error getting popup: ${error} `);
Expand Down

0 comments on commit f9069be

Please sign in to comment.