Skip to content

Commit fa98729

Browse files
committed
controller, service and repo added
1 parent bc59a69 commit fa98729

File tree

13 files changed

+225
-0
lines changed

13 files changed

+225
-0
lines changed

catalog-service/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@
122122
<version>5.5.6</version>
123123
<scope>test</scope>
124124
</dependency>
125+
<dependency>
126+
<groupId>org.projectlombok</groupId>
127+
<artifactId>lombok</artifactId>
128+
<version>1.18.42</version>
129+
<scope>provided</scope>
130+
</dependency>
125131

126132
</dependencies>
127133

@@ -131,11 +137,17 @@
131137
<groupId>org.apache.maven.plugins</groupId>
132138
<artifactId>maven-compiler-plugin</artifactId>
133139
<configuration>
140+
134141
<annotationProcessorPaths>
135142
<path>
136143
<groupId>org.springframework.boot</groupId>
137144
<artifactId>spring-boot-configuration-processor</artifactId>
138145
</path>
146+
<path>
147+
<groupId>org.projectlombok</groupId>
148+
<artifactId>lombok</artifactId>
149+
<version>1.18.42</version>
150+
</path>
139151
</annotationProcessorPaths>
140152
</configuration>
141153
</plugin>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.mayank.catalog_service;
2+
3+
import jakarta.validation.constraints.Min;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.boot.context.properties.bind.DefaultValue;
6+
7+
@ConfigurationProperties(prefix = "catalog")
8+
public record ApplicationProperties(
9+
@DefaultValue("10")
10+
@Min(1)
11+
int pageSize
12+
) {
13+
}

catalog-service/src/main/java/com/mayank/catalog_service/CatalogServiceApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
56

67
@SpringBootApplication
8+
@ConfigurationPropertiesScan
79
public class CatalogServiceApplication {
810

911
public static void main(String[] args) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.mayank.catalog_service.domain;
2+
3+
import java.util.List;
4+
5+
public record PagedResult<T>(
6+
List<T> data,
7+
long totalElements,
8+
int pageNumber,
9+
int totalPages,
10+
boolean isFirst,
11+
boolean isLast,
12+
boolean hasNext,
13+
boolean hasPrevious) {}
14+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.mayank.catalog_service.domain;
2+
3+
import java.math.BigDecimal;
4+
5+
public record Product(
6+
String code,
7+
String name,
8+
String description,
9+
String imageUrl,
10+
BigDecimal price) { }
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.mayank.catalog_service.domain;
2+
3+
import jakarta.persistence.*;
4+
import jakarta.validation.constraints.DecimalMin;
5+
import jakarta.validation.constraints.NotEmpty;
6+
import jakarta.validation.constraints.NotNull;
7+
import lombok.*;
8+
9+
import java.math.BigDecimal;
10+
11+
@Data
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
@Entity
15+
@Table(name = "products")
16+
public class ProductEntity
17+
{
18+
@Id
19+
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_id_generator")
20+
@SequenceGenerator(name = "product_id_generator", sequenceName = "product_id_seq")
21+
private Long id;
22+
23+
@Column(nullable = false, unique = true)
24+
@NotEmpty(message = "Product code is required")
25+
private String code;
26+
27+
@Column(nullable = false)
28+
@NotEmpty(message = "Product name is required")
29+
private String name;
30+
31+
private String description;
32+
private String imageUrl;
33+
34+
@Column(nullable = false)
35+
@NotNull(message = "Product price is required") @DecimalMin("0.1")
36+
private BigDecimal price;
37+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.mayank.catalog_service.domain;
2+
3+
public class ProductMapper
4+
{
5+
static Product toProduct(ProductEntity productEntity)
6+
{
7+
return new Product(
8+
productEntity.getCode(),
9+
productEntity.getName(),
10+
productEntity.getDescription(),
11+
productEntity.getImageUrl(),
12+
productEntity.getPrice()
13+
);
14+
}
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.mayank.catalog_service.domain;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
@Repository
7+
interface ProductRepository extends JpaRepository<ProductEntity, Long> {
8+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.mayank.catalog_service.domain;
2+
3+
import com.mayank.catalog_service.ApplicationProperties;
4+
import jakarta.transaction.Transactional;
5+
import org.springframework.data.domain.Page;
6+
import org.springframework.data.domain.PageRequest;
7+
import org.springframework.data.domain.Pageable;
8+
import org.springframework.data.domain.Sort;
9+
import org.springframework.stereotype.Service;
10+
11+
@Service
12+
@Transactional
13+
public class ProductService
14+
{
15+
private final ProductRepository productRepository;
16+
private final ApplicationProperties applicationProperties;
17+
18+
public ProductService(ProductRepository productRepository, ApplicationProperties applicationProperties) {
19+
this.productRepository = productRepository;
20+
this.applicationProperties = applicationProperties;
21+
}
22+
23+
public PagedResult<Product> getProducts(int pageNo)
24+
{
25+
Sort sort = Sort.by("name").ascending();
26+
pageNo = pageNo <= 1 ? 0 : pageNo - 1;
27+
Pageable pageable = PageRequest.of(pageNo, applicationProperties.pageSize(), sort);
28+
Page<Product> productsPage = productRepository.findAll(pageable).map(ProductMapper::toProduct);
29+
30+
return new PagedResult<>(
31+
productsPage.getContent(),
32+
productsPage.getTotalElements(),
33+
productsPage.getNumber() + 1,
34+
productsPage.getTotalPages(),
35+
productsPage.isFirst(),
36+
productsPage.isLast(),
37+
productsPage.hasNext(),
38+
productsPage.hasPrevious()
39+
);
40+
}
41+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.mayank.catalog_service.web.controllers;
2+
3+
import com.mayank.catalog_service.domain.PagedResult;
4+
import com.mayank.catalog_service.domain.Product;
5+
import com.mayank.catalog_service.domain.ProductService;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import java.util.List;
12+
13+
@RestController
14+
@RequestMapping("/api/products")
15+
public class ProductController
16+
{
17+
private final ProductService productService;
18+
19+
public ProductController(ProductService productService) {
20+
this.productService = productService;
21+
}
22+
23+
@GetMapping
24+
PagedResult<Product> getProducts(@RequestParam(name="page", defaultValue = "1")int pageNo)
25+
{
26+
return productService.getProducts(pageNo);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)