Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[박수환] sprint6 #108

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8435806
Merge pull request #5 from soohwanpak/react-박수환-sprint5
soohwanpak Oct 21, 2024
91cbbce
Merge pull request #6 from soohwanpak/react-박수환-sprint5
soohwanpak Oct 22, 2024
13affcd
Merge pull request #7 from soohwanpak/react-박수환-sprint5
soohwanpak Oct 22, 2024
af04f28
Merge pull request #8 from soohwanpak/react-박수환-sprint5
soohwanpak Oct 23, 2024
2646390
Merge pull request #9 from soohwanpak/react-박수환-sprint5
soohwanpak Oct 25, 2024
7049226
Merge pull request #10 from soohwanpak/react-박수환-sprint5
soohwanpak Oct 26, 2024
5eb9d8e
Merge pull request #11 from soohwanpak/react-박수환-sprint5
soohwanpak Oct 28, 2024
ac27296
1030
soohwanpak Oct 30, 2024
f8592bb
Merge pull request #12 from soohwanpak/react-박수환-sprint-6
soohwanpak Oct 30, 2024
94cb292
1030-2
soohwanpak Oct 30, 2024
8038353
1030-3
soohwanpak Oct 30, 2024
c3106e0
1102 : 코드리뷰 일부반영
soohwanpak Nov 1, 2024
16ebfe3
gitignore수정
soohwanpak Nov 1, 2024
bb9360e
gitignore수정2
soohwanpak Nov 1, 2024
1f810f2
gitignore수정3
soohwanpak Nov 1, 2024
43c6ab2
gitignore수정4
soohwanpak Nov 1, 2024
f33abf7
gitignore수정5
soohwanpak Nov 1, 2024
c8ed232
Merge pull request #13 from soohwanpak/react-박수환-sprint-6
soohwanpak Nov 1, 2024
2cc5220
1103마무리
soohwanpak Nov 3, 2024
9c3d505
1103마무리2
soohwanpak Nov 3, 2024
82f1cbb
Merge pull request #14 from soohwanpak/react-박수환-sprint-6
soohwanpak Nov 3, 2024
4b67c17
폴더명 수정
soohwanpak Nov 3, 2024
2816479
Merge pull request #16 from soohwanpak/react-박수환-sprint-6
soohwanpak Nov 3, 2024
4abe7d1
dotenv설정
soohwanpak Nov 3, 2024
621da39
Merge pull request #18 from soohwanpak/react-박수환-sprint-6
soohwanpak Nov 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions react-psh-sprint5/.gitignore → .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ pnpm-debug.log*
lerna-debug.log*

node_modules
/react-psh-sprint5/node_modules/
/express-psh-sprint6/node_modules/
/express-psh-sprint6/.env
/react-psh-sprint5/src/features/api/url.jsx
dist
dist-ssr
*.local

.env
# Editor directories and files
.vscode/*
!.vscode/extensions.json
Expand Down
88 changes: 88 additions & 0 deletions express-psh-sprint-6/controllers/Controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import Product from "../models/Product.js";

//작성한 API를 컨트롤러 함수로 정의

//상품 목록 조회, 화면에 보이는 데이터 조회
export const getItems = async (req, res) => {
try {
const { page = 1, pageSize = 10, orderBy = "recent", keyword = "" } = req.query;
const searchQuery = keyword ? {
$or: [
{ name: { $regex: keyword } },
{ description: { $regex: keyword } },
]
} : {};
const sortOption = orderBy === "recent" ? { createdAt: -1 } : { createdAt: +1 };

const offset = (page - 1) * pageSize;
const limit = pageSize;
const items = await Product.find(searchQuery).sort(sortOption).skip(offset).limit(limit);
const totalCount = await Product.countDocuments(searchQuery);

res.status(200).json({
list: items,
totalCount: totalCount,
});
} catch (err) {
res.status(500).send("실패");
}
};

// 상품 상세조회, id, name, description, price, tags, createdAt를 조회
export const getItemAdd = async (req, res) => {
try {
const id = req.params.id;
const getItemAdd = await Product.findById(id).select("name description price tags createdAt");
if (getItemAdd) {
res.status(200).send(getItemAdd);
} else {
res.status(204).send("해당 ID의 제품을 찾을 수 없습니다.");
}
} catch (err) {
res.status(400).send("실패");
}
};


//상품 등록, name, description, price, tags를 입력하여 상품을 등록
export const registrationProduct = async (req, res) => {
try {
const newProduct = await Product.create(req.body);
res.status(201).send(newProduct);
} catch (err) {
res.status(400).send("실패");
}
};

//상품 수정, name, description, price, tags를 입력하여 상품을 수정
export const editProduct = async (req, res) => {
try {
const id = req.params.id;
const editProduct = await Product.findById(id);
if (editProduct) {
editProduct.name = req.body.name;
editProduct.description = req.body.description;
editProduct.price = req.body.price;
editProduct.tags = req.body.tags;
}
await editProduct.save();
res.status(200).send(editProduct);
} catch (err) {
res.status(400).send("실패");
}
};

//상품 삭제, 선택한 상품의 id와 일치하는 데이터 삭제
export const deleteProduct = async (req, res) => {
try {
const id = req.params.id;
const deleteProduct = await Product.findByIdAndDelete(id);
if (deleteProduct) {
res.status(204).send("삭제 성공");
} else {
res.status(404).send("해당 ID의 제품을 찾을 수 없습니다.");
}
} catch (err) {
res.status(400).send("실패");
}
};
14 changes: 14 additions & 0 deletions express-psh-sprint-6/controllers/routes/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import express from "express";
import { getItems, getItemAdd, registrationProduct, editProduct, deleteProduct } from "../Controller.js";

//컨트롤러 함수를 각 라우트에 연결

const router = express.Router();

router.get("/items", getItems); //상품 목록 조회
router.get("/items/:id", getItemAdd) //상품 상세조회
router.post("/registration", registrationProduct); //상품등록
router.patch("/edit/:id", editProduct); //상품수정
router.delete("/delete/:id", deleteProduct); //상품삭제

export default router;
248 changes: 248 additions & 0 deletions express-psh-sprint-6/data/mockData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
export const Data = {
list: [
{
"name": "삼성 갤럭시 S21",
"description": "상태 좋은 최신 갤럭시 폰!",
"price": 850000,
"tags": ["스마트폰", "삼성"],
"createdAt": "2024-09-01T08:10:45.123Z",
"updatedAt": "2024-09-10T09:15:20.456Z"
},
{
"name": "애플 맥북 프로",
"description": "하루만 사용한 새 맥북!",
"price": 2500000,
"tags": ["노트북", "애플"],
"createdAt": "2024-09-02T09:12:34.789Z",
"updatedAt": "2024-09-12T11:30:00.234Z"
},
{
"name": "아이패드 에어 4",
"description": "필름 부착 완료된 아이패드!",
"price": 650000,
"tags": ["태블릿", "애플"],
"createdAt": "2024-09-03T10:20:45.567Z",
"updatedAt": "2024-09-13T12:45:15.876Z"
},
{
"name": "닌텐도 스위치",
"description": "컨트롤러 포함 풀세트",
"price": 400000,
"tags": ["게임기", "닌텐도"],
"createdAt": "2024-09-04T11:25:15.890Z",
"updatedAt": "2024-09-14T13:50:35.789Z"
},
{
"name": "소니 헤드폰 WH-1000XM4",
"description": "소음 차단이 뛰어난 헤드폰!",
"price": 300000,
"tags": ["음향기기", "소니"],
"createdAt": "2024-09-05T12:30:20.678Z",
"updatedAt": "2024-09-15T14:00:55.234Z"
},
{
"name": "로지텍 무선 마우스",
"description": "사용감 적은 무선 마우스",
"price": 50000,
"tags": ["컴퓨터", "로지텍"],
"createdAt": "2024-09-06T13:35:25.456Z",
"updatedAt": "2024-09-16T15:10:30.567Z"
},
{
"name": "샤오미 공기청정기",
"description": "깨끗한 공기를 위한 필수템",
"price": 120000,
"tags": ["가전제품", "샤오미"],
"createdAt": "2024-09-07T14:40:15.234Z",
"updatedAt": "2024-09-17T16:20:25.789Z"
},
{
"name": "아이폰 12 미니",
"description": "작지만 강력한 아이폰!",
"price": 600000,
"tags": ["스마트폰", "애플"],
"createdAt": "2024-09-08T15:45:10.345Z",
"updatedAt": "2024-09-18T17:25:30.123Z"
},
{
"name": "LG 올레드 TV",
"description": "화질 좋은 대형 TV",
"price": 1800000,
"tags": ["가전제품", "LG"],
"createdAt": "2024-09-09T16:50:25.456Z",
"updatedAt": "2024-09-19T18:30:45.789Z"
},
{
"name": "애플워치 SE",
"description": "애플 생태계를 완성하는 워치",
"price": 280000,
"tags": ["스마트워치", "애플"],
"createdAt": "2024-09-10T17:55:35.678Z",
"updatedAt": "2024-09-20T19:35:15.234Z"
},
{
"name": "삼성 갤럭시 워치4",
"description": "피트니스 트래킹에 강한 워치",
"price": 220000,
"tags": ["스마트워치", "삼성"],
"createdAt": "2024-09-11T18:05:45.123Z",
"updatedAt": "2024-09-21T20:40:25.789Z"
},
{
"name": "HP 프린터",
"description": "가정용으로 적합한 프린터",
"price": 150000,
"tags": ["가전제품", "HP"],
"createdAt": "2024-09-12T19:10:25.678Z",
"updatedAt": "2024-09-22T21:50:10.456Z"
},
{
"name": "LG 그램 17인치",
"description": "가벼운 무게의 대화면 노트북",
"price": 2300000,
"tags": ["노트북", "LG"],
"createdAt": "2024-09-13T20:15:40.789Z",
"updatedAt": "2024-09-23T22:55:15.678Z"
},
{
"name": "에어팟 프로",
"description": "액티브 노이즈 캔슬링 지원",
"price": 200000,
"tags": ["음향기기", "애플"],
"createdAt": "2024-09-14T21:20:15.234Z",
"updatedAt": "2024-09-24T23:00:25.345Z"
},
{
"name": "삼성 제트 청소기",
"description": "무선으로 편리한 청소기",
"price": 350000,
"tags": ["가전제품", "삼성"],
"createdAt": "2024-09-15T22:25:35.456Z",
"updatedAt": "2024-09-25T00:10:10.567Z"
},
{
"name": "아이폰 13",
"description": "상태 좋은 아이폰 13",
"price": 1200000,
"tags": ["스마트폰", "애플"],
"createdAt": "2024-09-16T23:30:45.789Z",
"updatedAt": "2024-09-26T01:15:20.678Z"
},
{
"name": "삼성 갤럭시 Z 플립",
"description": "접히는 스마트폰!",
"price": 1100000,
"tags": ["스마트폰", "삼성"],
"createdAt": "2024-09-17T00:35:10.234Z",
"updatedAt": "2024-09-27T02:20:15.890Z"
},
{
"name": "아이맥 24인치",
"description": "컬러풀한 애플 데스크탑",
"price": 1600000,
"tags": ["데스크탑", "애플"],
"createdAt": "2024-09-18T01:40:20.345Z",
"updatedAt": "2024-09-28T03:25:25.234Z"
},
{

"name": "삼성 탭 S7",
"description": "노트 필기와 작업을 위한 탭",
"price": 700000,
"tags": ["태블릿", "삼성"],
"createdAt": "2024-09-19T02:45:25.678Z",
"updatedAt": "2024-09-29T04:30:30.456Z"
},
{
"name": "소니 카메라 A7 III",
"description": "풀프레임 카메라",
"price": 1800000,
"tags": ["카메라", "소니"],
"createdAt": "2024-09-20T03:50:35.890Z",
"updatedAt": "2024-09-30T05:35:45.678Z"
},
{
"name": "샤오미 미밴드 6",
"description": "가성비 좋은 피트니스 밴드",
"price": 45000,
"tags": ["웨어러블", "샤오미"],
"createdAt": "2024-09-21T04:00:00.123Z",
"updatedAt": "2024-09-22T05:00:00.123Z"
},
{
"name": "삼성 QLED TV",
"description": "선명한 화질의 대형 TV",
"price": 2000000,
"tags": ["가전제품", "삼성"],
"createdAt": "2024-09-22T06:00:00.123Z",
"updatedAt": "2024-09-23T07:00:00.123Z"
},
{
"name": "닌텐도 스위치 라이트",
"description": "가벼운 휴대용 게임기",
"price": 250000,
"tags": ["게임기", "닌텐도"],
"createdAt": "2024-09-23T08:00:00.123Z",
"updatedAt": "2024-09-24T09:00:00.123Z"
},
{
"name": "델 XPS 13",
"description": "휴대성 좋은 고성능 노트북",
"price": 1900000,
"tags": ["노트북", "델"],
"createdAt": "2024-09-24T10:00:00.123Z",
"updatedAt": "2024-09-25T11:00:00.123Z"
},
{
"name": "캐논 EOS 90D",
"description": "전문가용 DSLR 카메라",
"price": 1500000,
"tags": ["카메라", "캐논"],
"createdAt": "2024-09-25T12:00:00.123Z",
"updatedAt": "2024-09-26T13:00:00.123Z"
},
{
"name": "애플 매직 키보드",
"description": "아이패드용 블루투스 키보드",
"price": 320000,
"tags": ["컴퓨터", "애플"],
"createdAt": "2024-09-26T14:00:00.123Z",
"updatedAt": "2024-09-27T15:00:00.123Z"
},
{
"name": "보스 QC35 헤드폰",
"description": "최고의 소음 차단 헤드폰",
"price": 400000,
"tags": ["음향기기", "보스"],
"createdAt": "2024-09-27T16:00:00.123Z",
"updatedAt": "2024-09-28T17:00:00.123Z"
},
{
"name": "필립스 공기청정기",
"description": "효율적인 공기 정화 기능",
"price": 220000,
"tags": ["가전제품", "필립스"],
"createdAt": "2024-09-28T18:00:00.123Z",
"updatedAt": "2024-09-29T19:00:00.123Z"
},
{
"name": "소니 플레이스테이션 5",
"description": "최신 콘솔 게임기",
"price": 500000,
"tags": ["게임기", "소니"],
"createdAt": "2024-09-29T20:00:00.123Z",
"updatedAt": "2024-09-30T21:00:00.123Z"
},
{
"name": "아마존 킨들 페이퍼화이트",
"description": "전자책 전용 태블릿",
"price": 150000,
"tags": ["태블릿", "아마존"],
"createdAt": "2024-09-30T22:00:00.123Z",
"updatedAt": "2024-10-01T23:00:00.123Z"
}
],
totalCount: 30
};

export default Data;
15 changes: 15 additions & 0 deletions express-psh-sprint-6/data/seeds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import mongoose, { mongo } from "mongoose";
import { Data } from "./mockData.js";
import dotenv from 'dotenv';
import Product from "../models/Product.js";

dotenv.config();
const dbUrl = process.env.DATABASE_URL;

mongoose.connect(dbUrl);

await Product.deleteMany({});
await Product.insertMany(Data.list);

console.log("success");
mongoose.connection.close();
Loading
Loading