Skip to content

Commit

Permalink
스프린트4
Browse files Browse the repository at this point in the history
  • Loading branch information
xcjnzvc committed Oct 17, 2024
1 parent 2756b03 commit 8ef0be7
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified 판다마켓/.DS_Store
Binary file not shown.
19 changes: 19 additions & 0 deletions 판다마켓/api/common/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function responseInterceptor (response) {
if(response.status === 200 || response.status === 201 || response.status === 204) {
console.log('성공')
return response.json().then((res) => ({ isSuccess : true, status : response.status, data:res }));
} else {
console.log('실패')
return response.json().then((res) => ({ isSuccess : false, status : response.status, data:res }));
}

// return response.json().then((res) => ({ isSuccess :
// response.status === 200 || response.status === 201 || response.status === 204
// , status : response.status, data:res }));
}

export function handleError (error) {
console.log(error)
throw errors
}

3 changes: 3 additions & 0 deletions 판다마켓/api/common/constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


export const apiUrl = "https://sprint-mission-api.vercel.app"
41 changes: 41 additions & 0 deletions 판다마켓/api/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ArticleService } from "./services/ArticleService.js";
import { ProductService } from "./services/ProductService.js";




//articleservice
const getArticleList = await ArticleService.getArticleList(1, 10, "aa")
console.log(getArticleList)

const getArticle = await ArticleService.getArticle(2)
console.log(getArticle)

const createArticle = await ArticleService.createArticle('제목','내용2','sdf.jpg')
console.log(createArticle)

const patchArticle = await ArticleService.patchArticle('aaa','제목','내용2','sdf.jpg')
console.log(patchArticle)

const deleteArticle = await ArticleService.deleteArticle(764)
console.log(deleteArticle)




//productservice
const getProductList = await ProductService.getProductList(2, 2, "수정")
console.log(getProductList)

const getProduct = await ProductService.getProduct(2)
console.log(getProduct)

const createProduct = await ProductService.createProduct('수정','dd',123,'a',['asf'],['sef.jpg'])
console.log(createProduct)

const patchProduct = await ProductService.patchProduct(80, '수정', 'fff', '333', 'aaa', ['ssf'],['asf.jpg'])
console.log(patchProduct)

const deleteProduct = await ProductService.deleteProduct(370)
console.log(deleteProduct)

3 changes: 3 additions & 0 deletions 판다마켓/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type" : "module"
}
98 changes: 98 additions & 0 deletions 판다마켓/api/services/ArticleService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { responseInterceptor, handleError } from "../common/common.js";
import { apiUrl } from "../common/constant.js";

/**
* 게시글 목록 조회
*
* @param page 페이지
* @param pageSize 페이지당 몇개 올 지
* @param keyword 키워드
*/
function getArticleList(page, pageSize, keyword){
return fetch(`${apiUrl}/articles?page=${page}&pageSize=${pageSize}&keyword=${keyword}`)
.then((response) => responseInterceptor(response))
.catch((error) => handleError(error))
}

async function getArticle(id){

return fetch(`https://sprint-mission-api.vercel.app/articles/${id}`, {
method : 'GET',
headers : {
'Content-Type' : 'application/json',
}
})
.then((response) => responseInterceptor(response))
.catch((error) => handleError(error))

// .then(function(response){
// return responseInterceptor(response)
// })


}

/**
* 게시글생성
*
* @param title 제목
* @param content 내용
* @param img 이미지 링크
*/
function createArticle(title, content, img) {

if (!title || !content || !img) return false;
const requestBody = {
title : title,
content : content,
image : img
}
return fetch('https://sprint-mission-api.vercel.app/articles', {
method : 'POST',
body : JSON.stringify(requestBody),
headers : {
'Content-Type' : 'application/json',
}
})
.then((response) => responseInterceptor(response))
.catch((error) => handleError(error))
}

function patchArticle(id, title, content, img) {
const requestBody = {
title : title,
content : content,
image : img
}

return fetch(`https://sprint-mission-api.vercel.app/articles/${id}`, {
method : 'PATCH',
body : JSON.stringify(requestBody),
headers : {
'Content-Type' : 'application/json',
}
})
.then((response) => responseInterceptor(response))
.catch((error) => handleError(error))

}

function deleteArticle(id) {
return fetch(`https://sprint-mission-api.vercel.app/articles/${id}`, {
method : 'DELETE',
headers : {
'Content-Type' : 'application/json',
},
})
.then((response) => responseInterceptor(response))
.catch((error) => handleError(error))
}

export const ArticleService = {
getArticleList,
getArticle,
createArticle,
patchArticle,
deleteArticle
}

136 changes: 136 additions & 0 deletions 판다마켓/api/services/ProductService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// import { responseInterceptor, handleError } from "../common/common.js";
import { apiUrl } from "../common/constant.js";



async function getProductList(page, pageSize, keyword){
try {

const requestBody = {
page : page,
pageSize : pageSize,
keyword : keyword
}

const response = await fetch(`${apiUrl}/products?page=${page}&pageSize=${pageSize}&keyword=${keyword}`)
const data = await response.json()

if(response.status === 200 || response.status === 201 || response.status === 204) {
console.log('성공')
}
return {
isSuccess : true,
status : response.status,
data : data
}
} catch(err) {
console.log('이게 에러임', err)
return "err catch"
}
}

async function getProduct(id){
try{
const response = await fetch(`${apiUrl}/products/${id}`)

const data = await response.json()
return {
isSuccess : true,
status : response.status,
data : data
}
} catch(err) {
console.log('이게 에러임', err)
return "err catch"
}
}

async function createProduct(name, description, price, manufacturer, tags, images){
try{
const requestBody = {
name : name,
description : description,
price : price,
manufacturer : manufacturer,
tags: tags,
images: images
}

const response = await fetch(`${apiUrl}/products`, {
method : 'POST',
body : JSON.stringify(requestBody),
headers : {
'Content-Type' : 'application/json',
}
})

const data = await response.json()
return {
isSuccess : true,
status : response.status,
data : data
}
} catch(err) {
console.log('이게 에러임', err)
return "err catch"
}
}

async function patchProduct(id, name, description, price, tags, images){
try{
const requestBody = {
"name": name,
"description": description,
"price": price,
"tags": tags,
"images": images
}

const response = await fetch(`${apiUrl}/products/${id}`,{
method : 'PATCH',
body : JSON.stringify(requestBody),
headers : {
'Content-Type' : 'application/json',
}
})
const data = await response.json()
return {
isSuccess : true,
status : response.status,
data : data
}
} catch(err) {
console.log('이게 에러임', err)
return "err catch"
}
}

async function deleteProduct(id){
const response = await fetch(`${apiUrl}/products/${id}`, {
method : 'DELETE',
headers : {
'Content-Type' : 'application/json',
}
})
if (response.status === 204) {
return {
isSuccess : true,
status : response.status,
data : null
}
} else {
return {
isSuccess : false,
status : response.status,
data : null
}
}
}

export const ProductService = {
getProductList,
getProduct,
createProduct,
patchProduct,
deleteProduct
}

0 comments on commit 8ef0be7

Please sign in to comment.