-
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.
Merge pull request #3 from ADPRO-C11/subbox-management
Testing deployment for sub box module
- Loading branch information
Showing
23 changed files
with
1,393 additions
and
13 deletions.
There are no files selected for viewing
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
112 changes: 112 additions & 0 deletions
112
...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 |
---|---|---|
@@ -0,0 +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.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@RestController | ||
@RequestMapping("/subscription-box") | ||
@CrossOrigin(origins = "http://localhost:3000") // Change to specific origin if needed | ||
public class SubscriptionBoxController { | ||
|
||
private final SubscriptionBoxService subscriptionBoxService; | ||
|
||
@Autowired | ||
public SubscriptionBoxController(SubscriptionBoxService subscriptionBoxService) { | ||
this.subscriptionBoxService = subscriptionBoxService; | ||
} | ||
|
||
@PostMapping("/create") | ||
public CompletableFuture<ResponseEntity<SubscriptionBox>> createSubscriptionBox(@RequestBody SubscriptionBox subscriptionBox) { | ||
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); | ||
} | ||
|
||
@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())); | ||
} | ||
|
||
@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) { | ||
try { | ||
UUID.fromString(id); | ||
} catch (IllegalArgumentException e) { | ||
return CompletableFuture.completedFuture(ResponseEntity.badRequest().build()); | ||
} | ||
|
||
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("/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); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Admin.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.model; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter @Setter | ||
public class Admin { | ||
private String username; | ||
private String password; | ||
private int itemsQuantity; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/Item.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,23 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Entity | ||
@Getter @Setter | ||
@Table (name = "Item") | ||
public class Item { | ||
@Id | ||
private String id; | ||
@Column(name = "item_name") | ||
private String name; | ||
|
||
@Column(name = "item_quantity") | ||
private int quantity; | ||
|
||
@ManyToMany(mappedBy = "items") | ||
private List<SubscriptionBox> subscriptionBoxes; | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/SubscriptionBox.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,73 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model; | ||
|
||
|
||
import jakarta.persistence.*; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
|
||
@Entity | ||
@Getter @Setter | ||
@Table(name = "subscriptionbox") | ||
public class SubscriptionBox { | ||
@Id | ||
String id; | ||
@Column(name = "box_name") | ||
String name; | ||
|
||
@Column(name = "box_type") | ||
String type; | ||
|
||
@Column(name = "box_price") | ||
int price; | ||
|
||
@ManyToMany(cascade = CascadeType.ALL) | ||
@JoinTable( | ||
name = "subscriptionbox_item", | ||
joinColumns = @JoinColumn(name = "subscriptionbox_id", referencedColumnName = "id"), | ||
inverseJoinColumns = @JoinColumn(name = "item_id", referencedColumnName = "id") | ||
) | ||
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"); | ||
} | ||
|
||
|
||
} | ||
} | ||
; |
41 changes: 41 additions & 0 deletions
41
...in/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemRepository.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,41 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository; | ||
|
||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; | ||
import lombok.Getter; | ||
import org.springframework.stereotype.Repository; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
@Repository | ||
public class ItemRepository { | ||
private List<Item> items = new ArrayList<>(); | ||
public Item createItem(Item item) { | ||
items.add(item); | ||
return item; | ||
} | ||
|
||
public Item getItemById(String id) { | ||
for (Item item : items) { | ||
if (item.getId().equals(id)) { | ||
return item; | ||
} | ||
} | ||
return null; | ||
} | ||
public Item deleteItem(Item item) { | ||
items.remove(item); | ||
return item; | ||
} | ||
public Item editItem(Item item) { | ||
for (Item itemToEdit : items) { | ||
if (item.getId().equals(item.getId())) { | ||
itemToEdit = item; | ||
return itemToEdit; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.