java">package ***.alatus.web;
import ***.alatus.constant.Constants;
import ***.alatus.model.TActivity;
import ***.alatus.query.ActivityQuery;
import ***.alatus.result.R;
import ***.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import ***.alatus.service.ActivityService;
@RestController
public class ActivityController {
@Resource
private ActivityService activityService;
@GetMapping(value = "/api/activities")
public R activityPage(@RequestParam(value = "current",required = false)Integer current,
ActivityQuery activityQuery){
if(current == null){
current = 1;
}
// 返回结果为PageInfo
PageInfo<TActivity> activityByPage = activityService.getActivityByPage(current,activityQuery);
return R.OK(activityByPage);
}
@PostMapping(value = "/api/activity/add")
public R addActivity(ActivityQuery activityQuery, @RequestHeader(value = Constants.TOKEN_NAME)String token){
activityQuery.setToken(token);
int result = activityService.saveActivity(activityQuery);
return result >= 1 ? R.OK() : R.FAIL();
}
@GetMapping(value = "/api/activity/{id}")
public R activityDetail(@PathVariable(value = Constants.ID)Integer id){
TActivity tActivity = activityService.getActivityById(id);
return R.OK(tActivity);
}
}
package ***.alatus.web;
import ***.alatus.constant.Constants;
import ***.alatus.model.TActivity;
import ***.alatus.query.ActivityQuery;
import ***.alatus.result.R;
import ***.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import ***.alatus.service.ActivityService;
@RestController
public class ActivityController {
@Resource
private ActivityService activityService;
@GetMapping(value = "/api/activities")
public R activityPage(@RequestParam(value = "current",required = false)Integer current,
ActivityQuery activityQuery){
if(current == null){
current = 1;
}
// 返回结果为PageInfo
PageInfo<TActivity> activityByPage = activityService.getActivityByPage(current,activityQuery);
return R.OK(activityByPage);
}
@PostMapping(value = "/api/activity/add")
public R addActivity(ActivityQuery activityQuery, @RequestHeader(value = Constants.TOKEN_NAME)String token){
activityQuery.setToken(token);
int result = activityService.saveActivity(activityQuery);
return result >= 1 ? R.OK() : R.FAIL();
}
@GetMapping(value = "/api/activity/{id}")
public R activityDetail(@PathVariable(value = Constants.ID)Integer id){
TActivity tActivity = activityService.getActivityById(id);
return R.OK(tActivity);
}
}
package ***.alatus.service;
import ***.alatus.model.TActivity;
import ***.alatus.query.ActivityQuery;
import ***.github.pagehelper.PageInfo;
public interface ActivityService {
PageInfo<TActivity> getActivityByPage(Integer current, ActivityQuery activityQuery);
int saveActivity(ActivityQuery activityQuery);
TActivity getActivityById(Integer id);
}
package ***.alatus.service;
import ***.alatus.model.TActivity;
import ***.alatus.query.ActivityQuery;
import ***.github.pagehelper.PageInfo;
public interface ActivityService {
PageInfo<TActivity> getActivityByPage(Integer current, ActivityQuery activityQuery);
int saveActivity(ActivityQuery activityQuery);
TActivity getActivityById(Integer id);
}
package ***.alatus.service.impl;
import ***.alatus.constant.Constants;
import ***.alatus.mapper.TActivityMapper;
import ***.alatus.model.TActivity;
import ***.alatus.query.ActivityQuery;
import ***.alatus.service.ActivityService;
import ***.alatus.util.JWTUtils;
import ***.github.pagehelper.PageHelper;
import ***.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
@Service
public class ActivityServiceImpl implements ActivityService {
@Resource
private TActivityMapper tActivityMapper;
@Override
public PageInfo<TActivity> getActivityByPage(Integer current, ActivityQuery activityQuery) {
// 设置PageHelper和分页情况
PageHelper.startPage(current, Constants.PAGE_SIZE);
// 查询
ArrayList<TActivity> list = tActivityMapper.selectActivityByPage(activityQuery);
// 封装分页到PageInfo中
PageInfo<TActivity> info = new PageInfo<>(list);
return info;
}
@Override
public int saveActivity(ActivityQuery activityQuery) {
TActivity tActivity = new TActivity();
// 把query对象的数据复制到activity对象里面
// 这个工具类的复制要求是两个对象的属性名要相同,属性要相同
BeanUtils.copyProperties(activityQuery,tActivity);
tActivity.setCreateTime(new Date());
// 通过token解析出的用户获取ID作为创建者的ID
Integer loginId = JWTUtils.parseUserFromJWT(activityQuery.getToken()).getId();
tActivity.setCreateBy(loginId);
return tActivityMapper.insertSelective(tActivity);
}
@Override
public TActivity getActivityById(Integer id) {
return tActivityMapper.selectByPrimaryKey(id);
}
}
package ***.alatus.service.impl;
import ***.alatus.constant.Constants;
import ***.alatus.mapper.TActivityMapper;
import ***.alatus.model.TActivity;
import ***.alatus.query.ActivityQuery;
import ***.alatus.service.ActivityService;
import ***.alatus.util.JWTUtils;
import ***.github.pagehelper.PageHelper;
import ***.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
@Service
public class ActivityServiceImpl implements ActivityService {
@Resource
private TActivityMapper tActivityMapper;
@Override
public PageInfo<TActivity> getActivityByPage(Integer current, ActivityQuery activityQuery) {
// 设置PageHelper和分页情况
PageHelper.startPage(current, Constants.PAGE_SIZE);
// 查询
ArrayList<TActivity> list = tActivityMapper.selectActivityByPage(activityQuery);
// 封装分页到PageInfo中
PageInfo<TActivity> info = new PageInfo<>(list);
return info;
}
@Override
public int saveActivity(ActivityQuery activityQuery) {
TActivity tActivity = new TActivity();
// 把query对象的数据复制到activity对象里面
// 这个工具类的复制要求是两个对象的属性名要相同,属性要相同
BeanUtils.copyProperties(activityQuery,tActivity);
tActivity.setCreateTime(new Date());
// 通过token解析出的用户获取ID作为创建者的ID
Integer loginId = JWTUtils.parseUserFromJWT(activityQuery.getToken()).getId();
tActivity.setCreateBy(loginId);
return tActivityMapper.insertSelective(tActivity);
}
@Override
public TActivity getActivityById(Integer id) {
return tActivityMapper.selectByPrimaryKey(id);
}
}