-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GREEN] [REFACTOR] Reimplement TDD with factory pattern on subscripti…
…on box :D
- Loading branch information
Showing
17 changed files
with
1,040 additions
and
336 deletions.
There are no files selected for viewing
121 changes: 84 additions & 37 deletions
121
...ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/DTOMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/SubscriptionBoxDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/Factory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
17 changes: 17 additions & 0 deletions
17
...va/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/SubscriptionBoxFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.