Market Data Service is a high-performance financial data API that provides comprehensive Symbol prices of different markets through both RESTful endpoints and real-time WebSocket connections.

symbolController.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. const { Symbol } = require('../models');
  2. const { Op } = require('sequelize');
  3. class SymbolController {
  4. // Get all symbols with optional filtering
  5. async getAllSymbols(req, res, next) {
  6. try {
  7. const {
  8. exchange,
  9. instrumentType,
  10. isActive,
  11. limit = 1000,
  12. offset = 0
  13. } = req.query;
  14. const where = {};
  15. if (exchange) where.exchange = exchange;
  16. if (instrumentType) where.instrumentType = instrumentType;
  17. if (isActive !== undefined && isActive !== '') {
  18. where.isActive = isActive === 'true' || isActive === true;
  19. }
  20. const symbols = await Symbol.findAndCountAll({
  21. where,
  22. limit: parseInt(limit),
  23. offset: parseInt(offset),
  24. order: [['symbol', 'ASC']]
  25. });
  26. res.json({
  27. success: true,
  28. data: symbols.rows,
  29. pagination: {
  30. total: symbols.count,
  31. limit: parseInt(limit),
  32. offset: parseInt(offset),
  33. hasMore: offset + symbols.rows.length < symbols.count
  34. }
  35. });
  36. } catch (error) {
  37. next(error);
  38. }
  39. }
  40. // Get symbol by ID
  41. async getSymbolById(req, res, next) {
  42. try {
  43. const { id } = req.params;
  44. const symbol = await Symbol.findByPk(id);
  45. if (!symbol) {
  46. const error = new Error('Symbol not found');
  47. error.statusCode = 404;
  48. return next(error);
  49. }
  50. res.json({
  51. success: true,
  52. data: symbol
  53. });
  54. } catch (error) {
  55. next(error);
  56. }
  57. }
  58. // Create new symbol
  59. async createSymbol(req, res, next) {
  60. try {
  61. const symbol = await Symbol.create(req.body);
  62. res.status(201).json({
  63. success: true,
  64. data: symbol,
  65. message: 'Symbol created successfully'
  66. });
  67. } catch (error) {
  68. next(error);
  69. }
  70. }
  71. // Update symbol
  72. async updateSymbol(req, res, next) {
  73. try {
  74. const { id } = req.params;
  75. const [updatedRowsCount] = await Symbol.update(req.body, {
  76. where: { id }
  77. });
  78. if (updatedRowsCount === 0) {
  79. const error = new Error('Symbol not found');
  80. error.statusCode = 404;
  81. return next(error);
  82. }
  83. const updatedSymbol = await Symbol.findByPk(id);
  84. res.json({
  85. success: true,
  86. data: updatedSymbol,
  87. message: 'Symbol updated successfully'
  88. });
  89. } catch (error) {
  90. next(error);
  91. }
  92. }
  93. // Delete symbol (soft delete by setting isActive to false)
  94. async deleteSymbol(req, res, next) {
  95. try {
  96. const { id } = req.params;
  97. const [updatedRowsCount] = await Symbol.update(
  98. { isActive: false },
  99. { where: { id } }
  100. );
  101. if (updatedRowsCount === 0) {
  102. const error = new Error('Symbol not found');
  103. error.statusCode = 404;
  104. return next(error);
  105. }
  106. res.json({
  107. success: true,
  108. message: 'Symbol deactivated successfully'
  109. });
  110. } catch (error) {
  111. next(error);
  112. }
  113. }
  114. // Search symbols by symbol name
  115. async searchSymbols(req, res, next) {
  116. try {
  117. const { q, limit = 20 } = req.query;
  118. if (!q) {
  119. const error = new Error('Search query is required');
  120. error.statusCode = 400;
  121. return next(error);
  122. }
  123. const symbols = await Symbol.findAll({
  124. where: {
  125. symbol: {
  126. [Op.iLike]: `%${q}%`
  127. },
  128. isActive: true
  129. },
  130. limit: parseInt(limit),
  131. order: [['symbol', 'ASC']]
  132. });
  133. res.json({
  134. success: true,
  135. data: symbols
  136. });
  137. } catch (error) {
  138. next(error);
  139. }
  140. }
  141. }
  142. module.exports = new SymbolController();