-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (97 loc) · 2.49 KB
/
main.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
116
117
118
package main
import (
"cashbook/cashbookerror"
"cashbook/model"
"cashbook/utils"
"database/sql"
"log"
"github.com/leaanthony/mewn"
"github.com/wailsapp/wails"
_ "github.com/mattn/go-sqlite3"
)
var initDb string
func startup() string {
return initDb
}
func init() {
var err error
model.DB, err = sql.Open("sqlite3", "file:cashbook.db")
if err != nil {
log.Fatalln(err)
initDb = "Database cannot be created"
}
log.Println("Database created successfully")
err = model.CreateTables()
if err != nil {
log.Fatalln(err)
initDb = "Tables cannot be created"
}
initDb = "Database and tables created successfully"
}
func initializeAccountCodes() bool {
err := model.InitializeAccountCodes()
if err != nil {
log.Println(err)
return false
}
return true
}
func findAllAccountCodes() string {
accountCodes, err := model.FindAllAccountCodes()
if err != nil {
log.Println(err)
return utils.RespondWithError(500, "Unknown error")
}
return utils.RespondWithJSON(accountCodes)
}
func findAccountCodeByCode(code string) string {
accountCode, err := model.FindAccountCodeByCode(code)
if err != nil {
accountCodeError, ok := err.(*cashbookerror.AccountCodeError)
if ok {
log.Println(err)
switch {
case accountCodeError.Code == 404:
return utils.RespondWithError(accountCodeError.Code, accountCodeError.Err.Error())
default:
return utils.RespondWithError(500, "Unknown error")
}
}
}
return utils.RespondWithJSON(accountCode)
}
func createAccountCode(accountCode model.AccountCode) string {
createdAccCode, err := model.AddAccountCode(accountCode)
if err != nil {
log.Println(err)
return utils.RespondWithError(500, "Account could not be created due to unknown reasons")
}
return utils.RespondWithJSON(createdAccCode)
}
func updateAccountCode(accountCode model.AccountCode) string {
updatedAccCode, err := model.UpdateAccountCode(accountCode)
if err != nil {
log.Println(err)
return utils.RespondWithError(500, "Account code could not be updated")
}
return utils.RespondWithJSON(updatedAccCode)
}
func main() {
js := mewn.String("./frontend/dist/my-app/main.js")
css := mewn.String("./frontend/dist/my-app/styles.css")
app := wails.CreateApp(&wails.AppConfig{
Width: 1024,
Height: 768,
Title: "CashBook",
JS: js,
CSS: css,
Colour: "#131313",
})
app.Bind(startup)
app.Bind(findAllAccountCodes)
app.Bind(findAccountCodeByCode)
app.Bind(createAccountCode)
app.Bind(updateAccountCode)
app.Bind(initializeAccountCodes)
app.Run()
}