Skip to content

Commit

Permalink
feat: artwork background, snowflake
Browse files Browse the repository at this point in the history
  • Loading branch information
kimploo committed Jan 3, 2024
1 parent 5367a5a commit 005cf3f
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 40 deletions.
3 changes: 0 additions & 3 deletions src/api/kakao/getKakaoUser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import axios from 'axios';
import QueryString from 'qs';
import kakaoAxios from '../base';
const { KAKAO_REST_API_KEY } = process.env;

interface Params {
access_token: string;
Expand Down
8 changes: 5 additions & 3 deletions src/artwork/artwork.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { DefaultResDTO } from '@/types/dto';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

import { Request, Response } from 'express';
import { ArtworkWithInfos } from './dto/getArtwork.dto';

const getPageStartEnd = (limit: number, page: number) => {
const pageStart = (page - 1) * limit;
Expand All @@ -10,9 +12,9 @@ const getPageStartEnd = (limit: number, page: number) => {
};

export default {
findMany: async (req: Request, res: Response) => {
findMany: async (req: Request, res: Response<DefaultResDTO<ArtworkWithInfos[], string>>) => {
const { limit, page } = req.query;
if (!limit || !page) return res.status(400).send('should have pagination parameter');
if (!limit || !page) return res.status(400).send({ error: 'not found' });

const { pageStart, pageEnd } = getPageStartEnd(Number(limit), Number(page));
const result = await prisma.artwork.findMany({
Expand All @@ -24,6 +26,6 @@ export default {
take: pageEnd,
});

return res.json(result);
return res.json({ data: result });
},
};
7 changes: 7 additions & 0 deletions src/artwork/dto/getArtwork.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Prisma } from '@prisma/client';

const artwork = Prisma.validator<Prisma.ArtworkDefaultArgs>()({
include: { ArtworkBackground: true, ArtworkSnowFlake: true },
});

export type ArtworkWithInfos = Prisma.ArtworkGetPayload<typeof artwork>;
22 changes: 11 additions & 11 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import axios from 'axios';
import qs from 'qs';
import { PrismaClient, User } from '@prisma/client';
import { Request, Response } from 'express';
const prisma = new PrismaClient();

import token from '@util/token';
import { KakaoTokenRes, KakaoUserInfo } from '@customType/kakaoRes';
import { Request, Response } from 'express';
import { add } from 'date-fns';

import cookieUtil from '@/util/cookie';
import refreshKakaoToken from '@/api/kakao/refreshKakaoToken';
import getKakaoUser from '@/api/kakao/getKakaoUser';
import kakaoLogout from '@/api/kakao/kakaoLogout';
import getKakaoToken from '@/api/kakao/getKakaoToken';

import token from '@/util/token';
import { KakaoTokenRes, KakaoUserInfo } from '@/types/kakaoRes';
import { kakaoTokenRefreshRes } from './auth.type';
import cookieUtil from '../util/cookie';
import refreshKakaoToken from 'src/api/kakao/refreshKakaoToken';
import getKakaoUser from 'src/api/kakao/getKakaoUser';
import kakaoLogout from 'src/api/kakao/kakaoLogout';
import getKakaoToken from 'src/api/kakao/getKakaoToken';

const prisma = new PrismaClient();
const isDev = process.env.IS_OFFLINE;
const domain = isDev ? 'localhost' : 'teamhh.link';
const { KAKAO_REST_API_KEY, CLIENT_URI_DEV, CLIENT_URI_PROD, SERVER_URI_DEV, SERVER_URI_PROD } = process.env;
Expand Down
35 changes: 23 additions & 12 deletions src/card/card.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Card, PrismaClient } from '@prisma/client';
import token from '@util/token';
const prisma = new PrismaClient();

import { Request, Response } from 'express';
import { ParamsDictionary } from 'express-serve-static-core';

import { DefaultResDTO } from '@/types/dto';
import { CreateCardReqDTO } from './dto/createCard.dto';
import { updateCardReqDTO } from './dto/updateCard.dto';
import { DefaultResDTO } from '@customType/dto';
import { getErrorMessage } from '@util/errorHandler';
const prisma = new PrismaClient();
import { getErrorMessage } from '@/util/errorHandler';
import { CardWithInfos } from './dto/getCard.dto';
import token from '@/util/token';

const getPageStartEnd = (limit: number, page: number) => {
const pageStart = (page - 1) * limit;
Expand All @@ -15,14 +18,19 @@ const getPageStartEnd = (limit: number, page: number) => {
};

export default {
findMany: async (req: Request, res: Response<DefaultResDTO<Card[], string>>) => {
findMany: async (req: Request, res: Response<DefaultResDTO<CardWithInfos[], string>>) => {
const { limit, page } = req.query;
if (!limit || !page) return res.status(400).send({ error: 'Bad Request' });

const { pageStart, pageEnd } = getPageStartEnd(Number(limit), Number(page));
let cards;
try {
cards = await prisma.card.findMany({
include: {
Artwork: true,
ArtworkBackground: true,
ArtworkSnowFlake: true,
},
skip: pageStart,
take: pageEnd,
});
Expand All @@ -36,12 +44,17 @@ export default {
});
},

findOne: async (req: Request, res: Response<DefaultResDTO<Card, string>>) => {
findOne: async (req: Request, res: Response<DefaultResDTO<CardWithInfos, string>>) => {
const uuid = req.params.uuid;
let card;

try {
card = await prisma.card.findUnique({
include: {
Artwork: true,
ArtworkBackground: true,
ArtworkSnowFlake: true,
},
where: { uuid },
});
} catch (e) {
Expand Down Expand Up @@ -78,8 +91,7 @@ export default {

if (!user) return res.status(401).json({ error: 'unauthorized' });

const { from, to, msg, artworkId, artworkUrl, artworkBackgroundId, bgColor, artworkSnowFlakeId, imgUrls } =
req.body;
const { from, to, msg, artworkId, artworkBackgroundId, artworkSnowFlakeId } = req.body;
let card;
try {
card = await prisma.card.create({
Expand Down Expand Up @@ -112,8 +124,7 @@ export default {
req: Request<ParamsDictionary, any, updateCardReqDTO>,
res: Response<DefaultResDTO<{ uuid: string | null }, string>>
) => {
const { from, to, msg, artworkId, artworkUrl, artworkBackgroundId, bgColor, artworkSnowFlakeId, imgUrls } =
req.body;
const { from, to, msg, artworkId, artworkUrl, artworkBackgroundId, bgInfo, artworkSnowFlakeId, imgUrls } = req.body;
const cardId = Number(req.params.id);

let card: Card | null;
Expand All @@ -140,7 +151,7 @@ export default {
id: artworkBackgroundId,
},
create: {
bgColor,
bgInfo,
},
},
},
Expand All @@ -163,7 +174,7 @@ export default {
id: artworkBackgroundId,
},
data: {
bgColor,
bgInfo,
},
},
},
Expand Down
3 changes: 0 additions & 3 deletions src/card/dto/createCard.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ export interface CreateCardReqDTO {
to: string;
msg: string;
artworkId: number;
artworkUrl: string;
artworkBackgroundId: number;
bgColor: string;
artworkSnowFlakeId: number;
imgUrls: string[];
}
7 changes: 7 additions & 0 deletions src/card/dto/getCard.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Prisma } from '@prisma/client';

const cardWithInfos = Prisma.validator<Prisma.CardDefaultArgs>()({
include: { Artwork: true, ArtworkBackground: true, ArtworkSnowFlake: true },
});

export type CardWithInfos = Prisma.CardGetPayload<typeof cardWithInfos>;
4 changes: 3 additions & 1 deletion src/card/dto/updateCard.dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Prisma } from '@prisma/client';

export interface updateCardReqDTO {
from: string;
to: string;
msg: string;
artworkId: number;
artworkUrl: string;
artworkBackgroundId: number;
bgColor: string;
bgInfo: Prisma.JsonObject;
artworkSnowFlakeId: number;
imgUrls: string[];
}
2 changes: 1 addition & 1 deletion src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import dotenv from 'dotenv';
import cookieParser from 'cookie-parser';

import { authRouter, cardRouter, loginRouter, logoutRouter, artworkRouter } from './router';
import cookieUtil from './util/cookie';
import cookieUtil from '@/util/cookie';

dotenv.config();
const app = express();
Expand Down
8 changes: 4 additions & 4 deletions src/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const prisma = new PrismaClient();
import { add } from 'date-fns';
import { RequestHandler } from 'express';

import token from '@util/token';
import cookieUtil from '@util/cookie';
import { kakaoTokenRefreshRes } from '@auth/auth.type';
import { KakaoUserInfo } from '@customType/kakaoRes';
import token from '@/util/token';
import cookieUtil from '@/util/cookie';
import { kakaoTokenRefreshRes } from '@/auth/auth.type';
import { KakaoUserInfo } from '@/types/kakaoRes';

import refreshKakaoToken from 'src/api/kakao/refreshKakaoToken';
import getKakaoUser from 'src/api/kakao/getKakaoUser';
Expand Down
5 changes: 3 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import express from 'express';
import authController from '@auth/auth.controller';
import authController from '@/auth/auth.controller';
import cardController from './card/card.controller';
import { authFunc } from './middleware/auth';
import artworkController from './artwork/artwork.controller';

const loginRouter = express.Router();
const logoutRouter = express.Router();
Expand All @@ -16,7 +17,7 @@ const auth = isDev ? (_: any, __: any, next: () => any) => next() : authFunc;
loginRouter.get('/', authController.login);
logoutRouter.post('/', authController.logout);
authRouter.get('/', authController.auth);
artworkRouter.get('/', auth, cardController.findMany);
artworkRouter.get('/', auth, artworkController.findMany);
cardRouter.get('/', auth, cardController.findMany);
cardRouter.get('/:uuid', cardController.findOne);
cardRouter.post('/', auth, cardController.createOne);
Expand Down

0 comments on commit 005cf3f

Please sign in to comment.