booking.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const express = require('express');
  2. const { sequelize } = require('../models');
  3. const { QueryTypes } = require('sequelize');
  4. const router = express.Router();
  5. router.post('/', async (req, res) => {
  6. try {
  7. await sequelize.query(`
  8. INSERT INTO bookings (client_id, hall_id, photographer_id, start_time, end_time, total_cost, status)
  9. VALUES (:clientId, :hallId, :photographerId, :startTime, :endTime, :totalCost, :status)
  10. `, {
  11. logging: false,
  12. type: QueryTypes.INSERT,
  13. replacements: {
  14. clientId: req.body.clientId,
  15. hallId: req.body.hallId,
  16. photographerId: req.body.photographerId || null,
  17. startTime: req.body.startTime,
  18. endTime: req.body.endTime,
  19. totalCost: req.body.totalCost,
  20. status: req.body.status || 'pending'
  21. }
  22. });
  23. res.status(201).send('Бронирование успешно создано');
  24. } catch (error) {
  25. console.warn('Ошибка при создании бронирования:', error.message);
  26. res.status(500).send(error.message);
  27. } finally {
  28. res.end();
  29. }
  30. });
  31. router.get('/', async (req, res) => {
  32. try {
  33. const bookings = await sequelize.query(`
  34. SELECT *
  35. FROM bookings
  36. `, {
  37. logging: false,
  38. type: QueryTypes.SELECT
  39. });
  40. res.json(bookings);
  41. } catch (error) {
  42. res.status(500).send(error.message);
  43. }
  44. });
  45. router.patch('/:id(\\d+)', async (req, res) => {
  46. try {
  47. await sequelize.query(`
  48. UPDATE bookings
  49. SET
  50. status = :status,
  51. total_cost = :totalCost
  52. WHERE booking_id = :id
  53. `, {
  54. logging: false,
  55. replacements: {
  56. id: req.params.id,
  57. status: req.body.status,
  58. totalCost: req.body.totalCost
  59. }
  60. });
  61. res.send('Бронирование обновлено');
  62. } catch (error) {
  63. res.status(500).send(error.message);
  64. } finally {
  65. res.end();
  66. }
  67. });
  68. router.delete('/:id(\\d+)', async (req, res) => {
  69. try {
  70. await sequelize.query(`
  71. DELETE FROM bookings WHERE booking_id = :id
  72. `, {
  73. logging: false,
  74. replacements: {
  75. id: req.params.id
  76. }
  77. });
  78. res.status(204).send();
  79. } catch (error) {
  80. res.status(500).send(error.message);
  81. } finally {
  82. res.end();
  83. }
  84. });
  85. module.exports = router;