You've already forked gun-manager-backend
71 lines
1.1 KiB
Go
71 lines
1.1 KiB
Go
package sql
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
)
|
|
|
|
func (dbClient *SqlDb) AllGuns() []Gun {
|
|
|
|
var guns []Gun
|
|
|
|
rows, _ := dbClient.db.Query("select guns.*, p.id, p.gun_id, p.filename from guns join photos p on guns.id = p.gun_id order by guns.id")
|
|
defer func(rows *sql.Rows) {
|
|
_ = rows.Close()
|
|
}(rows)
|
|
|
|
for rows.Next() {
|
|
i := Gun{}
|
|
p := Photo{}
|
|
err := rows.Scan(
|
|
&i.Id,
|
|
&i.Make,
|
|
&i.Model,
|
|
&i.SerialNumber,
|
|
&i.PurchaseAmount,
|
|
&i.ValueAmount,
|
|
&i.DatePurchased,
|
|
&i.Notes,
|
|
&p.Id,
|
|
&p.GunId,
|
|
&p.FileName,
|
|
)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
index := -1
|
|
for gunIndex, gun := range guns {
|
|
if gun.Id == i.Id {
|
|
index = gunIndex
|
|
continue
|
|
}
|
|
|
|
index = -1
|
|
}
|
|
|
|
if index == -1 {
|
|
guns = append(guns, i)
|
|
index = len(guns) - 1
|
|
}
|
|
|
|
guns[index].Photos = append(guns[index].Photos, p)
|
|
}
|
|
|
|
return guns
|
|
}
|
|
|
|
func (dbClient *SqlDb) GunById(id uint) (*Gun, error) {
|
|
var gun Gun
|
|
|
|
row, _ := dbClient.db.Query("select guns.*, p.id, p.gun_id, p.filename from guns join photos p on guns.id = p.gun_id where guns.id = ?", id)
|
|
|
|
err := row.Scan(gun)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &gun, nil
|
|
}
|