Creation of base template (#1)
* Sample template created * added findByName functionality for item * Solve Cors errors and inhibit DefaultExposure * changed project structure * Added frontend
This commit is contained in:
37
backend/src/main/whattocook/Application.java
Normal file
37
backend/src/main/whattocook/Application.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package whattocook.configs;
|
||||
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
@Component
|
||||
public class RestRepositoryConfigurator implements RepositoryRestConfigurer {
|
||||
|
||||
|
||||
@Override
|
||||
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
|
||||
config.disableDefaultExposure();
|
||||
RepositoryRestConfigurer.super.configureRepositoryRestConfiguration(config, cors);
|
||||
}
|
||||
}
|
||||
|
||||
60
backend/src/main/whattocook/controller/ItemController.java
Normal file
60
backend/src/main/whattocook/controller/ItemController.java
Normal file
@@ -0,0 +1,60 @@
|
||||
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.models.Unit;
|
||||
import whattocook.services.ItemService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@RestController()
|
||||
@RequestMapping("/api/v1")
|
||||
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"));
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
25
backend/src/main/whattocook/models/Item.java
Normal file
25
backend/src/main/whattocook/models/Item.java
Normal 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;
|
||||
}
|
||||
6
backend/src/main/whattocook/models/Unit.java
Normal file
6
backend/src/main/whattocook/models/Unit.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package whattocook.models;
|
||||
|
||||
public enum Unit {
|
||||
GRAMMS,
|
||||
MILLILETERS
|
||||
}
|
||||
12
backend/src/main/whattocook/repositories/ItemRepository.java
Normal file
12
backend/src/main/whattocook/repositories/ItemRepository.java
Normal 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);
|
||||
}
|
||||
14
backend/src/main/whattocook/services/ItemService.java
Normal file
14
backend/src/main/whattocook/services/ItemService.java
Normal 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();
|
||||
}
|
||||
13
backend/src/resources/application.yml
Normal file
13
backend/src/resources/application.yml
Normal 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
|
||||
Reference in New Issue
Block a user