源码获取:俺的博客首页 "资源" 里下载!
Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。
项目技术:
Spring + SpringBoot+ mybatis + Maven + Vue 等等组成,B/S模式 + Maven管理等等。
@RestController
@RequestMapping("/api/user")
@Api(tags = "共同前缀:/api/user", description = "UserController")
@Slf4j
public class UserController {
@Autowired
UserService userService;
@PostMapping
@ApiOperation("新增User")
public ResponseObject post(String username, String password) {
log.info("新增User");
if (username == null || username.equals("")) {
throw new ControllerException("username不可为null,也不可为空字符串");
} else if (password == null || password.equals("")) {
throw new ControllerException("password不可为null,也不可为空字符串");
} else if (userService.selectByUsername(username) != null) {
throw new ControllerException("该username已被使用");
} else {
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setRole("VIP1");
user.setNickname(new Date().getTime() + "");
user.setImage("default_user_image.png");
user.setEmail("该用户没有填写邮箱");
user.setPhone("该用户没有填写手机号码");
user.setProfile("该用户没有填写个人简介");
return new ResponseObject("200", "操作成功", userService.insert(user));
}
}
@GetMapping("/{id:[0-9]+}")
@ApiOperation("查询User")
public ResponseObject getById(@PathVariable Integer id) {
log.info("查询User");
return new ResponseObject("200", "操作成功", userService.selectById(id));
}
@PutMapping
@ApiOperation("修改User")
@PreAuthorize("isAuthenticated()")
public ResponseObject put(User user) {
log.info("修改User");
if (user.getNickname() == null || user.getNickname().equals("")) {
throw new ControllerException("nickname不可为null,也不可为空字符串");
} else if (user.getProfile() == null || user.getProfile().equals("")) {
throw new ControllerException("profile不可为null,也不可为空字符串");
} else if (user.getPhone() == null || user.getPhone().equals("")) {
throw new ControllerException("phone不可为null,也不可为空字符串");
} else if (user.getEmail() == null || user.getEmail().equals("")) {
throw new ControllerException("email不可为null,也不可为空字符串");
} else {
User user2 = userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
user2.setNickname(user.getNickname());
user2.setProfile(user.getProfile());
user2.setPhone(user.getPhone());
user2.setEmail(user.getEmail());
return new ResponseObject("200", "操作成功", userService.update(user2));
}
}
}
@RestController
@RequestMapping("/api/novel")
@Api(tags = "共同前缀:/api/novel", description = "NovelController")
@Slf4j
public class NovelController {
@Autowired
NovelService novelService;
@Autowired
UserService userService;
@PostMapping
@ApiOperation("新增Novel")
@PreAuthorize("isAuthenticated()")
public ResponseObject post(@RequestBody Novel novel) {
log.info("新增Novel");
if (novel.getId() != null) {
throw new ControllerException("id必须为null");
} else if (novel.getUser_id() == null) {
throw new ControllerException("user_id不可为null");
} else if (novel.getCategory_id() == null) {
throw new ControllerException("category_id不可为null");
} else if (novel.getName() == null || novel.getName().equals("")) {
throw new ControllerException("name不可为null,也不可为空字符串");
} else if (novel.getSummary() == null || novel.getSummary().equals("")) {
throw new ControllerException("summary不可为null,也不可为空字符串");
} else if (novel.getImage() == null || novel.getImage().equals("")) {
throw new ControllerException("image不可为null,也不可为空字符串");
} else if (novel.getIs_complete() == null) {
throw new ControllerException("is_complete不可为null");
} else {
novel.setMultiplier(10000);
novel.setAddend(0);
return new ResponseObject("200", "操作成功", novelService.insert(novel));
}
}
@GetMapping
@ApiOperation("查询Novel")
public ResponseObject get(Integer category_id, Integer user_id, String searchName) {
log.info("查询Novel");
if (category_id != null) {
return new ResponseObject("200", "操作成功", novelService.selectByCategory_id(category_id));
} else if (user_id != null) {
return new ResponseObject("200", "操作成功", novelService.selectByUser_id(user_id));
} else if (searchName != null && !searchName.equals("")) {
return new ResponseObject("200", "操作成功", novelService.selectByLikeName(searchName));
} else {
throw new ControllerException("category_id或user_id或searchName不可为null");
}
}
@GetMapping("/{id:[0-9]+}")
@ApiOperation("查询Novel")
public ResponseObject getById(@PathVariable Integer id) {
log.info("查询Novel");
if (id != null) {
return new ResponseObject("200", "操作成功", novelService.selectById(id));
} else {
throw new ControllerException("id不可为null");
}
}
@PatchMapping("/{id:[0-9]+}")
@ApiOperation("修改Novel")
@PreAuthorize("isAuthenticated()")
public ResponseObject patchById(@PathVariable Integer id, @RequestBody Novel novel) {
log.info("修改Novel");
if (id == null) {
throw new ControllerException("id不可为null");
} else {
Novel novel2 = novelService.selectById(id);
if (novel2 == null) {
throw new ControllerException("不存在为该id的novel");
} else {
User user = userService.selectByUsername(
(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
if (user.getId() == novel2.getUser_id() || user.getRole().equals("ADMIN")) {
if (novel.getName() != null && !novel.getName().equals("")) {
novel2.setName(novel.getName());
} else if (novel.getSummary() != null && !novel.getSummary().equals("")) {
novel2.setSummary(novel.getSummary());
} else if (novel.getCategory_id() != null) {
novel2.setCategory_id(novel.getCategory_id());
} else if (novel.getMultiplier() != null) {
novel2.setMultiplier(novel.getMultiplier());
} else if (novel.getAddend() != null) {
novel2.setAddend(novel.getAddend());
} else {
throw new ControllerException("请传入需要修改的数据,如name,summary,category_id,multiplier,addend");
}
return new ResponseObject("200", "操作成功", novelService.update(novel2));
} else {
throw new ControllerException("该用户无权限修改该小说");
}
}
}
}
@GetMapping("/rank")
@ApiOperation("查询Novel")
public ResponseObject Rank(Integer day_count, Integer limit_count) {
if (day_count == null) {
throw new ControllerException("day_count不可为null");
} else if (limit_count == null) {
throw new ControllerException("limit_count不可为null");
} else {
Date date = new Date(new Date().getTime() - (long) 1000 * 60 * 60 * 24 * day_count);
return new ResponseObject("200", "操作成功",
novelService.selectByRank(MyUtils.simpleDateFormat.format(date), limit_count));
}
}
@PostMapping("/image")
@ApiOperation("上传image")
@PreAuthorize("isAuthenticated()")
public ResponseObject postImage(MultipartFile file) throws Exception {
log.info("上传image");
if (file == null) {
throw new ControllerException("上传的文件不可为null");
} else {
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf("."));
if (!suffix.equals(".png")) {
throw new RuntimeException("上传的文件必须为png文件");
} else {
String directoryPath = ResourceUtils.getURL("src").getPath() + "main/resources/static/images/";
File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdirs();
}
String newFileName = new Date().getTime() + ".png";
File file2 = new File(directory, newFileName);
if (!file2.exists()) {
file2.createNewFile();
}
// 保存文件
file.transferTo(file2);
return new ResponseObject("200", "操作成功", newFileName);
}
}
}
@PatchMapping("/image")
@ApiOperation("修改image")
@PreAuthorize("isAuthenticated()")
public ResponseObject patchImage(@RequestBody HashMap data) {
log.info("修改image");
String image = data.get("image");
String novel_idString = data.get("novel_id");
if (image == null || image.equals("")) {
throw new ControllerException("image不可为null,也不可为空字符串");
} else if (novel_idString == null || novel_idString.equals("")) {
throw new ControllerException("novel_id不可为null");
} else {
Integer novel_id = Integer.parseInt(novel_idString);
Novel novel = novelService.selectById(novel_id);
if (novel == null) {
throw new ControllerException("根据novel_id查询出来的novel为null");
} else {
User user = userService.selectByUsername(
(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
if (user.getId() == novel.getUser_id() || user.getRole().equals("ADMIN")) {
novel.setImage(image);
return new ResponseObject("200", "操作成功", novelService.update(novel));
} else {
throw new ControllerException("该用户无权限修改小说头像");
}
}
}
}
}
@RestController
@RequestMapping("/api/category")
@Api(tags = "共同前缀:/api/category", description = "CategoryController")
@Slf4j
public class CategoryController {
@Autowired
CategoryService categoryService;
@PostMapping
@ApiOperation("新增Category")
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN')")
public ResponseObject post(@RequestBody Category category) {
log.info("新增Category");
if (category.getId() != null) {
throw new ControllerException("id必须为null");
} else if (category.getRank() == null) {
throw new ControllerException("rank不可为null");
} else if (category.getName() == null || category.getName().equals("")) {
throw new ControllerException("name不可为null,也不可为空字符串");
} else if (category.getParent_id() == null) {
throw new ControllerException("parent_id不可为null");
} else if (categoryService.selectByParent_idName(category.getParent_id(), category.getName()) != null) {
throw new ControllerException("name不可重复");
} else {
return new ResponseObject("200", "操作成功", categoryService.insert(category));
}
}
@PutMapping("/{id:[0-9]+}")
@ApiOperation("修改Category")
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN')")
public ResponseObject putById(@PathVariable Integer id, @RequestBody HashMap data) {
log.info("修改Category");
Category category = categoryService.selectById(id);
if (category == null) {
throw new ControllerException("使用该id的Category不存在");
} else if (categoryService.selectByParent_idName(category.getParent_id(), data.get("name")) != null) {
throw new ControllerException("同一个分类下的name不可重复");
} else {
category.setName(data.get("name"));
return new ResponseObject("200", "操作成功", categoryService.update(category));
}
}
@GetMapping
@ApiOperation("查询Category")
public ResponseObject getByParent_id(Integer rank, Integer parent_id) {
log.info("查询Category");
if (rank != null && parent_id != null) {
return new ResponseObject("200", "操作成功", categoryService.selectByRankParent_id(rank, parent_id));
} else if (rank != null && parent_id == null) {
return new ResponseObject("200", "操作成功", categoryService.selectByRank(rank));
} else if (rank == null && parent_id != null) {
return new ResponseObject("200", "操作成功", categoryService.selectByParent_id(parent_id));
} else {
throw new ControllerException("rank或者parent_id至少要有一个不为null");
}
}
@GetMapping("/{id:[0-9]+}")
@ApiOperation("查询Category")
public ResponseObject getById(@PathVariable Integer id) {
log.info("查询Category");
if (id != null) {
return new ResponseObject("200", "操作成功", categoryService.selectById(id));
} else {
throw new ControllerException("id不可为null");
}
}
}
源码获取:俺的博客首页 "资源" 里下载!