Newer
Older
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
package models
import (
"net/http"
"strings"
"github.com/jinzhu/gorm"
// Needed for sqlite
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
// DataHandler init a gorm DB an presents API handlers
type DataHandler struct {
db *gorm.DB
}
// ErrorIDDoesNotExist = "id does not exist"
const ErrorIDDoesNotExist = "id does not exist"
// ErrorIDIsMissing = "id is missing"
const ErrorIDIsMissing = "id is missing"
// ErrorCannotAccessRessource = "You can not access this ressource"
const ErrorCannotAccessRessource = "You can not access this ressource"
// ErrorRoleOfLoggedUser = "Could not get role of logged user"
const ErrorRoleOfLoggedUser = "Could not get role of logged user"
// ErrorNotAuthorizeMethodOnRessource = "You're not authorize to execute this method on this ressource."
const ErrorNotAuthorizeMethodOnRessource = "You're not authorize to execute this method on this ressource."
// NewDataHandler init a DataHandler and returns a pointer to it
func NewDataHandler() *DataHandler {
db, err := gorm.Open("sqlite3", "./data/test.db")
if err != nil {
panic("failed to connect database")
}
db.LogMode(true)
// Add FK with SQLite doesn't work
// db.Model(&Operation{}).AddForeignKey("creditor", "bank_account(id)", "Set Null", "Set Null")
// Migrate the schema
return &DataHandler{db: db}
}
// ProcessAPI redirect API call to DataHandler to each correct API func
func (d *DataHandler) ProcessAPI(w http.ResponseWriter, r *http.Request) {
api := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/"), "/")[0]
switch api {
}
}
func (d *DataHandler) getLoggedUser(w http.ResponseWriter, r *http.Request) interface{} {
// user := auth.GetLoggedUserTechnical(w, r)
// if user.Role != "" && (user.Role == "BANKER") {
// var o UserBanker
// if err := d.db.Where(reqUserID, user.ID).First(&o).Error; err != nil {
// o := UserBanker{UserID: user.ID, Name: user.Login}
// d.db.Create(&o)
// d.db.Where(reqUserID, user.ID).First(&o)
// return o
// }
// return o
// } else if user.Role != "" && (user.Role == "CLIENT") {
// var o UserClient
// if err := d.db.Where(reqUserID, user.ID).First(&o).Error; err != nil {
// o := UserClient{UserID: user.ID, Name: user.Login}
// d.db.Create(&o)
// d.db.Where(reqUserID, user.ID).First(&o)
// return o
// }
// return o
// }
return nil
}