feat: add public listing catalog boundary
This commit is contained in:
@@ -121,6 +121,21 @@ export function createListingRepository(db) {
|
||||
};
|
||||
},
|
||||
|
||||
async findPublicById(listingId) {
|
||||
const [result] = await db
|
||||
.select({ listing: listings })
|
||||
.from(listings)
|
||||
.innerJoin(inventoryItems, eq(inventoryItems.id, listings.inventoryItemId))
|
||||
.innerJoin(products, eq(products.id, inventoryItems.productId))
|
||||
.where(and(
|
||||
eq(listings.id, listingId),
|
||||
inArray(listings.status, ['AVAILABLE', 'PENDING']),
|
||||
eq(inventoryItems.status, 'ACTIVE'),
|
||||
));
|
||||
|
||||
return result?.listing ?? null;
|
||||
},
|
||||
|
||||
async updateStatus(listingId, status) {
|
||||
const [updatedListing] = await db
|
||||
.update(listings)
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { toPublicListingDto } from '@pokemon-inventory/contracts/public-listing';
|
||||
|
||||
function coerceNumber(value, fallback) {
|
||||
if (value === undefined || value === null || value === '') {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const parsed = Number.parseInt(String(value), 10);
|
||||
return Number.isNaN(parsed) ? fallback : parsed;
|
||||
}
|
||||
|
||||
export async function registerPublicListingRoutes(app, options) {
|
||||
const { publicListingService } = options;
|
||||
|
||||
app.get('/public/listings', async (request) => {
|
||||
const result = await publicListingService.list({
|
||||
page: coerceNumber(request.query.page, 1),
|
||||
limit: coerceNumber(request.query.limit, 20),
|
||||
});
|
||||
|
||||
return {
|
||||
data: {
|
||||
...result,
|
||||
items: result.items.map(toPublicListingDto),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
app.get('/public/listings/:id', async (request) => ({
|
||||
data: toPublicListingDto(await publicListingService.getById(request.params.id)),
|
||||
}));
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { createApplicationError } from '../../../shared/errors/application-error.js';
|
||||
|
||||
export function createPublicListingService({ listingRepository }) {
|
||||
return {
|
||||
async list(input = {}) {
|
||||
return listingRepository.searchPublic({
|
||||
query: '',
|
||||
page: input.page,
|
||||
limit: input.limit,
|
||||
});
|
||||
},
|
||||
|
||||
async getById(listingId) {
|
||||
const listing = await listingRepository.findPublicById(listingId);
|
||||
|
||||
if (!listing) {
|
||||
throw createApplicationError('LISTING_NOT_FOUND', 'Listing was not found.', 404);
|
||||
}
|
||||
|
||||
return listing;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user