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

完善程序4.14.1

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