Files
points-mall/supplied-service/src/controllers/product.controller.ts
T

54 lines
2.4 KiB
TypeScript

import type { Context } from '../utils/handler.js'
import { guardMethod } from '../utils/method.js'
import { readJson } from '../utils/request.js'
import { withErrorBoundary } from '../utils/handler.js'
import { productService } from '../services/product.service.js'
import { toHttpResponse } from './controller-response.js'
export const productListController = withErrorBoundary(async (request: Request, _context: Context) => {
const methodResponse = guardMethod(request, ['GET'])
if (methodResponse) return methodResponse
const url = new URL(request.url)
const keyword = (url.searchParams.get('keyword') || '').trim()
const categoryId = (url.searchParams.get('categoryId') || '').trim()
return toHttpResponse(await productService.listProducts(keyword, categoryId))
})
export const productDetailController = withErrorBoundary(async (request: Request, _context: Context) => {
const methodResponse = guardMethod(request, ['GET'])
if (methodResponse) return methodResponse
const url = new URL(request.url)
const id = (url.searchParams.get('id') || '').trim()
const sku = (url.searchParams.get('sku') || '').trim()
return toHttpResponse(await productService.getProductDetail(id, sku))
})
export const merchantProductListController = withErrorBoundary(async (request: Request, _context: Context) => {
const methodResponse = guardMethod(request, ['POST'])
if (methodResponse) return methodResponse
const body = await readJson<{ keyword?: string; categoryId?: string; pageNumber?: number; pageSize?: number }>(request)
return toHttpResponse(await productService.listManageProducts(String(body.keyword || '').trim(), String(body.categoryId || '').trim(), body.pageNumber, body.pageSize))
})
export const merchantProductSaveController = withErrorBoundary(async (request: Request, _context: Context) => {
const methodResponse = guardMethod(request, ['POST'])
if (methodResponse) return methodResponse
return toHttpResponse(await productService.saveManageProduct(
await readJson<Parameters<typeof productService.saveManageProduct>[0]>(request)
))
})
export const merchantProductDeleteController = withErrorBoundary(async (request: Request, _context: Context) => {
const methodResponse = guardMethod(request, ['POST'])
if (methodResponse) return methodResponse
const body = await readJson<{ id?: number }>(request)
return toHttpResponse(await productService.deleteManageProduct(Number(body.id || 0)))
})