-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
72 lines (56 loc) · 1.66 KB
/
example.go
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
61
62
63
64
65
66
67
68
69
70
71
72
/**
* This example is using single table "users"
*
* CREATE TABLE users (
* id serial PRIMARY KEY,
* name VARCHAR(50),
* email VARCHAR(50)
* );
*
* Endpoints:
*
* GET /users
* GET /users/{id}
* POST /users
* PUT /users/{id}
* DELETE /users/{id}
*
* Example:
*
* GET http://localhost:8001/users/1
* POST http://localhost:8001/users?name=John&email=doe@john.com
*/
package crema
import (
"database/sql"
"log"
"net/http"
//import crema
//. "github.com/gadp22/
)
func GetUser(conditions map[string]string) (*sql.Rows, error) {
q := GetGenericSelectQuery("users", conditions)
return ExecuteQuery(q.QueryString)
}
func PostUser(tx *sql.Tx, values map[string]string) *sql.Row {
q := GetGenericInsertQuery("users", values)
return ExecuteQueryRow(tx, q.QueryString)
}
func PutUser(tx *sql.Tx, values map[string]string) (sql.Result, error) {
q := GetGenericUpdateQuery("users", values)
return ExecuteNonQuery(q.QueryString)
}
func DeleteUser(tx *sql.Tx, conditions map[string]string) (sql.Result, error) {
q := GetGenericDeleteQuery("users", conditions)
return ExecuteNonQuery(q.QueryString)
}
func main_example() {
server := InitServer()
server.AddRoutes(http.MethodGet, "/users", MakeGenericGetHandler(GetUser))
server.AddRoutes(http.MethodGet, "/users/{id}", MakeGenericGetHandler(GetUser))
server.AddRoutes(http.MethodPost, "/users", MakeGenericPostHandler(PostUser))
server.AddRoutes(http.MethodPut, "/users/{id}", MakeGenericPutHandler(PutUser))
server.AddRoutes(http.MethodDelete, "/users/{id}", MakeGenericDeleteHandler(DeleteUser))
LogPrintf("server is started, listening to 8001")
log.Fatal(http.ListenAndServe(":8001", server.Router))
}