aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/dev/submelon/pantry/Item.java9
-rw-r--r--src/main/java/dev/submelon/pantry/ItemController.java6
-rw-r--r--src/main/java/dev/submelon/pantry/ItemRepository.java2
-rw-r--r--src/test/java/dev/submelon/pantry/ItemRepositoryTests.java51
4 files changed, 67 insertions, 1 deletions
diff --git a/src/main/java/dev/submelon/pantry/Item.java b/src/main/java/dev/submelon/pantry/Item.java
index 1df7e91..bdd4631 100644
--- a/src/main/java/dev/submelon/pantry/Item.java
+++ b/src/main/java/dev/submelon/pantry/Item.java
@@ -16,6 +16,15 @@ public class Item {
private String description;
private double quantity;
+
+ public Item() {
+ }
+
+ public Item(String name, String description, double quantity) {
+ this.name = name;
+ this.description = description;
+ this.quantity = quantity;
+ }
public Integer getId() {
return id;
diff --git a/src/main/java/dev/submelon/pantry/ItemController.java b/src/main/java/dev/submelon/pantry/ItemController.java
index cd2f3ff..778f93b 100644
--- a/src/main/java/dev/submelon/pantry/ItemController.java
+++ b/src/main/java/dev/submelon/pantry/ItemController.java
@@ -3,6 +3,7 @@ package dev.submelon.pantry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@@ -30,4 +31,9 @@ public class ItemController {
return itemRepository.findAll();
}
+ @GetMapping(path="/name/{name}")
+ public @ResponseBody Item getByName(@PathVariable String name) {
+ return itemRepository.findByName(name);
+ }
+
}
diff --git a/src/main/java/dev/submelon/pantry/ItemRepository.java b/src/main/java/dev/submelon/pantry/ItemRepository.java
index c1c9bb5..f1fd3f0 100644
--- a/src/main/java/dev/submelon/pantry/ItemRepository.java
+++ b/src/main/java/dev/submelon/pantry/ItemRepository.java
@@ -3,5 +3,5 @@ package dev.submelon.pantry;
import org.springframework.data.repository.CrudRepository;
public interface ItemRepository extends CrudRepository<Item, Integer> {
-
+ Item findByName(String name);
}
diff --git a/src/test/java/dev/submelon/pantry/ItemRepositoryTests.java b/src/test/java/dev/submelon/pantry/ItemRepositoryTests.java
new file mode 100644
index 0000000..9278599
--- /dev/null
+++ b/src/test/java/dev/submelon/pantry/ItemRepositoryTests.java
@@ -0,0 +1,51 @@
+package dev.submelon.pantry;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.TestInstance.Lifecycle;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@TestInstance(Lifecycle.PER_CLASS)
+@SpringBootTest
+public class ItemRepositoryTests {
+ @Autowired
+ private ItemRepository itemRepository;
+
+ @BeforeAll
+ public void before() throws Exception {
+ Item pb = new Item("Peanut Butter", "Crunchy", 14.0);
+ Item jelly = new Item("Strawberry Preserves", "The best", 12.8);
+ Item bread = new Item("Oatnut Bread", "Relatively healthy, right?", 10);
+ assertNull(pb.getId());
+ assertNull(jelly.getId());
+ assertNull(bread.getId());
+ this.itemRepository.save(pb);
+ this.itemRepository.save(jelly);
+ this.itemRepository.save(bread);
+ assertNotNull(pb.getId());
+ assertNotNull(jelly.getId());
+ assertNotNull(bread.getId());
+ }
+
+ @Test
+ public void testFetchData() {
+ Item pb = itemRepository.findByName("Peanut Butter");
+ assertNotNull(pb);
+ assertEquals(14.0, pb.getQuantity());
+ Iterable<Item> items = itemRepository.findAll();
+
+ int count = 0;
+ for (Item item : items) {
+ assertNotNull(item);
+ count++;
+ }
+
+ assertEquals(3, count);
+ }
+}