Skip to content

Commit

Permalink
Merge pull request #37 from NashTech-Labs/feature/merge_cors_changes
Browse files Browse the repository at this point in the history
merged cors changes and update get api
  • Loading branch information
abidknashtech authored Oct 6, 2023
2 parents d7433ec + 2574f24 commit 6b13069
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
6 changes: 6 additions & 0 deletions admin-service/cloud/gcpcloudfunction/googlecloud.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="SonarLintModuleSettings">
<option name="uniqueId" value="2a044d14-3541-46be-a4ff-cb46c068615e" />
</component>
</module>
4 changes: 1 addition & 3 deletions cart-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.nashtech.car.cart.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan
public class WebConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:4200/") // Allow requests from Angular app
.allowedMethods("GET", "POST", "PUT", "DELETE")
.maxAge(3600);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cart")
@Slf4j
Expand All @@ -35,8 +37,8 @@ public ResponseEntity<CartItem> removeFromCart(@RequestParam String productId, @
}

@GetMapping("/get")
public ResponseEntity<CartItem> getFromCart(@RequestParam String productId, @RequestParam String userId) {
return new ResponseEntity<>(cartService.getFromCart(productId,userId),HttpStatus.OK);
public ResponseEntity<List<CartItem>> getFromCart(@RequestParam String userId) {
return new ResponseEntity<>(cartService.getFromCart(userId),HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CartItemRepository extends JpaRepository<CartItem, Long> {
CartItem findByProductIdAndUserId(String productId,String userId);
List<CartItem> findByUserId(String userId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Objects;

@Service
Expand Down Expand Up @@ -76,8 +77,8 @@ public CartItem removeFromCart(String productId, int quantity, String userId) {
}

@Transactional
public CartItem getFromCart(String productId, String userId) {
CartItem cartItem = cartItemRepository.findByProductIdAndUserId(productId, userId);
public List<CartItem> getFromCart(String userId) {
List<CartItem> cartItem = cartItemRepository.findByUserId(userId);
if (cartItem == null) {
throw new IllegalStateException("Product not found from the cart");
}
Expand Down

0 comments on commit 6b13069

Please sign in to comment.