Skip to content

Commit

Permalink
Zinger framwework version-1.0 completed
Browse files Browse the repository at this point in the history
  • Loading branch information
ddlogesh committed May 9, 2020
1 parent 8a7e6b2 commit f585ed0
Show file tree
Hide file tree
Showing 25 changed files with 150 additions and 189 deletions.
167 changes: 83 additions & 84 deletions sql/DB_INIT.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ CREATE TABLE users
name VARCHAR(32) DEFAULT NULL,
email VARCHAR(64) DEFAULT NULL,
oauth_id VARCHAR(64) UNIQUE NOT NULL,
notif_token JSON DEFAULT NULL,
notif_token JSON DEFAULT NULL,
role ENUM ('CUSTOMER','SELLER','SHOP_OWNER','DELIVERY','SUPER_ADMIN') NOT NULL,
is_delete INT DEFAULT 0,
CONSTRAINT users_id_pk PRIMARY KEY (id)
Expand All @@ -67,28 +67,28 @@ CREATE TABLE item
is_veg INT DEFAULT 1,
is_available INT DEFAULT 1,
is_delete INT DEFAULT 0,
CONSTRAINT item_name_shop_id_pk PRIMARY KEY (name,shop_id),
CONSTRAINT item_name_shop_id_pk PRIMARY KEY (name, shop_id),
CONSTRAINT item_id_uq UNIQUE (id),
CONSTRAINT item_shop_id_fk FOREIGN KEY (shop_id) REFERENCES shop (id)
);

CREATE TABLE orders
(
id INT AUTO_INCREMENT,
user_id INT NOT NULL,
shop_id INT NOT NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
status ENUM ('PENDING', 'TXN_FAILURE', 'PLACED',
id INT AUTO_INCREMENT,
user_id INT NOT NULL,
shop_id INT NOT NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
status ENUM ('PENDING', 'TXN_FAILURE', 'PLACED',
'CANCELLED_BY_USER', 'ACCEPTED', 'CANCELLED_BY_SELLER',
'READY', 'OUT_FOR_DELIVERY', 'COMPLETED',
'DELIVERED', 'REFUND_INITIATED', 'REFUND_COMPLETED') DEFAULT NULL,
price DOUBLE NOT NULL,
delivery_price DOUBLE DEFAULT NULL,
delivery_location VARCHAR(128) DEFAULT NULL,
cooking_info VARCHAR(128) DEFAULT NULL,
rating DOUBLE(2, 1) DEFAULT NULL,
feedback VARCHAR(1024) DEFAULT NULL,
secret_key VARCHAR(10) DEFAULT NULL,
price DOUBLE NOT NULL,
delivery_price DOUBLE DEFAULT NULL,
delivery_location VARCHAR(128) DEFAULT NULL,
cooking_info VARCHAR(128) DEFAULT NULL,
rating DOUBLE(2, 1) DEFAULT NULL,
feedback VARCHAR(1024) DEFAULT NULL,
secret_key VARCHAR(10) DEFAULT NULL,
CONSTRAINT orders_id_pk PRIMARY KEY (id),
CONSTRAINT orders_user_id_fk FOREIGN KEY (user_id) REFERENCES users (id),
CONSTRAINT orders_shop_id_fk FOREIGN KEY (shop_id) REFERENCES shop (id)
Expand Down Expand Up @@ -144,12 +144,12 @@ CREATE TABLE orders_item

create table orders_status
(
order_id INT NOT NULL,
order_id INT NOT NULL,
status ENUM ('PENDING', 'TXN_FAILURE', 'PLACED',
'CANCELLED_BY_USER', 'ACCEPTED', 'CANCELLED_BY_SELLER',
'READY', 'OUT_FOR_DELIVERY', 'COMPLETED',
'DELIVERED', 'REFUND_INITIATED', 'REFUND_COMPLETED') NOT NULL,
updated_time DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_time DATETIME DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT orders_status_order_id_status_pk PRIMARY KEY (order_id, status),
CONSTRAINT orders_status_order_id_fk FOREIGN KEY (order_id) REFERENCES orders (id)
);
Expand Down Expand Up @@ -187,17 +187,15 @@ CREATE TABLE seller_archive
CONSTRAINT seller_archive_shop_id_fk FOREIGN KEY (shop_id) REFERENCES shop (id)
);


create table application_log (
request_type ENUM ('GET', 'POST', 'PUT',
'PATCH', 'DELETE', 'COPY',
'HEAD', 'OPTIONS', 'LINK',
'UNLINK', 'PURGE', 'LOCK','UNLOCK','PROPFIND','VIEW') NOT NULL,
endpoint_url VARCHAR(1024) DEFAULT NULL,
create table application_log
(
request_type ENUM ('GET', 'POST', 'PUT',
'PATCH', 'DELETE') NOT NULL,
endpoint_url VARCHAR(1024) DEFAULT NULL,
request_header LONGTEXT NOT NULL,
request_object LONGTEXT NOT NULL,
response_object LONGTEXT NOT NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
request_object LONGTEXT NOT NULL,
response_object LONGTEXT NOT NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

####################################################
Expand All @@ -221,83 +219,84 @@ CREATE TRIGGER notif_update
BEFORE UPDATE
ON users
FOR EACH ROW
BEGIN
DECLARE actual_notif_token JSON DEFAULT NULL;
DECLARE actual_notif_token_length BIGINT UNSIGNED DEFAULT NULL;

SELECT notif_token
INTO actual_notif_token
FROM users
where id = NEW.id;

IF actual_notif_token IS NULL AND NEW.notif_token IS NOT NULL THEN
SELECT JSON_ARRAY(NEW.notif_token) INTO actual_notif_token;
ELSEIF JSON_CONTAINS(actual_notif_token, NEW.notif_token) = 0 THEN
SET actual_notif_token_length = JSON_LENGTH(actual_notif_token);

IF actual_notif_token_length >= 5 THEN
SELECT JSON_REMOVE(actual_notif_token, '$[0]') INTO actual_notif_token;
END IF;
SELECT JSON_ARRAY_APPEND(actual_notif_token, '$', NEW.notif_token) INTO actual_notif_token;
BEGIN
DECLARE actual_notif_token JSON DEFAULT NULL;
DECLARE actual_notif_token_length BIGINT UNSIGNED DEFAULT NULL;

SELECT notif_token
INTO actual_notif_token
FROM users
where id = NEW.id;

IF actual_notif_token IS NULL AND NEW.notif_token IS NOT NULL THEN
SELECT JSON_ARRAY(NEW.notif_token) INTO actual_notif_token;
ELSEIF JSON_CONTAINS(actual_notif_token, NEW.notif_token) = 0 THEN
SET actual_notif_token_length = JSON_LENGTH(actual_notif_token);

IF actual_notif_token_length >= 5 THEN
SELECT JSON_REMOVE(actual_notif_token, '$[0]') INTO actual_notif_token;
END IF;
SET NEW.notif_token = actual_notif_token;
END;
SELECT JSON_ARRAY_APPEND(actual_notif_token, '$', NEW.notif_token) INTO actual_notif_token;
END IF;
SET NEW.notif_token = actual_notif_token;
END;
$$

DELIMITER $$
CREATE TRIGGER order_time_rating_update
BEFORE UPDATE
ON orders
FOR EACH ROW
BEGIN
IF (NEW.status = 'PLACED') OR (NEW.status = 'PENDING') OR (NEW.status = 'TXN_FAILURE') THEN
SET NEW.date = CURRENT_TIMESTAMP;
END IF;
IF NEW.rating IS NOT NULL THEN
BEGIN
DECLARE actual_status ENUM ('PENDING', 'TXN_FAILURE', 'PLACED',
'CANCELLED_BY_USER', 'ACCEPTED', 'CANCELLED_BY_SELLER',
'READY', 'OUT_FOR_DELIVERY', 'COMPLETED',
'DELIVERED', 'REFUND_INITIATED', 'REFUND_COMPLETED') DEFAULT NULL;

SELECT status
INTO actual_status
FROM orders
where id = NEW.id;

IF (OLD.rating IS NOT NULL) THEN
SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Error: Rating cannot be updated if already done!';
ELSEIF ((actual_status IS NULL) OR ((actual_status != 'COMPLETED') AND
(actual_status != 'DELIVERED') AND
(actual_status != 'CANCELLED_BY_USER') AND
(actual_status != 'CANCELLED_BY_SELLER') AND
(actual_status != 'REFUND_COMPLETED'))) THEN
SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Error: Rating cannot be updated before the order completes!';
END IF;
END;
BEGIN
IF (NEW.status = 'PLACED') OR (NEW.status = 'PENDING') OR (NEW.status = 'TXN_FAILURE') THEN
SET NEW.date = CURRENT_TIMESTAMP;
END IF;
IF NEW.rating IS NOT NULL THEN
BEGIN
DECLARE actual_status ENUM ('PENDING', 'TXN_FAILURE', 'PLACED',
'CANCELLED_BY_USER', 'ACCEPTED', 'CANCELLED_BY_SELLER',
'READY', 'OUT_FOR_DELIVERY', 'COMPLETED',
'DELIVERED', 'REFUND_INITIATED', 'REFUND_COMPLETED') DEFAULT NULL;

SELECT status
INTO actual_status
FROM orders
where id = NEW.id;

IF (OLD.rating IS NOT NULL) THEN
SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Error: Rating cannot be updated if already done!';
ELSEIF ((actual_status IS NULL) OR ((actual_status != 'COMPLETED') AND
(actual_status != 'DELIVERED') AND
(actual_status != 'CANCELLED_BY_USER') AND
(actual_status != 'CANCELLED_BY_SELLER') AND
(actual_status != 'REFUND_COMPLETED'))) THEN
SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT =
'Error: Rating cannot be updated before the order completes!';
END IF;
END;
END IF;
END;
$$

DELIMITER $$
CREATE TRIGGER order_status_rating_update
AFTER UPDATE
ON orders
FOR EACH ROW
BEGIN
IF (OLD.status is NULL OR OLD.status != NEW.status) THEN
INSERT INTO orders_status(order_id, status)
VALUES (NEW.id, NEW.status);
BEGIN
IF (OLD.status is NULL OR OLD.status != NEW.status) THEN
INSERT INTO orders_status(order_id, status)
VALUES (NEW.id, NEW.status);

IF (NEW.status = 'CANCELLED_BY_USER' OR NEW.status = 'CANCELLED_BY_SELLER') THEN
INSERT INTO orders_status(order_id, status)
VALUES (NEW.id, 'REFUND_INITIATED');
END IF;
IF (NEW.status = 'CANCELLED_BY_USER' OR NEW.status = 'CANCELLED_BY_SELLER') THEN
INSERT INTO orders_status(order_id, status)
VALUES (NEW.id, 'REFUND_INITIATED');
END IF;
IF (OLD.rating IS NULL AND NEW.rating IS NOT NULL) THEN
CALL shop_rating_update(OLD.shop_id);
END IF;
END;
END IF;
IF (OLD.rating IS NULL AND NEW.rating IS NOT NULL) THEN
CALL shop_rating_update(OLD.shop_id);
END IF;
END;
$$

####################################################
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/food/ordering/zinger/constant/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ public static final class UserShopColumn {
public static final class ApplicationLogColumn {
public static final String tableName = "application_log";

public static final String request_type = "request_type";
public static final String endpoint_url = "endpoint_url";
public static final String request_header = "request_header";
public static final String request_object = "request_object";
public static final String response_object = "response_object";
public static final String requestType = "request_type";
public static final String endpointUrl = "endpoint_url";
public static final String requestHeader = "request_header";
public static final String requestObject = "request_object";
public static final String responseObject = "response_object";
public static final String date = "date";
}

Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/food/ordering/zinger/constant/Enums.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,4 @@ public enum UserRole {
public enum NotificationType {
URL, NEW_ARRIVAL, USER_ORDER_STATUS, SELLER_ORDER_STATUS
}

public enum HttpRequestType {
GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, PURGE, LOCK,UNLOCK,PROPFIND,VIEW
}
}
22 changes: 11 additions & 11 deletions src/main/java/com/food/ordering/zinger/constant/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ public class Query {
public static final class AuditLogQuery {

public static final String insertLog = INSERT_INTO + ApplicationLogColumn.tableName + LEFT_PARANTHESIS +
ApplicationLogColumn.request_type + COMMA +
ApplicationLogColumn.endpoint_url + COMMA +
ApplicationLogColumn.request_header + COMMA +
ApplicationLogColumn.request_object + COMMA +
ApplicationLogColumn.response_object +
ApplicationLogColumn.requestType + COMMA +
ApplicationLogColumn.endpointUrl + COMMA +
ApplicationLogColumn.requestHeader + COMMA +
ApplicationLogColumn.requestObject + COMMA +
ApplicationLogColumn.responseObject +
RIGHT_PARANTHESIS + VALUES + LEFT_PARANTHESIS +
COLON + ApplicationLogColumn.request_type +
COMMA_COLON + ApplicationLogColumn.endpoint_url +
COMMA_COLON + ApplicationLogColumn.request_header +
COMMA_COLON + ApplicationLogColumn.request_object +
COMMA_COLON + ApplicationLogColumn.response_object +
COLON + ApplicationLogColumn.requestType +
COMMA_COLON + ApplicationLogColumn.endpointUrl +
COMMA_COLON + ApplicationLogColumn.requestHeader +
COMMA_COLON + ApplicationLogColumn.requestObject +
COMMA_COLON + ApplicationLogColumn.responseObject +
RIGHT_PARANTHESIS;
}

Expand All @@ -49,7 +49,7 @@ public static final class PlaceQuery {
notDeleted +
ORDER_BY + PlaceColumn.name + ASC;

//TODO: Super Admin API
//TODO: Issue #4 Zinger Admin API for Place
public static final String updatePlace = UPDATE + PlaceColumn.tableName + SET +
PlaceColumn.name + EQUAL_COLON + PlaceColumn.name + COMMA +
PlaceColumn.iconUrl + EQUAL_COLON + PlaceColumn.iconUrl + COMMA +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.food.ordering.zinger.controller;

import com.food.ordering.zinger.model.Response;
import com.food.ordering.zinger.model.UserShopModel;
import com.food.ordering.zinger.model.notification.NotificationModel;
import com.food.ordering.zinger.service.interfaces.NotifyService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.food.ordering.zinger.controller;

import com.food.ordering.zinger.model.*;
import com.food.ordering.zinger.model.Response;
import com.food.ordering.zinger.model.UserModel;
import com.food.ordering.zinger.model.UserPlaceModel;
import com.food.ordering.zinger.model.UserShopListModel;
import com.food.ordering.zinger.model.notification.UserNotificationModel;
import com.food.ordering.zinger.service.interfaces.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.food.ordering.zinger.constant.ApiConfig.UserApi.*;

@RestController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,23 @@ public class AuditLogDaoImpl implements AuditLogDao {
@Autowired
NamedParameterJdbcTemplate namedParameterJdbcTemplate;



/**
* Inserts the HTTP request and response log
*
* @param applicationLogModel ApplicationLogModel
* @return success response if the log insertion
* @return success response if the insertion is successful.
*/
@Override
public Response<String> insertLog(ApplicationLogModel applicationLogModel) {
Response<String> response = new Response<>();

try {
SqlParameterSource parameters = new MapSqlParameterSource()
.addValue(ApplicationLogColumn.request_type, applicationLogModel.getRequestType().name())
.addValue(ApplicationLogColumn.endpoint_url, applicationLogModel.getEndpointUrl())
.addValue(ApplicationLogColumn.request_header, applicationLogModel.getRequestHeader())
.addValue(ApplicationLogColumn.request_object, applicationLogModel.getRequestObject())
.addValue(ApplicationLogColumn.response_object, applicationLogModel.getResponseObject());
.addValue(ApplicationLogColumn.requestType, applicationLogModel.getRequestType().name())
.addValue(ApplicationLogColumn.endpointUrl, applicationLogModel.getEndpointUrl())
.addValue(ApplicationLogColumn.requestHeader, applicationLogModel.getRequestHeader())
.addValue(ApplicationLogColumn.requestObject, applicationLogModel.getRequestObject())
.addValue(ApplicationLogColumn.responseObject, applicationLogModel.getResponseObject());

int responseValue = namedParameterJdbcTemplate.update(AuditLogQuery.insertLog, parameters);
if (responseValue > 0) {
Expand All @@ -56,9 +54,6 @@ public Response<String> insertLog(ApplicationLogModel applicationLogModel) {
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}


return response;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Response<String> sendMulticast(NotificationModel notificationModel, List<String>
/**
* Send notification message to all clients subscribed to a topic. Success response if all subscribed clients
* receive the message
*
* @param notificationModel the notification model
* @param topic the topic
* @return the response
Expand Down Expand Up @@ -124,6 +125,7 @@ Response<String> sendTopicMessage(NotificationModel notificationModel, String to
/**
* Send notification message to all signed in users. Success response if all targeted users receive the message.
* Success response if all notifications are sent succesfully
*
* @param notificationModel the notification model
* @return the response
*/
Expand Down
Loading

0 comments on commit f585ed0

Please sign in to comment.