提交 20cfcbcb authored 作者: xuyang's avatar xuyang

完善程序4.14.1

上级 798cc988
HELP.md ReadME.md
target/ target/
!.mvn/wrapper/maven-wrapper.jar !.mvn/wrapper/maven-wrapper.jar
!**/src/main/** !**/src/main/**
......
# Introduction
A library demo for test
package com.springboot.demo1.controller; package com.springboot.demo1.controller;
import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.Books; import com.springboot.demo1.entity.Books;
import com.springboot.demo1.entity.Response;
import com.springboot.demo1.service.BooksService; import com.springboot.demo1.service.BooksService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
...@@ -15,42 +16,49 @@ public class BooksController { ...@@ -15,42 +16,49 @@ public class BooksController {
@Resource @Resource
private BooksService booksService; private BooksService booksService;
//获取图书列表 /**
* 获取图书列表
*/
@PostMapping(value = "/get") @PostMapping(value = "/get")
public List<Books> getBooksList(){ public Response getBooksList(){
return booksService.getBooksList(); List<Books> booksList = booksService.getBooksList();
return new Response().normalResponse(booksList);
} }
//根据书本ID查找书籍 /**
* 根据书本ID查找书籍
* @param bookId
* @return
*/
@PostMapping(value = "/get/{bookId}") @PostMapping(value = "/get/{bookId}")
public Books getBookById(@PathVariable Integer bookId){ public Response getBookById(@PathVariable Integer bookId) throws BaseException{
return booksService.getBookById(bookId); Books books = booksService.getBookById(bookId);
return new Response().normalResponse(books);
} }
//添加书籍 /**
* 添加书籍
* @param bookName
* @return
* @throws BaseException
*/
@PostMapping(value = "/add/{bookName}") @PostMapping(value = "/add/{bookName}")
public String addBook(@PathVariable String bookName) { public Response addBook(@PathVariable String bookName) throws BaseException{
if(bookName == null || bookName.length() < 1) { Response response = new Response();
return "书名信息有误"; booksService.addBook(bookName);
} else { return response.normalResponse();
String res = booksService.addBook(bookName);
if(res != null) {
return res;
} else {
return "内部错误";
}
}
} }
//根据书本ID删除书籍 /**
* 根据书本ID删除书籍
* @param bookId
* @return
*/
@PostMapping(value = "/delete/{bookId}") @PostMapping(value = "/delete/{bookId}")
public String deleteBook(@PathVariable Integer bookId) { public Response deleteBook(@PathVariable Integer bookId) throws BaseException{
String res = booksService.deleteBook(bookId); Response response = new Response();
if(res != null) { booksService.deleteBook(bookId);
return res; return response.normalResponse();
} else {
return "内部错误";
}
} }
} }
package com.springboot.demo1.controller; package com.springboot.demo1.controller;
import com.springboot.demo1.entity.BorrowInfo; import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.Response;
import com.springboot.demo1.service.BorrowInfoService; import com.springboot.demo1.service.BorrowInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
...@@ -15,29 +15,21 @@ public class BorrowInfoController { ...@@ -15,29 +15,21 @@ public class BorrowInfoController {
@Resource @Resource
private BorrowInfoService borrowInfoService; private BorrowInfoService borrowInfoService;
//获取借阅信息列表 /**
* 获取借阅信息列表
*/
@PostMapping(value = "/get") @PostMapping(value = "/get")
public List<BorrowInfo> getBorrowInfoList() { public Response getBorrowInfoList() {
return borrowInfoService.getBorrowInfoList(); return new Response().normalResponse(borrowInfoService.getBorrowInfoList());
} }
//借书还书 /**
* 借书/还书
*/
@PostMapping(value = "/update/{userId}/{bookId}/{type}") @PostMapping(value = "/update/{userId}/{bookId}/{type}")
public String updateBorrowInfo(@PathVariable Integer userId,@PathVariable Integer bookId,@PathVariable Integer type) { public Response updateBorrowInfo(@PathVariable Integer userId,@PathVariable Integer bookId,@PathVariable Integer type) throws BaseException {
if(userId == null || bookId == null || type == null) { borrowInfoService.updateBorrowInfoList(userId, bookId, type);
return "error 504"; return new Response().normalResponse();
} else {
BorrowInfo borrowInfo = new BorrowInfo();
borrowInfo.setUserId(userId);
borrowInfo.setBookId(bookId);
borrowInfo.setType(type);
String res = borrowInfoService.updateBorrowInfoList(borrowInfo);
if(res != null) {
return res;
} else {
return "内部错误";
}
}
} }
} }
package com.springboot.demo1.controller; package com.springboot.demo1.controller;
import com.springboot.demo1.entity.User; import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.Response;
import com.springboot.demo1.service.UserService; import com.springboot.demo1.service.UserService;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List;
@RestController @RestController
@RequestMapping(value = "/user") @RequestMapping(value = "/user")
...@@ -14,41 +14,30 @@ public class UserController { ...@@ -14,41 +14,30 @@ public class UserController {
@Resource @Resource
private UserService userService; private UserService userService;
//获取用户列表 /**
* 获取用户列表
*/
@PostMapping(value = "/get") @PostMapping(value = "/get")
public List<User> getUserList(){ public Response getUserList(){
return userService.getUserList(); return new Response().normalResponse(userService.getUserList());
} }
//根据用户ID获取用户 /**
* 根据用户ID获取用户
*/
@PostMapping(value = "/get/{userId}") @PostMapping(value = "/get/{userId}")
public User getUserById(@PathVariable Integer userId){ public Response getUserById(@PathVariable Integer userId){
return userService.getUserByUserId(userId); return new Response().normalResponse(userService.getUserByUserId(userId));
} }
//添加用户 /**
/*@PostMapping(value = "/add/{userName}/{passWord}/{email}/{nickName}") * 添加用户
public String addUser(@PathVariable String userName,@PathVariable String passWord,@PathVariable String email, */
@PathVariable String nickName){*/ @PostMapping(value = "/add/{userName}/{passWord}/{email}/{nickName}")
@PostMapping(value = "/add") public Response addUser(@PathVariable String userName,@PathVariable String passWord,@PathVariable String email,
public String addUser(String userName, String passWord, String email, String nickName){ @PathVariable String nickName) throws BaseException{
if(passWord == null || passWord.length() < 6) { userService.addUser(userName, passWord, email, nickName);
return "密码格式有误"; return new Response().normalResponse();
} else if(email == null || email.length() < 6) {
return "邮箱格式有误";
} else {
User user = new User();
user.setUserName(userName);
user.setPassWord(passWord);
user.setEmail(email);
user.setNickName(nickName);
String res = userService.addUser(user);
if(res != null) {
return res;
} else {
return "内部错误";
}
}
} }
} }
package com.springboot.demo1.entity;
import lombok.Data;
@Data
public class BaseException extends Exception {
/**
* 错误码
*/
private int code;
/**
* 消息
*/
private String message;
public BaseException(String message,int code){
super();
this.code = code;
this.message = message;
}
public BaseException(String message,int code,Throwable e){
super(message,e);
this.message = message;
this.code = code;
}
}
package com.springboot.demo1.entity; package com.springboot.demo1.entity;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors;
import javax.persistence.*; import javax.persistence.*;
@Entity @Entity
@Table(name = "books")
@Data @Data
public class Books { public class Books {
//书籍ID /**
* 书籍ID
*/
@Id @Id
@GeneratedValue(strategy= GenerationType.AUTO) @GeneratedValue(strategy= GenerationType.AUTO)
private Integer bookId; private Integer bookId;
//书名 /**
* 书名
*/
@Column(nullable = false, unique = true) @Column(nullable = false, unique = true)
private String bookName; private String bookName;
//借阅状态 ==0表示书籍未被借阅 ==1表示书籍已被借阅 /**
* 书籍状态 ==0表示书籍未被借阅 ==1表示书籍已被借阅
*/
@Column(nullable = false) @Column(nullable = false)
private Integer bookState; private Integer bookState;
......
package com.springboot.demo1.entity; package com.springboot.demo1.entity;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.*; import javax.persistence.*;
import java.util.Date;
@Entity @Entity
@Data @Data
@DynamicUpdate(true)
public class BorrowInfo { public class BorrowInfo {
//借阅信息ID /**
* 借阅信息ID
*/
@Id @Id
@GeneratedValue(strategy= GenerationType.AUTO) @GeneratedValue(strategy= GenerationType.AUTO)
private Integer borrowId; private Integer borrowId;
//用户ID /**
* 用户ID
*/
@Column(nullable = false) @Column(nullable = false)
private Integer userId; private Integer userId;
//书籍ID /**
* 书籍ID
*/
@Column(nullable = false) @Column(nullable = false)
private Integer bookId; private Integer bookId;
//借阅状态 ==1表示借书 ==0表示还书 /**
* 借阅状态 ==1表示借书 ==0表示还书
*/
@Column(nullable = false) @Column(nullable = false)
private Integer type; private Integer type;
@Column
private Date borrowTime;
@Column
private Date returnTime;
} }
package com.springboot.demo1.entity;
import lombok.Data;
import java.io.Serializable;
@Data
public class Response implements Serializable {
/**
* 返回信息
*/
private String msg = "";
/**
* 传输的数据
*/
private Object data;
/**
* 返回值,成功时返回“100”,其他码均为失败
*/
private String result = "100";
public Response(){}
public Response(String msg) {
this.msg = msg;
}
public Response(String msg, Object data) {
this.msg = msg;
this.data = data;
}
/**
* 正常响应 处理成功
* @return
*/
public Response normalResponse() {
return new Response("调用成功");
}
public Response normalResponse(Object data) {
return new Response("调用成功", data);
}
}
package com.springboot.demo1.entity; package com.springboot.demo1.entity;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors;
import javax.persistence.*; import javax.persistence.*;
...@@ -8,30 +9,35 @@ import javax.persistence.*; ...@@ -8,30 +9,35 @@ import javax.persistence.*;
@Data @Data
public class User { public class User {
/**
* 用户ID
*/
@Id @Id
@GeneratedValue(strategy= GenerationType.AUTO) @GeneratedValue(strategy= GenerationType.AUTO)
private Integer userId; private Integer userId;
/**
* 用户名
*/
@Column(nullable = false, unique = true) @Column(nullable = false, unique = true)
private String userName; private String userName;
/**
* 密码
*/
@Column(nullable = false) @Column(nullable = false)
private String passWord; private String passWord;
/**
* 邮箱
*/
@Column(nullable = false, unique = true) @Column(nullable = false, unique = true)
private String email; private String email;
/**
* 昵称
*/
@Column(nullable = true, unique = true) @Column(nullable = true, unique = true)
private String nickName; private String nickName;
public User(Integer userId, String userName, String passWord, String email, String nickName) {
this.userId = userId;
this.userName = userName;
this.passWord = passWord;
this.email = email;
this.nickName = nickName;
}
public User() {}
} }
package com.springboot.demo1.service; package com.springboot.demo1.service;
import com.springboot.demo1.dao.BooksRepository; import com.springboot.demo1.dao.BooksRepository;
import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.Books; import com.springboot.demo1.entity.Books;
import com.springboot.demo1.service.impl.BooksServiceImpl; import com.springboot.demo1.service.impl.BooksServiceImpl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -18,27 +19,34 @@ public class BooksService implements BooksServiceImpl { ...@@ -18,27 +19,34 @@ public class BooksService implements BooksServiceImpl {
return booksRepository.findAll(); return booksRepository.findAll();
} }
public Books getBookById(Integer bookId) { public Books getBookById(Integer bookId) throws BaseException{
if(!booksRepository.findById(bookId).isPresent()) {
throw new BaseException("书籍不存在", 506);
}
return booksRepository.findBooksByBookId(bookId); return booksRepository.findBooksByBookId(bookId);
} }
public String addBook(String bookName) { public void addBook(String bookName) throws BaseException {
if (bookName == null || bookName.length() < 1) {
throw new BaseException("书名信息有误", 506);
}
Integer numByBookName = booksRepository.countBooksByBookName(bookName); Integer numByBookName = booksRepository.countBooksByBookName(bookName);
if(numByBookName > 0) { if (numByBookName > 0) {
return "该书籍已经存在"; throw new BaseException("该书籍已经存在", 506);
} else { } else {
Books books = new Books(); Books books = new Books();
books.setBookName(bookName); books.setBookName(bookName);
books.setBookState(0); //bookState为0表示书籍未被借出 books.setBookState(0); //bookState为0表示书籍未被借出
booksRepository.save(books); booksRepository.save(books);
return "书籍上传成功";
} }
} }
public String deleteBook(Integer bookId) { public void deleteBook(Integer bookId) throws BaseException {
if(!booksRepository.findById(bookId).isPresent()) {
throw new BaseException("书籍不存在", 506);
}
booksRepository.deleteById(bookId); booksRepository.deleteById(bookId);
return "书籍删除成功";
} }
} }
...@@ -2,12 +2,14 @@ package com.springboot.demo1.service; ...@@ -2,12 +2,14 @@ package com.springboot.demo1.service;
import com.springboot.demo1.dao.BooksRepository; import com.springboot.demo1.dao.BooksRepository;
import com.springboot.demo1.dao.BorrowInfoRepository; import com.springboot.demo1.dao.BorrowInfoRepository;
import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.Books; import com.springboot.demo1.entity.Books;
import com.springboot.demo1.entity.BorrowInfo; import com.springboot.demo1.entity.BorrowInfo;
import com.springboot.demo1.service.impl.BorrowInfoServiceImpl; import com.springboot.demo1.service.impl.BorrowInfoServiceImpl;
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.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -23,35 +25,44 @@ public class BorrowInfoService implements BorrowInfoServiceImpl { ...@@ -23,35 +25,44 @@ public class BorrowInfoService implements BorrowInfoServiceImpl {
return borrowInfoRepository.findAll(); return borrowInfoRepository.findAll();
} }
public String updateBorrowInfoList(BorrowInfo borrowInfo) { public void updateBorrowInfoList(Integer userId, Integer bookId, Integer type) throws BaseException {
BorrowInfo borrowInfo1 = new BorrowInfo(); if (userId == null || bookId == null || type == null) {
Books books = new Books(); throw new BaseException("参数为空", 506);
books = booksRepository.findBooksByBookId(borrowInfo.getBookId());
if(books == null){
return "不存在该书籍";
} }
BorrowInfo borrowInfo = new BorrowInfo();
borrowInfo.setUserId(userId);
borrowInfo.setBookId(bookId);
borrowInfo.setType(type);
//根据传进的用户ID和书籍ID查找是否有过借书记录 //根据传进的用户ID和书籍ID查找是否有过借书记录
borrowInfo1 = borrowInfoRepository.getBorrowInfoByUserIdAndBookId(borrowInfo.getUserId(), borrowInfo.getBookId()); BorrowInfo borrowInfo1 = borrowInfoRepository.getBorrowInfoByUserIdAndBookId(userId, bookId);
Books books = booksRepository.findBooksByBookId(bookId);
if(books == null){
throw new BaseException("不存在该书籍", 506) ;
}
//根据书籍ID查找该书籍 //根据书籍ID查找该书籍
if (null != borrowInfo1) { if (null != borrowInfo1) {
//type==1表示借书,bookState==1表示书籍已借出 //type==1表示借书,bookState==1表示书籍已借出
if (borrowInfo.getType() == 1 && books.getBookState() == 0) { //借书 if (type == 1 && books.getBookState() == 0) { //借书
borrowInfo.setBorrowId(borrowInfo1.getBorrowId()); //将对应借书记录进行修改 borrowInfo.setBorrowId(borrowInfo1.getBorrowId()); //将对应借书记录进行修改
borrowInfo.setBorrowTime(new Date());
books.setBookState(1); books.setBookState(1);
} else if (borrowInfo.getType() == 0 && books.getBookState() == 1) { //还书 } else if (type == 0 && books.getBookState() == 1) { //还书
borrowInfo.setBorrowId(borrowInfo1.getBorrowId()); borrowInfo.setBorrowId(borrowInfo1.getBorrowId());
borrowInfo.setBorrowTime(borrowInfo1.getBorrowTime());
borrowInfo.setReturnTime(new Date());
books.setBookState(0); books.setBookState(0);
} else if (borrowInfo.getType() == 1 && books.getBookState() == 1) { } else if (type == 1 && books.getBookState() == 1) {
return "该书籍已被借出"; throw new BaseException("该书籍已被借出", 506);
} else { } else {
return "操作错误"; throw new BaseException("操作错误", 506);
} }
} else {//原先没有借书记录,此时必为借书 } else {//原先没有借书记录,此时必为借书
if (borrowInfo.getType() == 0) { if (type == 0) {
return "操作错误"; throw new BaseException("操作错误", 506);
} else if (books.getBookState() == 1) { } else if (books.getBookState() == 1) {
return "该书籍已被借出"; throw new BaseException("该书籍已被借出", 506);
} else { } else {
borrowInfo.setBorrowTime(new Date());
if(books.getBookState() == 0) { if(books.getBookState() == 0) {
books.setBookState(1); books.setBookState(1);
} else { } else {
...@@ -61,7 +72,5 @@ public class BorrowInfoService implements BorrowInfoServiceImpl { ...@@ -61,7 +72,5 @@ public class BorrowInfoService implements BorrowInfoServiceImpl {
} }
borrowInfoRepository.save(borrowInfo); borrowInfoRepository.save(borrowInfo);
booksRepository.save(books); booksRepository.save(books);
return "操作成功";
} }
} }
package com.springboot.demo1.service; package com.springboot.demo1.service;
import com.springboot.demo1.dao.UserRepository; import com.springboot.demo1.dao.UserRepository;
import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.User; import com.springboot.demo1.entity.User;
import com.springboot.demo1.service.impl.UserServiceImpl; import com.springboot.demo1.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -22,17 +23,28 @@ public class UserService implements UserServiceImpl { ...@@ -22,17 +23,28 @@ public class UserService implements UserServiceImpl {
return userRepository.getUserByUserId(userId); return userRepository.getUserByUserId(userId);
} }
public String addUser(User user) { public void addUser(String userName, String passWord, String email, String nickName) throws BaseException {
Integer numByEmail = userRepository.countUserByEmail(user.getEmail()); if(passWord == null || passWord.length() < 6) {
Integer numByUserName = userRepository.countUserByUserName(user.getUserName()); throw new BaseException("密码格式有误", 506) ;
}
if(email == null || email.length() < 6) {
throw new BaseException("邮箱格式有误", 506) ;
}
Integer numByEmail = userRepository.countUserByEmail(email);
Integer numByUserName = userRepository.countUserByUserName(userName);
if(numByEmail > 0) { if(numByEmail > 0) {
return "该邮箱已被使用"; throw new BaseException("该邮箱已被使用", 506);
} else if(numByUserName > 0) {
return "该用户名已被使用";
} else {
userRepository.save(user);
} }
return "新建用户成功"; if(numByUserName > 0) {
throw new BaseException("该用户名已被占用", 506);
}
User user = new User();
user.setUserName(userName);
user.setEmail(email);
user.setPassWord(passWord);
user.setNickName(nickName);
userRepository.save(user);
} }
......
package com.springboot.demo1.service.impl; package com.springboot.demo1.service.impl;
import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.Books; import com.springboot.demo1.entity.Books;
import java.util.List; import java.util.List;
...@@ -8,10 +9,10 @@ public interface BooksServiceImpl { ...@@ -8,10 +9,10 @@ public interface BooksServiceImpl {
public List<Books> getBooksList(); public List<Books> getBooksList();
public Books getBookById(Integer bookId); public Books getBookById(Integer bookId) throws BaseException;
public String addBook(String bookName); public void addBook(String bookName) throws BaseException;
public String deleteBook(Integer bookId); public void deleteBook(Integer bookId) throws BaseException;
} }
package com.springboot.demo1.service.impl; package com.springboot.demo1.service.impl;
import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.BorrowInfo; import com.springboot.demo1.entity.BorrowInfo;
import java.util.List; import java.util.List;
...@@ -8,6 +9,6 @@ public interface BorrowInfoServiceImpl { ...@@ -8,6 +9,6 @@ public interface BorrowInfoServiceImpl {
public List<BorrowInfo> getBorrowInfoList(); public List<BorrowInfo> getBorrowInfoList();
public String updateBorrowInfoList(BorrowInfo borrowInfo); public void updateBorrowInfoList(Integer userId, Integer bookId, Integer type) throws BaseException;
} }
package com.springboot.demo1.service.impl; package com.springboot.demo1.service.impl;
import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.User; import com.springboot.demo1.entity.User;
import java.util.List; import java.util.List;
...@@ -10,5 +11,5 @@ public interface UserServiceImpl { ...@@ -10,5 +11,5 @@ public interface UserServiceImpl {
public User getUserByUserId(Integer userId); public User getUserByUserId(Integer userId);
public String addUser(User user); public void addUser(String userName, String passWord, String email, String nickName) throws BaseException;
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论