Skip to content

Commit

Permalink
Merge pull request #24 from ADPRO-C11/add-item-admin
Browse files Browse the repository at this point in the history
Add item admin
  • Loading branch information
sitaamirasyarifah authored May 25, 2024
2 parents c436d9d + 7bac038 commit 694b176
Show file tree
Hide file tree
Showing 13 changed files with 503 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@EnableAsync

@SpringBootApplication
public class SnackscriptionSubscriptionboxApplication {
Expand All @@ -10,4 +17,15 @@ public static void main(String[] args) {
SpringApplication.run(SnackscriptionSubscriptionboxApplication.class, args);
}

@Bean
public Executor taskExecutor () {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(1);
executor.setMaxPoolSize(1);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("GithubLookup-");
executor.initialize();
return executor;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

public interface Factory <T> {
T create();

T create(String id, String name, String type, int price, List<Item> items , String description);
T create(String id, String name, int quantity);
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,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;
}
}



Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package id.ac.ui.cs.advprog.snackscription_subscriptionbox.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -20,15 +19,13 @@ public class Item {
private int quantity;

@ManyToMany(mappedBy = "items")
@JsonBackReference
private List<SubscriptionBox> subscriptionBoxes;

public Item() {}

public Item(String id, String name, int quantity) {
this.id = id;
this.name = name;
this.quantity = quantity;
}

public Item() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public Item editItem(Item item) {
}
return null;
}

public List<Item> getAllItems() {
return new ArrayList<>(items);
}

}


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();

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

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

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

Loading

0 comments on commit 694b176

Please sign in to comment.