mirror of
https://github.com/idanoo/autobrr
synced 2025-07-22 16:29:12 +00:00
Fix: update download client (#23)
* feat: use password fields * fix: update download client and better logs
This commit is contained in:
parent
d4aa2027c0
commit
f78ca85df2
4 changed files with 91 additions and 64 deletions
|
@ -21,7 +21,8 @@ func (r *DownloadClientRepo) List() ([]domain.DownloadClient, error) {
|
|||
|
||||
rows, err := r.db.Query("SELECT id, name, type, enabled, host, port, ssl, username, password, settings FROM client")
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
log.Error().Stack().Err(err).Msg("could not query download client rows")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -33,14 +34,13 @@ func (r *DownloadClientRepo) List() ([]domain.DownloadClient, error) {
|
|||
var settingsJsonStr string
|
||||
|
||||
if err := rows.Scan(&f.ID, &f.Name, &f.Type, &f.Enabled, &f.Host, &f.Port, &f.SSL, &f.Username, &f.Password, &settingsJsonStr); err != nil {
|
||||
log.Error().Err(err)
|
||||
}
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("could not scan download client to struct")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if settingsJsonStr != "" {
|
||||
if err := json.Unmarshal([]byte(settingsJsonStr), &f.Settings); err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("could not marshal download client settings %v", settingsJsonStr)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,7 @@ func (r *DownloadClientRepo) List() ([]domain.DownloadClient, error) {
|
|||
clients = append(clients, f)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
log.Error().Stack().Err(err).Msg("could not query download client rows")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -62,6 +63,7 @@ func (r *DownloadClientRepo) FindByID(id int32) (*domain.DownloadClient, error)
|
|||
|
||||
row := r.db.QueryRow(query, id)
|
||||
if err := row.Err(); err != nil {
|
||||
log.Error().Stack().Err(err).Msg("could not query download client rows")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -69,12 +71,13 @@ func (r *DownloadClientRepo) FindByID(id int32) (*domain.DownloadClient, error)
|
|||
var settingsJsonStr string
|
||||
|
||||
if err := row.Scan(&client.ID, &client.Name, &client.Type, &client.Enabled, &client.Host, &client.Port, &client.SSL, &client.Username, &client.Password, &settingsJsonStr); err != nil {
|
||||
log.Error().Err(err).Msg("could not scan download client to struct")
|
||||
log.Error().Stack().Err(err).Msg("could not scan download client to struct")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if settingsJsonStr != "" {
|
||||
if err := json.Unmarshal([]byte(settingsJsonStr), &client.Settings); err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("could not marshal download client settings %v", settingsJsonStr)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
@ -82,42 +85,6 @@ func (r *DownloadClientRepo) FindByID(id int32) (*domain.DownloadClient, error)
|
|||
return &client, nil
|
||||
}
|
||||
|
||||
func (r *DownloadClientRepo) FindByActionID(actionID int) ([]domain.DownloadClient, error) {
|
||||
|
||||
rows, err := r.db.Query("SELECT id, name, type, enabled, host, port, ssl, username, password, settings FROM client, action_client WHERE client.id = action_client.client_id AND action_client.action_id = ?", actionID)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
var clients []domain.DownloadClient
|
||||
for rows.Next() {
|
||||
var f domain.DownloadClient
|
||||
var settingsJsonStr string
|
||||
|
||||
if err := rows.Scan(&f.ID, &f.Name, &f.Type, &f.Enabled, &f.Host, &f.Port, &f.SSL, &f.Username, &f.Password, &settingsJsonStr); err != nil {
|
||||
log.Error().Err(err)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if settingsJsonStr != "" {
|
||||
if err := json.Unmarshal([]byte(settingsJsonStr), &f.Settings); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
clients = append(clients, f)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return clients, nil
|
||||
}
|
||||
|
||||
func (r *DownloadClientRepo) Store(client domain.DownloadClient) (*domain.DownloadClient, error) {
|
||||
var err error
|
||||
|
||||
|
@ -128,40 +95,95 @@ func (r *DownloadClientRepo) Store(client domain.DownloadClient) (*domain.Downlo
|
|||
|
||||
settingsJson, err := json.Marshal(&settings)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not marshal download client settings %v", settings)
|
||||
log.Error().Stack().Err(err).Msgf("could not marshal download client settings %v", settings)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if client.ID != 0 {
|
||||
log.Info().Msg("UPDATE existing record")
|
||||
_, err = r.db.Exec(`UPDATE client SET name = ?, type = ?, enabled = ?, host = ?, port = ?, ssl = ?, username = ?, password = ?, settings = json_set(?) WHERE id = ?`, client.Name, client.Type, client.Enabled, client.Host, client.Port, client.SSL, client.Username, client.Password, client.ID, settingsJson)
|
||||
_, err = r.db.Exec(`
|
||||
UPDATE
|
||||
client
|
||||
SET
|
||||
name = ?,
|
||||
type = ?,
|
||||
enabled = ?,
|
||||
host = ?,
|
||||
port = ?,
|
||||
ssl = ?,
|
||||
username = ?,
|
||||
password = ?,
|
||||
settings = json_set(?)
|
||||
WHERE
|
||||
id = ?`,
|
||||
client.Name,
|
||||
client.Type,
|
||||
client.Enabled,
|
||||
client.Host,
|
||||
client.Port,
|
||||
client.SSL,
|
||||
client.Username,
|
||||
client.Password,
|
||||
settingsJson,
|
||||
client.ID,
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("could not update download client: %v", client)
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
var res sql.Result
|
||||
|
||||
res, err = r.db.Exec(`INSERT INTO client(name, type, enabled, host, port, ssl, username, password, settings)
|
||||
VALUES (?, ?, ?, ?, ?, ? , ?, ?, json_set(?)) ON CONFLICT DO NOTHING`, client.Name, client.Type, client.Enabled, client.Host, client.Port, client.SSL, client.Username, client.Password, settingsJson)
|
||||
res, err = r.db.Exec(`INSERT INTO
|
||||
client(
|
||||
name,
|
||||
type,
|
||||
enabled,
|
||||
host,
|
||||
port,
|
||||
ssl,
|
||||
username,
|
||||
password,
|
||||
settings)
|
||||
VALUES (?, ?, ?, ?, ?, ? , ?, ?, json_set(?)) ON CONFLICT DO NOTHING`,
|
||||
client.Name,
|
||||
client.Type,
|
||||
client.Enabled,
|
||||
client.Host,
|
||||
client.Port,
|
||||
client.SSL,
|
||||
client.Username,
|
||||
client.Password,
|
||||
settingsJson,
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err)
|
||||
log.Error().Stack().Err(err).Msgf("could not store new download client: %v", client)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resId, _ := res.LastInsertId()
|
||||
log.Info().Msgf("LAST INSERT ID %v", resId)
|
||||
client.ID = int(resId)
|
||||
}
|
||||
|
||||
log.Info().Msgf("store download client: %v", client.Name)
|
||||
log.Trace().Msgf("store download client: %+v", client)
|
||||
|
||||
return &client, nil
|
||||
}
|
||||
|
||||
func (r *DownloadClientRepo) Delete(clientID int) error {
|
||||
res, err := r.db.Exec(`DELETE FROM client WHERE client.id = ?`, clientID)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("could not delete download client: %d", clientID)
|
||||
return err
|
||||
}
|
||||
|
||||
rows, _ := res.RowsAffected()
|
||||
|
||||
log.Info().Msgf("rows affected %v", rows)
|
||||
if rows == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info().Msgf("delete download client: %d", clientID)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue