upd
This commit is contained in:
@@ -137,6 +137,47 @@ itemsRouter.get('/selected', (req, res) => {
|
||||
* 400:
|
||||
* description: Invalid id
|
||||
*/
|
||||
/**
|
||||
* @openapi
|
||||
* /api/items/add-value:
|
||||
* post:
|
||||
* summary: Create a new item with a given value
|
||||
* tags: [Items]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [value]
|
||||
* properties:
|
||||
* value:
|
||||
* type: string
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Item created
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* value:
|
||||
* type: string
|
||||
* 400:
|
||||
* description: Invalid value
|
||||
*/
|
||||
itemsRouter.post('/add-value', (req, res) => {
|
||||
const value = req.body?.value
|
||||
if (typeof value !== 'string' || value.trim() === '') {
|
||||
res.status(400).json({ error: 'value must be a non-empty string' })
|
||||
return
|
||||
}
|
||||
const id = store.addItemByValue(value.trim())
|
||||
res.json({ id, value: value.trim() })
|
||||
})
|
||||
|
||||
itemsRouter.post('/add', (req, res) => {
|
||||
const id = req.body?.id
|
||||
if (id === undefined || id === null || typeof id !== 'number' || !Number.isInteger(id)) {
|
||||
|
||||
@@ -87,6 +87,15 @@ export function addItem(id: number): boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
let nextAutoId = 1_000_001
|
||||
|
||||
export function addItemByValue(value: string): number {
|
||||
const id = nextAutoId++
|
||||
itemsById.set(id, value)
|
||||
allItemsOrder.push(id)
|
||||
return id
|
||||
}
|
||||
|
||||
export function selectItem(id: number): boolean {
|
||||
if (selectedIds.has(id) || !itemsById.has(id)) return false
|
||||
selectedIds.add(id)
|
||||
|
||||
Reference in New Issue
Block a user