update api endpoint config and updated related tests (#26)

This commit is contained in:
cato
2022-06-14 19:33:28 +02:00
committed by GitHub
parent 7bfdf7a3e0
commit ebfd1b78e5
9 changed files with 99 additions and 158 deletions

View File

@@ -12,7 +12,6 @@ class RepositoryConfig implements RepositoryRestConfigurer {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
config.exposeIdsFor(Item.class);
config.setBasePath("/api/v1");
}
}

View File

@@ -1,57 +0,0 @@
package whattocook.controller;
import whattocook.exception.ItemNotFoundException;
import lombok.extern.slf4j.Slf4j;
import whattocook.models.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import whattocook.services.ItemService;
import java.util.List;
@Slf4j
@RestController()
public class ItemController {
@Autowired
private ItemService itemService;
@GetMapping("/item")
public Item getItem(@RequestParam String name){
return itemService.findByName(name).orElseThrow(() -> new ItemNotFoundException("item " + name + " not found"));
}
@GetMapping("/items")
public List<Item> getItemList() {
return itemService.findAll();
}
@GetMapping("/items/{itemId}")
public Item getItem(@PathVariable(value = "itemId") Long itemId) {
return itemService.findById(itemId).orElseThrow(() -> new ItemNotFoundException("itemId " + itemId + " not found"));
}
@PostMapping("/items")
public Item createItem(@RequestBody Item item) {
return itemService.save(item);
}
@PutMapping("/items/{itemId}")
public void updateItem(@PathVariable(value = "itemId") Long itemId, @RequestBody Item item) {
itemService.findById(itemId).map(i -> {
i.setName(item.getName());
i.setQuantity(item.getQuantity());
i.setUnit(item.getUnit());
itemService.save(i);
return "Item updated";
}).orElseThrow(() -> new ItemNotFoundException("itemId " + itemId + " not found"));
}
@DeleteMapping("/items/{itemId}")
public void deleteItem(@PathVariable(value = "itemId") Long itemId) {
itemService.findById(itemId).map(p -> {
itemService.deleteById(itemId);
return "Item deleted";
}).orElseThrow(() -> new ItemNotFoundException("itemId " + itemId + " not found"));
}
}

View File

@@ -6,7 +6,6 @@ import org.springframework.stereotype.Service;
import whattocook.repositories.ItemRepository;
import whattocook.services.ItemService;
import java.util.List;
import java.util.Optional;
@Service
@@ -36,7 +35,7 @@ public class ItemServiceImpl implements ItemService {
}
@Override
public List<Item> findAll() {
public Iterable<Item> findAll() {
return itemRepository.findAll();
}
}

View File

@@ -1,18 +1,22 @@
package whattocook.models;
import lombok.*;
import org.hibernate.Hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Objects;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Getter
@Setter
@ToString
@Entity
public class Item {
@Setter(AccessLevel.NONE)
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@@ -22,4 +26,27 @@ public class Item {
private Unit unit;
private int quantity;
public Item(String name, Unit unit, int quantity){
this.name = name;
this.unit = unit;
this.quantity = quantity;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) {
return false;
}
Item item = (Item) o;
return id != null && Objects.equals(id, item.id);
}
@Override
public int hashCode() {
return getClass().hashCode();
}
}

View File

@@ -1,12 +1,12 @@
package whattocook.repositories;
import org.springframework.data.repository.CrudRepository;
import whattocook.models.Item;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {
public interface ItemRepository extends CrudRepository<Item, Long> {
Optional<Item> findByName(String name);
}

View File

@@ -2,7 +2,6 @@ package whattocook.services;
import whattocook.models.Item;
import java.util.List;
import java.util.Optional;
public interface ItemService {
@@ -10,5 +9,5 @@ public interface ItemService {
void deleteById(Long id);
Optional<Item> findById(long id);
Optional<Item> findByName(String name);
List<Item> findAll();
Iterable<Item> findAll();
}