diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductController.java b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductController.java index 5e93b98..89ebf72 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductController.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductController.java @@ -5,7 +5,6 @@ import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; -import io.swagger.v3.oas.annotations.responses.ApiResponses; import java.util.List; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -22,50 +21,43 @@ public ProductController(ProductService productService) { this.productService = productService; } - @ApiResponses({ - @ApiResponse( - responseCode = "200", - description = "list all products", - content = { + @ApiResponse( + responseCode = "200", + description = "list all products", + content = { + @Content( + mediaType = "application/json", + array = + @ArraySchema( + schema = @Schema(implementation = ProductResponse.class))) + }) + @ApiResponse( + responseCode = "500", + description = "internal server error", + content = @Content( mediaType = "application/json", - array = - @ArraySchema( - schema = - @Schema( - implementation = - ProductResponse.class))) - }), - @ApiResponse( - responseCode = "500", - description = "internal server error", - content = - @Content( - mediaType = "application/json", - schema = @Schema(implementation = NotFoundException.class))) - }) + schema = @Schema(implementation = NotFoundException.class))) @GetMapping("/products") public List getProducts() { return productService.getAll(); } - @ApiResponses({ - @ApiResponse( - responseCode = "200", - description = "get product by sku", - content = { + @ApiResponse( + responseCode = "200", + description = "get product by sku", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ProductResponse.class)) + }) + @ApiResponse( + responseCode = "404", + description = "product not found", + content = @Content( mediaType = "application/json", - schema = @Schema(implementation = ProductResponse.class)) - }), - @ApiResponse( - responseCode = "404", - description = "product not found", - content = - @Content( - mediaType = "application/json", - schema = @Schema(implementation = NotFoundException.class))) - }) + schema = @Schema(implementation = NotFoundException.class))) @GetMapping("/products/{sku}") public ProductResponse getProductById(@PathVariable String sku) { return productService.getBySku(sku); diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductService.java b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductService.java index 6a5b144..bb56578 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductService.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductService.java @@ -15,7 +15,7 @@ public ProductService(ProductRepository productRepository) { } public List getAll() { - return productRepository.findAll().stream().map(p -> p.toResponse()).toList(); + return productRepository.findAll().stream().map(Product::toResponse).toList(); } public ProductResponse getBySku(String sku) { diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/promotion/PromotionController.java b/kbazaar/src/main/java/com/kampus/kbazaar/promotion/PromotionController.java index 923b014..2a836c1 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/promotion/PromotionController.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/promotion/PromotionController.java @@ -5,9 +5,9 @@ import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; -import io.swagger.v3.oas.annotations.responses.ApiResponses; import java.util.List; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -20,53 +20,46 @@ public PromotionController(PromotionService promotionService) { this.promotionService = promotionService; } - @ApiResponses({ - @ApiResponse( - responseCode = "200", - description = "list all promotions", - content = { + @ApiResponse( + responseCode = "200", + description = "list all promotions", + content = { + @Content( + mediaType = "application/json", + array = + @ArraySchema( + schema = @Schema(implementation = PromotionResponse.class))) + }) + @ApiResponse( + responseCode = "500", + description = "internal server error", + content = @Content( mediaType = "application/json", - array = - @ArraySchema( - schema = - @Schema( - implementation = - PromotionResponse.class))) - }), - @ApiResponse( - responseCode = "500", - description = "internal server error", - content = - @Content( - mediaType = "application/json", - schema = @Schema(implementation = NotFoundException.class))) - }) + schema = @Schema(implementation = NotFoundException.class))) @GetMapping("/promotions") public List getAllPromotions() { return promotionService.getAll(); } - @ApiResponses({ - @ApiResponse( - responseCode = "200", - description = "get promotion by code", - content = { - @Content( - mediaType = "application/json", - schema = @Schema(implementation = PromotionResponse.class)) - }), - @ApiResponse( - responseCode = "404", - description = "promotion not found", - content = { - @Content( - mediaType = "application/json", - schema = @Schema(implementation = NotFoundException.class)) - }) - }) + @ApiResponse( + responseCode = "200", + description = "get promotion by code", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = PromotionResponse.class)) + }) + @ApiResponse( + responseCode = "404", + description = "promotion not found", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = NotFoundException.class)) + }) @GetMapping("/promotions/{code}") - public PromotionResponse getPromotionByCode(String code) { + public PromotionResponse getPromotionByCode(@PathVariable String code) { return promotionService.getPromotionByCode(code); } } diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/promotion/PromotionService.java b/kbazaar/src/main/java/com/kampus/kbazaar/promotion/PromotionService.java index ece359a..ddfa668 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/promotion/PromotionService.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/promotion/PromotionService.java @@ -13,13 +13,13 @@ public PromotionService(PromotionRepository promotionRepository) { } public List getAll() { - return promotionRepository.findAll().stream().map(p -> p.toResponse()).toList(); + return promotionRepository.findAll().stream().map(Promotion::toResponse).toList(); } public PromotionResponse getPromotionByCode(String code) { return promotionRepository .findByCode(code) - .map(p -> p.toResponse()) + .map(Promotion::toResponse) .orElseThrow(() -> new NotFoundException("Promotion not found")); } } diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/shopper/ShopperController.java b/kbazaar/src/main/java/com/kampus/kbazaar/shopper/ShopperController.java index c2bb317..688bd3f 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/shopper/ShopperController.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/shopper/ShopperController.java @@ -5,7 +5,6 @@ import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; -import io.swagger.v3.oas.annotations.responses.ApiResponses; import java.util.List; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -22,52 +21,45 @@ public ShopperController(ShopperService shopperService) { this.shopperService = shopperService; } - @ApiResponses({ - @ApiResponse( - responseCode = "200", - description = "list all shoppers", - content = { + @ApiResponse( + responseCode = "200", + description = "list all shoppers", + content = { + @Content( + mediaType = "application/json", + array = + @ArraySchema( + schema = @Schema(implementation = ShopperResponse.class))) + }) + @ApiResponse( + responseCode = "500", + description = "internal server error", + content = @Content( mediaType = "application/json", - array = - @ArraySchema( - schema = - @Schema( - implementation = - ShopperResponse.class))) - }), - @ApiResponse( - responseCode = "500", - description = "internal server error", - content = - @Content( - mediaType = "application/json", - schema = @Schema(implementation = NotFoundException.class))) - }) + schema = @Schema(implementation = NotFoundException.class))) @GetMapping("/shopper") public List getAllUsers() { return shopperService.getAll(); } - @ApiResponses({ - @ApiResponse( - responseCode = "200", - description = "get shopper by id", - content = { + @ApiResponse( + responseCode = "200", + description = "get shopper by id", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ShopperResponse.class)) + }) + @ApiResponse( + responseCode = "500", + description = "internal server error", + content = @Content( mediaType = "application/json", - schema = @Schema(implementation = ShopperResponse.class)) - }), - @ApiResponse( - responseCode = "500", - description = "internal server error", - content = - @Content( - mediaType = "application/json", - schema = @Schema(implementation = NotFoundException.class))) - }) - @GetMapping("/shopper/{id}") - public ShopperResponse getUserById(@PathVariable String id) { - return shopperService.getById(id); + schema = @Schema(implementation = NotFoundException.class))) + @GetMapping("/shopper/{username}") + public ShopperResponse getUserByUsername(@PathVariable String username) { + return shopperService.getByUsername(username); } } diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/shopper/ShopperRepository.java b/kbazaar/src/main/java/com/kampus/kbazaar/shopper/ShopperRepository.java index e3327d0..c4f12d1 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/shopper/ShopperRepository.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/shopper/ShopperRepository.java @@ -3,5 +3,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository -public interface ShopperRepository extends JpaRepository {} +public interface ShopperRepository extends JpaRepository { + Optional findByUsername(String username); +} diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/shopper/ShopperService.java b/kbazaar/src/main/java/com/kampus/kbazaar/shopper/ShopperService.java index 322e9c7..0dad820 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/shopper/ShopperService.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/shopper/ShopperService.java @@ -14,13 +14,20 @@ public ShopperService(ShopperRepository shopperRepository) { } public List getAll() { - return shopperRepository.findAll().stream().map(s -> s.toResponse()).toList(); + return shopperRepository.findAll().stream().map(Shopper::toResponse).toList(); } public ShopperResponse getById(String id) { return shopperRepository .findById(Integer.parseInt(id)) - .map(s -> s.toResponse()) + .map(Shopper::toResponse) + .orElseThrow(() -> new NotFoundException("Shopper not found")); + } + + public ShopperResponse getByUsername(String username) { + return shopperRepository + .findByUsername(username) + .map(Shopper::toResponse) .orElseThrow(() -> new NotFoundException("Shopper not found")); } } diff --git a/kbazaar/src/test/java/com/kampus/kbazaar/shopper/ShopperServiceTest.java b/kbazaar/src/test/java/com/kampus/kbazaar/shopper/ShopperServiceTest.java index aed29ce..51cc3e0 100644 --- a/kbazaar/src/test/java/com/kampus/kbazaar/shopper/ShopperServiceTest.java +++ b/kbazaar/src/test/java/com/kampus/kbazaar/shopper/ShopperServiceTest.java @@ -66,4 +66,30 @@ void testGetById_ShouldThrowNotFoundException() { // Assertions assertThrows(NotFoundException.class, () -> shopperService.getById("1")); } + + @Test + void testGetByUsername_ShouldReturnShopper() { + // Mock data + Shopper shopper = new Shopper(1L, "DataGuru", "dataguru@example.com"); + + // Mock repository method + when(shopperRepository.findByUsername("DataGuru")).thenReturn(Optional.of(shopper)); + + // Call service method + ShopperResponse result = shopperService.getByUsername("DataGuru"); + + // Assertions + assertEquals("DataGuru", result.username()); + } + + @Test + void testGetByUsername_ShouldThrowNotFoundException() { + // Mock repository method returning empty optional + when(shopperRepository.findByUsername("DataGuru")).thenReturn(Optional.empty()); + + // Assertions + assertThrows(NotFoundException.class, () -> shopperService.getByUsername("DataGuru")); + } + + }