Add WIP delete route & service for locality

This commit is contained in:
ShootingStar91
2024-08-24 15:54:29 +03:00
parent bb08fc8905
commit a313fc6248
2 changed files with 34 additions and 0 deletions
+7
View File
@@ -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
+27
View File
@@ -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()
}