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

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