Commit ba5a0667 authored by bensinober's avatar bensinober
Browse files

Add user update method

parent 145f6900
......@@ -8,6 +8,14 @@ import (
)
const (
sqlGetClient = `SELECT c.id,c.name,c.hwaddr,c.ipaddr,c.shorttime,sr.resolution,
d.id,d.name AS dept_name,
b.id AS branch_id, b.name AS branch_name
FROM clients c
JOIN screen_resolutions sr ON (c.screen_resolution_id=sr.id)
JOIN departments d ON (d.id=c.department_id)
JOIN branches b ON (b.id=d.branch_id)
WHERE c.id = ?`
sqlGetClientByNameOrMac = `SELECT c.id,c.name,c.hwaddr,c.ipaddr,c.shorttime,sr.resolution,
d.id,d.name AS dept_name,
b.id AS branch_id, b.name AS branch_name
......@@ -57,9 +65,47 @@ func NewDB(conn *sql.DB) *DB {
return &DB{conn: conn}
}
func (d *DB) GetClient(id *int) (*client.Client, error) {
var cl client.Client
var conn client.Connection
var dep client.Department
var b client.Branch
err := d.conn.QueryRow(sqlGetClient, id).Scan(&cl.Id, &cl.Name, &conn.MAC, &conn.IP, &cl.ShortTime, &cl.ScreenRes, &dep.Id, &dep.Name, &b.Id, &b.Name)
if err != nil {
if err == sql.ErrNoRows {
return &cl, nil
} else {
return &cl, err
}
}
cl.Connection = conn
cl.Department = dep
cl.Branch = b
// Append user if active
u, err := d.GetClientActiveUser(cl.Id)
if err != nil {
return &cl, err
}
if u.Id > 0 { // dont add empty user struct
cl.User = u
}
// Append client options
opts, err := d.GetClientOptions(cl.Id)
if err != nil {
return &cl, err
}
cl.Options = opts
p, err := d.GetClientPrinters(b.Id)
if err != nil {
return &cl, err
}
cl.Printers = p
return &cl, nil
}
func (d *DB) GetClientByNameOrMac(param string) (*client.Client, error) {
var cl client.Client
//var co client.ClientOptions
var conn client.Connection
var dep client.Department
var b client.Branch
......@@ -97,6 +143,19 @@ func (d *DB) GetClientByNameOrMac(param string) (*client.Client, error) {
return &cl, nil
}
func (d *DB) GetUserByUsername(username string) (*client.User, error) {
var u client.User
err := d.conn.QueryRow(sqlGetUserByUsername, username).Scan(&u.Id, &u.Username, &u.Name, &u.Minutes, &u.Age, &u.Type, &u.Client)
if err != nil {
if err == sql.ErrNoRows {
return &u, nil
} else {
return &u, err
}
}
return &u, nil
}
func (d *DB) GetClientActiveUser(id int) (*client.User, error) {
var u client.User
err := d.conn.QueryRow(sqlGetClientActiveUser, id).Scan(&u.Id, &u.Username, &u.Name, &u.Minutes, &u.Age, &u.Type, &u.Client)
......@@ -137,7 +196,7 @@ func (d *DB) UpsertUser(c *client.Client, authResp client.User) (*client.User, e
}
}
c.User = &u
if _, err := d.UpdateUser(c); err != nil {
if _, err := d.UpdateUser(&u); err != nil {
log.Println(err)
return &u, err
}
......@@ -149,8 +208,8 @@ func (d *DB) InsertUser(authResp client.User) (sql.Result, error) {
return d.conn.Exec(sqlInsertUser, authResp.Username, authResp.Name, authResp.Minutes, authResp.Age, authResp.Type)
}
func (d *DB) UpdateUser(c *client.Client) (sql.Result, error) {
return d.conn.Exec(sqlUpdateUser, c.User.Username, c.User.Name, c.User.Minutes, c.Id, c.User.Id)
func (d *DB) UpdateUser(u *client.User) (sql.Result, error) {
return d.conn.Exec(sqlUpdateUser, u.Username, u.Name, u.Minutes, u.Client, u.Id)
}
// Hierarcy is Organization -> Branch -> Department -> Client
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment