Skip to content

Commit

Permalink
1005 #3 繼續做這個
Browse files Browse the repository at this point in the history
  • Loading branch information
Pulipuli Chen committed Oct 5, 2019
1 parent 08eaa26 commit af75caa
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 20 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ adonis migration:run
adonis migration:refresh
````

# Model

https://adonisjs.com/docs/4.1/lucid

````
adonis make:model User
````

# Route Controller

https://adonisjs.com/docs/4.1/controllers
Expand Down
23 changes: 22 additions & 1 deletion app/Controllers/Http/MessageController.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
'use strict'

const User = use('App/Models/User')
const Message = use('App/Models/Message')

class MessageController {
async add ({ request, response, view }) {
async list ({ request, response, view, session }) {
// 列出最近10則訊息
const messages = await Message.pickInverse(10)
return messages.toJSON()
}
async insert ({ request, response, view, session }) {
const query = request.get()

if (typeof(query.message) !== 'string') {
return false
}

let userId = session.get('user_id', false)
if (userId === false) {
return false
}

let user = await User.find(userId)

let message = new Message()
message.message = query.message

await user.messages.save(message)
return message.createAt
}
}

Expand Down
12 changes: 12 additions & 0 deletions app/Models/Message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')

class Message extends Model {
user () {
return this.belongsTo('App/Models/User')
}
}

module.exports = Message
4 changes: 4 additions & 0 deletions app/Models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class User extends Model {
tokens () {
return this.hasMany('App/Models/Token')
}

messages () {
return this.hasMany('App/Models/Message')
}
}

module.exports = User
2 changes: 1 addition & 1 deletion client-src/VueController.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ let VueController = {
}, // mounted: function () {
methods: {
checkLogin: async function () {
let result = await axios.get(`${this.config.baseURL}/check-login`)
let result = await axios.get(`${this.config.baseURL}/user/check-login`)
//console.log(result.data)
let path = this.$router.currentRoute.fullPath
if (result.data === false) {
Expand Down
2 changes: 1 addition & 1 deletion client-src/components/Chat/Chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<div class="three wide field">
<button type="button"
class="ui fluid button"
v-on:click="send"
v-on:click="insert"
v-bind:class="{disabled: (writingMessage.trim() === ''), green: (writingMessage.trim() !== '')}">
{{ $t('Send') }}
</button>
Expand Down
18 changes: 15 additions & 3 deletions client-src/components/Chat/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = {
return {
displayMessages: [],
writingMessage: '',
users: []

users: [] // for test
}
},
computed: {
Expand All @@ -25,6 +26,12 @@ module.exports = {
let users = await this.lib.axios.get('http://127.0.0.1:3333/user/all')
this.users = users.data
},
initDisplayMessages: async function () {
let messages = await this.lib.axios.get(`${this.config.baseURL}/message/list`)
console.log(messages.data)
this.messages = messages.data
//console.log(this.messages)
},
/*
addUser: async function () {
let unixMS = (new Date()).getTime()
Expand All @@ -39,11 +46,16 @@ module.exports = {
this.loadUsers()
},
*/
send: async function () {
insert: async function () {
let result = await this.lib.axios.get(`${this.config.baseURL}/message/insert`, {
params: {
message: this.message
}
})
console.log(this.message)
},
logout: async function () {
await this.lib.axios.get(`${this.config.baseURL}/logout`)
await this.lib.axios.get(`${this.config.baseURL}/user/logout`)
this.status.username = ''
this.$router.replace('/')
}
Expand Down
4 changes: 2 additions & 2 deletions client-src/components/Login/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = {

//console.log([this.username, this.email, this.password])

let result = await this.lib.axios.get(`${this.config.baseURL}/register`, {
let result = await this.lib.axios.get(`${this.config.baseURL}/user/register`, {
params: {
username: this.username,
email: this.email,
Expand Down Expand Up @@ -81,7 +81,7 @@ module.exports = {

//console.log([this.username, this.email, this.password])

let result = await this.lib.axios.get(`${this.config.baseURL}/login`, {
let result = await this.lib.axios.get(`${this.config.baseURL}/user/login`, {
params: {
username: this.username,
password: this.password,
Expand Down
26 changes: 19 additions & 7 deletions public/dist/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ let VueController = {
}, // mounted: function () {
methods: {
checkLogin: async function () {
let result = await axios__WEBPACK_IMPORTED_MODULE_4___default.a.get(`${this.config.baseURL}/check-login`)
let result = await axios__WEBPACK_IMPORTED_MODULE_4___default.a.get(`${this.config.baseURL}/user/check-login`)
//console.log(result.data)
let path = this.$router.currentRoute.fullPath
if (result.data === false) {
Expand Down Expand Up @@ -288,7 +288,8 @@ module.exports = {
return {
displayMessages: [],
writingMessage: '',
users: []

users: [] // for test
}
},
computed: {
Expand All @@ -305,6 +306,12 @@ module.exports = {
let users = await this.lib.axios.get('http://127.0.0.1:3333/user/all')
this.users = users.data
},
initDisplayMessages: async function () {
let messages = await this.lib.axios.get(`${this.config.baseURL}/message/list`)
console.log(messages.data)
this.messages = messages.data
//console.log(this.messages)
},
/*
addUser: async function () {
let unixMS = (new Date()).getTime()
Expand All @@ -319,11 +326,16 @@ module.exports = {
this.loadUsers()
},
*/
send: async function () {
insert: async function () {
let result = await this.lib.axios.get(`${this.config.baseURL}/message/insert`, {
params: {
message: this.message
}
})
console.log(this.message)
},
logout: async function () {
await this.lib.axios.get(`${this.config.baseURL}/logout`)
await this.lib.axios.get(`${this.config.baseURL}/user/logout`)
this.status.username = ''
this.$router.replace('/')
}
Expand Down Expand Up @@ -519,7 +531,7 @@ module.exports = {

//console.log([this.username, this.email, this.password])

let result = await this.lib.axios.get(`${this.config.baseURL}/register`, {
let result = await this.lib.axios.get(`${this.config.baseURL}/user/register`, {
params: {
username: this.username,
email: this.email,
Expand Down Expand Up @@ -549,7 +561,7 @@ module.exports = {

//console.log([this.username, this.email, this.password])

let result = await this.lib.axios.get(`${this.config.baseURL}/login`, {
let result = await this.lib.axios.get(`${this.config.baseURL}/user/login`, {
params: {
username: this.username,
password: this.password,
Expand Down Expand Up @@ -38601,7 +38613,7 @@ var render = function() {
green: _vm.writingMessage.trim() !== ""
},
attrs: { type: "button" },
on: { click: _vm.send }
on: { click: _vm.insert }
},
[_vm._v("\r\n " + _vm._s(_vm.$t("Send")) + "\r\n ")]
)
Expand Down
2 changes: 1 addition & 1 deletion public/dist/bundle.js.map

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions start/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ Route.get('/user/create', async ({ request, response, view }) => {
//return {'aaa': 'ok'}
})

Route.get('/login', 'UserController.login')
Route.get('/register', 'UserController.register')
Route.get('/logout', 'UserController.logout')
Route.get('/check-login', 'UserController.checkLogin')
Route.get('/user/login', 'UserController.login')
Route.get('/user/register', 'UserController.register')
Route.get('/user/logout', 'UserController.logout')
Route.get('/user/check-login', 'UserController.checkLogin')

Route.get('/message/list', 'MessageController.list')
Route.get('/message/insert', 'MessageController.insert')

0 comments on commit af75caa

Please sign in to comment.