-
Notifications
You must be signed in to change notification settings - Fork 0
add-shopping-cart-a #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package com.example.demo1.controller.api; | ||
|
|
||
| import com.example.demo1.model.dto.ShoppingCartDto; | ||
| import com.example.demo1.model.dto.ShoppingCartReturnDto; | ||
| import com.example.demo1.model.dto.ShoppingCartUpdateDto; | ||
| import com.example.demo1.service.ShoppingCartService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/shopping-cart") | ||
| @RequiredArgsConstructor | ||
| public class ShoppingCartApiController { | ||
| private final ShoppingCartService shoppingCartService; | ||
| //create ShoppingCart | ||
| @PostMapping | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public void createShoppingCart(@RequestBody ShoppingCartDto shoppingCartDto){ | ||
| shoppingCartService.create(shoppingCartDto); | ||
| } | ||
|
|
||
| //read ShoppingCart | ||
| @GetMapping | ||
| public List<ShoppingCartReturnDto> getShoppingCart(){ | ||
| return shoppingCartService.findAll(); | ||
| } | ||
|
|
||
| @GetMapping("/description/{description}") | ||
| public List<ShoppingCartReturnDto> getShoppingCartByDescription | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shopping carts are a bit special, you kinda want to set a cookie on the users' browser to identify his shopping cart id so that every time he comes to your website he has the products already in the cart. Maybe you want to implement something like this
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also don't take long strings as path variables it use request param better or request body. it will make your urls like this: |
||
| (@PathVariable("description") String description){ | ||
| return shoppingCartService.findByDescriptionProduct(description); | ||
| } | ||
|
|
||
| @GetMapping("/native-description/{description}") | ||
| public List<ShoppingCartReturnDto> getShoppingCartByNativeDescription | ||
| (@PathVariable("description") String description){ | ||
| return shoppingCartService.findByDescriptionProductNative(description); | ||
| } | ||
|
|
||
| @GetMapping("/hybrid-description/{description}") | ||
| public List<ShoppingCartReturnDto> getShoppingCartByHybridDescription | ||
| (@PathVariable String description){ | ||
| return shoppingCartService.findByDescriptionProductHybrid(description); | ||
| } | ||
|
|
||
| @GetMapping("/unit-price/{unitPrice}") | ||
| public List<ShoppingCartReturnDto> getShoppingCartByUnitPrice | ||
| (@PathVariable("unitPrice") Double unitPrice){ | ||
| return shoppingCartService.findByUnitPrice(unitPrice); | ||
| } | ||
|
|
||
| @GetMapping("/delivery-date/{dateOfDelivery}") | ||
| public List<ShoppingCartReturnDto> getShoppingCartByDateOfDelivery | ||
| (@PathVariable("dateOfDelivery") LocalDate dateOfDelivery){ | ||
| return shoppingCartService.findByDeliveryDate(dateOfDelivery); | ||
| } | ||
|
|
||
| //update ShoppingCart | ||
| @PutMapping | ||
| @ResponseStatus(HttpStatus.NO_CONTENT) | ||
| public void updateShoppingCart(@RequestBody ShoppingCartUpdateDto shoppingCartUpdateDto){ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add validation |
||
| shoppingCartService.update(shoppingCartUpdateDto); | ||
| } | ||
|
|
||
| //delete ShoppingCart | ||
| @DeleteMapping("/{id}") | ||
| public void deleteShoppingCart(@PathVariable Integer id){ | ||
| shoppingCartService.deleteShoppingCart(id); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,24 @@ | ||
| package com.example.demo1.model; | ||
|
|
||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
|
|
||
| import java.time.LocalDate; | ||
| //@Data | ||
| //@Builder | ||
| @Entity | ||
| @Table(name="shopping_cart") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use plural |
||
| @Data | ||
| @Builder | ||
| public class ShoppingCart { | ||
| private Long id; | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Id | ||
| private Integer id; | ||
| private Integer productId; | ||
| private String descriptionProduct; | ||
| private Double unitPrice; | ||
| private boolean availability; | ||
| private LocalDate dateOfDelivery; | ||
| private Integer customerId; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.example.demo1.model.dto; | ||
|
|
||
| import com.example.demo1.model.ShoppingCart; | ||
| import lombok.Data; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| @Data | ||
| public class ShoppingCartDto { | ||
| private Integer productId; | ||
| private String descriptionProduct; | ||
| private Double unitPrice; | ||
| private boolean availability; | ||
| private LocalDate dateOfDelivery; | ||
| private Integer customerId; | ||
|
|
||
| public ShoppingCart toObject(ShoppingCartDto shoppingCartDto){ | ||
| ShoppingCart shoppingCart = new ShoppingCart(); | ||
| shoppingCart.setProductId(shoppingCartDto.getProductId()); | ||
| shoppingCart.setDescriptionProduct(shoppingCartDto.getDescriptionProduct()); | ||
| shoppingCart.setUnitPrice(shoppingCartDto.getUnitPrice()); | ||
| shoppingCart.setDateOfDelivery(shoppingCartDto.getDateOfDelivery()); | ||
| shoppingCart.setAvailability(shoppingCartDto.isAvailability()); | ||
| shoppingCart.setCustomerId(shoppingCartDto.getCustomerId()); | ||
| return shoppingCart; | ||
| } | ||
| public ShoppingCartDto toDto(ShoppingCart shoppingCart){ | ||
| ShoppingCartDto shoppingCartDto = new ShoppingCartDto(); | ||
| shoppingCartDto.setProductId(shoppingCart.getProductId()); | ||
| shoppingCartDto.setDescriptionProduct(shoppingCart.getDescriptionProduct()); | ||
| shoppingCartDto.setUnitPrice(shoppingCart.getUnitPrice()); | ||
| shoppingCartDto.setDateOfDelivery(shoppingCart.getDateOfDelivery()); | ||
| shoppingCartDto.setAvailability(shoppingCart.isAvailability()); | ||
| shoppingCartDto.setCustomerId(shoppingCart.getCustomerId()); | ||
| return shoppingCartDto; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.example.demo1.model.dto; | ||
|
|
||
| import com.example.demo1.model.ShoppingCart; | ||
| import lombok.Data; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| @Data | ||
| public class ShoppingCartReturnDto { | ||
| private Integer Id; | ||
| private String descriptionProduct; | ||
| private Double unitPrice; | ||
| private boolean availability; | ||
| private LocalDate dateOfDelivery; | ||
|
|
||
|
|
||
| public ShoppingCart toObject(ShoppingCartReturnDto shoppingCartDto){ | ||
| ShoppingCart shoppingCart = new ShoppingCart(); | ||
| shoppingCart.setId(shoppingCartDto.getId()); | ||
| shoppingCart.setDescriptionProduct(shoppingCartDto.getDescriptionProduct()); | ||
| shoppingCart.setUnitPrice(shoppingCartDto.getUnitPrice()); | ||
| shoppingCart.setDateOfDelivery(shoppingCartDto.getDateOfDelivery()); | ||
| shoppingCart.setAvailability(shoppingCartDto.isAvailability()); | ||
| return shoppingCart; | ||
| } | ||
| public static ShoppingCartReturnDto toDto(ShoppingCart shoppingCart){ | ||
| ShoppingCartReturnDto shoppingCartDto = new ShoppingCartReturnDto(); | ||
| shoppingCartDto.setId(shoppingCart.getId()); | ||
| shoppingCartDto.setDescriptionProduct(shoppingCart.getDescriptionProduct()); | ||
| shoppingCartDto.setUnitPrice(shoppingCart.getUnitPrice()); | ||
| shoppingCartDto.setDateOfDelivery(shoppingCart.getDateOfDelivery()); | ||
| shoppingCartDto.setAvailability(shoppingCart.isAvailability()); | ||
| return shoppingCartDto; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.example.demo1.model.dto; | ||
|
|
||
| import com.example.demo1.model.ShoppingCart; | ||
| import lombok.Data; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| @Data | ||
| public class ShoppingCartUpdateDto { | ||
| private Integer Id; | ||
| private Integer productId; | ||
| private String descriptionProduct; | ||
| private Double unitPrice; | ||
| private boolean availability; | ||
| private LocalDate dateOfDelivery; | ||
| private Integer customerId; | ||
|
|
||
|
|
||
| public ShoppingCart toObject(ShoppingCartUpdateDto shoppingCartUpdateDto){ | ||
| ShoppingCart shoppingCart = new ShoppingCart(); | ||
| shoppingCart.setId(shoppingCartUpdateDto.getId()); | ||
| shoppingCart.setDescriptionProduct(shoppingCartUpdateDto.getDescriptionProduct()); | ||
| shoppingCart.setUnitPrice(shoppingCartUpdateDto.getUnitPrice()); | ||
| shoppingCart.setDateOfDelivery(shoppingCartUpdateDto.getDateOfDelivery()); | ||
| shoppingCart.setAvailability(shoppingCartUpdateDto.isAvailability()); | ||
| shoppingCart.setCustomerId(shoppingCartUpdateDto.getCustomerId()); | ||
| shoppingCart.setProductId(shoppingCartUpdateDto.getProductId()); | ||
| return shoppingCart; | ||
| } | ||
| public static ShoppingCartUpdateDto toDto(ShoppingCart shoppingCart){ | ||
| ShoppingCartUpdateDto shoppingCartDto = new ShoppingCartUpdateDto(); | ||
| shoppingCartDto.setId(shoppingCart.getId()); | ||
| shoppingCartDto.setDescriptionProduct(shoppingCart.getDescriptionProduct()); | ||
| shoppingCartDto.setUnitPrice(shoppingCart.getUnitPrice()); | ||
| shoppingCartDto.setDateOfDelivery(shoppingCart.getDateOfDelivery()); | ||
| shoppingCartDto.setAvailability(shoppingCart.isAvailability()); | ||
| shoppingCartDto.setId(shoppingCart.getId()); | ||
| shoppingCartDto.setCustomerId(shoppingCart.getCustomerId()); | ||
| shoppingCartDto.setProductId(shoppingCart.getProductId()); | ||
| return shoppingCartDto; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.example.demo1.repository; | ||
|
|
||
| import com.example.demo1.model.ShoppingCart; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
|
|
||
| @Repository | ||
| public interface ShoppingCartRepository extends JpaRepository<ShoppingCart, Integer> { | ||
|
|
||
| List<ShoppingCart> findByDescriptionProduct(String description); | ||
|
|
||
| @Query(value="SELECT * FROM public.shopping_cart s WHERE s.description_product = :description", | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice mix of native and jpa queries, good job |
||
| nativeQuery = true) | ||
| List<ShoppingCart> findByDescriptionProductNative(String description); | ||
|
|
||
| @Query(value="FROM ShoppingCart s WHERE s.descriptionProduct = :description", | ||
| nativeQuery = false) | ||
| List<ShoppingCart> findByDescriptionProductHybrid(String description); | ||
|
|
||
| List<ShoppingCart> findByUnitPrice(Double unitPrice); | ||
|
|
||
| @Query(value="SELECT * FROM public.shopping_cart s WHERE s.date_of_delivery >= :deliveryDate", | ||
| nativeQuery = true) | ||
| List<ShoppingCart> findByDeliveryDate(LocalDate deliveryDate); | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package com.example.demo1.service; | ||
|
|
||
| import com.example.demo1.model.ShoppingCart; | ||
| import com.example.demo1.model.dto.ShoppingCartDto; | ||
| import com.example.demo1.model.dto.ShoppingCartReturnDto; | ||
| import com.example.demo1.model.dto.ShoppingCartUpdateDto; | ||
| import com.example.demo1.repository.ShoppingCartRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class ShoppingCartService { | ||
| private final ShoppingCartRepository shoppingCartRepository; | ||
| public void create(ShoppingCartDto shoppingCartDto){ | ||
| if (!shoppingCartDto.isAvailability()) | ||
| { | ||
| throw new RuntimeException("Delivery is not available"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. define your own exception eg BusinessException and maybe an @ExceptionHandler too |
||
| } | ||
| if (shoppingCartDto.getDateOfDelivery().isBefore(LocalDate.now())) | ||
| { | ||
| throw new RuntimeException("Delivery cannot be made on date specified"); | ||
| } | ||
| if (shoppingCartDto.getDescriptionProduct().length()<1) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use the the @Valid on the dto to check for min length and such |
||
| { | ||
| throw new RuntimeException("Description product unspecified"); | ||
| } | ||
| if (shoppingCartDto.getProductId()==null) | ||
| { | ||
| throw new RuntimeException("Product unspecified"); | ||
| } | ||
| if (shoppingCartDto.getCustomerId()==null) | ||
| { | ||
| throw new RuntimeException("Customer unspecified"); | ||
| } | ||
|
|
||
| shoppingCartRepository.save(shoppingCartDto.toObject(shoppingCartDto)); | ||
| } | ||
|
|
||
| public List<ShoppingCartReturnDto> findAll(){ | ||
| return shoppingCartRepository | ||
| .findAll() | ||
| .stream() | ||
| .map(ShoppingCartReturnDto::toDto) | ||
| .toList(); | ||
| } | ||
|
|
||
| List<ShoppingCartReturnDto> findAllOldJava() { | ||
| List<ShoppingCart> shoppingCarts = shoppingCartRepository.findAll(); | ||
| List<ShoppingCartReturnDto> shoppingCartReturnDto = new ArrayList<>(); | ||
| for(ShoppingCart shoppingCart: shoppingCarts){ | ||
| ShoppingCartReturnDto shoppingCartReturnDto1 = ShoppingCartReturnDto.toDto(shoppingCart); | ||
| shoppingCartReturnDto.add(shoppingCartReturnDto1); | ||
| } | ||
| return shoppingCartReturnDto; | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| public ShoppingCart findById(Integer id){ | ||
| return shoppingCartRepository.findById(id) | ||
| .orElseThrow(()->new RuntimeException("Delivery not found "+id)); | ||
| } | ||
|
|
||
| public void update(ShoppingCartUpdateDto shoppingCartUpdateDto){ | ||
| ShoppingCart shoppingCart = findById(shoppingCartUpdateDto.getId()); | ||
| shoppingCart.setDateOfDelivery(shoppingCartUpdateDto.getDateOfDelivery()); | ||
| shoppingCart.setDescriptionProduct(shoppingCartUpdateDto.getDescriptionProduct()); | ||
| shoppingCart.setAvailability(shoppingCartUpdateDto.isAvailability()); | ||
| shoppingCart.setCustomerId(shoppingCartUpdateDto.getCustomerId()); | ||
| shoppingCart.setProductId(shoppingCartUpdateDto.getProductId()); | ||
| shoppingCart.setUnitPrice(shoppingCartUpdateDto.getUnitPrice()); | ||
| shoppingCartRepository.save(shoppingCart); | ||
| } | ||
|
|
||
| /*public ShoppingCart delete(ShoppingCart shoppingCart){ | ||
| ShoppingCart shoppingCartDeleted = findById(shoppingCart.getId()); | ||
| shoppingCartRepository.delete(shoppingCartDeleted); | ||
| return shoppingCartDeleted; | ||
| }*/ | ||
|
|
||
| public void deleteShoppingCart(Integer id){ | ||
| findById(id); | ||
| shoppingCartRepository.deleteById(id); | ||
| } | ||
|
|
||
| public List<ShoppingCartReturnDto> findByDescriptionProduct(String description) { | ||
| return shoppingCartRepository.findByDescriptionProduct(description) | ||
| .stream() | ||
| .map(ShoppingCartReturnDto::toDto) | ||
| .toList(); | ||
| } | ||
|
|
||
| public List<ShoppingCartReturnDto> findByUnitPrice(Double unitPrice) { | ||
| return shoppingCartRepository.findByUnitPrice(unitPrice) | ||
| .stream() | ||
| .map(ShoppingCartReturnDto::toDto) | ||
| .toList(); | ||
| } | ||
|
|
||
| public List<ShoppingCartReturnDto> findByDescriptionProductNative(String description) { | ||
| return shoppingCartRepository.findByDescriptionProductNative(description) | ||
| .stream() | ||
| .map(ShoppingCartReturnDto::toDto) | ||
| .toList(); | ||
| } | ||
|
|
||
| public List<ShoppingCartReturnDto> findByDescriptionProductHybrid(String description){ | ||
| return shoppingCartRepository | ||
| .findByDescriptionProductHybrid(description) | ||
| .stream() | ||
| .map(ShoppingCartReturnDto::toDto) | ||
| .toList(); | ||
| } | ||
|
|
||
| public List<ShoppingCartReturnDto> findByDeliveryDate(LocalDate deliveryDate){ | ||
| return shoppingCartRepository | ||
| .findByDeliveryDate(deliveryDate) | ||
| .stream() | ||
| .map(ShoppingCartReturnDto::toDto) | ||
| .toList(); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Valid