From ae4a33d59114897be3ff13c770b3b70887d465dc Mon Sep 17 00:00:00 2001 From: lastlink Date: Sat, 6 Mar 2021 18:23:57 -0500 Subject: [PATCH] init chat model --- model/conversations-model.php | 40 ++++++++++++++++++++++++----------- rest/conversations.rest | 5 ++--- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/model/conversations-model.php b/model/conversations-model.php index 9438f07..536d8bc 100644 --- a/model/conversations-model.php +++ b/model/conversations-model.php @@ -13,28 +13,44 @@ function getConversation($conversationId) return $prodInfo; } -function addMessage($input){ +function getMessage($id) +{ + $db = acmeConnect(); + $sql = 'SELECT c.* FROM conversations c where c.id = :id'; + $stmt = $db->prepare($sql); + $stmt->bindValue(':id', $id, PDO::PARAM_STR); + $stmt->execute(); + $prodInfo = $stmt->fetch(PDO::FETCH_ASSOC); + $stmt->closeCursor(); + return $prodInfo; +} + +function sendMessage($conversationId, $message) +{ + // $id = generateUuid(); // Create a connection object using the acme connection function $db = acmeConnect(); // The SQL statement - $sql = 'INSERT INTO `reviews` (`reviewText`, `reviewDate`, `invId`, `clientId`) VALUES (:reviewText, :reviewDate, :invId, :clientId)'; + $sql = 'INSERT INTO `conversations`(`threadId`, `message`) + VALUES (:parentId, :message)'; + // echo $sql; + $stmt = $db->prepare($sql); + + // $stmt->bindValue(':id', $id, PDO::PARAM_STR); + $stmt->bindValue(':parentId', $conversationId, PDO::PARAM_STR); + $stmt->bindValue(':message', $message, PDO::PARAM_STR); + // // Create the prepared statement using the acme connection - $stmt = $db->prepare($sql); - // // The next four lines replace the placeholders in the SQL - // // statement with the actual values in the variables - // // and tells the database the type of data it is - // $stmt->bindValue(':reviewText', $reviewText, PDO::PARAM_STR); - // $stmt->bindValue(':reviewDate', $reviewDate, PDO::PARAM_STR); - // $stmt->bindValue(':invId', $invId, PDO::PARAM_INT); - // $stmt->bindValue(':clientId', $clientId, PDO::PARAM_INT); // Insert the data $stmt->execute(); // Ask how many rows changed as a result of our insert $rowsChanged = $stmt->rowCount(); + $id = $db->lastinsertid(); // Close the database interaction $stmt->closeCursor(); // Return the indication of success (rows changed) - return $rowsChanged; -} \ No newline at end of file + $result = array('rowsChanged' => $rowsChanged, 'lastId' => $id); + return $result; +} diff --git a/rest/conversations.rest b/rest/conversations.rest index 09a3864..59f98ba 100644 --- a/rest/conversations.rest +++ b/rest/conversations.rest @@ -4,16 +4,15 @@ content-type: application/json Accept: application/json { - "threadId": "xxx" + "threadId": "effba2487ece11eb8e3a0242ac110002" } ### send charset POST http://127.0.0.1:8000/conversations/?action=submit HTTP/1.1 content-type: application/json Accept: application/json -Authorization: Bearer {{token}} { - "threadId": "xxx", + "threadId": "effba2487ece11eb8e3a0242ac110002", "message": "test comment" } \ No newline at end of file