Skip to content

Commit 6273199

Browse files
committed
Refactor code
1 parent 4a8ecdc commit 6273199

41 files changed

Lines changed: 7413 additions & 130 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

EZCheck_1.0_darwin_arm64/EZCheck

-11.6 MB
Binary file not shown.

components/machines/actions.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,31 @@ package machines
22

33
import (
44
"main/components/postgresmanager"
5-
"strconv"
5+
"main/components/types"
66
)
77

8-
type Action struct {
9-
ActionID int `json:"action_id"`
10-
}
11-
128
func AddAction(machineID string, actionID string) error {
13-
machine := Machine{ID: machineID}
14-
err := postgresmanager.Query(&machine, &machine)
15-
if err != nil {
16-
return err
17-
}
9+
var machine types.Machine
10+
err := postgresmanager.Query(&types.Machine{ID: machineID}, &machine)
1811

19-
actionInt, err := strconv.Atoi(actionID)
2012
if err != nil {
2113
return err
2214
}
2315

24-
machine.Actions = append(machine.Actions, Action{ActionID: actionInt})
16+
machine.Actions = append(machine.Actions, actionID)
2517

2618
return postgresmanager.Update(machine, &machine)
2719
}
2820

2921
func DeleteAction(machineID string, actionID string) error {
30-
machine := Machine{ID: machineID}
31-
err := postgresmanager.Query(&machine, &machine)
32-
if err != nil {
33-
return err
34-
}
35-
36-
actionInt, err := strconv.Atoi(actionID)
22+
var machine types.Machine
23+
err := postgresmanager.Query(&types.Machine{ID: machineID}, &machine)
3724
if err != nil {
3825
return err
3926
}
4027

4128
for i, action := range machine.Actions {
42-
if action.ActionID == actionInt {
29+
if action == actionID {
4330
machine.Actions = append(machine.Actions[:i], machine.Actions[i+1:]...)
4431
break
4532
}

components/machines/machines.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,51 @@ import (
44
"fmt"
55
"main/components/log"
66
"main/components/postgresmanager"
7-
"time"
8-
)
7+
"main/components/types"
98

10-
type Machine struct {
11-
ID string `json:"id" gorm:"primaryKey"`
12-
InUSE bool `json:"in_use" gorm:"index"`
13-
Actions []Action `json:"actions" gorm:"foreignKey:ActionID"`
14-
CreatedAt time.Time `json:"-" gorm:"index"`
15-
UpdatedAt time.Time `json:"-" gorm:"index"`
16-
}
9+
"github.com/lib/pq"
10+
)
1711

1812
func CreateMachine(id string) error {
19-
actions := make([]Action, 0)
20-
machine := Machine{ID: id, InUSE: false, Actions: actions}
13+
actions := make(pq.StringArray, 0)
14+
machine := types.Machine{ID: id, InUSE: false, Actions: actions}
2115
return postgresmanager.Save(&machine)
2216
}
2317

24-
func GetMachines() []Machine {
25-
var machines []Machine
18+
func GetMachines() []*types.Machine {
19+
var machines []*types.Machine
2620

2721
postgresmanager.QueryAll(&machines)
2822

2923
return machines
3024
}
3125

32-
func (m *Machine) SignIn() ([]Action, error) {
33-
return m.Actions, postgresmanager.Update(m, &Machine{InUSE: true})
26+
func SignIn(machineID string) ([]string, error) {
27+
var m *types.Machine
28+
err := postgresmanager.Query(types.Machine{ID: machineID}, &m)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
return m.Actions, postgresmanager.Update(m, &types.Machine{InUSE: true})
3434
}
3535

3636
func SignOut(name, machineID string) error {
37-
var m *Machine
38-
err := postgresmanager.Query(Machine{ID: machineID}, &m)
37+
var m *types.Machine
38+
err := postgresmanager.Query(types.Machine{ID: machineID}, &m)
3939
if err != nil {
4040
return err
4141
}
4242

4343
log.Log(fmt.Sprintf("%s signed out of machine: %s", name, m.ID))
44-
return postgresmanager.Update(m, &Machine{InUSE: false})
44+
return postgresmanager.Update(m, &types.Machine{InUSE: false})
4545
}
4646

4747
func DeleteMachine(id string) error {
48-
return postgresmanager.Delete(Machine{ID: id})
48+
err := postgresmanager.ClearAssociations(&types.Machine{ID: id}, "Users")
49+
if err != nil {
50+
return err
51+
}
52+
53+
return postgresmanager.Delete(types.Machine{ID: id})
4954
}

components/types/types.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package types
2+
3+
import (
4+
"time"
5+
"github.com/lib/pq"
6+
)
7+
8+
9+
type User struct {
10+
ID string `json:"id" gorm:"primaryKey"`
11+
Username string `json:"username" gorm:"uniqueIndex"`
12+
Password string `json:"password"`
13+
FirstName string `json:"first" gorm:"index"`
14+
LastName string `json:"last" gorm:"index"`
15+
Grade string `json:"grade" gorm:"index"`
16+
Code string `json:"code" gorm:"uniqueIndex"`
17+
Machines []*Machine `gorm:"many2many:users_machines"`
18+
CreatedAt time.Time `json:"-" gorm:"index"`
19+
UpdatedAt time.Time `json:"-" gorm:"index"`
20+
}
21+
22+
type Admin struct {
23+
ID string `json:"id" gorm:"primaryKey"`
24+
Username string `json:"username" gorm:"uniqueIndex"`
25+
Password string `json:"password"`
26+
FirstName string `json:"first" gorm:"index"`
27+
LastName string `json:"last" gorm:"index"`
28+
Code string `json:"code" gorm:"uniqueIndex"`
29+
CreatedAt time.Time `json:"-" gorm:"index"`
30+
UpdatedAt time.Time `json:"-" gorm:"index"`
31+
}
32+
33+
type Machine struct {
34+
ID string `json:"id" gorm:"primaryKey"`
35+
InUSE bool `json:"in_use" gorm:"index"`
36+
Users []*User `gorm:"many2many:users_machines"`
37+
Actions pq.StringArray `json:"actions" gorm:"type:text[]"`
38+
CreatedAt time.Time `json:"-" gorm:"index"`
39+
UpdatedAt time.Time `json:"-" gorm:"index"`
40+
}

components/users/admin.go

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,14 @@ import (
66
"main/components/log"
77
"main/components/machines"
88
"main/components/postgresmanager"
9-
"time"
9+
"main/components/types"
1010

1111
"golang.org/x/crypto/bcrypt"
1212
)
1313

14-
type Admin struct {
15-
ID string `json:"id" gorm:"primaryKey"`
16-
Username string `json:"username" gorm:"uniqueIndex"`
17-
Password string `json:"password"`
18-
FirstName string `json:"first" gorm:"index"`
19-
LastName string `json:"last" gorm:"index"`
20-
Code string `json:"code" gorm:"uniqueIndex"`
21-
CreatedAt time.Time `json:"-" gorm:"index"`
22-
UpdatedAt time.Time `json:"-" gorm:"index"`
23-
}
24-
2514
func CreateAdmin(username, password, firstName, lastName, code string) error {
26-
var a *Admin
27-
err := postgresmanager.Query(&Admin{Code: code}, &a)
15+
var a *types.Admin
16+
err := postgresmanager.Query(&types.Admin{Code: code}, &a)
2817

2918
if err != nil {
3019
if err.Error() != "record not found" {
@@ -37,39 +26,39 @@ func CreateAdmin(username, password, firstName, lastName, code string) error {
3726
return err
3827
}
3928

40-
admin := Admin{ID: internal.GenerateUUID(), Username: username, Password: hashedPassword, FirstName: firstName, LastName: lastName, Code: code}
29+
admin := types.Admin{ID: internal.GenerateUUID(), Username: username, Password: hashedPassword, FirstName: firstName, LastName: lastName, Code: code}
4130

4231
err = postgresmanager.Save(&admin)
4332

4433
return err
4534
}
4635

47-
func GetAdmin(username string) (Admin, error) {
48-
var admin Admin
49-
err := postgresmanager.Query(&Admin{Username: username}, &admin)
36+
func GetAdmin(username string) (*types.Admin, error) {
37+
var admin *types.Admin
38+
err := postgresmanager.Query(&types.Admin{Username: username}, &admin)
5039
if err != nil {
51-
return Admin{}, err
40+
return nil, err
5241
}
5342

5443
admin.Password = ""
5544
return admin, nil
5645
}
5746

5847
func CertifyUser(adminID, userID, machineID string) error {
59-
var user *User
60-
var admin *Admin
61-
var machine *machines.Machine
62-
err := postgresmanager.Query(User{ID: userID}, &user)
48+
var user *types.User
49+
var admin *types.Admin
50+
var machine *types.Machine
51+
err := postgresmanager.Query(types.User{ID: userID}, &user)
6352
if err != nil {
6453
return err
6554
}
6655

67-
err = postgresmanager.Query(Admin{ID: adminID}, &admin)
56+
err = postgresmanager.Query(types.Admin{ID: adminID}, &admin)
6857
if err != nil {
6958
return err
7059
}
7160

72-
err = postgresmanager.Query(machines.Machine{ID: machineID}, &machine)
61+
err = postgresmanager.Query(types.Machine{ID: machineID}, &machine)
7362
if err != nil {
7463
return err
7564
}
@@ -81,20 +70,20 @@ func CertifyUser(adminID, userID, machineID string) error {
8170
}
8271

8372
func UncertifyUser(adminID, userID, machineID string) error {
84-
var user *User
85-
var admin *Admin
86-
var machine *machines.Machine
87-
err := postgresmanager.Query(User{ID: userID}, &user)
73+
var user *types.User
74+
var admin *types.Admin
75+
var machine *types.Machine
76+
err := postgresmanager.Query(types.User{ID: userID}, &user)
8877
if err != nil {
8978
return err
9079
}
9180

92-
err = postgresmanager.Query(Admin{ID: adminID}, &admin)
81+
err = postgresmanager.Query(types.Admin{ID: adminID}, &admin)
9382
if err != nil {
9483
return err
9584
}
9685

97-
err = postgresmanager.Query(machines.Machine{ID: machineID}, &machine)
86+
err = postgresmanager.Query(types.Machine{ID: machineID}, &machine)
9887
if err != nil {
9988
return err
10089
}
@@ -105,13 +94,13 @@ func UncertifyUser(adminID, userID, machineID string) error {
10594
return err
10695
}
10796

108-
func SearchUsers(query map[string]interface{}) []User {
109-
var users []User
97+
func SearchUsers(query map[string]interface{}) []types.User {
98+
var users []types.User
11099

111100
if len(query) == 0 {
112101
postgresmanager.QueryAll(&users)
113102
for i, u := range users {
114-
var machines []*machines.Machine
103+
var machines []*types.Machine
115104
u.Password = ""
116105
postgresmanager.ReadAssociation(&u, "Machines", &machines)
117106
u.Machines = machines
@@ -122,18 +111,18 @@ func SearchUsers(query map[string]interface{}) []User {
122111

123112
postgresmanager.GroupQuery(query, &users)
124113
for i, u := range users {
125-
var machines []*machines.Machine
114+
var machines []*types.Machine
126115
u.Password = ""
127116
postgresmanager.ReadAssociation(&u, "Machines", &machines)
128117
u.Machines = machines
129118
users[i] = u
130119
}
131-
120+
132121
return users
133122
}
134123

135-
func SearchAdmins(query map[string]interface{}) []Admin {
136-
var admins []Admin
124+
func SearchAdmins(query map[string]interface{}) []*types.Admin {
125+
var admins []*types.Admin
137126

138127
if len(query) == 0 {
139128
postgresmanager.QueryAll(&admins)
@@ -154,29 +143,24 @@ func SearchAdmins(query map[string]interface{}) []Admin {
154143
}
155144

156145
func AuthenticateAdmin(code, machineID string) (string, error) {
157-
var admin *Admin
158-
159-
if err := postgresmanager.Query(&Admin{Code: code}, &admin); err != nil {
160-
return "", err
161-
}
146+
var admin *types.Admin
162147

163-
var machine *machines.Machine
164-
if err := postgresmanager.Query(&machines.Machine{ID: machineID}, &machine); err != nil {
148+
if err := postgresmanager.Query(&types.Admin{Code: code}, &admin); err != nil {
165149
return "", err
166150
}
167151

168-
actions, err := machine.SignIn()
152+
actions, err := machines.SignIn(machineID)
169153
if err != nil {
170154
log.Log(fmt.Sprintf("%s failed to sign in to machine %s", admin.Username, machineID))
171155
return "", err
172156
}
173157

174-
log.Log(fmt.Sprintf("%s %s (Username: %s) signed in to machine %s", admin.FirstName, admin.LastName, admin.Username, machine.ID))
158+
log.Log(fmt.Sprintf("%s %s (Username: %s) signed in to machine %s", admin.FirstName, admin.LastName, admin.Username, machineID))
175159
return fmt.Sprintf("{\"authorized\": true, \"name\": \"%s %s\", actions: %v}", admin.FirstName, admin.LastName, actions), nil
176160
}
177161

178162
func DeleteAdmin(id string) error {
179-
return postgresmanager.Delete(Admin{ID: id})
163+
return postgresmanager.Delete(types.Admin{ID: id})
180164
}
181165

182166
func HashPassword(password string) (string, error) {

0 commit comments

Comments
 (0)