Skip to content

Commit

Permalink
Merge pull request #7 from agilitytestbed/features/usermessages
Browse files Browse the repository at this point in the history
Features/usermessages
  • Loading branch information
dkooij authored Aug 5, 2018
2 parents 9dd82ff + 9b5ef09 commit c9eb25a
Show file tree
Hide file tree
Showing 10 changed files with 808 additions and 42 deletions.
15 changes: 14 additions & 1 deletion database_structure.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
CREATE TABLE IF NOT EXISTS User_Table(
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT,
highest_lifetime_balance FLOAT,
highest_transaction_id BIGINT,
highest_category_id BIGINT,
highest_category_rule_id BIGINT,
highest_saving_goal_id BIGINT,
highest_payment_request_id BIGINT
highest_payment_request_id BIGINT,
highest_user_message_id BIGINT
);

CREATE TABLE IF NOT EXISTS Transaction_Table(
Expand Down Expand Up @@ -86,3 +88,14 @@ CREATE TABLE IF NOT EXISTS Payment_Request_Transaction(
PRIMARY KEY(user_id, payment_request_id, transaction_id)
);

CREATE TABLE IF NOT EXISTS User_Message(
user_id INTEGER,
user_message_id BIGINT,
message TEXT,
date DATETIME,
read BOOLEAN,
type TEXT,
FOREIGN KEY(user_id) REFERENCES User_Table(user_id),
PRIMARY KEY(user_id, user_message_id)
);

56 changes: 54 additions & 2 deletions sql_queries_and_updates.txt
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,57 @@ AND pr.payment_request_id = ?;
INSERT INTO Payment_Request_Transaction (user_id, transaction_id, payment_request_id)
VALUES (?, ?, ?);

increaseHighestUserMessageID(user_id):
UPDATE User_Table
SET highest_user_message_id = highest_user_message_id + 1
WHERE user_id = ?;

getHighestUserMessageID(user_id):
SELECT highest_user_message_id
FROM User_Table
WHERE user_id = ?;

getUserMessage(user_id, user_message_id):
SELECT user_message_id, message, date, read, type
FROM User_Message
WHERE user_id = ?
AND user_message_id = ?;

createUserMessage(user_id, user_message_id, message, date, type):
INSERT INTO User_Message (user_id, user_message_id, message, date, read, type)
VALUES (?, ?, ?, ?, 0, ?);

getUnreadUserMessages(user_id):
SELECT user_message_id, message, date, read, type
FROM User_Message
WHERE user_id = ?
AND read = 0;

getAllUserMessages(user_id):
SELECT user_message_id, message, date, read, type
FROM User_Message
WHERE user_id = ?;

setUserMessageRead(user_id, user_message_id):
UPDATE User_Message
SET read = 1
WHERE user_id = ?
AND user_message_id = ?;

getHighestLifetimeBalance(user_id):
SELECT highest_lifetime_balance
FROM User_Table
WHERE user_id = ?;

updateHighestLifetimeBalance(current_balance, current_balance, user_id):
UPDATE User_Table
SET highest_lifetime_balance =
CASE WHEN ? > highest_lifetime_balance
THEN ?
ELSE highest_lifetime_balance
END
WHERE user_id = ?;

linkTransactionToCategory(user_id, transaction_id, category_id):
INSERT INTO Transaction_Category (user_id, transaction_id, category_id)
VALUES (?, ?, ?);
Expand Down Expand Up @@ -272,10 +323,11 @@ AND t.user_id = ?
AND t.transaction_id = ?;

createNewUser(session_id):
INSERT INTO User_Table (session_id, highest_transaction_id, highest_category_id, highest_category_rule_id, highest_saving_goal_id, highest_payment_request_id)
VALUES (?, 0, 0, 0, 0, 0);
INSERT INTO User_Table (session_id, highest_lifetime_balance, highest_transaction_id, highest_category_id, highest_category_rule_id, highest_saving_goal_id, highest_payment_request_id, highest_user_message_id)
VALUES (?, 0, 0, 0, 0, 0, 0, 0);

getUserID(session_id):
SELECT user_id
FROM User_Table
WHERE session_id = ?;

46 changes: 46 additions & 0 deletions src/main/java/nl/utwente/ing/api/MainRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,50 @@ public ResponseEntity postPaymentRequest(@RequestParam(value = "session_id", def
}
}

/**
* Method used to retrieve the unread UserMessages belonging to the user issuing the current request.
*
* @param pSessionID The sessionID specified in the request parameters.
* @param hSessionID The sessionID specified in the HTTP header.
* @return A ResponseEntity containing a HTTP status code and either a status message or
* an ArrayList of UserMessages belonging to the user issuing the current request.
*/
@RequestMapping(method = RequestMethod.GET,
value = RestControllerConstants.URI_PREFIX + "/messages")
public ResponseEntity getUnreadUserMessages(@RequestParam(value = "session_id", defaultValue = "") String pSessionID,
@RequestHeader(value = "X-session-ID", defaultValue = "") String hSessionID) {
try {
String sessionID = this.getSessionID(pSessionID, hSessionID);
ArrayList<UserMessage> userMessages = model.getUnreadUserMessages(sessionID);
return ResponseEntity.status(200).body(userMessages);
} catch (InvalidSessionIDException e) {
return ResponseEntity.status(401).body("Session ID is missing or invalid");
}
}

/**
* Method used to indicate that a certain UserMessage belonging to the user issuing the current request is read.
*
* @param pSessionID The sessionID specified in the request parameters.
* @param hSessionID The sessionID specified in the HTTP header.
* @param userMessageID The userMessageID of the UserMessage for which it is indicated that it is read.
* @return A ResponseEntity containing a HTTP status code and a status message.
*/
@RequestMapping(method = RequestMethod.PUT,
value = RestControllerConstants.URI_PREFIX + "/messages/{userMessageID}")
public ResponseEntity putUserMessageRead(@RequestParam(value = "session_id", defaultValue = "") String pSessionID,
@RequestHeader(value = "X-session-ID", defaultValue = "") String hSessionID,
@PathVariable String userMessageID) {
try {
String sessionID = this.getSessionID(pSessionID, hSessionID);
long userMessageIDLong = Long.parseLong(userMessageID);
model.setUserMessageRead(sessionID, userMessageIDLong);
return ResponseEntity.status(200).body("Successful operation");
} catch (InvalidSessionIDException e) {
return ResponseEntity.status(401).body("Session ID is missing or invalid");
} catch (NumberFormatException | ResourceNotFoundException e) {
return ResponseEntity.status(404).body("Resource not found");
}
}

}
15 changes: 13 additions & 2 deletions src/main/java/nl/utwente/ing/misc/date/IntervalHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class IntervalHelper {
* @param amount The amount of intervals to be generated.
* @return An array containing LocalDateTime objects representing the requested intervals.
*/
public static LocalDateTime[] getIntervals(IntervalPeriod intervalPeriod, int amount) {
public static LocalDateTime[] getIntervals(IntervalPeriod intervalPeriod, int amount, LocalDateTime until) {
LocalDateTime[] intervals = new LocalDateTime[amount + 2];
intervals[amount + 1] = LocalDateTime.now(ZoneOffset.UTC);
intervals[amount + 1] = until;
intervals[0] = LocalDateTime.parse("1970-01-01T00:00:00.000");

if (intervalPeriod == IntervalPeriod.YEAR) {
Expand Down Expand Up @@ -112,4 +112,15 @@ public static int getMonthIdentifier(String dateString) {
return (date.getYear() - 1970) * 12 + (date.getMonthValue() - 1);
}

/**
* Method used to convert a String object containing a date to a LocalDateTime object containing that date.
*
* @param s The String object containing a date that will be converted to a LocalDateTime object containing that
* date.
* @return The LocalDateTime object containing the date contained in the given String object.
*/
public static LocalDateTime toLocalDateTime(String s) {
return LocalDateTime.parse(s.split("Z")[0]);
}

}
18 changes: 17 additions & 1 deletion src/main/java/nl/utwente/ing/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,26 @@ void deleteSavingGoal(String sessionID, long savingGoalID)
/**
* Method used to create a new PaymentRequest for a certain user.
*
* @param sessionID The sessionID of the user.
* @param sessionID The sessionID of the user.
* @param paymentRequest The PaymentRequest object to be used to create the new PaymentRequest.
* @return The PaymentRequest created by this method.
*/
PaymentRequest postPaymentRequest(String sessionID, PaymentRequest paymentRequest) throws InvalidSessionIDException;

/**
* Method used to retrieve the unread UserMessages belonging to a certain user.
*
* @param sessionID The sessionID of the user.
* @return An ArrayList of UserMessages belonging to the user with sessionID.
*/
ArrayList<UserMessage> getUnreadUserMessages(String sessionID) throws InvalidSessionIDException;

/**
* Method used to indicate that a certain UserMessage of a certain user is read.
*
* @param sessionID The sessionID of the user.
* @param userMessageID The ID of the UserMessage of the certain user that should be marked as read.
*/
void setUserMessageRead(String sessionID, long userMessageID)
throws InvalidSessionIDException, ResourceNotFoundException;
}
132 changes: 132 additions & 0 deletions src/main/java/nl/utwente/ing/model/bean/UserMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package nl.utwente.ing.model.bean;

/**
* The UserMessage class.
* Used to store information about a UserMessage.
*
* @author Daan Kooij
*/
public class UserMessage {

private long id;
private String message;
private String date;
private boolean read;
private String type;

/**
* An empty constructor of PaymentRequest.
* Used by the Spring framework.
*/
public UserMessage() {

}

/**
* A constructor of UserMessage.
*
* @param id The ID of the to be created UserMessage.
* @param message The message of the to be created UserMessage.
* @param date The date of the to be created UserMessage.
* @param read The boolean indicating whether the to be created UserMessage is read.
* @param type The type of the to be created UserMessage.
*/
public UserMessage(long id, String message, String date, boolean read, String type) {
this.id = id;
this.message = message;
this.date = date;
this.read = read;
this.type = type;
}

/**
* Method used to retrieve the ID of UserMessage.
*
* @return The ID of UserMessage.
*/
public long getID() {
return id;
}

/**
* Method used to update the ID of UserMessage.
*
* @param id The new ID of UserMessage.
*/
public void setID(long id) {
this.id = id;
}

/**
* Method used to retrieve the message of UserMessage.
*
* @return The message of UserMessage.
*/
public String getMessage() {
return message;
}

/**
* Method used to update the message of UserMessage.
*
* @param message The new message of UserMessage.
*/
public void setMessage(String message) {
this.message = message;
}

/**
* Method used to retrieve the date of UserMessage.
*
* @return The date of UserMessage.
*/
public String getDate() {
return date;
}

/**
* Method used to update the date of UserMessage.
*
* @param date The new date of UserMessage.
*/
public void setDate(String date) {
this.date = date;
}

/**
* Method used to retrieve the boolean indicating whether UserMessage is read.
*
* @return The boolean indicating whether UserMessage is read.
*/
public boolean getRead() {
return read;
}

/**
* Method used to update the boolean indicating whether UserMessage is read.
*
* @param read The new boolean indicating whether UserMessage is read.
*/
public void setRead(boolean read) {
this.read = read;
}

/**
* Method used to retrieve the type of UserMessage.
*
* @return The ID of UserMessage.
*/
public String getType() {
return type;
}

/**
* Method used to update the type of UserMessage.
*
* @param type The new type of UserMessage.
*/
public void setType(String type) {
this.type = type;
}

}
Loading

0 comments on commit c9eb25a

Please sign in to comment.