|
|
@@ -1,20 +1,50 @@
|
|
|
import { Request, Response } from 'express'
|
|
|
-import { EquipmentData } from '../database/models/EquipmentData'
|
|
|
+import { User } from '../database/models/User'
|
|
|
+import { Equipment } from '../database/models/Equipment'
|
|
|
import { sequelize } from '../database'
|
|
|
+import { networkError } from './Error'
|
|
|
|
|
|
class EquipmentController {
|
|
|
async getEquipments(req: Request, res: Response) {
|
|
|
try {
|
|
|
const result = await sequelize.query(
|
|
|
- 'SELECT * FROM equipment_data WHERE id IN (SELECT max(id) FROM equipment_data GROUP BY aid_id)'
|
|
|
+ `SELECT * FROM equipment_data
|
|
|
+ JOIN equipments ON aid_id = equipments.id
|
|
|
+ WHERE equipment_data.id IN
|
|
|
+ (SELECT max(id) FROM equipment_data GROUP BY aid_id)
|
|
|
+ `
|
|
|
)
|
|
|
|
|
|
res.json(result[0])
|
|
|
- } catch (err) {
|
|
|
- res.status(500).json({
|
|
|
- msg: 'Internal network error',
|
|
|
- reason: JSON.stringify(err)
|
|
|
- })
|
|
|
+ } catch (error) {
|
|
|
+ networkError(res, JSON.stringify(error))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async assignUserToEquipment(req: Request, res: Response) {
|
|
|
+ try {
|
|
|
+ const { user_id: userId, equipment_id: equipmentId } = req.body
|
|
|
+ const user = await User.findByPk(userId)
|
|
|
+ const equipment = await Equipment.findByPk(equipmentId)
|
|
|
+
|
|
|
+ if (!user) {
|
|
|
+ res.status(400).json({
|
|
|
+ message: 'User not found'
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!equipment) {
|
|
|
+ res.status(400).json({
|
|
|
+ message: 'Equipment not found'
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ await equipment.update({ current_user_id: user.id })
|
|
|
+ res.status(200).json({ message: 'ok' })
|
|
|
+ } catch (error) {
|
|
|
+ networkError(res, JSON.stringify(error))
|
|
|
}
|
|
|
}
|
|
|
}
|