Skip to content

Commit 30cd472

Browse files
product-api: initial api impl
1 parent 40e6c72 commit 30cd472

File tree

7 files changed

+232
-1
lines changed

7 files changed

+232
-1
lines changed

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@
4040
<artifactId>spring-boot-starter-test</artifactId>
4141
<scope>test</scope>
4242
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-web</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.postgresql</groupId>
49+
<artifactId>postgresql</artifactId>
50+
<scope>runtime</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-data-jpa</artifactId>
55+
</dependency>
4356
</dependencies>
4457

4558
<build>

src/main/java/com/amigoscode/Main.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.amigoscode;
22

3+
import com.amigoscode.product.Product;
4+
import com.amigoscode.product.ProductRepository;
5+
import org.springframework.boot.CommandLineRunner;
36
import org.springframework.boot.SpringApplication;
47
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.context.annotation.Bean;
9+
10+
import java.math.BigDecimal;
11+
import java.util.UUID;
512

613
@SpringBootApplication
714
public class Main {
@@ -10,4 +17,20 @@ public static void main(String[] args) {
1017
SpringApplication.run(Main.class, args);
1118
}
1219

20+
@Bean
21+
public CommandLineRunner commandLineRunner(
22+
ProductRepository productRepository) {
23+
return args -> {
24+
Product product = new Product();
25+
product.setName("Macbook Pro");
26+
product.setDescription("Macbook Pro M4");
27+
product.setPrice(new BigDecimal(3000));
28+
product.setId(UUID.fromString(
29+
"d95062e6-9f0b-4224-bc9d-d0723949848f")
30+
);
31+
product.setStockLevel(100);
32+
productRepository.save(product);
33+
};
34+
}
35+
1336
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.amigoscode.product;
2+
3+
import jakarta.persistence.*;
4+
5+
import java.math.BigDecimal;
6+
import java.time.Instant;
7+
import java.util.Objects;
8+
import java.util.UUID;
9+
10+
@Entity
11+
@Table(
12+
name = "product"
13+
)
14+
public class Product {
15+
16+
@Id
17+
private UUID id;
18+
19+
@Column(nullable = false, length = 50)
20+
private String name;
21+
22+
private String description;
23+
24+
@Column(nullable = false, precision = 10, scale = 2)
25+
private BigDecimal price;
26+
27+
@Column(length = 200)
28+
private String imageUrl;
29+
30+
@Column(nullable = false)
31+
private Integer stockLevel;
32+
33+
@Column(nullable = false, updatable = false)
34+
private Instant createdAt;
35+
36+
private Instant updatedAt;
37+
38+
private Instant deletedAt;
39+
40+
@PrePersist
41+
public void prePersist(){
42+
if(this.id == null) {
43+
this.id = UUID.randomUUID();
44+
}
45+
this.createdAt = Instant.now();
46+
this.updatedAt = Instant.now();
47+
}
48+
49+
@PreUpdate
50+
public void preUpdate(){
51+
this.updatedAt = Instant.now();
52+
}
53+
54+
public UUID getId() {
55+
return id;
56+
}
57+
58+
public void setId(UUID id) {
59+
this.id = id;
60+
}
61+
62+
public String getName() {
63+
return name;
64+
}
65+
66+
public void setName(String name) {
67+
this.name = name;
68+
}
69+
70+
public String getDescription() {
71+
return description;
72+
}
73+
74+
public void setDescription(String description) {
75+
this.description = description;
76+
}
77+
78+
public BigDecimal getPrice() {
79+
return price;
80+
}
81+
82+
public void setPrice(BigDecimal price) {
83+
this.price = price;
84+
}
85+
86+
public String getImageUrl() {
87+
return imageUrl;
88+
}
89+
90+
public void setImageUrl(String imageUrl) {
91+
this.imageUrl = imageUrl;
92+
}
93+
94+
public int getStockLevel() {
95+
return stockLevel;
96+
}
97+
98+
public void setStockLevel(int stockLevel) {
99+
this.stockLevel = stockLevel;
100+
}
101+
102+
public Instant getCreatedAt() {
103+
return createdAt;
104+
}
105+
106+
public void setCreatedAt(Instant createdAt) {
107+
this.createdAt = createdAt;
108+
}
109+
110+
public Instant getUpdatedAt() {
111+
return updatedAt;
112+
}
113+
114+
public void setUpdatedAt(Instant updatedAt) {
115+
this.updatedAt = updatedAt;
116+
}
117+
118+
public Instant getDeletedAt() {
119+
return deletedAt;
120+
}
121+
122+
public void setDeletedAt(Instant deletedAt) {
123+
this.deletedAt = deletedAt;
124+
}
125+
126+
@Override
127+
public boolean equals(Object o) {
128+
if (o == null || getClass() != o.getClass()) return false;
129+
Product product = (Product) o;
130+
return stockLevel == product.stockLevel && Objects.equals(id, product.id) && Objects.equals(name, product.name) && Objects.equals(description, product.description) && Objects.equals(price, product.price) && Objects.equals(imageUrl, product.imageUrl) && Objects.equals(createdAt, product.createdAt) && Objects.equals(updatedAt, product.updatedAt) && Objects.equals(deletedAt, product.deletedAt);
131+
}
132+
133+
@Override
134+
public int hashCode() {
135+
return Objects.hash(id, name, description, price, imageUrl, stockLevel, createdAt, updatedAt, deletedAt);
136+
}
137+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.amigoscode.product;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
import java.util.List;
8+
9+
@RestController
10+
@RequestMapping("/api/v1/products")
11+
public class ProductController {
12+
13+
private final ProductService productService;
14+
15+
public ProductController(ProductService productService) {
16+
this.productService = productService;
17+
}
18+
19+
@GetMapping
20+
public List<Product> getAllProducts() {
21+
return productService.getAllProducts();
22+
}
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.amigoscode.product;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
import java.util.UUID;
6+
7+
public interface ProductRepository
8+
extends JpaRepository<Product, UUID> {
9+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.amigoscode.product;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
import java.util.List;
6+
7+
@Service
8+
public class ProductService {
9+
private final ProductRepository productRepository;
10+
11+
public ProductService(ProductRepository productRepository) {
12+
this.productRepository = productRepository;
13+
}
14+
15+
public List<Product> getAllProducts() {
16+
return productRepository.findAll();
17+
}
18+
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
spring.application.name=java-springboot-full-stack
1+
spring.application.name=jfs
2+
spring.datasource.url=jdbc:postgresql://localhost:5432/jfs
3+
spring.datasource.username=amigoscode
4+
spring.datasource.password=
5+
spring.datasource.driver-class-name=org.postgresql.Driver
6+
7+
spring.jpa.hibernate.ddl-auto=create-drop
8+
spring.jpa.show-sql=true
9+
spring.jpa.properties.hibernate.format_sql=true

0 commit comments

Comments
 (0)