作者 王勇

修改了一下关于队列的代码,增加了用户名的校验

... ... @@ -2,12 +2,10 @@ package com.sunyo.wlpt.message.bus.service.controller;
import com.github.pagehelper.PageInfo;
import com.sunyo.wlpt.message.bus.service.domain.BusQueue;
import com.sunyo.wlpt.message.bus.service.domain.UserInfo;
import com.sunyo.wlpt.message.bus.service.response.ResultJson;
import com.sunyo.wlpt.message.bus.service.service.BusQueueService;
import com.sunyo.wlpt.message.bus.service.service.UserInfoService;
import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
import io.netty.util.internal.StringUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
... ... @@ -29,6 +27,7 @@ public class BusQueueController {
@Resource
private UserInfoService userInfoService;
@Resource
private BusQueueService busQueueService;
... ... @@ -117,31 +116,28 @@ public class BusQueueController {
@PostMapping("/insert")
public ResultJson insertBusQueue(@RequestBody BusQueue busQueue) throws IOException, TimeoutException
{
//先验证,增加的虚拟主机的核心信息(交换机名称)是否已存在
String message = validateBusQueue(busQueue);
// 设置id
busQueue.setId(IdUtils.generateId());
//验证通过
return message == null
? busQueueService.insertSelective(busQueue) > 0
? new ResultJson<>("200", "添加消息队列,成功")
: new ResultJson<>("500", "添加消息队列,失败")
? busQueueService.insertSelective(busQueue)
: new ResultJson<>("400", message);
}
/**
* 校验-消息队列,添加和修改之前,是否已存在
* 校验-消息队列的队列名称的唯一性
*
* @param busQueue {@link BusQueue}
* @return 通过校验,无返回消息
*/
private String validateBusQueue(BusQueue busQueue)
{
if (!StringUtil.isNullOrEmpty(busQueue.getUserId())) {
// 根据用户id,填充用户名称;违背了单一职责原则,有时间再改
UserInfo userInfo = userInfoService.selectByPrimaryKey(busQueue.getUserId());
busQueue.setUsername(userInfo.getUsername());
}
// if (!StringUtil.isNullOrEmpty(busQueue.getUserId())) {
// // 根据用户id,填充用户名称;违背了单一职责原则,有时间再改
// UserInfo userInfo = userInfoService.selectByPrimaryKey(busQueue.getUserId());
// busQueue.setUsername(userInfo.getUsername());
// }
// 判断队列名称,是否为空
if ("".equals(busQueue.getQueueName()) || busQueue.getQueueName() == null) {
... ...
... ... @@ -2,6 +2,7 @@ package com.sunyo.wlpt.message.bus.service.service;
import com.github.pagehelper.PageInfo;
import com.sunyo.wlpt.message.bus.service.domain.BusQueue;
import com.sunyo.wlpt.message.bus.service.response.ResultJson;
import java.io.IOException;
import java.util.List;
... ... @@ -44,7 +45,7 @@ public interface BusQueueService {
* @param record the record
* @return insert count
*/
int insertSelective(BusQueue record) throws IOException, TimeoutException;
ResultJson insertSelective(BusQueue record) throws IOException, TimeoutException;
/**
* 查询,根据主键
... ...
... ... @@ -4,7 +4,9 @@ import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunyo.wlpt.message.bus.service.domain.BusQueue;
import com.sunyo.wlpt.message.bus.service.mapper.BusQueueMapper;
import com.sunyo.wlpt.message.bus.service.mapper.UserInfoMapper;
import com.sunyo.wlpt.message.bus.service.rabbit.utils.RabbitUtils;
import com.sunyo.wlpt.message.bus.service.response.ResultJson;
import com.sunyo.wlpt.message.bus.service.service.BusQueueService;
import io.netty.util.internal.StringUtil;
import org.springframework.context.annotation.Lazy;
... ... @@ -32,6 +34,9 @@ public class BusQueueServiceImpl implements BusQueueService {
@Resource
private RabbitUtils rabbitUtils;
@Resource
private UserInfoMapper userInfoMapper;
@Lazy
@Resource
private AsyncTaskService asyncTaskService;
... ... @@ -95,11 +100,23 @@ public class BusQueueServiceImpl implements BusQueueService {
return busQueueMapper.insert(record);
}
/**
* 校验用户名称是否存在
* <p>
* 创建队列于MQ服务器
* <p>
* 存储创建的队列信息于数据库
*/
@Override
public int insertSelective(BusQueue record) throws IOException, TimeoutException
public ResultJson insertSelective(BusQueue record) throws IOException, TimeoutException
{
if (userInfoMapper.selectUserExist(record.getUsername()).size() == 0) {
return new ResultJson<>("400", "该用户信息,不存在");
}
rabbitUtils.toCreateQueue(record);
return busQueueMapper.insertSelective(record);
return busQueueMapper.insertSelective(record) > 0
? new ResultJson<>("200", "添加消息队列,成功")
: new ResultJson<>("500", "添加消息队列,失败");
}
@Override
... ...