Skip to content

Commit add70a0

Browse files
committed
ProductInfo Service finished
1 parent e117dc4 commit add70a0

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.csjihe.springbootwechatordersystem.enums;
2+
3+
4+
import lombok.Getter;
5+
6+
/**
7+
* Product Status
8+
*/
9+
10+
@Getter
11+
public enum ProductStatusEnum {
12+
13+
14+
UP(0, "在架"),
15+
DOWN(0, "已下架");
16+
17+
private Integer code;
18+
19+
private String message;
20+
21+
ProductStatusEnum(Integer code, String message) {
22+
this.code = code;
23+
this.message = message;
24+
}
25+
26+
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.csjihe.springbootwechatordersystem.service;
2+
3+
import com.csjihe.springbootwechatordersystem.dataobject.ProductInfo;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
6+
7+
import java.util.List;
8+
9+
public interface ProductService {
10+
11+
ProductInfo findOne(String productId);
12+
13+
14+
/** Find all on-shelf product */
15+
List<ProductInfo> findUpAll();
16+
17+
Page<ProductInfo> findAll(Pageable pageable);
18+
19+
ProductInfo save(ProductInfo productInfo);
20+
21+
// Increase stock
22+
23+
24+
// Decrease stock
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.csjihe.springbootwechatordersystem.service.implementation;
2+
3+
import com.csjihe.springbootwechatordersystem.dataobject.ProductInfo;
4+
import com.csjihe.springbootwechatordersystem.enums.ProductStatusEnum;
5+
import com.csjihe.springbootwechatordersystem.repository.ProductInfoRepository;
6+
import com.csjihe.springbootwechatordersystem.service.ProductService;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.data.domain.Page;
9+
import org.springframework.data.domain.Pageable;
10+
import org.springframework.stereotype.Service;
11+
12+
import java.util.List;
13+
14+
15+
@Service
16+
public class ProductServiceImpl implements ProductService {
17+
18+
@Autowired
19+
private ProductInfoRepository repository;
20+
21+
@Override
22+
public ProductInfo findOne(String productId) {
23+
return repository.findOne(productId);
24+
}
25+
26+
@Override
27+
public List<ProductInfo> findUpAll() {
28+
return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
29+
}
30+
31+
@Override
32+
public Page<ProductInfo> findAll(Pageable pageable) {
33+
return repository.findAll(pageable);
34+
}
35+
36+
@Override
37+
public ProductInfo save(ProductInfo productInfo) {
38+
return repository.save(productInfo);
39+
}
40+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.csjihe.springbootwechatordersystem.service.implementation;
2+
3+
import com.csjihe.springbootwechatordersystem.dataobject.ProductInfo;
4+
import com.csjihe.springbootwechatordersystem.enums.ProductStatusEnum;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.data.domain.Page;
11+
import org.springframework.data.domain.PageRequest;
12+
import org.springframework.data.domain.Pageable;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
15+
16+
import java.math.BigDecimal;
17+
import java.util.List;
18+
19+
import static org.junit.Assert.*;
20+
21+
22+
@RunWith(SpringRunner.class)
23+
@SpringBootTest
24+
public class ProductServiceImplTest {
25+
26+
@Autowired
27+
private ProductServiceImpl productService;
28+
29+
@Test
30+
public void findOne() {
31+
ProductInfo productInfo = productService.findOne("123456");
32+
Assert.assertEquals("123456", productInfo.getProductId());
33+
}
34+
35+
@Test
36+
public void findUpAll() {
37+
List<ProductInfo> productInfoList = productService.findUpAll();
38+
Assert.assertNotEquals(0, productInfoList.size());
39+
}
40+
41+
@Test
42+
public void findAll() {
43+
PageRequest request = new PageRequest(0, 2);
44+
Page<ProductInfo> productInfoPage = productService.findAll(request);
45+
System.out.println(productInfoPage.getTotalElements());
46+
}
47+
48+
@Test
49+
public void save() {
50+
ProductInfo productInfo = new ProductInfo();
51+
52+
productInfo.setProductId("123457");
53+
productInfo.setProductName("皮皮虾");
54+
productInfo.setProductPrice(new BigDecimal(3.2));
55+
productInfo.setProductStock(100);
56+
productInfo.setProductDescription("Good Shrimp");
57+
productInfo.setProductIcon("http://xxx.jpg");
58+
productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
59+
productInfo.setCategoryType(2);
60+
61+
ProductInfo result = productService.save(productInfo);
62+
Assert.assertNotEquals(null, result);
63+
64+
}
65+
}

0 commit comments

Comments
 (0)