rating.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. import RatingModel from '../../models/ugc/rating'
  3. class Rating {
  4. constructor(){
  5. this.type = ['ratings', 'scores', 'tags'];
  6. this.getRatings = this.getRatings.bind(this);
  7. this.getScores = this.getScores.bind(this);
  8. this.getTags = this.getTags.bind(this);
  9. }
  10. async initData(restaurant_id){
  11. try{
  12. const status = await RatingModel.initData(restaurant_id);
  13. if (status) {
  14. console.log('初始化评论数据成功');
  15. }
  16. }catch(err){
  17. console.log('初始化评论数据失败');
  18. throw new Error(err);
  19. }
  20. }
  21. async getRatings(req, res, next){
  22. const restaurant_id = req.params.restaurant_id;
  23. if (!restaurant_id || !Number(restaurant_id)) {
  24. res.send({
  25. status: 0,
  26. type: 'ERROR_PARAMS',
  27. message: '餐馆ID参数错误'
  28. })
  29. return
  30. }
  31. try{
  32. const ratings = await RatingModel.getData(restaurant_id, this.type[0]);
  33. res.send(ratings)
  34. }catch(err){
  35. console.log('获取评论列表失败', err);
  36. res.send({
  37. status: 0,
  38. type: "ERROR_DATA",
  39. message: '未找到当前餐馆的评论数据'
  40. })
  41. }
  42. }
  43. async getScores(req, res, next){
  44. const restaurant_id = req.params.restaurant_id;
  45. if (!restaurant_id || !Number(restaurant_id)) {
  46. res.send({
  47. status: 0,
  48. type: 'ERROR_PARAMS',
  49. message: '餐馆ID参数错误'
  50. })
  51. return
  52. }
  53. try{
  54. const scores = await RatingModel.getData(restaurant_id, this.type[1]);
  55. res.send(scores)
  56. }catch(err){
  57. console.log('获取评论列表失败', err);
  58. res.send({
  59. status: 0,
  60. type: "ERROR_DATA",
  61. message: '未找到当前餐馆的评论数据'
  62. })
  63. }
  64. }
  65. async getTags(req, res, next){
  66. const restaurant_id = req.params.restaurant_id;
  67. if (!restaurant_id || !Number(restaurant_id)) {
  68. res.send({
  69. status: 0,
  70. type: 'ERROR_PARAMS',
  71. message: '餐馆ID参数错误'
  72. })
  73. return
  74. }
  75. try{
  76. const tags = await RatingModel.getData(restaurant_id, this.type[2]);
  77. res.send(tags)
  78. }catch(err){
  79. console.log('获取评论列表失败', err);
  80. res.send({
  81. status: 0,
  82. type: "ERROR_DATA",
  83. message: '未找到当前餐馆的评论数据'
  84. })
  85. }
  86. }
  87. }
  88. export default new Rating()