package postgres import ( "database/sql" "log" "github.com/dustinpianalto/quartermaster" ) type locationService struct { db *sql.DB } func (s locationService) Location(id int) (*quartermaster.Location, error) { var l quartermaster.Location var parent_id sql.NullInt32 queryString := "SELECT id, name, description, parent_id FROM locations WHERE id = $1" row := s.db.QueryRow(queryString, id) err := row.Scan(&l.ID, &l.Name, &l.Description, &parent_id) if err != nil { return nil, err } if parent_id.Valid { p, err := s.Location(int(parent_id.Int32)) if err != nil { return nil, err } l.Parent = p } return &l, nil } func (s locationService) AddLocation(l *quartermaster.Location) (*quartermaster.Location, error) { queryString := "INSERT INTO locations (name, description, parent_id VALUES ($1, $2, $3) RETURNING id" var err error if l.Parent != nil { err = s.db.QueryRow(queryString, l.Name, l.Description, l.Parent.ID).Scan(&l.ID) } else { err = s.db.QueryRow(queryString, l.Name, l.Description, nil).Scan(&l.ID) } return l, err } func (s locationService) UpdateLocation(l *quartermaster.Location) error { queryString := "UPDATE locations SET name = $2, description = $3, parent_id = $4 WHERE id = $1" var err error if l.Parent != nil { _, err = s.db.Exec(queryString, l.ID, l.Name, l.Description, l.Parent.ID) } else { _, err = s.db.Exec(queryString, l.ID, l.Name, l.Description, nil) } return err } func (s locationService) DeleteLocation(l *quartermaster.Location) error { queryString := "DELETE FROM locations WHERE id = $1" _, err := s.db.Exec(queryString, l.ID) return err } func (s locationService) GetChildren(l *quartermaster.Location) ([]*quartermaster.Location, error) { var locations []*quartermaster.Location queryString := "SELECT id FROM locations WHERE parent_id = $1" rows, err := s.db.Query(queryString, l.ID) if err != nil { return nil, err } for rows.Next() { var id int err = rows.Scan(&id) if err != nil { log.Println(err) continue } location, err := s.Location(id) if err != nil { log.Println(err) continue } locations = append(locations, location) } return locations, nil } func (s locationService) GetItems(l *quartermaster.Location) (map[*quartermaster.Item]int, error) { items := make(map[*quartermaster.Item]int) queryString := "SELECT item_id, count FROM x_items_locations WHERE location_id = $1" rows, err := s.db.Query(queryString, l.ID) if err != nil { return nil, err } for rows.Next() { var id, count int err = rows.Scan(&id, &count) if err != nil { log.Println(err) continue } item, err := ItemService.Item(id) if err != nil { log.Println(err) continue } items[item] = count } return items, nil } func (s locationService) GetTopLocations() ([]*quartermaster.Location, error) { var locations []*quartermaster.Location queryString := "SELECT id FROM locations WHERE parent_id IS NULL" rows, err := s.db.Query(queryString) if err != nil { return nil, err } for rows.Next() { var id int err = rows.Scan(&id) if err != nil { log.Println(err) continue } l, err := s.Location(id) if err != nil { log.Println(err) continue } locations = append(locations, l) } return locations, nil }