Commit 90701776 authored by Alex Pinto's avatar Alex Pinto

Merge branch 'master' into backend2

parents b909eba1 feb78f4d
...@@ -4,9 +4,9 @@ export const RECEIVE_ORDERS = 'RECEIVE_ORDERS'; ...@@ -4,9 +4,9 @@ export const RECEIVE_ORDERS = 'RECEIVE_ORDERS';
export const RECEIVE_ORDER = 'RECEIVE_ORDER'; export const RECEIVE_ORDER = 'RECEIVE_ORDER';
export const UPDATE_ORDER = 'UPDATE_ORDER'; export const UPDATE_ORDER = 'UPDATE_ORDER';
const receiveOrders = (payload) => ({ const receiveOrders = (orders) => ({
type: RECEIVE_ORDERS, type: RECEIVE_ORDERS,
payload, orders,
}) })
const receiveOrder = (order) => ({ const receiveOrder = (order) => ({
...@@ -32,8 +32,9 @@ export const createOrder = (order) => (dispatch) => ...@@ -32,8 +32,9 @@ export const createOrder = (order) => (dispatch) =>
dispatch(receiveOrder(res)) dispatch(receiveOrder(res))
}); });
export const editOrder = (order) => (dispatch) => export const editOrder = (order) => (dispatch) => {
OrderAPIUtil.editOrder(order) OrderAPIUtil.editOrder(order)
.then(res => { .then(res => {
dispatch(updateOrder(res)) dispatch(updateOrder(res))
}); });
}
\ No newline at end of file
import React from "react";
import { connect } from "react-redux";
import { editOrder } from "../../actions/order_actions";
const OrderButtons = ({ order, editOrder }) => {
// const { id, orderId, status, orderItems, createdAt, modifiedAt } = order;
const handleUpdate = (action) => {
console.log(action);
if (action === "FULFILL") {
editOrder({ ...order, status: "FULFILLED"})
} else if (action === "CANCEL") {
editOrder({ ...order, status: "CANCELLED" });
}
}
const fulfill = (
<button onClick={() => handleUpdate("FULFILL")}>Fulfill</button>
)
const cancel = (
<button onClick={() => handleUpdate("CANCEL")}>Cancel</button>
)
return (
<div>
{fulfill} {cancel}
</div>
);
};
const mapStateToProps = (state) => ({});
const mapDispatchToProps = (dispatch) => ({
editOrder: (order) => dispatch(editOrder(order)),
});
export default connect(mapStateToProps, mapDispatchToProps)(OrderButtons);
...@@ -23,17 +23,16 @@ const OrderIndex = ({ ...@@ -23,17 +23,16 @@ const OrderIndex = ({
<h1>Order Index</h1> <h1>Order Index</h1>
<button onClick={createOrder}>Create New Order</button> <button onClick={createOrder}>Create New Order</button>
<button onClick={editOrder}>Update Order</button> <button onClick={editOrder}>Update Order</button>
{orders.allIds.map(orderId => {
<br />
<br />
{orders.allIds.map((orderId) => {
const order = orders.byId[orderId]; const order = orders.byId[orderId];
return ( return <OrderIndexItem key={order.id} order={order} />;
<OrderIndexItem
key={order.id}
order={order}
/>
)
})} })}
</div> </div>
) );
} }
......
import React from 'react'; import React from 'react';
import OrderButtons from './OrderButtons';
const OrderIndexItem = ({ order }) => { const OrderIndexItem = ({ order }) => {
const {
id,
orderId,
status,
orderItems,
createdAt,
modifiedAt
} = order;
const actions = ( status === "RECEIVED" ?
<OrderButtons order={order} />
:
<div>{status}</div>)
return ( return (
<div> <div>
{order.orderId} {order.status} <div>{`Order #: ${orderId}`}</div>
{actions}
</div> </div>
) )
}; };
......
...@@ -16,10 +16,18 @@ const OrdersReducer = (oldState = initialState, action) => { ...@@ -16,10 +16,18 @@ const OrdersReducer = (oldState = initialState, action) => {
switch (action.type) { switch (action.type) {
case RECEIVE_ORDERS: case RECEIVE_ORDERS:
return action.payload; const orderState = {
byId: {},
allIds: [],
};
Object.keys(action.orders).forEach( wareId => {
orderState.allIds.push(wareId);
orderState.byId[wareId] = action.orders[wareId];
})
return orderState;
case RECEIVE_ORDER: case RECEIVE_ORDER:
newState.byId[order.id] = order; newState.byId[order.id] = order;
newState.allIds.unshift(order.id); newState.allIds.push(order.id);
return newState; return newState;
case UPDATE_ORDER: case UPDATE_ORDER:
newState.byId[order.id] = order; newState.byId[order.id] = order;
......
// import axios from 'axios'; // import axios from 'axios';
const RECEIVED = "RECEIVED";
// const FULFILLED = "FULFILLED";
// const CANCELLED = "CANCELLED";
const sampleGetAll = { const sampleGetAll = {
allIds: ["1", "2", "3"], // allIds: ["1", "2", "3"],
byId: { // byId: {
"1": { 1: {
id: "1", id: "1",
orderId: "o1", orderId: "o1",
status: "unfulfilled", status: RECEIVED,
orderItems: [
{
itemId: "17",
itemName: "Item 1",
itemQuantity: 4,
itemPrice: 17.99,
itemSku: 8765309,
},
{
itemId: "18",
itemName: "Item 2",
itemQuantity: 42,
itemPrice: 17.99,
itemSku: 8715309,
}, },
"2": { ],
},
2: {
id: "2", id: "2",
orderId: "o2", orderId: "o2",
status: "unfulfilled", status: RECEIVED,
orderItems: [
{
itemId: "17",
itemName: "Item 1",
itemQuantity: 3,
itemPrice: 17.99,
itemSku: 8765309,
},
{
itemId: "18",
itemName: "Item 2",
itemQuantity: 41,
itemPrice: 17.99,
itemSku: 8715309,
}, },
"3": { ],
},
3: {
id: "3", id: "3",
orderId: "o3", orderId: "o3",
status: "unfulfilled", status: RECEIVED,
orderItems: [
{
itemId: "17",
itemName: "Item 1",
itemQuantity: 2,
itemPrice: 17.99,
itemSku: 8765309,
},
{
itemId: "18",
itemName: "Item 2",
itemQuantity: 40,
itemPrice: 17.99,
itemSku: 8715309,
}, },
],
}, },
// },
}; };
const sampleNew = { const sampleNew = {
id: "4", id: "4",
orderId: "o4", orderId: "o4",
status: "unfulfilled", status: RECEIVED,
};
const sampleUpdateFul = {
id: "3",
orderId: "o3",
status: "fulfilled",
};
const sampleUpdateCan = {
id: "2",
orderId: "o2",
status: "cancelled",
}; };
const getAllPromise = new Promise( (resolve, reject) => { const getAllPromise = new Promise( (resolve, reject) => {
...@@ -45,12 +85,6 @@ const getAllPromise = new Promise( (resolve, reject) => { ...@@ -45,12 +85,6 @@ const getAllPromise = new Promise( (resolve, reject) => {
const createNewPromise = new Promise( (resolve, reject) => { const createNewPromise = new Promise( (resolve, reject) => {
resolve(sampleNew) resolve(sampleNew)
}) })
const updateFulfillPromise = new Promise( (resolve, reject) => {
resolve(sampleUpdateFul)
})
const updateCancelPromise = new Promise( (resolve, reject) => {
resolve(sampleUpdateCan)
})
export const getOrders = () => { export const getOrders = () => {
return getAllPromise; return getAllPromise;
...@@ -61,7 +95,9 @@ export const createOrder = (order) => { ...@@ -61,7 +95,9 @@ export const createOrder = (order) => {
} }
export const editOrder = (order) => { export const editOrder = (order) => {
return updateFulfillPromise; return new Promise( (resolve, reject) => {
resolve(order);
})
} }
// export const getOrders =() => { // export const getOrders =() => {
......
...@@ -29,6 +29,10 @@ ...@@ -29,6 +29,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId> <artifactId>spring-boot-starter-webflux</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.apache.kafka</groupId> <groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId> <artifactId>kafka-streams</artifactId>
......
package com.ascendfinalproject.warehouse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service
public class Consumer {
private final Logger logger = LoggerFactory.getLogger(Consumer.class);
// this is placeholder
@KafkaListener(topics = "fulfilled", groupId = "WAREHOUSE_MANAGEMENT")
public void consume(String message) throws IOException {
logger.info(String.format("#### -> Consumed message -> %s", message));
}
}
package com.ascendfinalproject.warehouse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
@Service
public class Producer {
private static final Logger logger = LoggerFactory.getLogger(Producer.class);
private static final String FULFILLED = "fulfilled";
private static final String CANCELLED = "cancelled";
@Autowired
// publish messages to the topic
private KafkaTemplate<String, String> kafkaTemplate;
public void orderFulfilled(String message) {
logger.info(String.format("#### -> this order is fulfilled -> %s", message));
this.kafkaTemplate.send(FULFILLED, message);
}
public void orderCancelled(String message) {
logger.info(String.format("#### -> this order is cancelled -> %s", message));
this.kafkaTemplate.send(CANCELLED, message);
}
}
...@@ -11,3 +11,4 @@ public class WarehouseApplication { ...@@ -11,3 +11,4 @@ public class WarehouseApplication {
} }
} }
package com.ascendfinalproject.warehouse.controllers;
import com.ascendfinalproject.warehouse.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/kafka")
public class KafkaController {
private final Producer producer;
@Autowired
KafkaController(Producer producer) {
this.producer = producer;
}
@PostMapping(value = "/fulfilled")
public void sendMessageToKafkaTopic(@RequestParam("message") String message) {
this.producer.orderFulfilled(message);
}
}
spring.data.mongodb.uri=mongodb+srv://warehouse1:ascendWarehouseProject@warehouse-cluster.xopll.mongodb.net/myFirstDatabase?retryWrites=true&w=majority spring.data.mongodb.uri=mongodb+srv://warehouse1:ascendWarehouseProject@warehouse-cluster.xopll.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
spring.data.mongodb.database=test spring.data.mongodb.database=test
server:
port: 9000
spring:
kafka:
consumer:
bootstrap-servers: localhost:9092
group-id: WAREHOUSE_MANAGEMENT
auto-offset-reset: earliest
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
producer:
bootstrap-servers: localhost:9092
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment