Browse Source

update QueueController and QueueService

pull/1/MERGE
ligang 6 years ago
parent
commit
6548195a0c
  1. 80
      escheduler-api/src/main/java/cn/escheduler/api/controller/QueueController.java
  2. 103
      escheduler-api/src/main/java/cn/escheduler/api/service/QueueService.java
  3. 60
      escheduler-api/src/test/java/cn/escheduler/api/controller/QueueControllerTest.java

80
escheduler-api/src/main/java/cn/escheduler/api/controller/QueueController.java

@ -17,6 +17,7 @@
package cn.escheduler.api.controller; package cn.escheduler.api.controller;
import cn.escheduler.api.enums.Status;
import cn.escheduler.api.service.QueueService; import cn.escheduler.api.service.QueueService;
import cn.escheduler.api.utils.Constants; import cn.escheduler.api.utils.Constants;
import cn.escheduler.api.utils.Result; import cn.escheduler.api.utils.Result;
@ -29,7 +30,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.Map; import java.util.Map;
import static cn.escheduler.api.enums.Status.QUERY_QUEUE_LIST_ERROR; import static cn.escheduler.api.enums.Status.*;
/** /**
@ -63,5 +64,82 @@ public class QueueController extends BaseController{
} }
} }
/**
* query queue list paging
* @param loginUser
* @return
*/
@GetMapping(value="/list-paging")
@ResponseStatus(HttpStatus.OK)
public Result queryQueueListPaging(@RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("pageNo") Integer pageNo,
@RequestParam(value = "searchVal", required = false) String searchVal,
@RequestParam("pageSize") Integer pageSize){
try{
logger.info("login user {}, query queue list,search value:{}", loginUser.getUserName(),searchVal);
Map<String, Object> result = checkPageParams(pageNo, pageSize);
if(result.get(Constants.STATUS) != Status.SUCCESS){
return returnDataListPaging(result);
}
result = queueService.queryList(loginUser,searchVal,pageNo,pageSize);
return returnDataList(result);
}catch (Exception e){
logger.error(QUERY_QUEUE_LIST_ERROR.getMsg(),e);
return error(QUERY_QUEUE_LIST_ERROR.getCode(), QUERY_QUEUE_LIST_ERROR.getMsg());
}
}
/**
* create queue
*
* @param loginUser
* @param queue
* @param queueName
* @return
*/
@PostMapping(value = "/create")
@ResponseStatus(HttpStatus.CREATED)
public Result createQueue(@RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam(value = "queue") String queue,
@RequestParam(value = "queueName") String queueName) {
logger.info("login user {}, create queue, queue: {}, queueName: {}",
loginUser.getUserName(), queue, queueName);
try {
Map<String, Object> result = queueService.createQueue(loginUser,queue,queueName);
return returnDataList(result);
}catch (Exception e){
logger.error(CREATE_QUEUE_ERROR.getMsg(),e);
return error(CREATE_QUEUE_ERROR.getCode(), CREATE_QUEUE_ERROR.getMsg());
}
}
/**
* update queue
*
* @param loginUser
* @param queue
* @param queueName
* @return
*/
@PostMapping(value = "/update")
@ResponseStatus(HttpStatus.CREATED)
public Result updateQueue(@RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam(value = "id") int id,
@RequestParam(value = "queue") String queue,
@RequestParam(value = "queueName") String queueName) {
logger.info("login user {}, update queue, id: {}, queue: {}, queueName: {}",
loginUser.getUserName(), id,queue, queueName);
try {
Map<String, Object> result = queueService.updateQueue(loginUser,id,queue,queueName);
return returnDataList(result);
}catch (Exception e){
logger.error(UPDATE_QUEUE_ERROR.getMsg(),e);
return error(UPDATE_QUEUE_ERROR.getCode(), UPDATE_QUEUE_ERROR.getMsg());
}
}
} }

103
escheduler-api/src/main/java/cn/escheduler/api/service/QueueService.java

@ -18,12 +18,15 @@ package cn.escheduler.api.service;
import cn.escheduler.api.enums.Status; import cn.escheduler.api.enums.Status;
import cn.escheduler.api.utils.Constants; import cn.escheduler.api.utils.Constants;
import cn.escheduler.api.utils.PageInfo;
import cn.escheduler.dao.mapper.QueueMapper; import cn.escheduler.dao.mapper.QueueMapper;
import cn.escheduler.dao.model.Queue; import cn.escheduler.dao.model.Queue;
import cn.escheduler.dao.model.User; import cn.escheduler.dao.model.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -57,4 +60,104 @@ public class QueueService extends BaseService{
return result; return result;
} }
/**
* query queue list paging
* @param loginUser
* @param searchVal
* @param pageNo
* @param pageSize
* @return
*/
public Map<String, Object> queryList(User loginUser,String searchVal, Integer pageNo, Integer pageSize) {
Map<String, Object> result = new HashMap<>(5);
if (checkAdmin(loginUser, result)) {
return result;
}
Integer count = queueMapper.countQueuePaging(searchVal);
PageInfo<Queue> pageInfo = new PageInfo<>(pageNo,pageSize);
List<Queue> queueList = queueMapper.queryQueuePaging(searchVal,pageInfo.getStart(),pageSize);
pageInfo.setTotalCount(count);
pageInfo.setLists(queueList);
result.put(Constants.DATA_LIST, queueList);
putMsg(result,Status.SUCCESS);
return result;
}
public Map<String, Object> createQueue(User loginUser,String queue, String queueName) {
Map<String, Object> result = new HashMap<>(5);
if (checkAdmin(loginUser, result)) {
return result;
}
if (checkQueueExists(queue)){
putMsg(result, Status.QUEUE_EXIST, queue);
return result;
}
Queue queueObj = new Queue();
Date now = new Date();
queueObj.setQueue(queue);
queueObj.setQueueName(queueName);
queueObj.setCreateTime(now);
queueObj.setUpdateTime(now);
queueMapper.insert(queueObj);
putMsg(result, Status.SUCCESS);
return result;
}
public Map<String, Object> updateQueue(User loginUser,int id,String queue, String queueName) {
Map<String, Object> result = new HashMap<>(5);
if (checkAdmin(loginUser, result)) {
return result;
}
Queue queueObj = queueMapper.queryById(id);
if (queueObj == null) {
putMsg(result,Status.QUEUE_NOT_EXIST,id);
return result;
}
//update queue
if (StringUtils.isNotEmpty(queue)) {
if (!queue.equals(queueObj.getQueue()) && checkQueueExists(queue)) {
putMsg(result, Status.QUEUE_EXIST, queue);
return result;
}
queueObj.setQueue(queue);
}
if (StringUtils.isNotEmpty(queueName)) {
queueObj.setQueueName(queueName);
}
Date now = new Date();
queueObj.setUpdateTime(now);
queueMapper.update(queueObj);
putMsg(result, Status.SUCCESS);
return result;
}
/**
* check queue exists
*
* @param queue
* @return
*/
private boolean checkQueueExists(String queue) {
return queueMapper.queryByQueue(queue) == null ? false : true;
}
} }

60
escheduler-api/src/test/java/cn/escheduler/api/controller/QueueControllerTest.java

@ -32,9 +32,12 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -65,4 +68,61 @@ public class QueueControllerTest {
Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue()); Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue());
logger.info(mvcResult.getResponse().getContentAsString()); logger.info(mvcResult.getResponse().getContentAsString());
} }
@Test
public void queryPagingList() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
//paramsMap.add("processInstanceId","1380");
paramsMap.add("searchVal","");
paramsMap.add("pageNo","1");
paramsMap.add("pageSize","20");
MvcResult mvcResult = mockMvc.perform(get("/queue/list-paging")
.header("sessionId", "d4541e0d-0349-4f05-9c68-300176cd3c91")
.params(paramsMap))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andReturn();
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue());
logger.info(mvcResult.getResponse().getContentAsString());
}
@Test
public void createQueue() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("queue","ait1111");
paramsMap.add("queueName","aitName");
MvcResult mvcResult = mockMvc.perform(post("/queue/create")
.header("sessionId", "d4541e0d-0349-4f05-9c68-300176cd3c91")
.params(paramsMap))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andReturn();
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
//Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue());
logger.info(mvcResult.getResponse().getContentAsString());
}
@Test
public void updateQueue() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("id","2");
paramsMap.add("queue","ait1");
paramsMap.add("queueName","aitName");
MvcResult mvcResult = mockMvc.perform(post("/queue/update")
.header("sessionId", "d4541e0d-0349-4f05-9c68-300176cd3c91")
.params(paramsMap))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andReturn();
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
//Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue());
logger.info(mvcResult.getResponse().getContentAsString());
}
} }
Loading…
Cancel
Save