@ -0,0 +1,41 @@ | |||
package cn.edu.ecnu.stu.bookstore.controller; | |||
import cn.edu.ecnu.stu.bookstore.component.Result; | |||
import cn.edu.ecnu.stu.bookstore.pojo.Book; | |||
import cn.edu.ecnu.stu.bookstore.pojo.Store; | |||
import cn.edu.ecnu.stu.bookstore.service.SellerService; | |||
import com.alibaba.fastjson.JSONObject; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.web.bind.annotation.PostMapping; | |||
import org.springframework.web.bind.annotation.RequestBody; | |||
import org.springframework.web.bind.annotation.RequestMapping; | |||
import org.springframework.web.bind.annotation.RestController; | |||
@RestController | |||
@RequestMapping("/seller") | |||
public class SellerController { | |||
@Autowired | |||
public SellerService sellerService; | |||
@PostMapping("/create_store") | |||
public Result createStore(@RequestBody Store store) { | |||
sellerService.createStore(store); | |||
return Result.success(); | |||
} | |||
@PostMapping("add_book") | |||
public Result addBook(@RequestBody Book book) { | |||
sellerService.addBook(book); | |||
return Result.success(); | |||
} | |||
@PostMapping("add_stock_level") | |||
public Result addStockLevel(@RequestBody JSONObject body) { | |||
String storeId = body.getString("storeId"); | |||
String bookId = body.getString("bookId"); | |||
int addStockLevel = body.getIntValue("addStockLevel"); | |||
sellerService.addStockLevel(storeId, bookId, addStockLevel); | |||
return Result.success(); | |||
} | |||
} |
@ -0,0 +1,36 @@ | |||
package cn.edu.ecnu.stu.bookstore.mapper; | |||
import cn.edu.ecnu.stu.bookstore.pojo.Book; | |||
import org.apache.ibatis.annotations.*; | |||
import java.util.List; | |||
@Mapper | |||
public interface BookMapper { | |||
@Insert("insert into t_book(book_id, store_id, title, author, publisher," + | |||
"original_title, translator, pub_year, pages, price, binding, isbn, stock_level)" + | |||
"values (#{book.bookId}, #{book.storeId}, #{book.title}, #{book.author}, #{book.publisher}, #{book.originalTitle}, " + | |||
"#{book.translator}, #{book.pubYear}, #{book.pages}, #{book.price}, #{book.binding}, #{book.isbn}, #{book.stockLevel})") | |||
int insert(@Param("book") Book book); | |||
@Select("select tag from t_book_tag where book_id = #{book_id}") | |||
List<String> selectTags(@Param("book_id") String bookId); | |||
int insertTags(@Param("book_id") String bookId, @Param("tags") List<String> tags); | |||
@Select("select picture from t_book_picture where book_id = #{book_id}") | |||
List<String> selectPictureUrl(@Param("book_id") String bookId); | |||
int insertPictures(@Param("book_id") String bookId, @Param("pictures") List<String> pictures); | |||
@Insert("insert into t_book_detail(book_id, author_intro, book_intro, content) values " + | |||
"(#{book.bookId}, #{book.authorIntro}, #{book.bookIntro}, #{book.content})") | |||
int insertBookDetail(@Param("book") Book book); | |||
@Select("select count(*) from t_book where store_id = #{storeId} and book_id = #{bookId}") | |||
int checkBook(@Param("storeId") String storeId, @Param("bookId") String bookId); | |||
@Update("update t_book set stock_level = stock_level + #{addStockLevel} where store_id = #{storeId} and book_id = #{bookId}") | |||
void addStockLevel(@Param("storeId") String storeId, @Param("bookId") String bookId, @Param("addStockLevel") int addStockLevel); | |||
} |
@ -0,0 +1,20 @@ | |||
package cn.edu.ecnu.stu.bookstore.mapper; | |||
import cn.edu.ecnu.stu.bookstore.pojo.Store; | |||
import org.apache.ibatis.annotations.Insert; | |||
import org.apache.ibatis.annotations.Mapper; | |||
import org.apache.ibatis.annotations.Param; | |||
import org.apache.ibatis.annotations.Select; | |||
@Mapper | |||
public interface StoreMapper { | |||
@Select("select count(*) from t_store where store_id = #{store_id}") | |||
int checkStoreExist(@Param("store_id") String storeId); | |||
@Select("select count(*) from t_store where store_id = #{store_id} and store_id = #{store_id}") | |||
int checkStore(@Param("seller_id") Integer sellerId, @Param("store_id") String storeId); | |||
@Insert("insert into t_store(store_id, seller_id) values(#{store.storeId}, #{store.sellerId})") | |||
int insert(@Param("store") Store store); | |||
} |
@ -0,0 +1,50 @@ | |||
package cn.edu.ecnu.stu.bookstore.pojo; | |||
import lombok.AllArgsConstructor; | |||
import lombok.Data; | |||
import lombok.NoArgsConstructor; | |||
import java.math.BigDecimal; | |||
import java.util.List; | |||
@Data | |||
@AllArgsConstructor | |||
@NoArgsConstructor | |||
public class Book { | |||
private String bookId; | |||
private String storeId; | |||
private String title; | |||
private String author; | |||
private String publisher; | |||
private String originalTitle; | |||
private String translator; | |||
private String pubYear; | |||
private Integer pages; | |||
private BigDecimal price; | |||
private String binding; | |||
private String isbn; | |||
private int stockLevel; | |||
private List<String> tags; | |||
private List<String> pictures; | |||
private String authorIntro; | |||
private String bookIntro; | |||
private String content; | |||
} |
@ -0,0 +1,19 @@ | |||
package cn.edu.ecnu.stu.bookstore.pojo; | |||
import lombok.AllArgsConstructor; | |||
import lombok.Data; | |||
import lombok.NoArgsConstructor; | |||
import java.sql.Date; | |||
@Data | |||
@NoArgsConstructor | |||
@AllArgsConstructor | |||
public class Store { | |||
private String storeId; | |||
private int sellerId; | |||
private Date createTime; | |||
} |
@ -0,0 +1,17 @@ | |||
package cn.edu.ecnu.stu.bookstore.service; | |||
import cn.edu.ecnu.stu.bookstore.pojo.Book; | |||
import cn.edu.ecnu.stu.bookstore.pojo.Store; | |||
import java.util.List; | |||
public interface SellerService { | |||
void createStore(Store store); | |||
void addBook(Book book); | |||
void insertBook(Book book, List<String> tags, List<String> pictures); | |||
void addStockLevel(String storeId, String bookId, int addStockLevel); | |||
} |
@ -1,83 +1,17 @@ | |||
package cn.edu.ecnu.stu.bookstore.service; | |||
import cn.edu.ecnu.stu.bookstore.component.AppException; | |||
import cn.edu.ecnu.stu.bookstore.component.Constants; | |||
import cn.edu.ecnu.stu.bookstore.mapper.UserMapper; | |||
import cn.edu.ecnu.stu.bookstore.pojo.LoginUser; | |||
import cn.edu.ecnu.stu.bookstore.pojo.User; | |||
import cn.edu.ecnu.stu.bookstore.utils.JwtUtil; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.data.redis.core.RedisTemplate; | |||
import org.springframework.security.authentication.AuthenticationManager; | |||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | |||
import org.springframework.security.core.Authentication; | |||
import org.springframework.security.core.context.SecurityContextHolder; | |||
import org.springframework.security.crypto.password.PasswordEncoder; | |||
import org.springframework.stereotype.Service; | |||
import org.springframework.util.StringUtils; | |||
import java.util.concurrent.TimeUnit; | |||
public interface UserService { | |||
@Service | |||
public class UserService { | |||
void register(User user); | |||
void unregister(User user); | |||
User selectUserByName(String username); | |||
String login(User user); | |||
@Autowired | |||
private UserMapper userMapper; | |||
void logout(String username); | |||
@Autowired | |||
private AuthenticationManager authenticationManager; | |||
void changePassword(String username, String oldPassword, String newPassword); | |||
@Autowired | |||
private PasswordEncoder passwordEncoder; | |||
@Autowired | |||
private RedisTemplate redisTemplate; | |||
public void register(User user) { | |||
if(!StringUtils.hasLength(user.getUsername())) | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE); | |||
if(userMapper.checkUserByUsername(user.getUsername()) != 0) | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.REGISTER_ERROR_MESSAGE); | |||
String encoded = passwordEncoder.encode(user.getPassword()); | |||
user.setPassword(encoded); | |||
userMapper.insert(user); | |||
} | |||
public void unregister(User user) { | |||
User user1 = userMapper.selectOneByName(user.getUsername()); | |||
if(user1 == null || !passwordEncoder.matches(user.getPassword(), user1.getPassword())) { | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.PASSWORD_ERROR); | |||
} | |||
userMapper.deleteByName(user.getUsername()); | |||
} | |||
public User selectUserByName(String username) { | |||
return userMapper.selectOneByName(username); | |||
} | |||
public String login(User user) { | |||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()); | |||
Authentication authenticate = authenticationManager.authenticate(token); | |||
LoginUser loginUser = (LoginUser)authenticate.getPrincipal(); | |||
redisTemplate.opsForValue().set("userId:" + loginUser.getUser().getId(), "1", 1, TimeUnit.DAYS); | |||
return JwtUtil.getToken(loginUser.getUser()); | |||
} | |||
public void logout(String username) { | |||
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | |||
if(!user.getUsername().equals(username)) { | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE); | |||
} | |||
redisTemplate.delete("userId:" + user.getId()); | |||
} | |||
public void changePassword(String username, String oldPassword, String newPassword) { | |||
User user = userMapper.selectOneByName(username); | |||
if(user == null || !passwordEncoder.matches(oldPassword, user.getPassword())){ | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.PASSWORD_ERROR); | |||
} | |||
newPassword = passwordEncoder.encode(newPassword); | |||
userMapper.updatePassword(username, newPassword); | |||
redisTemplate.delete("userId:" + user.getId()); | |||
} | |||
} |
@ -0,0 +1,74 @@ | |||
package cn.edu.ecnu.stu.bookstore.service.impl; | |||
import cn.edu.ecnu.stu.bookstore.component.AppException; | |||
import cn.edu.ecnu.stu.bookstore.component.Constants; | |||
import cn.edu.ecnu.stu.bookstore.mapper.BookMapper; | |||
import cn.edu.ecnu.stu.bookstore.mapper.StoreMapper; | |||
import cn.edu.ecnu.stu.bookstore.pojo.Book; | |||
import cn.edu.ecnu.stu.bookstore.pojo.Store; | |||
import cn.edu.ecnu.stu.bookstore.pojo.User; | |||
import cn.edu.ecnu.stu.bookstore.service.SellerService; | |||
import cn.edu.ecnu.stu.bookstore.utils.ImageUtil; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.context.annotation.Lazy; | |||
import org.springframework.security.core.context.SecurityContextHolder; | |||
import org.springframework.stereotype.Service; | |||
import org.springframework.transaction.annotation.Transactional; | |||
import java.util.List; | |||
import java.util.stream.Collectors; | |||
@Service | |||
public class SellerServiceImpl implements SellerService { | |||
@Autowired | |||
private StoreMapper storeMapper; | |||
@Autowired | |||
private BookMapper bookMapper; | |||
@Autowired | |||
@Lazy | |||
private SellerService sellerService; | |||
public void createStore(Store store) { | |||
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | |||
store.setSellerId(user.getId()); | |||
if(storeMapper.checkStoreExist(store.getStoreId()) != 0) | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.STORE_ID_ERROR); | |||
storeMapper.insert(store); | |||
} | |||
public void addBook(Book book) { | |||
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | |||
int userId = user.getId(); | |||
if(storeMapper.checkStore(userId, book.getStoreId()) == 0) | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.STORE_NON_EXIST_ERROR); | |||
List<String> tags = book.getTags(); | |||
List<String> pictures = book.getPictures(); | |||
pictures = pictures.stream().map(ImageUtil::convertBase64StrToImage).collect(Collectors.toList()); | |||
sellerService.insertBook(book, tags, pictures); | |||
} | |||
@Transactional | |||
public void insertBook(Book book, List<String> tags, List<String> pictures) { | |||
bookMapper.insert(book); | |||
bookMapper.insertBookDetail(book); | |||
bookMapper.insertTags(book.getBookId(), tags); | |||
bookMapper.insertPictures(book.getBookId(), pictures); | |||
} | |||
@Override | |||
public void addStockLevel(String storeId, String bookId, int addStockLevel) { | |||
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | |||
int userId = user.getId(); | |||
if(storeMapper.checkStore(userId, storeId) == 0) | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.STORE_NON_EXIST_ERROR); | |||
if(bookMapper.checkBook(storeId, bookId) == 0) | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.BOOK_ERROR); | |||
bookMapper.addStockLevel(storeId, bookId, addStockLevel); | |||
} | |||
} |
@ -1,9 +1,10 @@ | |||
package cn.edu.ecnu.stu.bookstore.service; | |||
package cn.edu.ecnu.stu.bookstore.service.impl; | |||
import cn.edu.ecnu.stu.bookstore.component.AppException; | |||
import cn.edu.ecnu.stu.bookstore.component.Constants; | |||
import cn.edu.ecnu.stu.bookstore.pojo.LoginUser; | |||
import cn.edu.ecnu.stu.bookstore.pojo.User; | |||
import cn.edu.ecnu.stu.bookstore.service.UserService; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.security.core.userdetails.UserDetails; | |||
import org.springframework.security.core.userdetails.UserDetailsService; |
@ -0,0 +1,88 @@ | |||
package cn.edu.ecnu.stu.bookstore.service.impl; | |||
import cn.edu.ecnu.stu.bookstore.component.AppException; | |||
import cn.edu.ecnu.stu.bookstore.component.Constants; | |||
import cn.edu.ecnu.stu.bookstore.mapper.UserMapper; | |||
import cn.edu.ecnu.stu.bookstore.pojo.LoginUser; | |||
import cn.edu.ecnu.stu.bookstore.pojo.User; | |||
import cn.edu.ecnu.stu.bookstore.service.UserService; | |||
import cn.edu.ecnu.stu.bookstore.utils.JwtUtil; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.data.redis.core.RedisTemplate; | |||
import org.springframework.security.authentication.AuthenticationManager; | |||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | |||
import org.springframework.security.core.Authentication; | |||
import org.springframework.security.core.context.SecurityContextHolder; | |||
import org.springframework.security.crypto.password.PasswordEncoder; | |||
import org.springframework.stereotype.Service; | |||
import org.springframework.util.StringUtils; | |||
import java.util.concurrent.TimeUnit; | |||
@Service | |||
public class UserServiceImpl implements UserService { | |||
@Autowired | |||
private UserMapper userMapper; | |||
@Autowired | |||
private AuthenticationManager authenticationManager; | |||
@Autowired | |||
private PasswordEncoder passwordEncoder; | |||
@Autowired | |||
private RedisTemplate redisTemplate; | |||
public User selectOneById(Integer id) { | |||
return userMapper.selectOneById(id); | |||
} | |||
public void register(User user) { | |||
if(!StringUtils.hasLength(user.getUsername())) | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE); | |||
if(userMapper.checkUserByUsername(user.getUsername()) != 0) | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.REGISTER_ERROR_MESSAGE); | |||
String encoded = passwordEncoder.encode(user.getPassword()); | |||
user.setPassword(encoded); | |||
userMapper.insert(user); | |||
} | |||
public void unregister(User user) { | |||
User user1 = userMapper.selectOneByName(user.getUsername()); | |||
if(user1 == null || !passwordEncoder.matches(user.getPassword(), user1.getPassword())) { | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.PASSWORD_ERROR); | |||
} | |||
userMapper.deleteByName(user.getUsername()); | |||
} | |||
public User selectUserByName(String username) { | |||
return userMapper.selectOneByName(username); | |||
} | |||
public String login(User user) { | |||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()); | |||
Authentication authenticate = authenticationManager.authenticate(token); | |||
LoginUser loginUser = (LoginUser)authenticate.getPrincipal(); | |||
redisTemplate.opsForValue().set("userId:" + loginUser.getUser().getId(), "1", 1, TimeUnit.DAYS); | |||
return JwtUtil.getToken(loginUser.getUser()); | |||
} | |||
public void logout(String username) { | |||
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | |||
if(!user.getUsername().equals(username)) { | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE); | |||
} | |||
redisTemplate.delete("userId:" + user.getId()); | |||
} | |||
public void changePassword(String username, String oldPassword, String newPassword) { | |||
User user = userMapper.selectOneByName(username); | |||
if(user == null || !passwordEncoder.matches(oldPassword, user.getPassword())){ | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.PASSWORD_ERROR); | |||
} | |||
newPassword = passwordEncoder.encode(newPassword); | |||
userMapper.updatePassword(username, newPassword); | |||
redisTemplate.delete("userId:" + user.getId()); | |||
} | |||
} |
@ -0,0 +1,93 @@ | |||
package cn.edu.ecnu.stu.bookstore.utils; | |||
import cn.edu.ecnu.stu.bookstore.component.AppException; | |||
import cn.edu.ecnu.stu.bookstore.component.Constants; | |||
import java.awt.image.BufferedImage; | |||
import java.io.ByteArrayInputStream; | |||
import java.io.ByteArrayOutputStream; | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.util.Base64; | |||
import java.util.UUID; | |||
import javax.imageio.ImageIO; | |||
public class ImageUtil { | |||
/** | |||
* 图片转Base64字符串 | |||
* @param imageFileName | |||
* @return | |||
*/ | |||
public static String convertImageToBase64Str(String imageFileName) { | |||
ByteArrayOutputStream baos = null; | |||
try { | |||
//获取图片类型 | |||
String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1); | |||
//构建文件 | |||
File imageFile = new File("pictures/" + imageFileName); | |||
//通过ImageIO把文件读取成BufferedImage对象 | |||
BufferedImage bufferedImage = ImageIO.read(imageFile); | |||
//构建字节数组输出流 | |||
baos = new ByteArrayOutputStream(); | |||
//写入流 | |||
ImageIO.write(bufferedImage, suffix, baos); | |||
//通过字节数组流获取字节数组 | |||
byte[] bytes = baos.toByteArray(); | |||
//获取JDK8里的编码器Base64.Encoder转为base64字符 | |||
return Base64.getEncoder().encodeToString(bytes); | |||
} catch (Exception e) { | |||
e.printStackTrace(); | |||
} finally { | |||
try { | |||
if (baos != null) { | |||
baos.close(); | |||
} | |||
} catch (IOException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
return null; | |||
} | |||
/** | |||
* Base64字符串转图片 | |||
* @param base64String | |||
*/ | |||
public static String convertBase64StrToImage(String base64String) { | |||
ByteArrayInputStream bais = null; | |||
try { | |||
//获取图片类型 | |||
String suffix = "png"; | |||
//获取JDK8里的解码器Base64.Decoder,将base64字符串转为字节数组 | |||
byte[] bytes = Base64.getDecoder().decode(base64String); | |||
//构建字节数组输入流 | |||
bais = new ByteArrayInputStream(bytes); | |||
//通过ImageIO把字节数组输入流转为BufferedImage | |||
BufferedImage bufferedImage = ImageIO.read(bais); | |||
String fileName = UUID.randomUUID() + "." + suffix; | |||
String fullPath = "pictures/" + fileName; | |||
//构建文件 | |||
File imageFile = new File(fullPath); | |||
//写入生成文件 | |||
ImageIO.write(bufferedImage, suffix, imageFile); | |||
return fileName; | |||
} catch (Exception e) { | |||
throw new AppException(Constants.CLIENT_ERROR, Constants.PICTURE_ERROR); | |||
} finally { | |||
try { | |||
if (bais != null) { | |||
bais.close(); | |||
} | |||
} catch (IOException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
} | |||
public static void main(String[] args) { | |||
System.out.println(convertImageToBase64Str("d8c8d6d0ffb2a0257a1aa80835cbb81c.jpeg")); | |||
} | |||
} | |||
@ -0,0 +1,19 @@ | |||
<!DOCTYPE mapper | |||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |||
<mapper namespace="cn.edu.ecnu.stu.bookstore.mapper.BookMapper"> | |||
<insert id="insertTags"> | |||
insert into t_book_tag(book_id, tag) values | |||
<foreach collection="tags" item="tag" separator=","> | |||
(#{book_id}, #{tag}) | |||
</foreach> | |||
</insert> | |||
<insert id="insertPictures"> | |||
insert into t_book_picture(book_id, picture) values | |||
<foreach collection="pictures" item="picture"> | |||
(#{book_id}, #{picture}) | |||
</foreach> | |||
</insert> | |||
</mapper> |
@ -0,0 +1,7 @@ | |||
<!DOCTYPE mapper | |||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |||
<mapper namespace="cn.edu.ecnu.stu.bookstore.mapper.StoreMapper"> | |||
</mapper> |
@ -1,28 +0,0 @@ | |||
package cn.edu.ecnu.stu.bookstore; | |||
import org.junit.jupiter.api.Test; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.boot.test.context.SpringBootTest; | |||
import org.springframework.data.redis.core.RedisTemplate; | |||
import org.springframework.data.redis.core.ValueOperations; | |||
@SpringBootTest | |||
class BookstoreApplicationTests { | |||
@Test | |||
void contextLoads() { | |||
} | |||
@Test | |||
public void SetTest(@Autowired RedisTemplate redisTemplate){ | |||
ValueOperations value = redisTemplate.opsForValue(); | |||
value.set("springBoot","RedisOnSpringBoot"); | |||
} | |||
@Test | |||
public void GetTest(@Autowired RedisTemplate redisTemplate){ | |||
ValueOperations value = redisTemplate.opsForValue(); | |||
Object o = value.get("springBoot"); | |||
System.out.println(o); | |||
} | |||
} |
@ -0,0 +1,103 @@ | |||
package cn.edu.ecnu.stu.bookstore; | |||
import cn.edu.ecnu.stu.bookstore.component.Constants; | |||
import cn.edu.ecnu.stu.bookstore.component.Result; | |||
import cn.edu.ecnu.stu.bookstore.utils.ImageUtil; | |||
import cn.edu.ecnu.stu.bookstore.utils.RequestUtil; | |||
import com.alibaba.fastjson.JSONObject; | |||
import org.junit.jupiter.api.MethodOrderer; | |||
import org.junit.jupiter.api.Order; | |||
import org.junit.jupiter.api.Test; | |||
import org.junit.jupiter.api.TestMethodOrder; | |||
import org.springframework.boot.test.context.SpringBootTest; | |||
import javax.annotation.PostConstruct; | |||
import java.math.BigDecimal; | |||
import java.util.*; | |||
@SpringBootTest | |||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | |||
public class SellerTest { | |||
private String token; | |||
private final String storeId = "store" + UUID.randomUUID(); | |||
private final String bookId = "book" + UUID.randomUUID(); | |||
@PostConstruct | |||
public void init() { | |||
String url = Constants.URL_PREFIX + "/auth/register"; | |||
Map<String, Object> map = new HashMap<>(); | |||
String username = UUID.randomUUID().toString(); | |||
String password = username + "x"; | |||
map.put("username", username); | |||
map.put("password", password); | |||
Result result = RequestUtil.post(url, map, null); | |||
assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS); | |||
url = Constants.URL_PREFIX + "/auth/login"; | |||
result = RequestUtil.post(url, map, null); | |||
assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS); | |||
JSONObject data = (JSONObject) result.getData(); | |||
token = data.getString("token"); | |||
} | |||
@Order(1) | |||
@Test | |||
public void testCreateStore() { | |||
String url = Constants.URL_PREFIX + "/seller/create_store"; | |||
HashMap<String, Object> map = new HashMap<>(); | |||
map.put("storeId", storeId); | |||
Result result = RequestUtil.post(url, map, token); | |||
assert result != null && Constants.SUCCESS.equals(result.getCode()); | |||
result = RequestUtil.post(url, map, token); | |||
assert result != null && Constants.CLIENT_ERROR.equals(result.getCode()); | |||
} | |||
@Order(2) | |||
@Test | |||
public void testAddBook() { | |||
testCreateStore(); | |||
String url = Constants.URL_PREFIX + "/seller/add_book"; | |||
String title = "title" + UUID.randomUUID(); | |||
String author = "author" + UUID.randomUUID(); | |||
String publisher = "publisher" + UUID.randomUUID(); | |||
BigDecimal price = new BigDecimal("29.88"); | |||
String authorIntro = "authorIntro" + UUID.randomUUID(); | |||
String bookIntro = "bookIntro" + UUID.randomUUID(); | |||
String content = "content" + UUID.randomUUID(); | |||
List<String> tags = Arrays.asList("tag" + UUID.randomUUID(), "tag" + UUID.randomUUID()); | |||
List<String> pictures = Arrays.asList(ImageUtil.convertImageToBase64Str("13b2e8c0-c0fd-4d29-b3d6-820019216b2e.png")); | |||
HashMap<String, Object> map = new HashMap<>(); | |||
map.put("storeId", storeId); | |||
map.put("bookId", bookId); | |||
map.put("title", title); | |||
map.put("author", author); | |||
map.put("publisher", publisher); | |||
map.put("price", price); | |||
map.put("authorIntro", authorIntro); | |||
map.put("bookIntro", bookIntro); | |||
map.put("content", content); | |||
map.put("tags", tags); | |||
map.put("pictures", pictures); | |||
Result result = RequestUtil.post(url, map, token); | |||
assert result != null && Constants.SUCCESS.equals(result.getCode()); | |||
} | |||
@Test | |||
public void testAddStockLevel() { | |||
testAddBook(); | |||
String url = Constants.URL_PREFIX + "/seller/add_stock_level"; | |||
HashMap<String, Object> map = new HashMap<>(); | |||
map.put("storeId", storeId); | |||
map.put("bookId", bookId); | |||
map.put("addStockLevel", 10); | |||
Result result = RequestUtil.post(url, map, token); | |||
assert result != null && Constants.SUCCESS.equals(result.getCode()); | |||
} | |||
} |