diff --git a/internal/database/download_client.go b/internal/database/download_client.go index a4043c1..3861eb5 100644 --- a/internal/database/download_client.go +++ b/internal/database/download_client.go @@ -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 } diff --git a/web/src/domain/interfaces.ts b/web/src/domain/interfaces.ts index 157c046..124c7cf 100644 --- a/web/src/domain/interfaces.ts +++ b/web/src/domain/interfaces.ts @@ -117,9 +117,14 @@ export enum DOWNLOAD_CLIENT_TYPES { } export interface DownloadClient { - id: number; + id?: number; name: string; enabled: boolean; + host: string; + port: number; + ssl: boolean; + username: string; + password: string; type: DownloadClientType; settings: object; } diff --git a/web/src/forms/settings/downloadclient/shared.tsx b/web/src/forms/settings/downloadclient/shared.tsx index 7fa3680..0b203a3 100644 --- a/web/src/forms/settings/downloadclient/shared.tsx +++ b/web/src/forms/settings/downloadclient/shared.tsx @@ -1,12 +1,12 @@ import { Fragment } from "react"; import { SwitchGroup, TextFieldWide } from "../../../components/inputs"; -import { NumberFieldWide } from "../../../components/inputs/wide"; +import { NumberFieldWide, PasswordFieldWide } from "../../../components/inputs/wide"; import { useField } from "react-final-form"; function FormFieldsDefault() { return ( - + @@ -15,7 +15,7 @@ function FormFieldsDefault() { - + ); } @@ -24,9 +24,9 @@ function FormFieldsArr() { const { input } = useField("settings.basic.auth"); return ( - + - +
@@ -35,7 +35,7 @@ function FormFieldsArr() { {input.value === true && ( - + )} diff --git a/web/src/screens/settings/DownloadClient.tsx b/web/src/screens/settings/DownloadClient.tsx index 4ec45ea..2206aff 100644 --- a/web/src/screens/settings/DownloadClient.tsx +++ b/web/src/screens/settings/DownloadClient.tsx @@ -15,15 +15,8 @@ interface DownloadLClientSettingsListItemProps { } function DownloadClientSettingsListItem({ client, idx }: DownloadLClientSettingsListItemProps) { - const [enabled, setEnabled] = useState(client.enabled) const [updateClientIsOpen, toggleUpdateClient] = useToggle(false) - const toggleActive = (status: boolean) => { - console.log(status) - setEnabled(status) - // call api - } - return ( {updateClientIsOpen && @@ -32,7 +25,7 @@ function DownloadClientSettingsListItem({ client, idx }: DownloadLClientSettings {client.name} + {client.host} {DownloadClientTypeNameMap[client.type]} @@ -115,6 +109,12 @@ function DownloadClientSettings() { > Name + + Host +