mirror of
https://github.com/nowcommunity/nowdatabase.git
synced 2026-10-12 19:13:52 +03:00
Add WIP delete route & service for locality
This commit is contained in:
@@ -5,6 +5,7 @@ import { EditDataType, EditMetaData, LocalityDetailsType } from '../../../fronte
|
||||
import { write } from '../services/write/write'
|
||||
import { requireOneOf } from '../middlewares/authorizer'
|
||||
import { Role } from '../../../frontend/src/types'
|
||||
import { deleteLocality } from '../services/write/delete'
|
||||
|
||||
const router = Router()
|
||||
|
||||
@@ -47,4 +48,10 @@ router.put(
|
||||
}
|
||||
)
|
||||
|
||||
router.delete('/:id', requireOneOf([Role.Admin]), async (req, res) => {
|
||||
const id = parseInt(req.params.id)
|
||||
await deleteLocality(id)
|
||||
return res.status(200).send()
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { PoolConnection } from 'mariadb'
|
||||
import { AllowedTables } from './writeUtils'
|
||||
import { NOW_DB_NAME } from '../../utils/config'
|
||||
import { pool } from '../../utils/db'
|
||||
import { logger } from '../../utils/logger'
|
||||
|
||||
const deleteFromTable = async (
|
||||
table: AllowedTables,
|
||||
idColumn: string,
|
||||
id: string | number,
|
||||
returnColumns: string[],
|
||||
connection: PoolConnection
|
||||
) => {
|
||||
return await connection.query<Array<{ [key: string]: string | number }>>(
|
||||
`DELETE FROM ${NOW_DB_NAME}.${table} WHERE ${idColumn} = ? RETURNING ${returnColumns.join(', ')}`,
|
||||
[id]
|
||||
)
|
||||
}
|
||||
|
||||
export const deleteLocality = async (id: string | number) => {
|
||||
const connection = await pool.getConnection()
|
||||
const deletedLocalitySpecies = await deleteFromTable('now_ls', 'lid', id, ['lid', 'species_id'], connection)
|
||||
logger.info(`Deleted following locality species: ${deletedLocalitySpecies.toString()}`)
|
||||
await deleteFromTable('now_loc', 'lid', id, ['lid'], connection)
|
||||
// TODO: Log, inside a transaction
|
||||
await connection.end()
|
||||
}
|
||||
Reference in New Issue
Block a user