/
common.go
115 lines (98 loc) · 2.9 KB
/
common.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2016-2020, Filippov Alex
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see
// <https://www.gnu.org/licenses/>.
package controllers
import (
"github.com/op/go-logging"
"github.com/e154/smart-home/adaptors"
"github.com/e154/smart-home/system/core"
"github.com/gin-gonic/gin"
"strconv"
"github.com/e154/smart-home/system/access_list"
m "github.com/e154/smart-home/models"
"fmt"
"github.com/e154/smart-home/endpoint"
)
var (
log = logging.MustGetLogger("controllers")
)
type ControllerCommon struct {
adaptors *adaptors.Adaptors
core *core.Core
accessList *access_list.AccessListService
endpoint *endpoint.Endpoint
}
func NewControllerCommon(adaptors *adaptors.Adaptors,
core *core.Core,
accessList *access_list.AccessListService,
endpoint *endpoint.Endpoint) *ControllerCommon {
return &ControllerCommon{
adaptors: adaptors,
core: core,
accessList: accessList,
endpoint: endpoint,
}
}
//query
//limit
//offset
func (c ControllerCommon) select2(ctx *gin.Context) (query string, limit, offset int) {
query = ctx.Request.URL.Query().Get("query")
limit, _ = strconv.Atoi(ctx.Request.URL.Query().Get("limit"))
offset, _ = strconv.Atoi(ctx.Request.URL.Query().Get("offset"))
return
}
//query
//sortby
//order
//limit
//offset
func (c ControllerCommon) list(ctx *gin.Context) (query, sortBy, order string, limit, offset int) {
limit = 15
offset = 0
order = "DESC"
sortBy = "created_at"
if ctx.Request.URL.Query().Get("query") != "" {
query = ctx.Request.URL.Query().Get("query")
}
if ctx.Request.URL.Query().Get("sortby") != "" {
sortBy = ctx.Request.URL.Query().Get("sortby")
}
if ctx.Request.URL.Query().Get("order") != "" {
order = ctx.Request.URL.Query().Get("order")
}
if ctx.Request.URL.Query().Get("limit") != "" {
limit, _ = strconv.Atoi(ctx.Request.URL.Query().Get("limit"))
}
if ctx.Request.URL.Query().Get("offset") != "" {
offset, _ = strconv.Atoi(ctx.Request.URL.Query().Get("offset"))
}
return
}
func (c ControllerCommon) getUser(ctx *gin.Context) (user *m.User, err error) {
u, ok := ctx.Get("currentUser")
if !ok {
err = fmt.Errorf("bad user object")
return
}
user, ok = u.(*m.User)
if !ok {
err = fmt.Errorf("bad user object")
return
}
return
}