This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.drinkool.entities;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.transaction.Transactional;
|
||||
|
||||
@Entity
|
||||
@Table(name = "inventory_items")
|
||||
public class InventoryItemEntity extends BaseEntity {
|
||||
|
||||
public String name;
|
||||
public int quantity;
|
||||
public String unit;
|
||||
|
||||
// Create
|
||||
@Transactional
|
||||
public void add(String name, int quantity, String unit) {
|
||||
this.name = name;
|
||||
this.quantity = quantity;
|
||||
this.unit = unit;
|
||||
|
||||
this.persist();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void add(String name, int quantity) {
|
||||
add(name, quantity, "unit");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.drinkool.services;
|
||||
|
||||
import com.drinkool.dtos.AddInventory;
|
||||
import com.drinkool.entities.InventoryItemEntity;
|
||||
import jakarta.transaction.Transactional;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
@Path("/api/inventory")
|
||||
public class InventoryService {
|
||||
|
||||
@POST
|
||||
@Path("/add")
|
||||
@Transactional
|
||||
public Response add(AddInventory input) {
|
||||
InventoryItemEntity newItem = new InventoryItemEntity();
|
||||
|
||||
newItem.add(input.name, input.quantity, input.unit);
|
||||
|
||||
return Response.ok().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.drinkool.services;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.drinkool.dtos.AddInventory;
|
||||
import com.drinkool.entities.InventoryItemEntity;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@QuarkusTest
|
||||
public class InventoryServiceTest {
|
||||
|
||||
@Test
|
||||
void add() {
|
||||
Random random = new Random();
|
||||
|
||||
String name = UUID.randomUUID().toString();
|
||||
|
||||
AddInventory input = new AddInventory();
|
||||
input.name = name;
|
||||
input.quantity = random.nextInt(100);
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body(input)
|
||||
.when()
|
||||
.post("/api/inventory/add")
|
||||
.then();
|
||||
|
||||
assertEquals(1, InventoryItemEntity.find("name", name).count());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.drinkool.dtos;
|
||||
|
||||
public class AddInventory {
|
||||
public String name;
|
||||
public int quantity;
|
||||
public String unit;
|
||||
}
|
||||
Reference in New Issue
Block a user