Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@
<artifactId>groovy</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.15.0.RELEASE</version>
</dependency>
</dependencies>

<build>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/tacos/data/IngredientRepository.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package tacos.data;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;

import tacos.Ingredient;

public interface IngredientRepository extends CrudRepository<Ingredient, String> {
public interface IngredientRepository extends PagingAndSortingRepository<Ingredient, String> {

}
5 changes: 5 additions & 0 deletions src/main/java/tacos/data/OrderRepository.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package tacos.data;

import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;

import tacos.Order;
import tacos.User;

public interface OrderRepository extends CrudRepository<Order, Long> {

List<Order> findByUserOrderByPlacedAtDesc(User user, Pageable pageable);
}
3 changes: 2 additions & 1 deletion src/main/java/tacos/data/TacoRepository.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package tacos.data;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;

import tacos.Taco;

public interface TacoRepository extends CrudRepository<Taco, Long> {
public interface TacoRepository extends PagingAndSortingRepository<Taco, Long> {

}
94 changes: 0 additions & 94 deletions src/main/java/tacos/web/DesignTacoController.java

This file was deleted.

22 changes: 22 additions & 0 deletions src/main/java/tacos/web/OrderProps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tacos.web;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import org.springframework.boot.context.properties.
ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import lombok.Data;

@Component
@ConfigurationProperties(prefix="taco.orders")
@Data
@Validated
public class OrderProps {

@Min(value=5, message="must be between 5 and 25")
@Max(value=25, message="must be between 5 and 25")
private int pageSize = 20;

}
1 change: 1 addition & 0 deletions src/main/java/tacos/web/WebConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tacos.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/tacos/web/api/DesignTacoController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package tacos.web.api;

import java.util.Optional;

import tacos.Taco;
import tacos.data.TacoRepository;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "/design", produces = "application/json")
@CrossOrigin(origins = "*")
public class DesignTacoController {
private final TacoRepository tacoRepo;

public DesignTacoController(TacoRepository tacoRepo) {
this.tacoRepo = tacoRepo;
}

@GetMapping("/recent")
public Iterable<Taco> recentTacos() {
PageRequest page = PageRequest.of(0, 12, Sort.by("createdAt")
.descending());
return tacoRepo.findAll(page)
.getContent();
}

@GetMapping("/{id}")
public ResponseEntity<Taco> tacoById(@PathVariable("id") Long id) {
Optional<Taco> optTaco = tacoRepo.findById(id);
return optTaco.map(taco -> new ResponseEntity<>(taco, HttpStatus.OK))
.orElseGet(() -> new ResponseEntity<>(null, HttpStatus.NOT_FOUND));
}

@PostMapping(consumes = "application/json")
@ResponseStatus(HttpStatus.CREATED)
public Taco postTaco(@RequestBody Taco taco) {
return tacoRepo.save(taco);
}
}
28 changes: 28 additions & 0 deletions src/main/java/tacos/web/api/IngredientController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tacos.web.api;

import tacos.Ingredient;
import tacos.data.IngredientRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "/ingredientsx", produces = "application/json")
@CrossOrigin(origins = "*")
public class IngredientController {

private IngredientRepository repo;

@Autowired
public IngredientController(IngredientRepository repo) {
this.repo = repo;
}

@GetMapping
public Iterable<Ingredient> allIngredients() {
return repo.findAll();
}
}
21 changes: 21 additions & 0 deletions src/main/java/tacos/web/api/IngredientResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tacos.web.api;

import lombok.Getter;
import tacos.Ingredient;
import tacos.Ingredient.Type;

import org.springframework.hateoas.ResourceSupport;

public class IngredientResource extends ResourceSupport {

@Getter
private String name;

@Getter
private Type type;

public IngredientResource(Ingredient ingredient) {
this.name = ingredient.getName();
this.type = ingredient.getType();
}
}
22 changes: 22 additions & 0 deletions src/main/java/tacos/web/api/IngredientResourceAssembler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tacos.web.api;

import org.springframework.hateoas.mvc.ResourceAssemblerSupport;

import tacos.Ingredient;

class IngredientResourceAssembler extends ResourceAssemblerSupport<Ingredient, IngredientResource> {

public IngredientResourceAssembler() {
super(IngredientController.class, IngredientResource.class);
}

@Override
public IngredientResource toResource(Ingredient ingredient) {
return createResourceWithId(ingredient.getId(), ingredient);
}

@Override
protected IngredientResource instantiateResource(Ingredient ingredient) {
return new IngredientResource(ingredient);
}
}
Loading