|
8 | 8 | "time" |
9 | 9 |
|
10 | 10 | "github.com/google/uuid" |
| 11 | + "github.com/jackc/pgx/v5" |
11 | 12 | "github.com/jackc/pgx/v5/pgconn" |
12 | 13 | "github.com/jackc/pgx/v5/pgxpool" |
13 | 14 | "github.com/raphico/go-device-telemetry-api/internal/common/pagination" |
@@ -54,7 +55,7 @@ func (r *CommandRepository) Create(ctx context.Context, c *command.Command) erro |
54 | 55 | return fmt.Errorf("failed to insert command: %w", err) |
55 | 56 | } |
56 | 57 |
|
57 | | - if err := c.UpdateStatus(status); err != nil { |
| 58 | + if err := c.Status.SetStatus(status); err != nil { |
58 | 59 | return fmt.Errorf("corrupt status: %w", err) |
59 | 60 | } |
60 | 61 |
|
@@ -141,3 +142,84 @@ func (r *CommandRepository) FindCommands( |
141 | 142 |
|
142 | 143 | return result, nextCur, nil |
143 | 144 | } |
| 145 | + |
| 146 | +func (r *CommandRepository) FindById( |
| 147 | + ctx context.Context, |
| 148 | + id command.CommandID, |
| 149 | + deviceID device.DeviceID, |
| 150 | +) (*command.Command, error) { |
| 151 | + var ( |
| 152 | + commandID uuid.UUID |
| 153 | + dbDeviceID uuid.UUID |
| 154 | + commandName string |
| 155 | + payload []byte |
| 156 | + status string |
| 157 | + executedAt *time.Time |
| 158 | + createdAt time.Time |
| 159 | + ) |
| 160 | + |
| 161 | + query := ` |
| 162 | + SELECT id, device_id, command_name, payload, status, executed_at, created_at |
| 163 | + FROM commands |
| 164 | + WHERE id = $1 AND device_id = $2 |
| 165 | + ` |
| 166 | + |
| 167 | + err := r.db.QueryRow(ctx, query, id, deviceID).Scan( |
| 168 | + &commandID, |
| 169 | + &dbDeviceID, |
| 170 | + &commandName, |
| 171 | + &payload, |
| 172 | + &status, |
| 173 | + &executedAt, |
| 174 | + &createdAt, |
| 175 | + ) |
| 176 | + |
| 177 | + if err != nil { |
| 178 | + if errors.Is(err, pgx.ErrNoRows) { |
| 179 | + return nil, command.ErrCommandNotFound |
| 180 | + } |
| 181 | + |
| 182 | + return nil, fmt.Errorf("failed to find command by id: %w", err) |
| 183 | + } |
| 184 | + |
| 185 | + return command.RehydrateCommand( |
| 186 | + commandID, |
| 187 | + dbDeviceID, |
| 188 | + commandName, |
| 189 | + payload, |
| 190 | + status, |
| 191 | + executedAt, |
| 192 | + createdAt, |
| 193 | + ) |
| 194 | +} |
| 195 | + |
| 196 | +func (r *CommandRepository) UpdateStatus(ctx context.Context, c *command.Command) error { |
| 197 | + if !c.ExecutedAt.Valid() { |
| 198 | + return fmt.Errorf("invalid executed_at") |
| 199 | + } |
| 200 | + |
| 201 | + query := ` |
| 202 | + UPDATE commands |
| 203 | + SET status = $1, executed_at = $2 |
| 204 | + WHERE id = $3 AND device_id = $4 |
| 205 | + ` |
| 206 | + |
| 207 | + tag, err := r.db.Exec( |
| 208 | + ctx, |
| 209 | + query, |
| 210 | + c.Status.String(), |
| 211 | + c.ExecutedAt.Time(), |
| 212 | + c.ID, |
| 213 | + c.DeviceID, |
| 214 | + ) |
| 215 | + |
| 216 | + if err != nil { |
| 217 | + return fmt.Errorf("failed to update command: %w", err) |
| 218 | + } |
| 219 | + |
| 220 | + if tag.RowsAffected() == 0 { |
| 221 | + return command.ErrCommandNotFound |
| 222 | + } |
| 223 | + |
| 224 | + return nil |
| 225 | +} |
0 commit comments