import { Server } from 'miragejs'
import { tableRoute } from './tableList'
export function makeServer({ environment = 'development' } = {}) {
let server = new Server({
environment,
seeds(server) {
server.db.loadData({
todos: [
{ text: 'Buy groceries', isDone: false },
{ text: 'Walk the dog', isDone: false },
{ text: 'Do laundry', isDone: false }
]
})
server.db.loadData({
tableList: [
{
title: '火星创地球',
address: '火星',
name: '洛雨天涯'
}
]
})
},
routes() {
this.namespace = 'api'
this.timing = 750
tableRoute(this)
this.get('/todos', ({ db }) => {
return db.todos
})
this.patch('/todos/:id', (schema, request) => {
let todo = JSON.parse(request.requestBody).data
return schema.db.todos.update(todo.id, todo)
})
this.post('/todos', (schema, request) => {
let todo = JSON.parse(request.requestBody).data
return schema.db.todos.insert(todo)
})
this.delete('/todos/:id', (schema, request) => {
return schema.db.todos.remove(request.params.id)
})
}
})
return server
}
export default {
name: 'About',
components: { Card },
data () {
return {
todos: [],
value: null
}
},
mounted () {
this.getData()
this.axios.get('/api/tableList').then(({ data }) => {
console.log(data)
})
},
methods: {
getData () {
this.axios.get('/api/todos').then(({ data }) => {
console.log(data)
this.todos = data
})
},
add () {
const data = {
text: 'About this',
isDone: false
}
this.axios.post('/api/todos', { data }).then(res => {
this.getData()
console.log(res)
})
},
update () {
const data = {
id: this.value.toString(),
text: 'About this edit',
isDone: false
}
this.axios.patch(`/api/todos/${data.id}`, { data }).then(res => {
this.getData()
})
}
}
}