room.js 921 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class RoomService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'room');
  10. this.model = this.ctx.model.Room;
  11. this.rumodel = this.ctx.model.Roomuser;
  12. }
  13. async create(data) {
  14. const { name } = data;
  15. const room_ = await this.model.findOne({ name });
  16. if (room_) {
  17. throw new BusinessError('此房间已经存在,请重新输入');
  18. }
  19. const roomuser = await this.rumodel.findById(data.anchorid);
  20. if (!roomuser) {
  21. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  22. }
  23. data.username = roomuser.name;
  24. const res = await this.model.create(data);
  25. return res;
  26. }
  27. }
  28. module.exports = RoomService;