-
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 #10 from ADPRO-C11/subbox-management
Subbox management
- Loading branch information
Showing
15 changed files
with
355 additions
and
303 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ name: Java CI/CD Pipeline | |
on: | ||
push: | ||
branches: | ||
- staging | ||
- master | ||
jobs: | ||
build: | ||
name: Build | ||
|
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
68 changes: 54 additions & 14 deletions
68
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/ItemDTO.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,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; | ||
} |
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
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.