-
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 #24 from ADPRO-C11/add-item-admin
Add item admin
- Loading branch information
Showing
13 changed files
with
503 additions
and
7 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
69 changes: 69 additions & 0 deletions
69
...in/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemController.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,69 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; | ||
|
||
|
||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Getter | ||
@RestController | ||
@RequestMapping("/items") | ||
public class ItemController { | ||
|
||
private final List<Item> items = new ArrayList<>(); | ||
|
||
@GetMapping | ||
public ResponseEntity<List<Item>> getAllItems() { | ||
return new ResponseEntity<>(items, HttpStatus.OK); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<Item> getItemById(@PathVariable String id) { | ||
for (Item item : items) { | ||
if (item.getId().equals(id)) { | ||
return new ResponseEntity<>(item, HttpStatus.OK); | ||
} | ||
} | ||
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<Item> createItem(@RequestBody Item item) { | ||
items.add(item); | ||
return new ResponseEntity<>(item, HttpStatus.CREATED); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ResponseEntity<Item> updateItem(@PathVariable String id, @RequestBody Item updatedItem) { | ||
for (Item item : items) { | ||
if (item.getId().equals(id)) { | ||
item.setName(updatedItem.getName()); | ||
item.setQuantity(updatedItem.getQuantity()); | ||
return new ResponseEntity<>(item, HttpStatus.OK); | ||
} | ||
} | ||
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteItem(@PathVariable String id) { | ||
for (Item item : items) { | ||
if (item.getId().equals(id)) { | ||
items.remove(item); | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
} | ||
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | ||
} | ||
|
||
public void setItems(List<Item> items) { | ||
this.items.clear(); | ||
this.items.addAll(items); | ||
} | ||
|
||
} |
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
23 changes: 23 additions & 0 deletions
23
src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactory.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.factory; | ||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; | ||
|
||
import java.util.List; | ||
|
||
public class ItemFactory implements Factory<Item> { | ||
@Override | ||
public Item create(){ | ||
return new Item(); | ||
} | ||
|
||
public Item create(String id, String name, int quantity){ | ||
return new Item(id, name, quantity); | ||
} | ||
|
||
@Override | ||
public Item create(String id, String name, String type, int price, List<Item> items) { | ||
return null; | ||
} | ||
} | ||
|
||
|
||
|
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
13 changes: 13 additions & 0 deletions
13
src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemService.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,13 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; | ||
|
||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; | ||
import java.util.List; | ||
|
||
public interface ItemService { | ||
Item createItem(String id, String name, int quantity); | ||
Item getItemById(String id); | ||
Item deleteItem(String id); | ||
Item editItem(String id, String name, int quantity); | ||
List<Item> getAllItems(); | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/ItemServiceImpl.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,56 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.service; | ||
|
||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; | ||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository.ItemRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class ItemServiceImpl implements ItemService { | ||
|
||
private final ItemRepository itemRepository; | ||
|
||
@Autowired | ||
public ItemServiceImpl(ItemRepository itemRepository) { | ||
this.itemRepository = itemRepository; | ||
} | ||
|
||
@Override | ||
public Item createItem(String id, String name, int quantity) { | ||
Item item = new Item(id, name, quantity); | ||
return itemRepository.createItem(item); | ||
} | ||
|
||
@Override | ||
public Item getItemById(String id) { | ||
return itemRepository.getItemById(id); | ||
} | ||
|
||
@Override | ||
public Item deleteItem(String id) { | ||
Item item = itemRepository.getItemById(id); | ||
if (item != null) { | ||
return itemRepository.deleteItem(item); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Item editItem(String id, String name, int quantity) { | ||
Item existingItem = itemRepository.getItemById(id); | ||
if (existingItem != null) { | ||
existingItem.setName(name); | ||
existingItem.setQuantity(quantity); | ||
return itemRepository.editItem(existingItem); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public List<Item> getAllItems() { | ||
return itemRepository.getAllItems(); | ||
} | ||
} | ||
|
86 changes: 86 additions & 0 deletions
86
...ava/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/ItemControllerTest.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,86 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; | ||
|
||
|
||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ItemControllerTest { | ||
|
||
private ItemController itemController; | ||
private List<Item> items; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
itemController = new ItemController(); | ||
items = new ArrayList<>(); | ||
items.add(new Item("1", "Snack A", 5)); | ||
items.add(new Item("2", "Snack B", 10)); | ||
itemController.setItems(items); | ||
} | ||
|
||
@Test | ||
public void testGetAllItems() { | ||
ResponseEntity<List<Item>> responseEntity = itemController.getAllItems(); | ||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); | ||
assertEquals(items, responseEntity.getBody()); | ||
} | ||
|
||
@Test | ||
public void testGetItemById() { | ||
ResponseEntity<Item> responseEntity = itemController.getItemById("1"); | ||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); | ||
assertEquals("Snack A", Objects.requireNonNull(responseEntity.getBody()).getName()); | ||
} | ||
|
||
@Test | ||
public void testGetItemById_NotFound() { | ||
ResponseEntity<Item> responseEntity = itemController.getItemById("100"); | ||
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); | ||
} | ||
|
||
@Test | ||
public void testCreateItem() { | ||
Item newItem = new Item("3", "Snack C", 8); | ||
ResponseEntity<Item> responseEntity = itemController.createItem(newItem); | ||
assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode()); | ||
assertEquals(newItem, responseEntity.getBody()); | ||
} | ||
|
||
@Test | ||
public void testUpdateItem() { | ||
Item updatedItem = new Item("1", "New Snack A", 7); | ||
ResponseEntity<Item> responseEntity = itemController.updateItem("1", updatedItem); | ||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); | ||
assertEquals(updatedItem, responseEntity.getBody()); | ||
} | ||
|
||
@Test | ||
public void testUpdateItem_NotFound() { | ||
Item updatedItem = new Item("100", "New Snack A", 7); | ||
ResponseEntity<Item> responseEntity = itemController.updateItem("100", updatedItem); | ||
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); | ||
} | ||
|
||
@Test | ||
public void testDeleteItem() { | ||
ResponseEntity<Void> responseEntity = itemController.deleteItem("1"); | ||
assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode()); | ||
assertEquals(1, itemController.getItems().size()); | ||
} | ||
|
||
@Test | ||
public void testDeleteItem_NotFound() { | ||
ResponseEntity<Void> responseEntity = itemController.deleteItem("100"); | ||
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/factory/ItemFactoryTest.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,45 @@ | ||
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.factory; | ||
|
||
import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; | ||
import org.junit.jupiter.api.Test; | ||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class ItemFactoryTest { | ||
|
||
@Test | ||
public void testCreateDefaultItem() { | ||
ItemFactory itemFactory = new ItemFactory(); | ||
Item item = itemFactory.create(); | ||
|
||
assertTrue(isValidUUID(item.getId())); | ||
assertNull(item.getName()); | ||
assertEquals(0, item.getQuantity()); | ||
} | ||
|
||
@Test | ||
public void testCreateItemWithParameters() { | ||
ItemFactory itemFactory = new ItemFactory(); | ||
String id = UUID.randomUUID().toString(); | ||
String name = "Snack A"; | ||
int quantity = 5; | ||
|
||
Item item = itemFactory.create(id, name, quantity); | ||
|
||
// Memeriksa apakah nilai parameter sesuai dengan nilai yang diatur di item | ||
assertEquals(id, item.getId()); | ||
assertEquals(name, item.getName()); | ||
assertEquals(quantity, item.getQuantity()); | ||
} | ||
|
||
private boolean isValidUUID(String uuidString) { | ||
try { | ||
UUID.fromString(uuidString); | ||
return true; | ||
} catch (IllegalArgumentException e) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.