Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions 6-sprint-mission
Submodule 6-sprint-mission added at b99a6c
77 changes: 77 additions & 0 deletions ArticleService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const url = 'https://panda-market-api-crud.vercel.app/articles';

export function getArticleList(page = 1, pageSize = 10, keyword = '') {
return fetch(`${url}?page=${page}&pageSize=${pageSize}&keyword=${keyword}`)
.then((res) => {
if (!res.ok) {
throw new Error('게시글 리스트 불러오기 실패');
}
return res.json();
})
.then((data) => data.items)
.catch((err) => {
console.error(err);
});
}

export function getArticle(id) {
return fetch(`${url}/${id}`)
.then((res) => {
if (!res.ok) {
throw new Error('게시글 불러오기 실패');
}
return res.json();
})
.catch((err) => {
console.error(err);
});
}

export function createArticle(title, content, image) {
return fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title, content, image }),
})
.then((res) => {
if (!res.ok) {
throw new Error('게시글 등록 실패');
}
return res.json();
})
.catch((err) => {
console.error(err);
});
}

export function patchArticle(id, data) {
return fetch(`${url}/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
})
.then((res) => {
if (!res.ok) {
throw new Error('게시글 수정 실패');
}
return res.json();
})
.catch((err) => {
console.error(err);
});
}

export function deleteArticle(id) {
return fetch(`${url}/${id}`, {
method: 'DELETE',
})
.then((res) => {
if (!res.ok) {
throw new Error('게시글 삭제 실패');
}
return res.json();
})
.catch((err) => {
console.error(err);
});
}
72 changes: 72 additions & 0 deletions ProductService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const url = 'https://panda-market-api-crud.vercel.app/products';

export async function getProductList(page = 1, pageSize = 10, keyword = '') {
try {
const response = await fetch(
`${url}?page=${page}&pageSize=${pageSize}&keyword=${keyword}`
);
if (!response.ok) {
throw new Error('상품 리스트 불러오기 실패');
}
const data = await response.json();
return data.items;
} catch (error) {
console.error(error);
}
}

export async function getProduct(id) {
try {
const response = await fetch(`${url}/${id}`);
if (!response.ok) {
throw new Error('상품 불러오기 실패');
}
return await response.json();
} catch (error) {
console.error(error);
}
}

export async function createProduct(name, description, price, tags, images) {
try {
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, description, price, tags, images }),
});
if (!response.ok) {
throw new Error('상품 등록 실패');
}
return await response.json();
} catch (error) {
console.error(error);
}
}

export async function patchProduct(id, data) {
try {
const response = await fetch(`${url}/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error('상품 수정 실패');
}
return await response.json();
} catch (error) {
console.error(error);
}
}

export async function deleteProduct(id) {
try {
const response = await fetch(`${url}/${id}`, { method: 'DELETE' });
if (!response.ok) {
throw new Error('상품 삭제 실패');
}
return await response.json();
} catch (error) {
console.error(error);
}
}
63 changes: 63 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { getProductList } from './ProductService.js';
import { getArticleList } from './ArticleService.js';

class Product {
constructor(name, description, price, tags, images, favoriteCount = 0) {
this.name = name;
this.description = description;
this.price = price;
this.tags = tags;
this.images = images;
this.favoriteCount = favoriteCount;
}

favorite() {
this.favoriteCount += 1;
}
}

class ElectronicProduct extends Product {
constructor(
name,
description,
price,
tags,
images,
favoriteCount,
manufacturer
) {
super(name, description, price, tags, images, favoriteCount);
this.manufacturer = manufacturer;
}
}

class Article {
constructor(title, content, writer, likeCount = 0) {
this.title = title;
this.content = content;
this.writer = writer;
this.likeCount = likeCount;
this.createdAt = new Date(); // 생성 시간 저장
}

like() {
this.likeCount += 1;
}
}

async function main() {
console.log('=== 상품 가져오기 ===');
const products = await getProductList(1, 5, '');
console.log(products);

console.log('=== 찜한상품 가져오기 ===');
getArticleList(1, 5, '')
.then((articles) => {
console.log(articles);
})
.catch((err) => {
console.error('에러:', err);
});
}

main();
Empty file added node_modules/.bin/mime
Empty file.
Empty file added node_modules/.bin/mkdirp
Empty file.
Empty file added node_modules/.bin/prisma
Empty file.
Loading