-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #650 from hngprojects/feature/dashboard
feat: implement dashboard for products
- Loading branch information
Showing
11 changed files
with
237 additions
and
12 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/hng_java_boilerplate/dashboard/controller/DashboardController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package hng_java_boilerplate.dashboard.controller; | ||
|
||
import hng_java_boilerplate.dashboard.service.DashboardService; | ||
import hng_java_boilerplate.product.dto.GetProductsDTO; | ||
import hng_java_boilerplate.product.dto.ProductCountDto; | ||
import hng_java_boilerplate.product.dto.ProductDTO; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@PreAuthorize("hasRole('SUPER_ADMIN')") | ||
@RequestMapping("/api/v1/dashboards") | ||
@Tag(name = "dashboard", description = "admin dashboard") | ||
public class DashboardController { | ||
private final DashboardService dashboardService; | ||
|
||
@GetMapping("/products") | ||
@Tag(name = "Products", description = "get all the products in the database") | ||
public ResponseEntity<GetProductsDTO> getProducts() { | ||
return ResponseEntity.ok(dashboardService.getAllProducts()); | ||
} | ||
|
||
@GetMapping("/products/count") | ||
@Tag(name = "Product count", description = "get the total counts of products") | ||
public ResponseEntity<ProductCountDto> getProductCount() { | ||
return ResponseEntity.ok(dashboardService.getProductCount()); | ||
} | ||
|
||
@GetMapping("/products/{productId}") | ||
@Tag(name = "product", description = "get a product by the product id") | ||
public ResponseEntity<ProductDTO> getProductById(@PathVariable String productId) { | ||
return ResponseEntity.ok(dashboardService.getProductById(productId)); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/hng_java_boilerplate/dashboard/service/DashboardService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package hng_java_boilerplate.dashboard.service; | ||
|
||
import hng_java_boilerplate.product.dto.GetProductsDTO; | ||
import hng_java_boilerplate.product.dto.ProductCountDto; | ||
import hng_java_boilerplate.product.dto.ProductDTO; | ||
import hng_java_boilerplate.product.service.ProductService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class DashboardService { | ||
private final ProductService productService; | ||
|
||
public ProductCountDto getProductCount() { | ||
return productService.getProductsCount(); | ||
} | ||
|
||
public GetProductsDTO getAllProducts() { | ||
return productService.getProducts(); | ||
} | ||
|
||
public ProductDTO getProductById(String productId) { | ||
return productService.getProductById(productId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/hng_java_boilerplate/product/dto/GetProductsDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package hng_java_boilerplate.product.dto; | ||
|
||
import lombok.Builder; | ||
|
||
import java.util.List; | ||
|
||
@Builder | ||
public record GetProductsDTO(int status_code, String status, List<ProductDTO> data) { | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/hng_java_boilerplate/product/dto/ProductCountDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package hng_java_boilerplate.product.dto; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record ProductCountDto(int status_code, String status, CountData data) { | ||
|
||
@Builder | ||
public record CountData(int count) {} | ||
} |
7 changes: 4 additions & 3 deletions
7
src/main/java/hng_java_boilerplate/product/dto/ProductDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
package hng_java_boilerplate.product.dto; | ||
|
||
import jakarta.persistence.Column; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Data | ||
public class ProductDTO { | ||
private String id; | ||
private String name; | ||
private String description; | ||
private String category; | ||
private double price; | ||
private String imageUrl; | ||
private String image_url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/test/java/hng_java_boilerplate/product/service/ProductServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package hng_java_boilerplate.product.service; | ||
|
||
import hng_java_boilerplate.product.dto.GetProductsDTO; | ||
import hng_java_boilerplate.product.dto.ProductCountDto; | ||
import hng_java_boilerplate.product.dto.ProductDTO; | ||
import hng_java_boilerplate.product.entity.Product; | ||
import hng_java_boilerplate.product.repository.ProductRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ProductServiceImplTest { | ||
@Mock | ||
private ProductRepository productRepository; | ||
@InjectMocks | ||
private ProductServiceImpl underTest; | ||
|
||
private Product product; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
product = new Product(); | ||
product.setId("prod-id"); | ||
product.setName("product"); | ||
product.setDescription("prod-desc"); | ||
product.setCategory("prod-cat"); | ||
} | ||
|
||
|
||
@Test | ||
void shouldGetProductsCount() { | ||
when(productRepository.findAll()).thenReturn(List.of(product)); | ||
|
||
ProductCountDto response = underTest.getProductsCount(); | ||
|
||
assertThat(response.status_code()).isEqualTo(200); | ||
assertThat(response.status()).isEqualTo("success"); | ||
assertThat(response.data()).hasFieldOrPropertyWithValue("count", 1); | ||
|
||
verify(productRepository).findAll(); | ||
} | ||
|
||
@Test | ||
void getProducts() { | ||
when(productRepository.findAll()).thenReturn(List.of(product)); | ||
GetProductsDTO products = underTest.getProducts(); | ||
|
||
assertThat(products.status_code()).isEqualTo(200); | ||
assertThat(products.status()).isEqualTo("success"); | ||
assertThat(products.data()).hasSize(1); | ||
assertThat(products.data().get(0)).hasFieldOrPropertyWithValue("id", product.getId()); | ||
assertThat(products.data().get(0)).hasFieldOrPropertyWithValue("name", product.getName()); | ||
|
||
verify(productRepository).findAll(); | ||
} | ||
|
||
@Test | ||
void getProductById() { | ||
when(productRepository.findById("prod-id")).thenReturn(Optional.of(product)); | ||
|
||
ProductDTO response = underTest.getProductById("prod-id"); | ||
|
||
assertThat(response.getId()).isEqualTo(product.getId()); | ||
assertThat(response.getName()).isEqualTo(product.getName()); | ||
assertThat(response.getDescription()).isEqualTo(product.getDescription()); | ||
|
||
verify(productRepository).findById("prod-id"); | ||
} | ||
} |