Skip to content

Commit 734cb3b

Browse files
Merge pull request #55 from AnswerConsulting/BENCH-161
BENCH-161 Refactored codebase
2 parents bdbc46f + 296a011 commit 734cb3b

30 files changed

+626
-622
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Answer King REST API
22

3-
The Answer King REST API is the backend for a fast food business. It allows items and categories to be created and
4-
orders to be placed. An item can be included in multiple categories.
3+
The Answer King REST API is the backend for a fast food business. It allows products and categories to be created and
4+
orders to be placed. A product can be included in multiple categories.
55

66
## Docker
77

@@ -26,7 +26,7 @@ and execute the following commands:
2626

2727
[Docker Compose](https://docs.docker.com/compose/install/)
2828

29-
[AMAZON CORRETTO 17](https://docs.aws.amazon.com/corretto/latest/corretto-17-ug/downloads-list.html/)
29+
[AMAZON CORRETTO 17](https://docs.aws.amazon.com/corretto/latest/corretto-17-ug/downloads-list.html)
3030

3131
## Maven
3232

@@ -52,7 +52,7 @@ If Maven isn't installed on your system, the Maven Wrapper can be used by using
5252

5353
### Prerequisites
5454

55-
[AMAZON CORRETTO 17](https://docs.aws.amazon.com/corretto/latest/corretto-17-ug/downloads-list.html/)
55+
[AMAZON CORRETTO 17](https://docs.aws.amazon.com/corretto/latest/corretto-17-ug/downloads-list.html)
5656

5757
## MySQL
5858

init_scripts/schema.sql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE TABLE IF NOT EXISTS item (
1+
CREATE TABLE IF NOT EXISTS product (
22
id BIGINT NOT NULL AUTO_INCREMENT,
33
name VARCHAR(255) NOT NULL,
44
description VARCHAR(255) NOT NULL,
@@ -16,13 +16,13 @@ CREATE TABLE IF NOT EXISTS category (
1616
PRIMARY KEY (id)
1717
);
1818

19-
CREATE TABLE IF NOT EXISTS item_category (
19+
CREATE TABLE IF NOT EXISTS product_category (
2020
id BIGINT NOT NULL AUTO_INCREMENT,
21-
item_id BIGINT NOT NULL,
21+
product_id BIGINT NOT NULL,
2222
category_id BIGINT NOT NULL,
2323

2424
PRIMARY KEY (id),
25-
FOREIGN KEY (item_id) REFERENCES item(id),
25+
FOREIGN KEY (product_id) REFERENCES product(id),
2626
FOREIGN KEY (category_id) REFERENCES category(id)
2727
);
2828

@@ -34,13 +34,13 @@ CREATE TABLE IF NOT EXISTS `order` (
3434
PRIMARY KEY (id)
3535
);
3636

37-
CREATE TABLE IF NOT EXISTS order_item (
37+
CREATE TABLE IF NOT EXISTS order_product (
3838
id BIGINT NOT NULL AUTO_INCREMENT,
3939
order_id BIGINT NOT NULL,
40-
item_id BIGINT NOT NULL,
40+
product_id BIGINT NOT NULL,
4141
quantity INT NOT NULL,
4242

4343
PRIMARY KEY (id),
4444
FOREIGN KEY (order_id) REFERENCES `order`(id),
45-
FOREIGN KEY (item_id) REFERENCES item(id)
45+
FOREIGN KEY (product_id) REFERENCES product(id)
4646
);

src/main/java/com/answerdigital/academy/answerking/controller/CategoryController.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ public ResponseEntity<Collection<Category>> getAllCategories() {
4747
return new ResponseEntity<>(categories, categories.isEmpty() ? HttpStatus.NO_CONTENT : HttpStatus.OK);
4848
}
4949

50-
@PutMapping("/{categoryId}/additem/{itemId}")
51-
public ResponseEntity<Category> addItemToCategory(@PathVariable @NotNull final Long categoryId,
52-
@PathVariable @NotNull final Long itemId) {
53-
return new ResponseEntity<>(categoryService.addItemToCategory(categoryId, itemId), HttpStatus.OK);
50+
@PutMapping("/{categoryId}/addproduct/{productId}")
51+
public ResponseEntity<Category> addProductToCategory(@PathVariable @NotNull final Long categoryId,
52+
@PathVariable @NotNull final Long productId) {
53+
return new ResponseEntity<>(categoryService.addProductToCategory(categoryId, productId), HttpStatus.OK);
5454
}
5555

56-
@PutMapping("/{categoryId}/removeitem/{itemId}")
57-
public ResponseEntity<Category> removeItemFromCategory(@PathVariable @NotNull final Long categoryId,
58-
@PathVariable @NotNull final Long itemId) {
59-
return new ResponseEntity<>(categoryService.removeItemFromCategory(categoryId, itemId), HttpStatus.OK);
56+
@PutMapping("/{categoryId}/removeproduct/{productId}")
57+
public ResponseEntity<Category> removeProductFromCategory(@PathVariable @NotNull final Long categoryId,
58+
@PathVariable @NotNull final Long productId) {
59+
return new ResponseEntity<>(categoryService.removeProductFromCategory(categoryId, productId), HttpStatus.OK);
6060
}
6161

6262
@PutMapping("/{categoryId}")

src/main/java/com/answerdigital/academy/answerking/controller/ItemController.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/main/java/com/answerdigital/academy/answerking/controller/OrderController.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,23 @@ public ResponseEntity<List<Order>> getAllOrders() {
5656
return new ResponseEntity<>(foundOrders, foundOrders.isEmpty() ? HttpStatus.NO_CONTENT : HttpStatus.OK);
5757
}
5858

59-
@PostMapping(path = "/{orderId}/item/{itemId}/quantity/{quantity}")
60-
public ResponseEntity<Order> addItemToBasket(@PathVariable @NotNull final Long orderId,
61-
@PathVariable @NotNull final Long itemId,
62-
@PathVariable @NotNull final Integer quantity) {
63-
return new ResponseEntity<>(orderService.addItemToBasket(orderId, itemId, quantity), HttpStatus.OK);
59+
@PostMapping(path = "/{orderId}/product/{productId}/quantity/{quantity}")
60+
public ResponseEntity<Order> addProductToBasket(@PathVariable @NotNull final Long orderId,
61+
@PathVariable @NotNull final Long productId,
62+
@PathVariable @NotNull final Integer quantity) {
63+
return new ResponseEntity<>(orderService.addProductToBasket(orderId, productId, quantity), HttpStatus.OK);
6464
}
6565

66-
@DeleteMapping(path = "/{orderId}/item/{itemId}")
67-
public ResponseEntity<Order> deleteItemInBasket(@PathVariable @NotNull final Long orderId,
68-
@PathVariable @NotNull final Long itemId) {
69-
return new ResponseEntity<>(orderService.deleteItemInBasket(orderId, itemId), HttpStatus.OK);
66+
@DeleteMapping(path = "/{orderId}/product/{productId}")
67+
public ResponseEntity<Order> deleteProductInBasket(@PathVariable @NotNull final Long orderId,
68+
@PathVariable @NotNull final Long productId) {
69+
return new ResponseEntity<>(orderService.deleteProductInBasket(orderId, productId), HttpStatus.OK);
7070
}
7171

72-
@PutMapping(path = "/{orderId}/item/{itemId}/quantity/{quantity}")
73-
public ResponseEntity<Order> updateItemQuantity(@PathVariable @NotNull final Long orderId,
74-
@PathVariable @NotNull final Long itemId,
75-
@PathVariable @NotNull final Integer quantity) {
76-
return new ResponseEntity<>(orderService.updateItemQuantity(orderId, itemId, quantity), HttpStatus.OK);
72+
@PutMapping(path = "/{orderId}/product/{productId}/quantity/{quantity}")
73+
public ResponseEntity<Order> updateProductQuantity(@PathVariable @NotNull final Long orderId,
74+
@PathVariable @NotNull final Long productId,
75+
@PathVariable @NotNull final Integer quantity) {
76+
return new ResponseEntity<>(orderService.updateProductQuantity(orderId, productId, quantity), HttpStatus.OK);
7777
}
7878
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.answerdigital.academy.answerking.controller;
2+
3+
import com.answerdigital.academy.answerking.model.Product;
4+
import com.answerdigital.academy.answerking.request.ProductRequest;
5+
import com.answerdigital.academy.answerking.service.ProductService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.validation.Errors;
10+
import org.springframework.validation.annotation.Validated;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
import org.springframework.web.bind.annotation.PutMapping;
14+
import org.springframework.web.bind.annotation.PathVariable;
15+
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
import javax.validation.Valid;
20+
import javax.validation.constraints.NotNull;
21+
import java.util.List;
22+
23+
@Validated
24+
@RestController
25+
@RequestMapping(path = "/products")
26+
public class ProductController {
27+
private final ProductService productService;
28+
29+
@Autowired
30+
public ProductController(final ProductService productService) {
31+
this.productService = productService;
32+
}
33+
34+
@GetMapping
35+
public ResponseEntity<List<Product>> getAllProducts() {
36+
final List<Product> products = productService.findAll();
37+
return new ResponseEntity<>(products, products.isEmpty() ? HttpStatus.NO_CONTENT : HttpStatus.OK);
38+
}
39+
40+
@GetMapping(path = "/{id}")
41+
public ResponseEntity<Product> getProductById(@Valid @PathVariable @NotNull final Long id) {
42+
return new ResponseEntity<>(productService.findById(id), HttpStatus.OK);
43+
}
44+
45+
@PostMapping
46+
public ResponseEntity<Product> addProduct(@Valid @RequestBody final ProductRequest productRequest, final Errors errors) {
47+
return new ResponseEntity<>(productService.addNewProduct(productRequest),
48+
errors.hasErrors() ? HttpStatus.BAD_REQUEST : HttpStatus.OK);
49+
}
50+
51+
@PutMapping("/{id}")
52+
public ResponseEntity<Product> updateProduct(@PathVariable @NotNull final long id,
53+
@Valid @RequestBody final ProductRequest productRequest,
54+
final Errors errors) {
55+
return new ResponseEntity<>(productService.updateProduct(id, productRequest),
56+
errors.hasErrors() ? HttpStatus.BAD_REQUEST : HttpStatus.OK);
57+
}
58+
}

src/main/java/com/answerdigital/academy/answerking/exception/custom/ItemUnavailableException.java renamed to src/main/java/com/answerdigital/academy/answerking/exception/custom/ProductUnavailableException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
import java.util.Collection;
77
import java.util.List;
88

9-
public class ItemUnavailableException extends AnswerKingException {
9+
public class ProductUnavailableException extends AnswerKingException {
1010

1111
private static final String TYPE = "https://www.rfc-editor.org/rfc/rfc7231";
1212

13-
private static final String TITLE = "Item Unavailable Exception";
13+
private static final String TITLE = "Product Unavailable Exception";
1414

1515
private static final HttpStatus STATUS = HttpStatus.BAD_REQUEST;
1616

1717
private static final String DETAIL = null;
1818

19-
public ItemUnavailableException(final Collection<String> errors) {
19+
public ProductUnavailableException(final Collection<String> errors) {
2020
super(TYPE, TITLE, STATUS, DETAIL, errors);
2121
}
2222

23-
public ItemUnavailableException(final String error) {
23+
public ProductUnavailableException(final String error) {
2424
this(List.of(error));
2525
}
2626
}

src/main/java/com/answerdigital/academy/answerking/mapper/ItemMapper.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/main/java/com/answerdigital/academy/answerking/mapper/OrderMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public interface OrderMapper {
1212

1313
@Mapping(target = "orderStatus", constant = "IN_PROGRESS")
14-
@Mapping(target = "orderItems", expression = "java(Collections.EMPTY_SET)")
14+
@Mapping(target = "lineItems", expression = "java(Collections.EMPTY_SET)")
1515
Order addRequestToOrder(OrderRequest orderRequest);
1616

1717
Order updateOrderRequest(@MappingTarget Order order, OrderRequest orderRequest);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.answerdigital.academy.answerking.mapper;
2+
3+
import com.answerdigital.academy.answerking.model.Product;
4+
import com.answerdigital.academy.answerking.request.ProductRequest;
5+
import org.mapstruct.Mapper;
6+
import org.mapstruct.MappingTarget;
7+
8+
@Mapper(componentModel = "spring")
9+
public interface ProductMapper {
10+
Product addRequestToProduct(ProductRequest productRequest);
11+
12+
Product updateRequestToProduct(@MappingTarget Product product, ProductRequest productRequest);
13+
}

0 commit comments

Comments
 (0)