728x90
0. services/message.js
class Messages {
constructor(message) {
this.message = message;
}
status200() {
return {
status: 200,
message: `${this.message}에 성공하였습니다.`,
};
}
status400() {
return {
status: 400,
message: `${this.message}에 실패하였습니다.`,
};
}
nosubject() {
return {
status: 400,
message: `${this.message}을 입력해 주세요.`,
};
}
}
export default Messages;
1. src/db/index.js 작성
import sequelize from './sequelize.js';
import Items from './models/items.js';
export { sequelize, Items };
2. src/db/models/item.js 작성
import { Model, DataTypes } from 'sequelize';
import sequelize from '../sequelize.js';
class Items extends Model {}
Items.init(
{
name: DataTypes.STRING,
option_id: { type: DataTypes.BIGINT, defaultValue: 0 },
price: DataTypes.BIGINT,
type: { type: DataTypes.ENUM, values: ['COFFEE', 'JUICE', 'FOOD'] },
amount: { type: DataTypes.BIGINT, defaultValue: 0 },
},
{
sequelize,
modelName: 'Items',
},
);
export default Items;
3. preitterrc.js > prettierrc.cjs로 변경 (type:module일 경우 적용이 안 되어서)
4. src/routes/items.route.js
import { Router } from 'express';
import ItemsController from '../controllers/itmes.controller.js';
const router = Router();
const itemsController = new ItemsController();
router.post('/items', itemsController.makeItem);
// router.get('/items', itemsController.getItemList);
export default router;
5. src/controllers/items.controller.js
import ItemsService from '../services/items.service.js';
class ItemsController {
itemsService = new ItemsService();
makeItem = async (req, res) => {
const { name, price, type } = req.body;
const { status, message } = await this.itemsService.makeItem(
name,
price,
type,
);
return res.status(status).json({ message });
};
}
export default ItemsController;
6. src/services/items.service.js
import Messages from './message.js';
import ItemsRepository from '../repositories/items.repository.js';
const noname = new Messages('이름');
const noprice = new Messages('가격');
class ItemsService {
itemsRepository = new ItemsRepository();
makeItem = async (name, price, type) => {
const messages = new Messages('상품 추가');
try {
console.log(type);
if (!name.length) {
return noname.nosubject();
} else if (!price) {
return noprice.nosubject();
} else if (!['COFFEE', 'JUICE', 'FOOD'].includes(type)) {
return {
status: 400,
message: '알맞은 타입을 지정해 주세요.',
};
}
const item = await this.itemsRepository.makeItem(name, price, type);
if (item.name) {
return messages.status200();
}
} catch (err) {
return messages.status400();
}
};
}
export default ItemsService;
7. src/services/items.respository.js
import Items from '../db/models/items.js';
class ItemsRepository {
makeItem = async (name, price, type) => {
const item = await Items.create({
name,
price,
type,
});
return item;
};
}
export default ItemsRepository;
8. app.js 수정
import express from 'express';
import itemsRouter from './routes/items.route.js';
export class ExpressApp {
app = express();
constructor() {
this.setAppSettings();
this.setAppRouter();
}
setAppSettings = () => {
this.app.use(express.json());
};
setAppRouter = () => {
this.app.use('/api', itemsRouter, (error, request, response) => {
response.status(400).json({
success: false,
error: error.message,
});
});
this.app.use('/ping', (req, res, next) => {
return res.status(200).json({ message: 'pong' });
});
};
}
결과.
'내일 배움 캠프 > kiosk' 카테고리의 다른 글
kiosk 6일차 8/1 주문 관리 (2) (0) | 2023.07.28 |
---|---|
kiosk 5일차 7/31 주문 관리 (0) | 2023.07.28 |
kiosk 4일차 7/28 상품 발주 (0) | 2023.07.27 |
kiosk 3일차 7/27 상품 관리 (2) (0) | 2023.07.26 |
kiosk 1일차 7/25 프로젝트 세팅 (0) | 2023.07.25 |