Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
akhil-datla committed Feb 22, 2023
1 parent 4a8ecdc commit 6273199
Show file tree
Hide file tree
Showing 41 changed files with 7,413 additions and 130 deletions.
Binary file removed EZCheck_1.0_darwin_arm64/EZCheck
Binary file not shown.
27 changes: 7 additions & 20 deletions components/machines/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,31 @@ package machines

import (
"main/components/postgresmanager"
"strconv"
"main/components/types"
)

type Action struct {
ActionID int `json:"action_id"`
}

func AddAction(machineID string, actionID string) error {
machine := Machine{ID: machineID}
err := postgresmanager.Query(&machine, &machine)
if err != nil {
return err
}
var machine types.Machine
err := postgresmanager.Query(&types.Machine{ID: machineID}, &machine)

actionInt, err := strconv.Atoi(actionID)
if err != nil {
return err
}

machine.Actions = append(machine.Actions, Action{ActionID: actionInt})
machine.Actions = append(machine.Actions, actionID)

return postgresmanager.Update(machine, &machine)
}

func DeleteAction(machineID string, actionID string) error {
machine := Machine{ID: machineID}
err := postgresmanager.Query(&machine, &machine)
if err != nil {
return err
}

actionInt, err := strconv.Atoi(actionID)
var machine types.Machine
err := postgresmanager.Query(&types.Machine{ID: machineID}, &machine)
if err != nil {
return err
}

for i, action := range machine.Actions {
if action.ActionID == actionInt {
if action == actionID {
machine.Actions = append(machine.Actions[:i], machine.Actions[i+1:]...)
break
}
Expand Down
43 changes: 24 additions & 19 deletions components/machines/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,51 @@ import (
"fmt"
"main/components/log"
"main/components/postgresmanager"
"time"
)
"main/components/types"

type Machine struct {
ID string `json:"id" gorm:"primaryKey"`
InUSE bool `json:"in_use" gorm:"index"`
Actions []Action `json:"actions" gorm:"foreignKey:ActionID"`
CreatedAt time.Time `json:"-" gorm:"index"`
UpdatedAt time.Time `json:"-" gorm:"index"`
}
"github.com/lib/pq"
)

func CreateMachine(id string) error {
actions := make([]Action, 0)
machine := Machine{ID: id, InUSE: false, Actions: actions}
actions := make(pq.StringArray, 0)
machine := types.Machine{ID: id, InUSE: false, Actions: actions}
return postgresmanager.Save(&machine)
}

func GetMachines() []Machine {
var machines []Machine
func GetMachines() []*types.Machine {
var machines []*types.Machine

postgresmanager.QueryAll(&machines)

return machines
}

func (m *Machine) SignIn() ([]Action, error) {
return m.Actions, postgresmanager.Update(m, &Machine{InUSE: true})
func SignIn(machineID string) ([]string, error) {
var m *types.Machine
err := postgresmanager.Query(types.Machine{ID: machineID}, &m)
if err != nil {
return nil, err
}

return m.Actions, postgresmanager.Update(m, &types.Machine{InUSE: true})
}

func SignOut(name, machineID string) error {
var m *Machine
err := postgresmanager.Query(Machine{ID: machineID}, &m)
var m *types.Machine
err := postgresmanager.Query(types.Machine{ID: machineID}, &m)
if err != nil {
return err
}

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

func DeleteMachine(id string) error {
return postgresmanager.Delete(Machine{ID: id})
err := postgresmanager.ClearAssociations(&types.Machine{ID: id}, "Users")
if err != nil {
return err
}

return postgresmanager.Delete(types.Machine{ID: id})
}
40 changes: 40 additions & 0 deletions components/types/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package types

import (
"time"
"github.com/lib/pq"
)


type User struct {
ID string `json:"id" gorm:"primaryKey"`
Username string `json:"username" gorm:"uniqueIndex"`
Password string `json:"password"`
FirstName string `json:"first" gorm:"index"`
LastName string `json:"last" gorm:"index"`
Grade string `json:"grade" gorm:"index"`
Code string `json:"code" gorm:"uniqueIndex"`
Machines []*Machine `gorm:"many2many:users_machines"`
CreatedAt time.Time `json:"-" gorm:"index"`
UpdatedAt time.Time `json:"-" gorm:"index"`
}

type Admin struct {
ID string `json:"id" gorm:"primaryKey"`
Username string `json:"username" gorm:"uniqueIndex"`
Password string `json:"password"`
FirstName string `json:"first" gorm:"index"`
LastName string `json:"last" gorm:"index"`
Code string `json:"code" gorm:"uniqueIndex"`
CreatedAt time.Time `json:"-" gorm:"index"`
UpdatedAt time.Time `json:"-" gorm:"index"`
}

type Machine struct {
ID string `json:"id" gorm:"primaryKey"`
InUSE bool `json:"in_use" gorm:"index"`
Users []*User `gorm:"many2many:users_machines"`
Actions pq.StringArray `json:"actions" gorm:"type:text[]"`
CreatedAt time.Time `json:"-" gorm:"index"`
UpdatedAt time.Time `json:"-" gorm:"index"`
}
80 changes: 32 additions & 48 deletions components/users/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,14 @@ import (
"main/components/log"
"main/components/machines"
"main/components/postgresmanager"
"time"
"main/components/types"

"golang.org/x/crypto/bcrypt"
)

type Admin struct {
ID string `json:"id" gorm:"primaryKey"`
Username string `json:"username" gorm:"uniqueIndex"`
Password string `json:"password"`
FirstName string `json:"first" gorm:"index"`
LastName string `json:"last" gorm:"index"`
Code string `json:"code" gorm:"uniqueIndex"`
CreatedAt time.Time `json:"-" gorm:"index"`
UpdatedAt time.Time `json:"-" gorm:"index"`
}

func CreateAdmin(username, password, firstName, lastName, code string) error {
var a *Admin
err := postgresmanager.Query(&Admin{Code: code}, &a)
var a *types.Admin
err := postgresmanager.Query(&types.Admin{Code: code}, &a)

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

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

err = postgresmanager.Save(&admin)

return err
}

func GetAdmin(username string) (Admin, error) {
var admin Admin
err := postgresmanager.Query(&Admin{Username: username}, &admin)
func GetAdmin(username string) (*types.Admin, error) {
var admin *types.Admin
err := postgresmanager.Query(&types.Admin{Username: username}, &admin)
if err != nil {
return Admin{}, err
return nil, err
}

admin.Password = ""
return admin, nil
}

func CertifyUser(adminID, userID, machineID string) error {
var user *User
var admin *Admin
var machine *machines.Machine
err := postgresmanager.Query(User{ID: userID}, &user)
var user *types.User
var admin *types.Admin
var machine *types.Machine
err := postgresmanager.Query(types.User{ID: userID}, &user)
if err != nil {
return err
}

err = postgresmanager.Query(Admin{ID: adminID}, &admin)
err = postgresmanager.Query(types.Admin{ID: adminID}, &admin)
if err != nil {
return err
}

err = postgresmanager.Query(machines.Machine{ID: machineID}, &machine)
err = postgresmanager.Query(types.Machine{ID: machineID}, &machine)
if err != nil {
return err
}
Expand All @@ -81,20 +70,20 @@ func CertifyUser(adminID, userID, machineID string) error {
}

func UncertifyUser(adminID, userID, machineID string) error {
var user *User
var admin *Admin
var machine *machines.Machine
err := postgresmanager.Query(User{ID: userID}, &user)
var user *types.User
var admin *types.Admin
var machine *types.Machine
err := postgresmanager.Query(types.User{ID: userID}, &user)
if err != nil {
return err
}

err = postgresmanager.Query(Admin{ID: adminID}, &admin)
err = postgresmanager.Query(types.Admin{ID: adminID}, &admin)
if err != nil {
return err
}

err = postgresmanager.Query(machines.Machine{ID: machineID}, &machine)
err = postgresmanager.Query(types.Machine{ID: machineID}, &machine)
if err != nil {
return err
}
Expand All @@ -105,13 +94,13 @@ func UncertifyUser(adminID, userID, machineID string) error {
return err
}

func SearchUsers(query map[string]interface{}) []User {
var users []User
func SearchUsers(query map[string]interface{}) []types.User {
var users []types.User

if len(query) == 0 {
postgresmanager.QueryAll(&users)
for i, u := range users {
var machines []*machines.Machine
var machines []*types.Machine
u.Password = ""
postgresmanager.ReadAssociation(&u, "Machines", &machines)
u.Machines = machines
Expand All @@ -122,18 +111,18 @@ func SearchUsers(query map[string]interface{}) []User {

postgresmanager.GroupQuery(query, &users)
for i, u := range users {
var machines []*machines.Machine
var machines []*types.Machine
u.Password = ""
postgresmanager.ReadAssociation(&u, "Machines", &machines)
u.Machines = machines
users[i] = u
}

return users
}

func SearchAdmins(query map[string]interface{}) []Admin {
var admins []Admin
func SearchAdmins(query map[string]interface{}) []*types.Admin {
var admins []*types.Admin

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

func AuthenticateAdmin(code, machineID string) (string, error) {
var admin *Admin

if err := postgresmanager.Query(&Admin{Code: code}, &admin); err != nil {
return "", err
}
var admin *types.Admin

var machine *machines.Machine
if err := postgresmanager.Query(&machines.Machine{ID: machineID}, &machine); err != nil {
if err := postgresmanager.Query(&types.Admin{Code: code}, &admin); err != nil {
return "", err
}

actions, err := machine.SignIn()
actions, err := machines.SignIn(machineID)
if err != nil {
log.Log(fmt.Sprintf("%s failed to sign in to machine %s", admin.Username, machineID))
return "", err
}

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

func DeleteAdmin(id string) error {
return postgresmanager.Delete(Admin{ID: id})
return postgresmanager.Delete(types.Admin{ID: id})
}

func HashPassword(password string) (string, error) {
Expand Down
Loading

0 comments on commit 6273199

Please sign in to comment.