init
This commit is contained in:
11
backend/eslint.config.js
Normal file
11
backend/eslint.config.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import antfu from '@antfu/eslint-config'
|
||||
|
||||
export default antfu({
|
||||
typescript: true,
|
||||
node: true,
|
||||
formatters: true,
|
||||
rules: {
|
||||
'no-console': 'warn',
|
||||
'node/prefer-global/process': 'off',
|
||||
},
|
||||
})
|
||||
29
backend/package.json
Normal file
29
backend/package.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "tmc-backend",
|
||||
"type": "module",
|
||||
"version": "1.0.0",
|
||||
"scripts": {
|
||||
"dev": "tsx watch src/index.ts",
|
||||
"build": "tsc && tsc-alias",
|
||||
"start": "node dist/index.js",
|
||||
"lint": "eslint ."
|
||||
},
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.19.2",
|
||||
"swagger-jsdoc": "^6.2.8",
|
||||
"swagger-ui-express": "^5.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@antfu/eslint-config": "^3.9.2",
|
||||
"@types/cors": "^2.8.17",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/node": "^22.0.0",
|
||||
"@types/swagger-jsdoc": "^6.0.4",
|
||||
"@types/swagger-ui-express": "^4.1.6",
|
||||
"eslint": "^9.0.0",
|
||||
"tsc-alias": "^1.8.10",
|
||||
"tsx": "^4.16.2",
|
||||
"typescript": "^5.5.3"
|
||||
}
|
||||
}
|
||||
4817
backend/pnpm-lock.yaml
generated
Normal file
4817
backend/pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
20
backend/src/index.ts
Normal file
20
backend/src/index.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import cors from 'cors'
|
||||
import express from 'express'
|
||||
import { itemsRouter } from './routes/items.js'
|
||||
import { setupSwagger } from './swagger.js'
|
||||
|
||||
const app = express()
|
||||
|
||||
app.use(cors({ origin: 'http://localhost:3000' }))
|
||||
app.use(express.json())
|
||||
|
||||
setupSwagger(app)
|
||||
app.use('/api/items', itemsRouter)
|
||||
|
||||
const PORT = 1337
|
||||
app.listen(PORT, () => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(`Server running on http://localhost:${PORT}`)
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(`Swagger UI: http://localhost:${PORT}/docs`)
|
||||
})
|
||||
5
backend/src/middleware/batcher.ts
Normal file
5
backend/src/middleware/batcher.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { NextFunction, Request, Response } from 'express'
|
||||
|
||||
export function batcherMiddleware(_req: Request, _res: Response, next: NextFunction): void {
|
||||
next()
|
||||
}
|
||||
259
backend/src/routes/items.ts
Normal file
259
backend/src/routes/items.ts
Normal file
@@ -0,0 +1,259 @@
|
||||
import { Router } from 'express'
|
||||
import * as store from '../services/itemsStore.js'
|
||||
import { queue } from '../services/queue.js'
|
||||
|
||||
export const itemsRouter = Router()
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* components:
|
||||
* schemas:
|
||||
* Item:
|
||||
* type: object
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* PaginatedItems:
|
||||
* type: object
|
||||
* properties:
|
||||
* data:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Item'
|
||||
* total:
|
||||
* type: integer
|
||||
* page:
|
||||
* type: integer
|
||||
* limit:
|
||||
* type: integer
|
||||
* hasMore:
|
||||
* type: boolean
|
||||
*/
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /api/items:
|
||||
* get:
|
||||
* summary: Get unselected items
|
||||
* tags: [Items]
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: page
|
||||
* schema:
|
||||
* type: integer
|
||||
* default: 1
|
||||
* - in: query
|
||||
* name: limit
|
||||
* schema:
|
||||
* type: integer
|
||||
* default: 20
|
||||
* maximum: 20
|
||||
* - in: query
|
||||
* name: search
|
||||
* schema:
|
||||
* type: string
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Paginated list of unselected items
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/PaginatedItems'
|
||||
*/
|
||||
itemsRouter.get('/', (req, res) => {
|
||||
const page = Math.max(1, Number(req.query.page) || 1)
|
||||
const limit = Math.min(20, Math.max(1, Number(req.query.limit) || 20))
|
||||
const search = req.query.search ? String(req.query.search) : undefined
|
||||
res.json(store.getItems(page, limit, search))
|
||||
})
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /api/items/selected:
|
||||
* get:
|
||||
* summary: Get selected items in order
|
||||
* tags: [Items]
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: page
|
||||
* schema:
|
||||
* type: integer
|
||||
* default: 1
|
||||
* - in: query
|
||||
* name: limit
|
||||
* schema:
|
||||
* type: integer
|
||||
* default: 20
|
||||
* maximum: 20
|
||||
* - in: query
|
||||
* name: search
|
||||
* schema:
|
||||
* type: string
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Paginated list of selected items
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/PaginatedItems'
|
||||
*/
|
||||
itemsRouter.get('/selected', (req, res) => {
|
||||
const page = Math.max(1, Number(req.query.page) || 1)
|
||||
const limit = Math.min(20, Math.max(1, Number(req.query.limit) || 20))
|
||||
const search = req.query.search ? String(req.query.search) : undefined
|
||||
res.json(store.getSelectedItems(page, limit, search))
|
||||
})
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /api/items/add:
|
||||
* post:
|
||||
* summary: Queue an item for addition
|
||||
* tags: [Items]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [id]
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Item queued
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* queued:
|
||||
* type: boolean
|
||||
* id:
|
||||
* type: integer
|
||||
* 400:
|
||||
* description: Invalid id
|
||||
*/
|
||||
itemsRouter.post('/add', (req, res) => {
|
||||
const id = req.body?.id
|
||||
if (id === undefined || id === null || typeof id !== 'number' || !Number.isInteger(id)) {
|
||||
res.status(400).json({ error: 'id must be an integer' })
|
||||
return
|
||||
}
|
||||
queue.enqueue({ type: 'add', id })
|
||||
res.json({ queued: true, id })
|
||||
})
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /api/items/select:
|
||||
* post:
|
||||
* summary: Queue an item for selection (move to right panel)
|
||||
* tags: [Items]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [id]
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Item queued
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* queued:
|
||||
* type: boolean
|
||||
*/
|
||||
itemsRouter.post('/select', (req, res) => {
|
||||
const id = req.body?.id
|
||||
if (id === undefined || id === null || typeof id !== 'number' || !Number.isInteger(id)) {
|
||||
res.status(400).json({ error: 'id must be an integer' })
|
||||
return
|
||||
}
|
||||
queue.enqueue({ type: 'select', id })
|
||||
res.json({ queued: true })
|
||||
})
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /api/items/deselect:
|
||||
* post:
|
||||
* summary: Queue an item for deselection (move back to left panel)
|
||||
* tags: [Items]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [id]
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Item queued
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* queued:
|
||||
* type: boolean
|
||||
*/
|
||||
itemsRouter.post('/deselect', (req, res) => {
|
||||
const id = req.body?.id
|
||||
if (id === undefined || id === null || typeof id !== 'number' || !Number.isInteger(id)) {
|
||||
res.status(400).json({ error: 'id must be an integer' })
|
||||
return
|
||||
}
|
||||
queue.enqueue({ type: 'deselect', id })
|
||||
res.json({ queued: true })
|
||||
})
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /api/items/reorder:
|
||||
* put:
|
||||
* summary: Queue a reorder of selected items
|
||||
* tags: [Items]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [ids]
|
||||
* properties:
|
||||
* ids:
|
||||
* type: array
|
||||
* items:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Reorder queued
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* queued:
|
||||
* type: boolean
|
||||
*/
|
||||
itemsRouter.put('/reorder', (req, res) => {
|
||||
const ids = req.body?.ids
|
||||
if (!Array.isArray(ids)) {
|
||||
res.status(400).json({ error: 'ids must be an array' })
|
||||
return
|
||||
}
|
||||
queue.enqueue({ type: 'reorder', ids })
|
||||
res.json({ queued: true })
|
||||
})
|
||||
79
backend/src/services/itemsStore.ts
Normal file
79
backend/src/services/itemsStore.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
export interface PaginatedResult {
|
||||
data: { id: number }[]
|
||||
total: number
|
||||
page: number
|
||||
limit: number
|
||||
hasMore: boolean
|
||||
}
|
||||
|
||||
const allItems: number[] = Array.from({ length: 1_000_000 }, (_, i) => i + 1)
|
||||
const selectedIds = new Set<number>()
|
||||
const orderedSelected: number[] = []
|
||||
|
||||
export function getItems(page: number, limit: number, search?: string): PaginatedResult {
|
||||
let filtered: number[]
|
||||
if (search) {
|
||||
filtered = allItems.filter(id => !selectedIds.has(id) && String(id).includes(search))
|
||||
}
|
||||
else {
|
||||
filtered = allItems.filter(id => !selectedIds.has(id))
|
||||
}
|
||||
const total = filtered.length
|
||||
const start = (page - 1) * limit
|
||||
const slice = filtered.slice(start, start + limit)
|
||||
return {
|
||||
data: slice.map(id => ({ id })),
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
hasMore: start + limit < total,
|
||||
}
|
||||
}
|
||||
|
||||
export function getSelectedItems(page: number, limit: number, search?: string): PaginatedResult {
|
||||
let filtered: number[]
|
||||
if (search) {
|
||||
filtered = orderedSelected.filter(id => String(id).includes(search))
|
||||
}
|
||||
else {
|
||||
filtered = [...orderedSelected]
|
||||
}
|
||||
const total = filtered.length
|
||||
const start = (page - 1) * limit
|
||||
const slice = filtered.slice(start, start + limit)
|
||||
return {
|
||||
data: slice.map(id => ({ id })),
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
hasMore: start + limit < total,
|
||||
}
|
||||
}
|
||||
|
||||
export function addItem(id: number): boolean {
|
||||
if (allItems.includes(id) || selectedIds.has(id)) return false
|
||||
allItems.push(id)
|
||||
return true
|
||||
}
|
||||
|
||||
export function selectItem(id: number): boolean {
|
||||
if (selectedIds.has(id)) return false
|
||||
selectedIds.add(id)
|
||||
orderedSelected.push(id)
|
||||
return true
|
||||
}
|
||||
|
||||
export function deselectItem(id: number): boolean {
|
||||
if (!selectedIds.has(id)) return false
|
||||
selectedIds.delete(id)
|
||||
const idx = orderedSelected.indexOf(id)
|
||||
if (idx !== -1) orderedSelected.splice(idx, 1)
|
||||
return true
|
||||
}
|
||||
|
||||
export function reorderSelected(ids: number[]): void {
|
||||
orderedSelected.length = 0
|
||||
for (const id of ids) {
|
||||
if (selectedIds.has(id)) orderedSelected.push(id)
|
||||
}
|
||||
}
|
||||
65
backend/src/services/queue.ts
Normal file
65
backend/src/services/queue.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import * as store from './itemsStore.js'
|
||||
|
||||
interface AddTask { type: 'add', id: number }
|
||||
interface SelectTask { type: 'select', id: number }
|
||||
interface DeselectTask { type: 'deselect', id: number }
|
||||
interface ReorderTask { type: 'reorder', ids: number[] }
|
||||
|
||||
type Task = AddTask | SelectTask | DeselectTask | ReorderTask
|
||||
|
||||
class RequestQueue {
|
||||
private addQueue: AddTask[] = []
|
||||
private actionQueue: ReorderTask[] = []
|
||||
private pendingKeys = new Set<string>()
|
||||
private pendingReorder: ReorderTask | null = null
|
||||
|
||||
constructor() {
|
||||
setInterval(() => this.flushAdd(), 10_000)
|
||||
setInterval(() => this.flushActions(), 1_000)
|
||||
}
|
||||
|
||||
enqueue(task: Task): boolean {
|
||||
if (task.type === 'reorder') {
|
||||
this.pendingReorder = task
|
||||
this.actionQueue = this.actionQueue.filter(t => t.type !== 'reorder')
|
||||
this.actionQueue.push(task)
|
||||
return true
|
||||
}
|
||||
|
||||
const key = `${task.type}:${(task as AddTask | SelectTask | DeselectTask).id}`
|
||||
if (this.pendingKeys.has(key)) return false
|
||||
this.pendingKeys.add(key)
|
||||
|
||||
if (task.type === 'add') {
|
||||
this.addQueue.push(task)
|
||||
if (this.addQueue.length >= 100) this.flushAdd()
|
||||
}
|
||||
else {
|
||||
// select/deselect применяются немедленно, чтобы GET после POST видел актуальный state
|
||||
if (task.type === 'select') store.selectItem(task.id)
|
||||
else if (task.type === 'deselect') store.deselectItem(task.id)
|
||||
this.pendingKeys.delete(key)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private flushAdd(): void {
|
||||
const batch = this.addQueue.splice(0)
|
||||
for (const task of batch) {
|
||||
store.addItem(task.id)
|
||||
this.pendingKeys.delete(`add:${task.id}`)
|
||||
}
|
||||
}
|
||||
|
||||
private flushActions(): void {
|
||||
const batch = this.actionQueue.splice(0)
|
||||
this.pendingReorder = null
|
||||
for (const task of batch) {
|
||||
if (task.type === 'reorder') {
|
||||
store.reorderSelected(task.ids)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const queue = new RequestQueue()
|
||||
23
backend/src/swagger.ts
Normal file
23
backend/src/swagger.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { Express } from 'express'
|
||||
import swaggerJsdoc from 'swagger-jsdoc'
|
||||
import swaggerUi from 'swagger-ui-express'
|
||||
|
||||
const options: swaggerJsdoc.Options = {
|
||||
definition: {
|
||||
openapi: '3.0.0',
|
||||
info: {
|
||||
title: 'TMC Items API',
|
||||
version: '1.0.0',
|
||||
description: 'API for managing items with selection and ordering',
|
||||
},
|
||||
servers: [{ url: 'http://localhost:1337' }],
|
||||
},
|
||||
apis: ['./src/routes/*.ts'],
|
||||
}
|
||||
|
||||
const spec = swaggerJsdoc(options)
|
||||
|
||||
export function setupSwagger(app: Express): void {
|
||||
app.get('/docs/json', (_req, res) => res.json(spec))
|
||||
app.use('/docs', swaggerUi.serve, swaggerUi.setup(spec))
|
||||
}
|
||||
17
backend/tsconfig.json
Normal file
17
backend/tsconfig.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
Reference in New Issue
Block a user