Skip to content

Commit 8040c37

Browse files
authored
Merge pull request #23 from funktechno/f/updates
F/updates 0.0.8
2 parents 7d99c0d + f52d066 commit 8040c37

File tree

6 files changed

+52
-8
lines changed

6 files changed

+52
-8
lines changed

conversations/index.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,17 @@
3838
case 'submit':
3939
$threadId = $input['threadId'];
4040
$message = $input['message'];
41-
if (empty($threadId) || empty($message)) {
41+
if (empty($message)) {
4242
$errorStatus->response(400, "threadId, message field(s) are required");
4343
}
44+
if (empty($threadId)) {
45+
$regOutcome = startThread();
46+
if ($regOutcome['rowsChanged'] === 1) {
47+
$threadId = $regOutcome['lastId'];
48+
} else {
49+
$errorStatus->response(500, "Error starting thread");
50+
}
51+
}
4452
$regOutcome = sendMessage($threadId, $message);
4553
if ($regOutcome['rowsChanged'] === 1) {
4654
$result = getMessage($regOutcome['lastId']);

examples/js/vue.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var app = new Vue({
2626
example: "comments",
2727
status: null,
2828
form: {},
29+
threadId: null,
2930
conversations: [],
3031
loading: {
3132
general: false,
@@ -88,12 +89,16 @@ var app = new Vue({
8889
if (this.loading.chat)
8990
return;
9091
// retrieve from cookie
91-
let threadId = "effba2487ece11eb8e3a0242ac110002"
92+
// will start from
93+
94+
// let threadId = "effba2487ece11eb8e3a0242ac110002"
95+
let threadId = this.threadId;
9296
let config = {}
9397
let request = {
94-
threadId: threadId,
98+
threadId: threadId || null,
9599
message: this.chat.message
96100
}
101+
console.log(request);
97102
this.loading.chat = true;
98103

99104
this.$http.post("/conversations/?action=submit", request, config).then((response) => {
@@ -103,6 +108,8 @@ var app = new Vue({
103108
if (response.status == 201) {
104109
this.chat.message = "";
105110
console.log(response.data);
111+
this.threadId = response.data.threadId;
112+
Cookies.set('thread', this.threadId, { expires: 7, path: '' })
106113
this.conversations.push(response.data);
107114
// this.$set()
108115
// this.comments = response.data;
@@ -192,7 +199,8 @@ var app = new Vue({
192199
},
193200
getConversation() {
194201
// check from cookie for conversation id
195-
let threadId = "effba2487ece11eb8e3a0242ac110002"
202+
let threadId = this.threadId;
203+
// "effba2487ece11eb8e3a0242ac110002"
196204
this.loading.chat = true;
197205
this.errors = null;
198206
this.$http.post("/conversations/?action=get", { "threadId": threadId }).then((response) => {
@@ -236,6 +244,8 @@ var app = new Vue({
236244
},
237245
mounted() {
238246
this.getComments();
239-
this.getConversation();
247+
this.threadId = Cookies.get('thread');
248+
if (this.threadId)
249+
this.getConversation();
240250
}
241251
})

examples/vue.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
2626
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
27+
<script src="https://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js"></script>
2728

2829
</head>
2930

library/Core.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
//should contain app meta data such as version
3-
define('APP_VERSION', '0.0.7');
3+
define('APP_VERSION', '0.0.8');
44

55
define('APP_Name','CommentsPress');
66
// comment out this line if you don't want this meta response header

model/conversations-model.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@ function getMessage($id)
2525
return $prodInfo;
2626
}
2727

28+
function startThread(){
29+
$id = generateUuid();
30+
// Create a connection object using the acme connection function
31+
$db = acmeConnect();
32+
// The SQL statement
33+
$sql = 'INSERT INTO threads(id)
34+
values(:id);';
35+
// echo $sql;
36+
$stmt = $db->prepare($sql);
37+
38+
// $stmt->bindValue(':id', $id, PDO::PARAM_STR);
39+
$stmt->bindValue(':id', $id, PDO::PARAM_STR);
40+
41+
// // Create the prepared statement using the acme connection
42+
43+
// Insert the data
44+
$stmt->execute();
45+
// Ask how many rows changed as a result of our insert
46+
$rowsChanged = $stmt->rowCount();
47+
// Close the database interaction
48+
$stmt->closeCursor();
49+
// Return the indication of success (rows changed)
50+
$result = array('rowsChanged' => $rowsChanged, 'lastId' => $id);
51+
return $result;
52+
}
2853
function sendMessage($conversationId, $message)
2954
{
3055
// $id = generateUuid();

rest/conversations.rest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ Accept: application/json
77
"threadId": "effba2487ece11eb8e3a0242ac110002"
88
}
99

10-
### send charset
10+
### sendmessage, pass null threadId to start a new conversation
1111
POST http://127.0.0.1:8000/conversations/?action=submit HTTP/1.1
1212
content-type: application/json
1313
Accept: application/json
1414

1515
{
16-
"threadId": "effba2487ece11eb8e3a0242ac110002",
16+
"threadId": null,
1717
"message": "test comment"
1818
}

0 commit comments

Comments
 (0)