Commit 1b6ac182 authored by 0Tyler's avatar 0Tyler

feat: change id to uuid type

parent c5c9b132
......@@ -11,6 +11,7 @@ import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
......@@ -30,20 +31,25 @@ public class DefaultDataBean implements ApplicationRunner {
userService.addRoleToAccount("user1", "ADMIN");
userService.addRoleToAccount("user1", "USER");
productService.create(Product.builder()
.name("test")
.tags(List.of("test"))
.name("product1")
.tags(List.of("tag1", "tag2"))
.quantity(10)
.build());
TheOrder a = orderService.create(TheOrder.builder()
productService.create(Product.builder()
.name("product2")
.tags(List.of("tag2", "tag3"))
.quantity(20)
.build());
TheOrder order1 = orderService.create(TheOrder.builder()
.email("email")
.state("state")
.checkoutDate("2020-07-14 00:00:00")
.checkoutDate(LocalDateTime.now())
.build())
.orElseThrow(() -> new EntityNotFoundException("no order"));
Item b = orderService.create(Item.builder()
.sku(UUID.randomUUID().toString())
Item item1 = orderService.create(Item.builder()
.quantity(1)
.build())
.orElseThrow(() -> new EntityNotFoundException("no Item"));
orderService.addItemToOrder(a.getIid(), b.getSku());
orderService.addItemToOrder(order1.getIid(), item1.getSku());
}
}
......@@ -56,13 +56,20 @@ public class SecurityConfig {
.and()
.authorizeRequests()
.antMatchers("/h2**").permitAll()
// .addFilterBefore(new JwtAuthenticationFilter(authenticationManager, jwtTokenProvider, jwtConfig), UsernamePasswordAuthenticationFilter.class)
// .addFilter(new JwtAuthorizationFilter(authenticationManager, jwtTokenProvider))
.anyRequest().permitAll();
.and()
.addFilterBefore(jwtAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class)
.addFilter(new JwtAuthorizationFilter(authenticationManager, jwtTokenProvider))
.authorizeRequests();
http.headers().frameOptions().disable();
return http.build();
}
public JwtAuthenticationFilter jwtAuthorizationFilter() {
JwtAuthenticationFilter jwtAuthenticationFilter = new JwtAuthenticationFilter(authenticationManager, jwtTokenProvider, jwtConfig);
jwtAuthenticationFilter.setFilterProcessesUrl(jwtConfig.getUrl());
return jwtAuthenticationFilter;
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
......
......@@ -10,8 +10,11 @@ import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class ProductController {
private final ProductServiceImpl productService;
......@@ -19,7 +22,7 @@ public class ProductController {
@GetMapping("/product/{sku}")
public ResponseEntity<ResponseModel> getProduct(@PathVariable Integer sku) {
public ResponseEntity<ResponseModel> getProduct(@PathVariable String sku) {
return productService.getProduct(sku)
.map(MapperUtil::objectToJsonNode)
.map(it -> ResponseEntity.ok()
......@@ -30,6 +33,15 @@ public class ProductController {
.orElseGet(() -> ResponseEntity.notFound().build());
}
@PostMapping("/product")
public ResponseEntity<ResponseModel> getProductsByTags(@RequestBody List<String> tags) {
return ResponseEntity.ok()
.body(ResponseModel.builder()
.status("status")
.data(MapperUtil.objectToJsonNode(productService.getProductsByTags(tags)))
.build());
}
@GetMapping("/product")
public ResponseEntity<ResponseModel> getProducts() {
return ResponseEntity.ok()
......
......@@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
......@@ -15,7 +16,9 @@ import javax.persistence.*;
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "uuid2")
@Column(length = 36, nullable = false, updatable = false)
private String sku;
@Column
......
......@@ -4,10 +4,12 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Data
@Builder
......@@ -15,14 +17,18 @@ import java.util.List;
@AllArgsConstructor
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer iid;
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "uuid2")
@Column(length = 36, nullable = false, updatable = false)
private String sku;
@Column
private String name;
@Column
private Integer quantity;
@Builder.Default
@ElementCollection(fetch = FetchType.EAGER)
private List<String> tags = new ArrayList<>();
......
......@@ -6,6 +6,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
......@@ -29,8 +30,8 @@ public class TheOrder {
@Column
private String email;
@Column
private String checkoutDate;
@Column(columnDefinition = "TIMESTAMP")
private LocalDateTime checkoutDate;
@Column
private String state;
......
......@@ -49,9 +49,9 @@ public class JwtAuthenticationFilter extends UsernamePasswordAuthenticationFilte
.map(GrantedAuthority::getAuthority)
.collect(Collectors.toList()));
PrintWriter out = res.getWriter();
res.setContentType("application/json");
res.setContentType("application/jwt");
res.setCharacterEncoding("UTF-8");
out.print("{\"token\":\"" + config.getPrefix() + token + "\"}");
out.print(config.getPrefix() + token);
out.flush();
}
}
......@@ -10,7 +10,7 @@ import org.springframework.stereotype.Component;
@Component
public class JwtConfig {
private String url = "/login";
private String url = "/api/login";
private String header = "Authorization";
......
......@@ -6,7 +6,8 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
import java.util.UUID;
@Repository
public interface ProductRepo extends JpaRepository<Product, Integer> {
public interface ProductRepo extends JpaRepository<Product, String> {
}
......@@ -53,7 +53,7 @@ public class OrderServiceImpl implements OrderService {
.map(it -> orderRepo.save(TheOrder.builder()
.email(orderModel.getEmail())
.state("created")
.checkoutDate(LocalDateTime.now().toString())
.checkoutDate(LocalDateTime.now())
.build()))
.map(order -> {
orderModel.getLineItems().stream()
......
......@@ -7,6 +7,8 @@ import java.util.Optional;
public interface ProductService {
Optional<Product> create(Product product);
Optional<Product> getProduct(Integer id);
Optional<Product> getProduct(String sku);
List<Product> getProductsByTags(List<String> tags);
List<Product> getProducts();
}
......@@ -7,24 +7,33 @@ import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
@RequiredArgsConstructor
@Service
public class ProductServiceImpl implements ProductService{
public class ProductServiceImpl implements ProductService {
private final ProductRepo productRepo;
@Override
public Optional<Product> create(Product product){
public Optional<Product> create(Product product) {
return Optional.of(product).map(productRepo::save);
}
@Override
public Optional<Product> getProduct(Integer id) {
return productRepo.findById(id);
public Optional<Product> getProduct(String sku) {
return productRepo.findById(sku);
}
@Override
public List<Product> getProductsByTags(List<String> tags) {
return productRepo.findAll().stream()
.filter(it -> it.getTags().stream().anyMatch(tags::contains))
.collect(Collectors.toList());
}
@Override
public List<Product> getProducts() {
return productRepo.findAll();
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment