Skip to content

Commit

Permalink
feat: api呼び出し側で型を組み立てる
Browse files Browse the repository at this point in the history
  • Loading branch information
kuroweb committed Nov 8, 2024
1 parent 686cc7d commit 0736721
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 21 deletions.
37 changes: 30 additions & 7 deletions volumes/frontend/api/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ export type Product = {
name: string
createdAt: string
updatedAt: string
iosysCrawlSetting: IosysCrawlSetting
janparaCrawlSetting: JanparaCrawlSetting
mercariCrawlSetting: MercariCrawlSetting
pcKoubouCrawlSetting: PcKoubouCrawlSetting
usedSofmapCrawlSetting: UsedSofmapCrawlSetting
yahooAuctionCrawlSetting: YahooAuctionCrawlSetting
category: Category
}

export type YahooAuctionCrawlSetting = {
Expand All @@ -20,6 +13,24 @@ export type YahooAuctionCrawlSetting = {
minPrice: number
maxPrice: number
enabled: boolean
createdAt: string
updatedAt: string
}

export type YahooAuctionCrawlSettingExcludeKeyword = {
id: number
yahooAuctionCrawlSettingId: number
keyword: string
createdAt: string
updatedAt: string
}

export type YahooAuctionCrawlSettingRequiredKeyword = {
id: number
yahooAuctionCrawlSettingId: number
keyword: string
createdAt: string
updatedAt: string
}

export type MercariCrawlSetting = {
Expand All @@ -30,6 +41,8 @@ export type MercariCrawlSetting = {
minPrice: number
maxPrice: number
enabled: boolean
createdAt: string
updatedAt: string
}

export type JanparaCrawlSetting = {
Expand All @@ -39,6 +52,8 @@ export type JanparaCrawlSetting = {
minPrice: number
maxPrice: number
enabled: boolean
createdAt: string
updatedAt: string
}

export type IosysCrawlSetting = {
Expand All @@ -48,6 +63,8 @@ export type IosysCrawlSetting = {
minPrice: number
maxPrice: number
enabled: boolean
createdAt: string
updatedAt: string
}

export type PcKoubouCrawlSetting = {
Expand All @@ -57,6 +74,8 @@ export type PcKoubouCrawlSetting = {
minPrice: number
maxPrice: number
enabled: boolean
createdAt: string
updatedAt: string
}

export type UsedSofmapCrawlSetting = {
Expand All @@ -66,10 +85,14 @@ export type UsedSofmapCrawlSetting = {
minPrice: number
maxPrice: number
enabled: boolean
createdAt: string
updatedAt: string
}

export type Category = {
id: number
parentId: number | null
name: string
createdAt: string
updatedAt: string
}
71 changes: 59 additions & 12 deletions volumes/frontend/api/products.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import type { Product } from './model'
import type {
Category,
IosysCrawlSetting,
JanparaCrawlSetting,
MercariCrawlSetting,
PcKoubouCrawlSetting,
Product,
UsedSofmapCrawlSetting,
YahooAuctionCrawlSetting,
} from '@/api'

import { http } from '@/lib/axios-client'

// GET: /api/v1/products

export interface GetProductsParams {
// TODO: 後で実装する
}

// GET

export interface GetProducts {
products: Product[]
type GetProducts = {
products: (Product & {
iosysCrawlSetting: IosysCrawlSetting
janparaCrawlSetting: JanparaCrawlSetting
mercariCrawlSetting: MercariCrawlSetting
pcKoubouCrawlSetting: PcKoubouCrawlSetting
usedSofmapCrawlSetting: UsedSofmapCrawlSetting
yahooAuctionCrawlSetting: YahooAuctionCrawlSetting
category: Category
})[]
}

export async function getProducts(params?: GetProductsParams) {
Expand All @@ -19,9 +36,19 @@ export async function getProducts(params?: GetProductsParams) {
})
}

// CREATE
// GET: /api/v1/products

// export interface GetProduct extends Product {}

// export async function getProduct(productId: number) {
// return http<GetProduct>(`/api/v1/products/${productId}`, {
// method: 'GET',
// })
// }

// POST: /api/v1/products

export interface CreateProductData {
export type CreateProductData = {
name: string
categoryId: number
yahooAuctionCrawlSetting: {
Expand Down Expand Up @@ -64,7 +91,17 @@ export interface CreateProductData {
}
}

export interface CreateProduct extends Product {}
export type CreateProduct = {
products: Product & {
iosysCrawlSetting: IosysCrawlSetting
janparaCrawlSetting: JanparaCrawlSetting
mercariCrawlSetting: MercariCrawlSetting
pcKoubouCrawlSetting: PcKoubouCrawlSetting
usedSofmapCrawlSetting: UsedSofmapCrawlSetting
yahooAuctionCrawlSetting: YahooAuctionCrawlSetting
category: Category
}
}

export async function createProduct(data: CreateProductData) {
return http<CreateProduct>('/api/v1/products', {
Expand All @@ -73,9 +110,9 @@ export async function createProduct(data: CreateProductData) {
})
}

// UPDATE
// PUT: /api/v1/products/:productId

export interface UpdateProductData {
export type UpdateProductData = {
name: string
categoryId: number
yahooAuctionCrawlSetting: {
Expand Down Expand Up @@ -118,7 +155,17 @@ export interface UpdateProductData {
}
}

export interface UpdateProduct extends Product {}
export type UpdateProduct = {
products: Product & {
iosysCrawlSetting: IosysCrawlSetting
janparaCrawlSetting: JanparaCrawlSetting
mercariCrawlSetting: MercariCrawlSetting
pcKoubouCrawlSetting: PcKoubouCrawlSetting
usedSofmapCrawlSetting: UsedSofmapCrawlSetting
yahooAuctionCrawlSetting: YahooAuctionCrawlSetting
category: Category
}
}

export async function updateProduct(productId: number, data: UpdateProductData) {
return http<UpdateProduct>(`/api/v1/products/${productId}`, {
Expand All @@ -127,7 +174,7 @@ export async function updateProduct(productId: number, data: UpdateProductData)
})
}

// DELETE
// DELETE: /api/v1/products/:productId

export async function destroyProduct(productId: number) {
return http(`/api/v1/products/${productId}`, {
Expand Down
7 changes: 5 additions & 2 deletions volumes/frontend/server-actions/api.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use server'

import * as Api from '../api'

import * as Api from '@/api'
import { revalidateAdminPaths, revalidateProductsPaths } from '@/lib/revalidate-paths'

export async function getProducts(params?: Api.GetProductsParams) {
return await Api.getProducts(params)
}

// export async function getProduct(productId: number) {
// return await Api.getProduct(productId)
// }

export async function createProduct(params: Api.CreateProductData) {
const res = await Api.createProduct(params)
revalidateAdminPaths()
Expand Down

0 comments on commit 0736721

Please sign in to comment.