增加查询活动列表接口

master
pan 5 years ago
parent bda3bfe16e
commit 55b7f36048
  1. 9
      src/main/java/com/share/help/Constants.java
  2. 55
      src/main/java/com/share/help/controller/ActivityController.java
  3. 52
      src/main/java/com/share/help/dao/ActivityMapper.java
  4. 3
      src/main/java/com/share/help/dao/UserMapper.java
  5. 28
      src/main/java/com/share/help/form/FindActivityForm.java
  6. 23
      src/main/java/com/share/help/form/SendHelpForm.java
  7. 43
      src/main/java/com/share/help/res/ActivitySimple.java
  8. 16
      src/main/java/com/share/help/res/FindActivityRes.java
  9. 53
      src/main/java/com/share/help/res/Page.java
  10. 11
      src/main/java/com/share/help/res/SendHelpRes.java
  11. 86
      src/main/java/com/share/help/service/ActivityService.java
  12. 7
      src/main/java/com/share/help/service/ImageService.java
  13. 3
      src/main/java/com/share/help/service/UserService.java
  14. 7
      src/main/resources/application.yaml
  15. 23
      src/main/resources/mapping/ActivityMapper.xml
  16. 18
      src/test/java/com/share/help/DefaultTest.java
  17. 29
      src/test/java/com/share/help/HelpApplicationTests.java

@ -13,4 +13,13 @@ public class Constants {
//用户登陆接口 //用户登陆接口
public static final String USER_INTERFACE_LOGIN = "/login"; public static final String USER_INTERFACE_LOGIN = "/login";
//求助接口前缀
public static final String ACTIVITY_INTERFACE = "/api/activity";
//发布求助信息
public static final String ACTIVITY_INTERFACE_SEND_HELP = "/sendHelp";
//查找活动列表
public static final String ACTIVITY_INTERFACE_FIND = "/find";
} }

@ -0,0 +1,55 @@
package com.share.help.controller;
import com.share.help.Constants;
import com.share.help.form.FindActivityForm;
import com.share.help.form.SendHelpForm;
import com.share.help.res.FindActivityRes;
import com.share.help.res.JSONResponse;
import com.share.help.res.Result;
import com.share.help.res.SendHelpRes;
import com.share.help.service.ActivityService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(Constants.ACTIVITY_INTERFACE)
public class ActivityController {
@Autowired
private ActivityService activityService;
/**
* 发布求助信息
* @return 返回发布结果
*/
@PutMapping(Constants.ACTIVITY_INTERFACE_SEND_HELP)
public JSONResponse<SendHelpRes,Void> send(SendHelpForm sendHelpForm){
if(StringUtils.isNoneEmpty(sendHelpForm.getTitle(),sendHelpForm.getContent(),sendHelpForm.getServiceAddress())&&
ObjectUtils.allNotNull(sendHelpForm.getActivityStartTime(),sendHelpForm.getActivityEndTime()
,sendHelpForm.getActivityImgFile())){
return activityService.sendHelp(sendHelpForm);
}else{
return new JSONResponse<>(SendHelpRes.fail,Result.FAIL);
}
}
/**
* 返回活动列表数据
* @param findActivityForm 查询表单
* @return 返回活动列表数据
*/
@GetMapping(Constants.ACTIVITY_INTERFACE_FIND)
public JSONResponse<FindActivityRes.Res,FindActivityRes> find(FindActivityForm findActivityForm){
if(findActivityForm.getCurrentPage()==null){
return new JSONResponse<>(FindActivityRes.Res.fail,Result.FAIL);
}else{
return activityService.find(findActivityForm);
}
}
}

@ -0,0 +1,52 @@
package com.share.help.dao;
import com.share.help.entity.ActivityEntity;
import com.share.help.res.ActivitySimple;
import com.share.help.res.FindActivityRes;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* 活动数据接口
*/
public interface ActivityMapper {
/**
* 保存活动信息
* @param activityEntity 活动实体
* @return 返回保存结果
*/
@Insert("insert into activity(activity_id,title,content,seek_help_time,activity_start_time,activity_end_time,activity_img,service_address)value(#{activityId},#{title},#{content},#{seekHelpTime},#{activityStartTime},#{activityEndTime},#{activityImg},#{serviceAddress})")
boolean insert(ActivityEntity activityEntity);
/**
* 根据活动id删除活动信息
* @param activityId 活动id
* @return 返回删除结果
*/
@Delete("delete from activity where activity_id=#{activityId}")
boolean delete(@Param("activityId") Long activityId);
/**
* 根据活动id查找活动信息
* @param activityId 活动id
* @return 返回活动信息
*/
@Select("select * from activity where activity_id=#{activityId}")
@ResultMap("activityMap")
ActivityEntity findOne(@Param("activityId") Long activityId);
/**
* 分页查询活动
* @param start 起始索引
* @param count 查询个数
* @return 返回活动列表
*/
@Select("select activity_id,title,content,activity_img from activity limit #{start},#{count}")
@ResultMap("activitySimpleMap")
List<ActivitySimple> getPage(@Param("start") Integer start, @Param("count") Integer count);
@Select("select count(*) as count,ceil(count(*)/#{pageSize}) as totalPage from activity")
FindActivityRes count(@Param("pageSize") Integer pageSize);
}

@ -45,11 +45,10 @@ public interface UserMapper {
/** /**
* *
* @param userEntity 更新用户
* @return 返回更新结果 * @return 返回更新结果
*/ */
@Update("update user set password=#{password} where user_id=#{userId}") @Update("update user set password=#{password} where user_id=#{userId}")
boolean update(UserEntity userEntity); boolean updatePassword(@Param("userId") String userId, @Param("password") String password);
/** /**
* 根据用户id判断用户是否存在 * 根据用户id判断用户是否存在

@ -0,0 +1,28 @@
package com.share.help.form;
/**
* 查找活动列表表单
*/
public class FindActivityForm {
//查找分页
private Integer currentPage;
//标题信息
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
}

@ -0,0 +1,23 @@
package com.share.help.form;
import com.share.help.entity.ActivityEntity;
import org.springframework.web.multipart.MultipartFile;
/**
* 发布求助信息表单
*/
public class SendHelpForm extends ActivityEntity {
/**
* 活动背景图
*/
private MultipartFile activityImgFile;
public MultipartFile getActivityImgFile() {
return activityImgFile;
}
public void setActivityImgFile(MultipartFile activityImgFile) {
this.activityImgFile = activityImgFile;
}
}

@ -0,0 +1,43 @@
package com.share.help.res;
/**
* 活动列表实体
*/
public class ActivitySimple {
private Long activityId;
private String title;
private String content;
private String activityImg;
public Long getActivityId() {
return activityId;
}
public void setActivityId(Long activityId) {
this.activityId = activityId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getActivityImg() {
return activityImg;
}
public void setActivityImg(String activityImg) {
this.activityImg = activityImg;
}
}

@ -0,0 +1,16 @@
package com.share.help.res;
import java.util.List;
/**
* 查找活动列表结果
*/
public class FindActivityRes extends Page<List<List<ActivitySimple>>>{
public enum Res{
//成功
ok,
//失败
fail
}
}

@ -0,0 +1,53 @@
package com.share.help.res;
public class Page<T> {
private Integer currentPage;
private Integer totalPage;
private Long count;
private Integer pageSize;
private T body;
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Long getCount() {
return count;
}
public void setCount(Long count) {
this.count = count;
}
public T getBody() {
return body;
}
public void setBody(T body) {
this.body = body;
}
}

@ -0,0 +1,11 @@
package com.share.help.res;
/**
* 发布求助信息响应
*/
public enum SendHelpRes {
//发布成功
ok,
//发布失败请联系管理员
fail
}

@ -0,0 +1,86 @@
package com.share.help.service;
import com.share.help.dao.ActivityMapper;
import com.share.help.entity.ActivityEntity;
import com.share.help.form.FindActivityForm;
import com.share.help.form.SendHelpForm;
import com.share.help.res.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
/**
* 活动服务层
*/
@Service
public class ActivityService extends ImageService{
@Autowired
private ActivityMapper activityMapper;
//活动分页配置
@Value("${activity.page-size}")
private int pageSize;
@Value("${activity.row-size}")
private int rowSize;
/**
* 发布求助信息
* @param sendHelpForm 求助信息表单
* @return 发布结果
*/
public JSONResponse<SendHelpRes,Void> sendHelp(SendHelpForm sendHelpForm){
ActivityEntity activityEntity=new ActivityEntity();
activityEntity.setTitle(sendHelpForm.getTitle());
activityEntity.setContent(sendHelpForm.getContent());
activityEntity.setSeekHelpTime(new Timestamp(System.currentTimeMillis()));
activityEntity.setActivityStartTime(sendHelpForm.getActivityStartTime());
activityEntity.setActivityEndTime(sendHelpForm.getActivityEndTime());
//保存活动背景图
String imageName=saveImg(sendHelpForm.getActivityImgFile());
if(imageName==null){
return new JSONResponse<>(SendHelpRes.fail, Result.FAIL);
}
activityEntity.setActivityImg(imageName);
activityEntity.setServiceAddress(sendHelpForm.getServiceAddress());
return activityMapper.insert(activityEntity)?new JSONResponse<>(SendHelpRes.ok,Result.OK):new JSONResponse<>(SendHelpRes.fail,Result.FAIL);
}
/**
* 分页查询活动信息
* @param findActivityForm 查询活动信息
* @return 返回活动信息
*/
public JSONResponse<FindActivityRes.Res,FindActivityRes> find(FindActivityForm findActivityForm){
FindActivityRes findActivityRes=activityMapper.count(pageSize);
findActivityRes.setPageSize(pageSize);
findActivityRes.setCurrentPage(findActivityForm.getCurrentPage());
List<ActivitySimple> activitySimpleList=activityMapper.getPage((findActivityForm.getCurrentPage()-1)*pageSize,pageSize);
List<List<ActivitySimple>> dataList=new ArrayList<>();
if(!activitySimpleList.isEmpty()){
if(activitySimpleList.size()<rowSize){
dataList.add(activitySimpleList);
}else {
for (int i = rowSize, j = 0; i < activitySimpleList.size(); j = i, i += rowSize) {
dataList.add(activitySimpleList.subList(j, i));
if (i + rowSize >= activitySimpleList.size()) {
dataList.add(activitySimpleList.subList(i, activitySimpleList.size()));
}
}
}
findActivityRes.setBody(dataList);
return new JSONResponse<FindActivityRes.Res,FindActivityRes>(FindActivityRes.Res.ok,Result.OK).setBody(findActivityRes);
}else{
return new JSONResponse<>(FindActivityRes.Res.fail, Result.FAIL);
}
}
}

@ -9,13 +9,14 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.UUID; import java.util.UUID;
enum ImageType{
public abstract class ImageService {
private enum ImageType{
jpg, jpg,
jpeg, jpeg,
png png
} }
public abstract class ImageService {
private Logger logger = LoggerFactory.getLogger(ImageService.class); private Logger logger = LoggerFactory.getLogger(ImageService.class);
@Value("${spring.resources.static-locations}") @Value("${spring.resources.static-locations}")

@ -13,6 +13,9 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils; import org.springframework.util.DigestUtils;
/**
* 用户服务层
*/
@Service @Service
public class UserService extends ImageService{ public class UserService extends ImageService{

@ -25,3 +25,10 @@ user:
register: register:
#初始时间币 #初始时间币
time-score: 20 time-score: 20
#活动
activity:
# 分页大小
page-size: 6
# 前端一行显示数量
row-size: 3

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.share.help.dao.ActivityMapper">
<resultMap id="activityMap" type="com.share.help.entity.ActivityEntity">
<id property="activityId" column="activity_id"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<result property="seekHelpTime" column="seek_help_time"/>
<result property="activityStartTime" column="activity_start_time"/>
<result property="activityEndTime" column="activity_end_time"/>
<result property="activityImg" column="activity_img"/>
<result property="serviceAddress" column="service_address"/>
</resultMap>
<resultMap id="activitySimpleMap" type="com.share.help.res.ActivitySimple">
<id property="activityId" column="activity_id"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<result property="activityImg" column="activity_img"/>
</resultMap>
</mapper>

@ -1,10 +1,12 @@
package com.share.help; package com.share.help;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.UUID; import java.util.ArrayList;
import java.util.List;
public class DefaultTest { public class DefaultTest {
@ -12,6 +14,18 @@ public class DefaultTest {
@Test @Test
public void test() { public void test() {
logger.info(UUID.randomUUID().toString().length()+"");
List<Integer> s=new ArrayList<Integer>(){{
for(int i=0;i<100;i++){
add(i);
}
}};
for(int i=3,j=0;i<s.size();j=i,i+=3){
logger.info(ArrayUtils.toString(s.subList(j,i)));
if(i+3>=s.size()){
logger.info( ArrayUtils.toString(s.subList(i,s.size())));
}
}
} }
} }

@ -1,6 +1,8 @@
package com.share.help; package com.share.help;
import com.share.help.dao.ActivityMapper;
import com.share.help.dao.UserMapper; import com.share.help.dao.UserMapper;
import com.share.help.entity.ActivityEntity;
import com.share.help.entity.UserEntity; import com.share.help.entity.UserEntity;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -33,6 +35,9 @@ class HelpApplicationTests {
@Autowired @Autowired
private UserMapper userMapper; private UserMapper userMapper;
@Autowired
private ActivityMapper activityMapper;
private Logger logger = LoggerFactory.getLogger(HelpApplicationTests.class); private Logger logger = LoggerFactory.getLogger(HelpApplicationTests.class);
@BeforeEach @BeforeEach
@ -83,7 +88,7 @@ class HelpApplicationTests {
userEntity.setPassword(newPassword); userEntity.setPassword(newPassword);
Assertions.assertTrue(userMapper.update(userEntity),"更新成功"); Assertions.assertTrue(userMapper.updatePassword(userId,newPassword),"更新成功");
userEntity=userMapper.findOne(userId); userEntity=userMapper.findOne(userId);
@ -110,4 +115,26 @@ class HelpApplicationTests {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post(Constants.USER_INTERFACE+Constants.USER_INTERFACE_REGISTER); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post(Constants.USER_INTERFACE+Constants.USER_INTERFACE_REGISTER);
mvc.perform(builder).andDo(MockMvcResultHandlers.print()); mvc.perform(builder).andDo(MockMvcResultHandlers.print());
} }
/**
* 测试活动分页查询
*/
@Test
public void testActivity(){
// FindActivityRes findActivityRes=activityMapper.count(10L);
// List<ActivitySimple> list=activityMapper.getPage(0,10);
}
@Test
public void testAddActivity(){
ActivityEntity activityEntity=activityMapper.findOne(1L);
for(int i=0;i<20;i++){
activityEntity.setActivityId(null);
activityMapper.insert(activityEntity);
}
}
} }

Loading…
Cancel
Save