提交 798cc988 authored 作者: xuyang's avatar xuyang

first commit

上级 46ae20e5
...@@ -3,24 +3,54 @@ package com.springboot.demo1.controller; ...@@ -3,24 +3,54 @@ package com.springboot.demo1.controller;
import com.springboot.demo1.entity.Books; import com.springboot.demo1.entity.Books;
import com.springboot.demo1.service.BooksService; import com.springboot.demo1.service.BooksService;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
@Controller @RestController
@RequestMapping(value = "/books") @RequestMapping(value = "/books")
public class BooksController { public class BooksController {
@Resource @Resource
private BooksService booksService; private BooksService booksService;
//获取图书列表
@PostMapping(value = "/get") @PostMapping(value = "/get")
@ResponseBody
public List<Books> getBooksList(){ public List<Books> getBooksList(){
return booksService.getBooksList(); return booksService.getBooksList();
} }
//根据书本ID查找书籍
@PostMapping(value = "/get/{bookId}")
public Books getBookById(@PathVariable Integer bookId){
return booksService.getBookById(bookId);
}
//添加书籍
@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 "内部错误";
}
}
}
//根据书本ID删除书籍
@PostMapping(value = "/delete/{bookId}")
public String deleteBook(@PathVariable Integer bookId) {
String res = booksService.deleteBook(bookId);
if(res != null) {
return res;
} else {
return "内部错误";
}
}
} }
...@@ -3,29 +3,26 @@ package com.springboot.demo1.controller; ...@@ -3,29 +3,26 @@ package com.springboot.demo1.controller;
import com.springboot.demo1.entity.BorrowInfo; import com.springboot.demo1.entity.BorrowInfo;
import com.springboot.demo1.service.BorrowInfoService; import com.springboot.demo1.service.BorrowInfoService;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
@Controller @RestController
@RequestMapping(value = "/borrowInfo") @RequestMapping(value = "/borrowInfo")
public class BorrowInfoController { public class BorrowInfoController {
@Resource @Resource
private BorrowInfoService borrowInfoService; private BorrowInfoService borrowInfoService;
//获取借阅信息列表
@PostMapping(value = "/get") @PostMapping(value = "/get")
@ResponseBody
public List<BorrowInfo> getBorrowInfoList() { public List<BorrowInfo> getBorrowInfoList() {
return borrowInfoService.getBorrowInfoList(); return borrowInfoService.getBorrowInfoList();
} }
//借书还书
@PostMapping(value = "/update/{userId}/{bookId}/{type}") @PostMapping(value = "/update/{userId}/{bookId}/{type}")
@ResponseBody
public String updateBorrowInfo(@PathVariable Integer userId,@PathVariable Integer bookId,@PathVariable Integer type) { public String updateBorrowInfo(@PathVariable Integer userId,@PathVariable Integer bookId,@PathVariable Integer type) {
if(userId == null || bookId == null || type == null) { if(userId == null || bookId == null || type == null) {
return "error 504"; return "error 504";
...@@ -40,7 +37,6 @@ public class BorrowInfoController { ...@@ -40,7 +37,6 @@ public class BorrowInfoController {
} else { } else {
return "内部错误"; return "内部错误";
} }
} }
} }
......
...@@ -2,26 +2,53 @@ package com.springboot.demo1.controller; ...@@ -2,26 +2,53 @@ package com.springboot.demo1.controller;
import com.springboot.demo1.entity.User; import com.springboot.demo1.entity.User;
import com.springboot.demo1.service.UserService; import com.springboot.demo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
@Controller @RestController
@RequestMapping(value = "/user") @RequestMapping(value = "/user")
public class UserController { public class UserController {
@Resource @Resource
private UserService userService; private UserService userService;
//获取用户列表
@PostMapping(value = "/get") @PostMapping(value = "/get")
@ResponseBody
public List<User> getUserList(){ public List<User> getUserList(){
return userService.getUserList(); return userService.getUserList();
} }
//根据用户ID获取用户
@PostMapping(value = "/get/{userId}")
public User getUserById(@PathVariable Integer userId){
return 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 "内部错误";
}
}
}
} }
...@@ -7,4 +7,6 @@ public interface BooksRepository extends JpaRepository<Books, Integer> { ...@@ -7,4 +7,6 @@ public interface BooksRepository extends JpaRepository<Books, Integer> {
Books findBooksByBookId(Integer bookId); Books findBooksByBookId(Integer bookId);
Integer countBooksByBookName(String bookName);
} }
...@@ -11,4 +11,8 @@ public interface UserRepository extends JpaRepository<User,Integer> { ...@@ -11,4 +11,8 @@ public interface UserRepository extends JpaRepository<User,Integer> {
User getUserByUserId(Integer userId); User getUserByUserId(Integer userId);
Integer countUserByEmail(String email);
Integer countUserByUserName(String userName);
} }
...@@ -9,13 +9,16 @@ import javax.persistence.*; ...@@ -9,13 +9,16 @@ import javax.persistence.*;
@Data @Data
public class Books { public class Books {
//书籍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表示书籍已被借阅
@Column(nullable = false) @Column(nullable = false)
private Integer bookState; private Integer bookState;
......
...@@ -8,16 +8,20 @@ import javax.persistence.*; ...@@ -8,16 +8,20 @@ import javax.persistence.*;
@Data @Data
public class BorrowInfo { public class BorrowInfo {
//借阅信息ID
@Id @Id
@GeneratedValue(strategy= GenerationType.AUTO) @GeneratedValue(strategy= GenerationType.AUTO)
private Integer borrowId; private Integer borrowId;
//用户ID
@Column(nullable = false) @Column(nullable = false)
private Integer userId; private Integer userId;
//书籍ID
@Column(nullable = false) @Column(nullable = false)
private Integer bookId; private Integer bookId;
//借阅状态 ==1表示借书 ==0表示还书
@Column(nullable = false) @Column(nullable = false)
private Integer type; private Integer type;
......
...@@ -14,8 +14,31 @@ public class BooksService implements BooksServiceImpl { ...@@ -14,8 +14,31 @@ public class BooksService implements BooksServiceImpl {
@Autowired @Autowired
private BooksRepository booksRepository; private BooksRepository booksRepository;
public List<Books> getBooksList(){ public List<Books> getBooksList() {
return booksRepository.findAll(); return booksRepository.findAll();
} }
public Books getBookById(Integer bookId) {
return booksRepository.findBooksByBookId(bookId);
}
public String addBook(String bookName) {
Integer numByBookName = booksRepository.countBooksByBookName(bookName);
if(numByBookName > 0) {
return "该书籍已经存在";
} else {
Books books = new Books();
books.setBookName(bookName);
books.setBookState(0); //bookState为0表示书籍未被借出
booksRepository.save(books);
return "书籍上传成功";
}
}
public String deleteBook(Integer bookId) {
booksRepository.deleteById(bookId);
return "书籍删除成功";
}
} }
...@@ -18,5 +18,22 @@ public class UserService implements UserServiceImpl { ...@@ -18,5 +18,22 @@ public class UserService implements UserServiceImpl {
return userRepository.findAll(); return userRepository.findAll();
} }
public User getUserByUserId(Integer userId) {
return userRepository.getUserByUserId(userId);
}
public String addUser(User user) {
Integer numByEmail = userRepository.countUserByEmail(user.getEmail());
Integer numByUserName = userRepository.countUserByUserName(user.getUserName());
if(numByEmail > 0) {
return "该邮箱已被使用";
} else if(numByUserName > 0) {
return "该用户名已被使用";
} else {
userRepository.save(user);
}
return "新建用户成功";
}
} }
...@@ -8,4 +8,10 @@ public interface BooksServiceImpl { ...@@ -8,4 +8,10 @@ public interface BooksServiceImpl {
public List<Books> getBooksList(); public List<Books> getBooksList();
public Books getBookById(Integer bookId);
public String addBook(String bookName);
public String deleteBook(Integer bookId);
} }
...@@ -8,5 +8,7 @@ public interface UserServiceImpl { ...@@ -8,5 +8,7 @@ public interface UserServiceImpl {
public List<User> getUserList(); public List<User> getUserList();
public User getUserByUserId(Integer userId);
public String addUser(User user);
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论