Skip to content
Snippets Groups Projects
Select Git revision
  • a71d1779582b5f0cad1e90a4986296318f5356d5
  • main default protected
2 results

my.js

Blame
  • my.js 3.12 KiB
    import { Router } from 'express';
    import myService from '../services/myService.js';
    import authMiddleware from '../middlewares/authMiddleware.js';
    import { wrapAsync } from '../utils.js';
    import { ReportableError } from '../errors.js';
    
    const myRouter = Router();
    
    myRouter.get(
      '/pc',
      authMiddleware,
      wrapAsync(async (req, res) => {
        const userId = req.user?.userId;
    
        if (!userId) {
          throw new ReportableError(400, '유효한 사용자 정보가 없습니다.');
        }
    
        const data = await myService.getUserPCs(userId);
        res.sendResponse('', 200, data);
      })
    );
    
    myRouter.delete(
      '/pc/:pcId',
      authMiddleware,
      wrapAsync(async (req, res) => {
        const { pcId } = req.params;
        const userId = req.user?.userId;
    
        if (!userId) {
          throw new ReportableError(400, '유효한 사용자 정보가 없습니다.');
        }
    
        if (!pcId) {
          throw new ReportableError(400, 'PC ID가 필요합니다.');
        }
    
        const result = await myService.deleteUserPC(userId, pcId);
    
        if (result.success) {
          return res.sendResponse(result.message, 200, {});
        } else {
          throw new ReportableError(500, 'PC 삭제 중 문제가 발생했습니다.');
        }
      })
    );
    
    myRouter.patch(
      '/pc/:combinationId/name',
      authMiddleware,
      wrapAsync(async (req, res) => {
        const { combinationId } = req.params;
        const { newName } = req.body;
        const userId = req.user?.userId;
    
        if (!newName || typeof newName !== 'string') {
          throw new ReportableError(400, '유효한 새로운 이름이 필요합니다.');
        }
    
        const result = await myService.updateCombinationName(
          userId,
          combinationId,
          newName
        );
    
        return res.status(200).json(result);
      })
    );
    
    myRouter.post(
      '/pc/:combinationId/uuid',
      authMiddleware,
      wrapAsync(async (req, res) => {
        const { combinationId } = req.params;
        const userId = req.user?.userId;
    
        const uuid = await myService.createCombinationUuid(userId, combinationId);
        res.sendResponse('', 200, { uuid });
      })
    );
    
    myRouter.get(
      '/registration-code',
      authMiddleware,
      wrapAsync(async (req, res) => {
        const userId = req.user?.userId;
    
        if (!userId) {
          throw new ReportableError(400, '사용자 ID가 없습니다.');
        }
    
        const code = await myService.createRegistrationCode(userId);
        res.sendResponse('', 200, code);
      })
    );
    
    myRouter.get(
      '/registration-code/:code',
      authMiddleware,
      wrapAsync(async (req, res) => {
        const { code } = req.params;
    
        if (!code) {
          throw new ReportableError(400, '코드가 필요합니다.');
        }
    
        const data = await myService.getCombinationId(code);
        res.sendResponse('', 200, data);
      })
    );
    
    myRouter.post(
      '/',
      wrapAsync(async (req, res) => {
        const authorizationHeader = req.headers['authorization'];
        const code = authorizationHeader?.split(' ')[1];
    
        if (!code) {
          throw new ReportableError(401, '코드가 필요합니다.');
        }
    
        const { xml } = req.body;
    
        if (!xml) {
          throw new ReportableError(400, 'XML 데이터가 필요합니다.');
        }
    
        await myService.saveDocument(code, xml);
        res.sendResponse('', 200, {});
      })
    );
    
    export default myRouter;