Skip to content

Commit

Permalink
Merge pull request #22 from funktechno/f/updates
Browse files Browse the repository at this point in the history
F/updates
  • Loading branch information
lastlink authored Mar 6, 2021
2 parents cd765a3 + ae4a33d commit 7d99c0d
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 16 deletions.
42 changes: 42 additions & 0 deletions conversations/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,45 @@
}

$directoryURI = $_SERVER['REQUEST_URI'];

switch ($action) {
case 'get':
$threadId = $input['threadId'];
if (empty($threadId)) {
$errorStatus->response(400, "threadId field is required");
}

$comments = getConversation($threadId);
// recursively updated comments w/ child comments

echo json_encode($comments);
break;
case 'submit':
$threadId = $input['threadId'];
$message = $input['message'];
if (empty($threadId) || empty($message)) {
$errorStatus->response(400, "threadId, message field(s) are required");
}
$regOutcome = sendMessage($threadId, $message);
if ($regOutcome['rowsChanged'] === 1) {
$result = getMessage($regOutcome['lastId']);

$statuscode = 201;

header("HTTP/1.1 " . $statuscode);

$response = array('Status' => 'success');

echo json_encode($result);
} else {
$errorStatus->response(500, "Error saving message");
}

break;
default;
// header('location: /accounts/');
$errorStatus->response(404, "Method not valid");
// include '../view/admin.php';
break;
// include '../view/404.php';
}
60 changes: 59 additions & 1 deletion examples/js/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ var app = new Vue({
example: "comments",
status: null,
form: {},
conversations: [],
loading: {
general: false,
comments: false,
form: false
form: false,
chat: false
},
userForm: {},
modal: {
Expand Down Expand Up @@ -81,6 +83,38 @@ var app = new Vue({
});


},
sendMessage() {
if (this.loading.chat)
return;
// retrieve from cookie
let threadId = "effba2487ece11eb8e3a0242ac110002"
let config = {}
let request = {
threadId: threadId,
message: this.chat.message
}
this.loading.chat = true;

this.$http.post("/conversations/?action=submit", request, config).then((response) => {
this.loading.chat = false;
console.log(response)
// this.message = response.data.message;
if (response.status == 201) {
this.chat.message = "";
console.log(response.data);
this.conversations.push(response.data);
// this.$set()
// this.comments = response.data;
} else {
this.errors = "Failed to send message"
}
}).catch((error) => {
this.errors = error.data
console.log(error)
this.loading.chat = null;

});
},
addComment() {
// don't run if not logged in or loading
Expand Down Expand Up @@ -156,6 +190,29 @@ var app = new Vue({
}
this.modal.signIn = false;
},
getConversation() {
// check from cookie for conversation id
let threadId = "effba2487ece11eb8e3a0242ac110002"
this.loading.chat = true;
this.errors = null;
this.$http.post("/conversations/?action=get", { "threadId": threadId }).then((response) => {
this.loading.chat = false;
console.log(response)
// this.message = response.data.message;
if (response.status == 200) {
console.log(response.data)
this.conversations = response.data;
} else {
this.errors = "Failed to load chat"
}
}).catch((error) => {
this.errors = "Failed to get chat"
console.log(error)
this.loading.chat = null;

});

},
getComments() {
this.loading.comments = true;
this.errors = null;
Expand All @@ -179,5 +236,6 @@ var app = new Vue({
},
mounted() {
this.getComments();
this.getConversation();
}
})
4 changes: 2 additions & 2 deletions examples/vue.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ <h5 class="auth-form-title auth-module__title_f76db">Use Social Network</h5>
<div id="intercom-container" class="intercom-namespace" role="main" aria-live="polite">
<div tabindex="-1" role="region">
<ul>
<li>message</li>
<li v-for="m in conversations">{{ m.message }}</li>
</ul>
<textarea v-model="chat.message"></textarea>
<button>Send</button>
<button v-on:click="sendMessage()">Send</button>
<!-- <div>
<div class="intercom-1xha1pw e16fp1gy0">
<div class="intercom-messenger intercom-messenger-new-conversation intercom-k9lbvn e16fp1gy1 intercom-messenger-main-animated-appear-done intercom-messenger-main-animated-enter-done" tabindex="0">
Expand Down
42 changes: 29 additions & 13 deletions model/conversations-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
function getConversation($conversationId)
{
$db = acmeConnect();
$sql = 'SELECT c.* FROM conversations c WHERE c.id = :id';
$sql = 'SELECT c.* FROM conversations c WHERE c.threadId = :id';
$stmt = $db->prepare($sql);
$stmt->bindValue(':id', $conversationId, PDO::PARAM_STR);
$stmt->execute();
Expand All @@ -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;
}
$result = array('rowsChanged' => $rowsChanged, 'lastId' => $id);
return $result;
}
18 changes: 18 additions & 0 deletions rest/conversations.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### get thread
POST http://127.0.0.1:8000/conversations/?action=get HTTP/1.1
content-type: application/json
Accept: application/json

{
"threadId": "effba2487ece11eb8e3a0242ac110002"
}

### send charset
POST http://127.0.0.1:8000/conversations/?action=submit HTTP/1.1
content-type: application/json
Accept: application/json

{
"threadId": "effba2487ece11eb8e3a0242ac110002",
"message": "test comment"
}

0 comments on commit 7d99c0d

Please sign in to comment.