Proof of concept (#6)

* Sample template created

* added findByName functionality for item

* Solve Cors errors and inhibit DefaultExposure

* changed project structure

* Added frontend

* Creation of base template (#1)

* changed base path of REST api and updated frontend api quering
This commit is contained in:
cato
2022-06-02 19:11:46 +02:00
committed by GitHub
parent edaf3c557e
commit f6385b40f6
32 changed files with 7031 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
package whattocook;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.Collections;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
// Fix the CORS errors
@Bean
public FilterRegistrationBean<CorsFilter> simpleCorsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
// *** URL below needs to match the Vue client URL and port ***
config.setAllowedOrigins(Collections.singletonList("http://localhost:8080"));
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowedHeaders(Collections.singletonList("*"));
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
}

View File

@@ -0,0 +1,19 @@
package whattocook.configs;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import whattocook.models.Item;
@Configuration
class RepositoryConfig implements RepositoryRestConfigurer {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
config.exposeIdsFor(Item.class);
config.setBasePath("/api/v1");
}
}

View File

@@ -0,0 +1,57 @@
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) {
itemService.save(item);
return item;
}
@PutMapping("/items/{itemId}")
public String updateItem(@PathVariable(value = "itemId") Long itemId, @RequestBody Item item) {
return 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 String deleteItem(@PathVariable(value = "itemId") Long itemId) {
return itemService.findById(itemId).map(p -> {
itemService.deleteById(itemId);
return "Item deleted";
}).orElseThrow(() -> new ItemNotFoundException("itemId " + itemId + " not found"));
}
}

View File

@@ -0,0 +1,23 @@
package whattocook.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ItemNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ItemNotFoundException(){
super();
}
public ItemNotFoundException(String message) {
super(message);
}
public ItemNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,42 @@
package whattocook.implementation;
import whattocook.models.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import whattocook.repositories.ItemRepository;
import whattocook.services.ItemService;
import java.util.List;
import java.util.Optional;
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemRepository itemRepository;
@Override
public Item save(Item item) {
return itemRepository.save(item);
}
@Override
public void deleteById(Long id) {
itemRepository.deleteById(id);
}
@Override
public Optional<Item> findById(long id) {
return itemRepository.findById(id);
}
@Override
public Optional<Item> findByName(String name) {
return itemRepository.findByName(name);
}
@Override
public List<Item> findAll() {
return itemRepository.findAll();
}
}

View File

@@ -0,0 +1,25 @@
package whattocook.models;
import lombok.*;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
public class Item {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NonNull
private String name;
private Unit unit;
private int quantity;
}

View File

@@ -0,0 +1,6 @@
package whattocook.models;
public enum Unit {
GRAMMS,
MILLILETERS
}

View File

@@ -0,0 +1,12 @@
package whattocook.repositories;
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> {
Optional<Item> findByName(String name);
}

View File

@@ -0,0 +1,14 @@
package whattocook.services;
import whattocook.models.Item;
import java.util.List;
import java.util.Optional;
public interface ItemService {
Item save(Item item);
void deleteById(Long id);
Optional<Item> findById(long id);
Optional<Item> findByName(String name);
List<Item> findAll();
}

View File

@@ -0,0 +1,13 @@
server:
port: 9000
spring:
datasource:
url: jdbc:h2:mem:whattocook
username: sa
password:
driverClassName: org.h2.Driver
logging:
level:
root: DEBUG