-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.lua
More file actions
60 lines (51 loc) · 1.89 KB
/
user.lua
File metadata and controls
60 lines (51 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
local os = require('ffi').os
local env = require('env')
local tmpdir = os == 'Windows' and env.get('TMP') or '/tmp'
local db = require("./luz/db").DB:new("sqlite3", tmpdir.."/test.sqlite3")
local User = require("./luz/db").Model:extend():new(db)
local app = require("./luz/app").app:new()
local JSON = require('rapidjson')
-- prepare db
User.db:run"DROP TABLE user"
User.db:run[[
CREATE TABLE user(
id INT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(50)
)
]]
local list = {
{ id=1, name="Jose das Couves", email="jose@couves.com", },
{ id=2, name="Jack", email="manoel.joaquim@cafundo.com", },
{ id=3, name="Jack", email="maria@dores.com", },
}
print("------------------------ User:save")
for i, p in pairs (list) do
User:save({id=p.id, name=p.name, email=p.email})
end
print("------------------------ User:all")
p(User:all())
print("------------------------ User:find(1)")
p(User:find(1))
print("------------------------ User:find(4)")
p(User:find(4))
print("------------------------ User:where({name='Jack'})")
p(User:where({name='Jack'}))
print("------------------------ User:where(where({name='Jack',email='maria@dores.com'})")
p(User:where({name='Jack',email='maria@dores.com'}))
print("------------------------ User:update({name='Jack2'},{email='maria@dores.com'})")
p(User:update({name='Jack2'},{email='maria@dores.com'}))
print("------------------------ User:where({name='Jack2'})")
p(User:where({name='Jack2'}))
print("------------------------ User:destroy({id=2})")
p(User:destroy({id=2}))
print("------------------------ User:destroy({name='Jack2'})")
p(User:destroy({name='Jack2'}))
print("------------------------ User:all")
p(User:all())
app:get('/user/:id', function(params)
local user = User:find(params.id)
return JSON.encode(user)
end)
app:listen({port=8003})
print("Http Server listening at http://0.0.0.0:8003/user/:id")