Skip to content

Commit

Permalink
Merge pull request #3 from ADPRO-C11/subbox-management
Browse files Browse the repository at this point in the history
Testing deployment for sub box module
  • Loading branch information
pesolosep authored May 16, 2024
2 parents 897483e + 227764c commit 8245255
Show file tree
Hide file tree
Showing 23 changed files with 1,393 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
push:
branches:
- master

- staging
jobs:
build:
name: Build
Expand Down Expand Up @@ -88,7 +88,7 @@ jobs:
- name: check directory
run: ls -al
- name: Login to Docker Hub
run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.REGISTRY_USER }} --password-stdin docker.io
run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.REGISTRY_USER }} --password-stdin docker.io
- name: Set Docker BuildKit
run: export DOCKER_BUILDKIT=1
- name: Build Docker Image
Expand Down
Empty file added README.md
Empty file.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ repositories {
}

dependencies {
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")
compileOnly("org.projectlombok:lombok")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("org.postgresql:postgresql")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
annotationProcessor("org.projectlombok:lombok")
testImplementation("org.springframework.boot:spring-boot-starter-test")
Expand Down
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);
}
}
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
@@ -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;
}
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;
}
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");
}


}
}
;
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;
}
}


Loading

0 comments on commit 8245255

Please sign in to comment.