Skip to content

Commit

Permalink
[GREEN] [REFACTOR] Reimplement TDD with factory pattern on subscripti…
Browse files Browse the repository at this point in the history
…on box :D
  • Loading branch information
pesolosep committed May 16, 2024
1 parent af4a43b commit 0b5a2d6
Show file tree
Hide file tree
Showing 17 changed files with 1,040 additions and 336 deletions.
Original file line number Diff line number Diff line change
@@ -1,65 +1,112 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller;

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;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.service.SubscriptionBoxService;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

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

private final SubscriptionBoxService subscriptionBoxService;

public class SubscriptionBoxController {
@Autowired
private SubscriptionBoxService subscriptionBoxService;
String createHTML = "userCreate";
@GetMapping("../")
public String createUserPage(Model model) {
return "<h1>Subscription Box Management sudah berhasil!</h1>";
public SubscriptionBoxController(SubscriptionBoxService subscriptionBoxService) {
this.subscriptionBoxService = subscriptionBoxService;
}

@PostMapping("/create")
public ResponseEntity<SubscriptionBox> createSubscriptionBox(@RequestBody SubscriptionBox subscriptionBox, Model model) {
SubscriptionBox newBox = subscriptionBoxService.addBox(subscriptionBox);
return ResponseEntity.ok(newBox);
public CompletableFuture<ResponseEntity<SubscriptionBox>> createSubscriptionBox(@RequestBody SubscriptionBox subscriptionBox) {
return subscriptionBoxService.save(subscriptionBox)
.thenApply(ResponseEntity::ok)
.exceptionally(ex -> ResponseEntity.badRequest().build());
}

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

@GetMapping("/view-details/{boxId}")
public ResponseEntity<String> viewDetails(@PathVariable String boxId) {
String boxName = subscriptionBoxService.viewDetails(boxId);
return ResponseEntity.ok(boxName);
@GetMapping("/{id}")
public CompletableFuture<ResponseEntity<SubscriptionBox>> findById(@PathVariable String id) {
try {
UUID.fromString(id);
} catch (IllegalArgumentException e) {
return CompletableFuture.completedFuture(ResponseEntity.badRequest().build());
}

return subscriptionBoxService.findById(id)
.thenApply(optionalSubscriptionBox ->
optionalSubscriptionBox.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build()));
}

@DeleteMapping("/delete/{boxId}")
public ResponseEntity<SubscriptionBox> deleteBox(@PathVariable String boxId) {
SubscriptionBox deletedBox = subscriptionBoxService.deleteBox(boxId);
return ResponseEntity.ok(deletedBox);
@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);
}
});
}

@PutMapping("/edit/{boxId}")
public ResponseEntity<SubscriptionBox> editBox(@PathVariable String boxId, @RequestBody SubscriptionBox subscriptionBox) {
SubscriptionBox editedBox = subscriptionBoxService.editBox(boxId, subscriptionBox);
if (editedBox == null) {
return ResponseEntity.notFound().build();
@DeleteMapping("/{id}")
public CompletableFuture<ResponseEntity<String>> deleteSubscriptionBox(@PathVariable String id) {
try {
UUID.fromString(id);
} catch (IllegalArgumentException e) {
return CompletableFuture.completedFuture(ResponseEntity.badRequest().build());
}
return ResponseEntity.ok(editedBox);

return subscriptionBoxService.delete(id)
.thenApply(result -> ResponseEntity.ok("DELETE SUCCESS"))
.exceptionally(ex -> ResponseEntity.notFound().build());
}

@GetMapping("/price/less-than/{price}")
public CompletableFuture<ResponseEntity<List<SubscriptionBox>>> 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) {
return subscriptionBoxService.findByPriceGreaterThan(price)
.thenApply(ResponseEntity::ok);
}

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

@GetMapping("/name/{name}")
public CompletableFuture<ResponseEntity<Optional<List<SubscriptionBox>>>> findByName(@PathVariable String name) {
return subscriptionBoxService.findByName(name)
.thenApply(ResponseEntity::ok);
}

@GetMapping("/filterByPrice/{price}")
public ResponseEntity<List<SubscriptionBox>> filterByPrice(@PathVariable int price) {
List<SubscriptionBox> filteredBoxes = subscriptionBoxService.filterByPrice(price);
return ResponseEntity.ok(filteredBoxes);
@GetMapping("/distinct-names")
public CompletableFuture<ResponseEntity<Optional<List<String>>>> findDistinctNames() {
return subscriptionBoxService.findDistinctNames()
.thenApply(ResponseEntity::ok);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 java.util.List;
import java.util.Optional;

@Component
public class DTOMapper {
public static SubscriptionBoxDTO convertModeltoDto (SubscriptionBox subscriptionBox){
return new SubscriptionBoxDTO(
subscriptionBox.getId(),
subscriptionBox.getName(),
subscriptionBox.getType(),
subscriptionBox.getPrice(),
subscriptionBox.getItems()
);
}

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 updateSubscriptionBox(SubscriptionBox subscriptionBox, SubscriptionBoxDTO subscriptionBoxDTO){
Optional.ofNullable(subscriptionBoxDTO.getItems()).ifPresent(subscriptionBox::setItems);
Optional.of(
subscriptionBoxDTO.getPrice()).ifPresent(subscriptionBox::setPrice);
return subscriptionBox;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto;

import lombok.*;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item;

import java.util.List;

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

import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item;

import java.util.List;

public interface Factory <T> {
T create();

T create(String id, String name, String type, int price, List<Item> items );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory;

import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item;
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox;

import java.util.List;

public class SubscriptionBoxFactory implements Factory<SubscriptionBox> {
@Override
public SubscriptionBox create(){
return new SubscriptionBox();
}

public SubscriptionBox create(String id, String name, String type, int price, List<Item> items ){
return new SubscriptionBox( name, type, price, items);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Getter;
import lombok.Setter;
import java.util.List;
import java.util.UUID;


@Entity
Expand All @@ -31,4 +32,42 @@ public class SubscriptionBox {
)
List<Item> items;
// Rating rating;
}

public SubscriptionBox(){
this.id = UUID.randomUUID().toString();
}

public SubscriptionBox( String name, String type, int price, List<Item> items){
this.id = UUID.randomUUID().toString();
this.name = name;
this.setType(type);
this.setPrice(price);
this.items = items;
}

public void setType(String type) {
if (type.equalsIgnoreCase("monthly") |
type.equalsIgnoreCase("quarterly") |
type.equalsIgnoreCase("semi-annual")
){
this.type = type;
}
else{
throw new IllegalArgumentException("Invalid type");
}


}

public void setPrice(int price) {
if (price >0){
this.price = price;
}
else{
throw new IllegalArgumentException("Invalid Price, Please enter integer above 0");
}


}
}
;
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ public SubscriptionBoxBuilder setName(String name) {
}

public SubscriptionBoxBuilder setType(String type) {
if (!type.equals("MONTHLY") | !type.equals("QUARTERLY") | !type.equals("SEMI-ANNUALLY")){
throw new IllegalArgumentException("Invalid Type.");
if (type.equals("MONTHLY") | type.equals("QUARTERLY") | type.equals("SEMI-ANNUALLY")){
return this;

}
return this;
throw new IllegalArgumentException("Invalid Type.");
}

public SubscriptionBoxBuilder setPrice(int price) {
Expand Down
Loading

0 comments on commit 0b5a2d6

Please sign in to comment.