Skip to content

Commit

Permalink
Merge pull request #10 from ADPRO-C11/subbox-management
Browse files Browse the repository at this point in the history
Subbox management
  • Loading branch information
pesolosep authored May 23, 2024
2 parents 8904564 + 1b7eb87 commit a5779fd
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 303 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: Java CI/CD Pipeline
on:
push:
branches:
- staging
- master
jobs:
build:
name: Build
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ repositories {
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter-logging")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto.DTOMapper;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto.SubscriptionBoxDTO;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.service.SubscriptionBoxService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -13,31 +16,51 @@

@RestController
@RequestMapping("/subscription-box")
@CrossOrigin(origins = "http://localhost:3000") // Change to specific origin if needed
@CrossOrigin(origins = "*") // Change to specific origin if needed
public class SubscriptionBoxController {

private final SubscriptionBoxService subscriptionBoxService;
private static final Logger logger = LoggerFactory.getLogger(SubscriptionBoxController.class);

@Autowired
public SubscriptionBoxController(SubscriptionBoxService subscriptionBoxService) {
this.subscriptionBoxService = subscriptionBoxService;
}

@PostMapping("/create")
public CompletableFuture<ResponseEntity<SubscriptionBox>> createSubscriptionBox(@RequestBody SubscriptionBox subscriptionBox) {
public CompletableFuture<ResponseEntity<SubscriptionBox>> createSubscriptionBox(@RequestBody SubscriptionBoxDTO subscriptionBoxDTO) {
SubscriptionBox subscriptionBox = DTOMapper.convertDTOtoModel(subscriptionBoxDTO);
return subscriptionBoxService.save(subscriptionBox)
.thenApply(ResponseEntity::ok)
.exceptionally(ex -> ResponseEntity.badRequest().build());
}


@GetMapping("/list")
public CompletableFuture<ResponseEntity<List<SubscriptionBox>>> findAll() {
return subscriptionBoxService.findAll()
.thenApply(ResponseEntity::ok);
}

@PatchMapping("/update")
public CompletableFuture<ResponseEntity<SubscriptionBox>> updateSubscriptionBox(@RequestBody SubscriptionBoxDTO subscriptionBoxDTO) {
if (subscriptionBoxDTO.getId() == null || subscriptionBoxDTO.getId().isEmpty()) {
return CompletableFuture.completedFuture(ResponseEntity.badRequest().build());
}

return subscriptionBoxService.findById(subscriptionBoxDTO.getId())
.thenCompose(optionalSubscriptionBox -> {
if (optionalSubscriptionBox.isEmpty()) {
return CompletableFuture.completedFuture(ResponseEntity.notFound().build());
} else {
return subscriptionBoxService.update(subscriptionBoxDTO)
.thenApply(ResponseEntity::ok);
}
});
}

@GetMapping("/{id}")
public CompletableFuture<ResponseEntity<SubscriptionBox>> findById(@PathVariable String id) {
public CompletableFuture<ResponseEntity<SubscriptionBoxDTO>> findById(@PathVariable String id) {
try {
UUID.fromString(id);
} catch (IllegalArgumentException e) {
Expand All @@ -50,22 +73,6 @@ public CompletableFuture<ResponseEntity<SubscriptionBox>> findById(@PathVariable
.orElse(ResponseEntity.notFound().build()));
}

@PatchMapping("/update")
public CompletableFuture<ResponseEntity<SubscriptionBox>> updateSubscriptionBox(@RequestBody SubscriptionBox subscriptionBox) {
if (subscriptionBox.getId() == null || subscriptionBox.getId().isEmpty()) {
return CompletableFuture.completedFuture(ResponseEntity.badRequest().build());
}

return subscriptionBoxService.findById(subscriptionBox.getId())
.thenCompose(optionalSubscriptionBox -> {
if (optionalSubscriptionBox.isEmpty()) {
return CompletableFuture.completedFuture(ResponseEntity.notFound().build());
} else {
return subscriptionBoxService.update(subscriptionBox)
.thenApply(ResponseEntity::ok);
}
});
}

@DeleteMapping("/{id}")
public CompletableFuture<ResponseEntity<String>> deleteSubscriptionBox(@PathVariable String id) {
Expand All @@ -81,25 +88,28 @@ public CompletableFuture<ResponseEntity<String>> deleteSubscriptionBox(@PathVari
}

@GetMapping("/price/less-than/{price}")
public CompletableFuture<ResponseEntity<List<SubscriptionBox>>> findByPriceLessThan(@PathVariable int price) {
public CompletableFuture<ResponseEntity<List<SubscriptionBoxDTO>>> findByPriceLessThan(@PathVariable int price) {
return subscriptionBoxService.findByPriceLessThan(price)
.thenApply(ResponseEntity::ok);
}

@GetMapping("/price/greater-than/{price}")
public CompletableFuture<ResponseEntity<List<SubscriptionBox>>> findByPriceGreaterThan(@PathVariable int price) {
public CompletableFuture<ResponseEntity<List<SubscriptionBoxDTO>>> findByPriceGreaterThan(@PathVariable int price) {
return subscriptionBoxService.findByPriceGreaterThan(price)
.thenApply(ResponseEntity::ok);
}

@GetMapping("/price/equals/{price}")
public CompletableFuture<ResponseEntity<List<SubscriptionBox>>> findByPriceEquals(@PathVariable int price) {
public CompletableFuture<ResponseEntity<List<SubscriptionBoxDTO>>> findByPriceEquals(@PathVariable int price) {
return subscriptionBoxService.findByPriceEquals(price)
.thenApply(ResponseEntity::ok);
}

@GetMapping("/name/{name}")
public CompletableFuture<ResponseEntity<Optional<List<SubscriptionBox>>>> findByName(@PathVariable String name) {
@GetMapping("/name/{nameURL}")
public CompletableFuture<ResponseEntity<Optional<List<SubscriptionBoxDTO>>>> findByName(@PathVariable String nameURL) {
// logger.info("Searching for SubscriptionBox with name before split: {}", nameURL);
String name = nameURL.replaceAll("-", " ");
// logger.info("Searching for SubscriptionBox with name: {}", name);
return subscriptionBoxService.findByName(name)
.thenApply(ResponseEntity::ok);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,79 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto;

import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item;
import org.springframework.stereotype.Component;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory.SubscriptionBoxFactory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
public class DTOMapper {
public static SubscriptionBoxDTO convertModeltoDto (SubscriptionBox subscriptionBox){
public static SubscriptionBoxDTO convertModelToDto(SubscriptionBox subscriptionBox) {
List<ItemDTO> itemDTOs = Optional.ofNullable(subscriptionBox.getItems())
.map(items -> items.stream()
.map(DTOMapper::convertItemToDto)
.collect(Collectors.toList()))
.orElse(null);

return new SubscriptionBoxDTO(
subscriptionBox.getId(),
subscriptionBox.getName(),
subscriptionBox.getType(),
subscriptionBox.getPrice(),
subscriptionBox.getItems()
itemDTOs
);
}

public static SubscriptionBox convertDTOtoModel(SubscriptionBoxDTO subscriptionBoxDTO){
String id = subscriptionBoxDTO.getId();
String name = subscriptionBoxDTO.getName();
String type = subscriptionBoxDTO.getType();
int price = subscriptionBoxDTO.getPrice();
List<Item> items = subscriptionBoxDTO.getItems();
return new SubscriptionBoxFactory().create(id,name,type,price,items);
public static SubscriptionBox convertDTOtoModel(SubscriptionBoxDTO subscriptionBoxDTO) {
List<Item> items = Optional.ofNullable(subscriptionBoxDTO.getItems()).map(dtoItems ->
dtoItems.stream()
.map(DTOMapper::convertDtoToItem)
.collect(Collectors.toList())
).orElse(null);

return new SubscriptionBoxFactory().create(
subscriptionBoxDTO.getId(),
subscriptionBoxDTO.getName(),
subscriptionBoxDTO.getType(),
subscriptionBoxDTO.getPrice(),
items
);
}

public static SubscriptionBox updateSubscriptionBox(SubscriptionBox subscriptionBox, SubscriptionBoxDTO subscriptionBoxDTO){
Optional.ofNullable(subscriptionBoxDTO.getItems()).ifPresent(subscriptionBox::setItems);
Optional.of(
subscriptionBoxDTO.getPrice()).ifPresent(subscriptionBox::setPrice);
public static SubscriptionBox updateSubscriptionBox(SubscriptionBox subscriptionBox, SubscriptionBoxDTO subscriptionBoxDTO) {
Optional.ofNullable(subscriptionBoxDTO.getItems()).ifPresent(dtoItems -> {
List<Item> items = dtoItems.stream()
.map(DTOMapper::convertDtoToItem)
.collect(Collectors.toList());
subscriptionBox.setItems(items);
});
Optional.of(subscriptionBoxDTO.getPrice()).ifPresent(subscriptionBox::setPrice);
return subscriptionBox;
}

public static ItemDTO convertItemToDto(Item item) {
return new ItemDTO(
item.getId(),
item.getName(),
item.getQuantity()
);
}

public static Item convertDtoToItem(ItemDTO itemDTO) {
return new Item(
itemDTO.getId(),
itemDTO.getName(),
itemDTO.getQuantity()
);
}

public static Item updateItem(Item item, ItemDTO itemDTO) {
Optional.ofNullable(itemDTO.getId()).ifPresent(item::setId);
Optional.ofNullable(itemDTO.getName()).ifPresent(item::setName);
Optional.of(itemDTO.getQuantity()).ifPresent(item::setQuantity);
return item;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto;

import lombok.*;


@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class ItemDTO {
private String id;
private String name;
private int quantity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

@AllArgsConstructor
@NoArgsConstructor
@Getter @Setter
@Getter
@Setter
public class SubscriptionBoxDTO {
String id;
String name;
String type;
int price;
List<Item> items;
}
private String id;
private String name;
private String type;
private int price;
private List<ItemDTO> items;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -19,5 +20,15 @@ public class Item {
private int quantity;

@ManyToMany(mappedBy = "items")
@JsonBackReference
private List<SubscriptionBox> subscriptionBoxes;

public Item(String id, String name, int quantity) {
this.id = id;
this.name = name;
this.quantity = quantity;
}

public Item() {
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model;


import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -30,6 +31,7 @@ public class SubscriptionBox {
joinColumns = @JoinColumn(name = "subscriptionbox_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "item_id", referencedColumnName = "id")
)
@JsonManagedReference
List<Item> items;
// Rating rating;

Expand All @@ -39,7 +41,7 @@ public SubscriptionBox(){

public SubscriptionBox( String name, String type, int price, List<Item> items){
this.id = UUID.randomUUID().toString();
this.name = name;
this.setName(name);
this.setType(type);
this.setPrice(price);
this.items = items;
Expand All @@ -50,15 +52,25 @@ public void setType(String type) {
type.equalsIgnoreCase("quarterly") |
type.equalsIgnoreCase("semi-annual")
){
this.type = type;
this.type = type.toUpperCase();
}
else{
throw new IllegalArgumentException("Invalid type");
}


}
public void setName(String name) {
if (!name.contains("-")
){
this.name = name;
}
else{
throw new IllegalArgumentException("Do not put '-' inside your name!");
}


}
public void setPrice(int price) {
if (price >0){
this.price = price;
Expand Down
Loading

0 comments on commit a5779fd

Please sign in to comment.