160 lines
5.0 KiB
Go
160 lines
5.0 KiB
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"lostcavewireplanner/internal/models"
|
|
)
|
|
|
|
func (s *Store) ModelGetAll() ([]models.DeviceModel, error) {
|
|
rows, err := s.DB.Query(`SELECT id, name, manufacturer, is_rack_mountable, height_units, front_image, back_image, is_patch_panel, is_wall_socket, is_power_strip, created_at, updated_at FROM device_models ORDER BY name`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var list []models.DeviceModel
|
|
for rows.Next() {
|
|
var m models.DeviceModel
|
|
if err := rows.Scan(&m.ID, &m.Name, &m.Manufacturer, &m.IsRackMountable, &m.HeightUnits,
|
|
&m.FrontImage, &m.BackImage, &m.IsPatchPanel, &m.IsWallSocket, &m.IsPowerStrip, &m.CreatedAt, &m.UpdatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
list = append(list, m)
|
|
}
|
|
return list, rows.Err()
|
|
}
|
|
|
|
func (s *Store) ModelGetByID(id int64) (*models.DeviceModel, error) {
|
|
m := &models.DeviceModel{}
|
|
err := s.DB.QueryRow(`SELECT id, name, manufacturer, is_rack_mountable, height_units, front_image, back_image, is_patch_panel, is_wall_socket, is_power_strip, created_at, updated_at FROM device_models WHERE id = ?`, id).
|
|
Scan(&m.ID, &m.Name, &m.Manufacturer, &m.IsRackMountable, &m.HeightUnits,
|
|
&m.FrontImage, &m.BackImage, &m.IsPatchPanel, &m.IsWallSocket, &m.IsPowerStrip, &m.CreatedAt, &m.UpdatedAt)
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
m.Ports, err = s.ModelPortGetByModelID(m.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func (s *Store) modelGetByIDTx(tx *sql.Tx, id int64) (*models.DeviceModel, error) {
|
|
m := &models.DeviceModel{}
|
|
err := tx.QueryRow(`SELECT id, name, manufacturer, is_rack_mountable, height_units, front_image, back_image, is_patch_panel, is_wall_socket, is_power_strip FROM device_models WHERE id = ?`, id).
|
|
Scan(&m.ID, &m.Name, &m.Manufacturer, &m.IsRackMountable, &m.HeightUnits,
|
|
&m.FrontImage, &m.BackImage, &m.IsPatchPanel, &m.IsWallSocket, &m.IsPowerStrip)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
portRows, err := tx.Query(`SELECT id, device_model_id, name, side, position FROM device_model_ports WHERE device_model_id = ? ORDER BY side, position`, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer portRows.Close()
|
|
for portRows.Next() {
|
|
var p models.DeviceModelPort
|
|
if err := portRows.Scan(&p.ID, &p.DeviceModelID, &p.Name, &p.Side, &p.Position); err != nil {
|
|
return nil, err
|
|
}
|
|
m.Ports = append(m.Ports, p)
|
|
}
|
|
return m, portRows.Err()
|
|
}
|
|
|
|
func (s *Store) ModelCreate(m *models.DeviceModel, imageDir string) (int64, error) {
|
|
tx, err := s.DB.Begin()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
res, err := tx.Exec(`INSERT INTO device_models (name, manufacturer, is_rack_mountable, height_units, front_image, back_image, is_patch_panel, is_wall_socket, is_power_strip) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
m.Name, m.Manufacturer, m.IsRackMountable, m.HeightUnits, m.FrontImage, m.BackImage, m.IsPatchPanel, m.IsWallSocket, m.IsPowerStrip)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
modelID, err := res.LastInsertId()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
for i := range m.Ports {
|
|
m.Ports[i].DeviceModelID = modelID
|
|
}
|
|
if err := s.modelPortCreateTx(tx, modelID, m.Ports); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
return 0, err
|
|
}
|
|
return modelID, nil
|
|
}
|
|
|
|
func (s *Store) ModelUpdate(m *models.DeviceModel) error {
|
|
tx, err := s.DB.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
_, err = tx.Exec(`UPDATE device_models SET name=?, manufacturer=?, is_rack_mountable=?, height_units=?, front_image=?, back_image=?, is_patch_panel=?, is_wall_socket=?, is_power_strip=?, updated_at=datetime('now') WHERE id=?`,
|
|
m.Name, m.Manufacturer, m.IsRackMountable, m.HeightUnits, m.FrontImage, m.BackImage, m.IsPatchPanel, m.IsWallSocket, m.IsPowerStrip, m.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := tx.Exec(`DELETE FROM device_model_ports WHERE device_model_id = ?`, m.ID); err != nil {
|
|
return err
|
|
}
|
|
|
|
for i := range m.Ports {
|
|
m.Ports[i].DeviceModelID = m.ID
|
|
}
|
|
if err := s.modelPortCreateTx(tx, m.ID, m.Ports); err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.Commit()
|
|
}
|
|
|
|
func (s *Store) ModelDelete(id int64) error {
|
|
_, err := s.DB.Exec(`DELETE FROM device_models WHERE id=?`, id)
|
|
return err
|
|
}
|
|
|
|
func (s *Store) ModelPortGetByModelID(modelID int64) ([]models.DeviceModelPort, error) {
|
|
rows, err := s.DB.Query(`SELECT id, device_model_id, name, side, position FROM device_model_ports WHERE device_model_id = ? ORDER BY side, position`, modelID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var ports []models.DeviceModelPort
|
|
for rows.Next() {
|
|
var p models.DeviceModelPort
|
|
if err := rows.Scan(&p.ID, &p.DeviceModelID, &p.Name, &p.Side, &p.Position); err != nil {
|
|
return nil, err
|
|
}
|
|
ports = append(ports, p)
|
|
}
|
|
return ports, rows.Err()
|
|
}
|
|
|
|
func (s *Store) modelPortCreateTx(tx *sql.Tx, modelID int64, ports []models.DeviceModelPort) error {
|
|
for _, p := range ports {
|
|
_, err := tx.Exec(`INSERT INTO device_model_ports (device_model_id, name, side, position) VALUES (?, ?, ?, ?)`,
|
|
modelID, p.Name, p.Side, p.Position)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|