createAuth creates a new auth object in the database. On success, the ID is set to the new database ID & timestamp fields are set to the current time.
(ctx context.Context, tx *Tx, auth *wtf.Auth)
| 248 | // createAuth creates a new auth object in the database. On success, the |
| 249 | // ID is set to the new database ID & timestamp fields are set to the current time. |
| 250 | func createAuth(ctx context.Context, tx *Tx, auth *wtf.Auth) error { |
| 251 | // Set timestamp fields to current time. |
| 252 | auth.CreatedAt = tx.now |
| 253 | auth.UpdatedAt = auth.CreatedAt |
| 254 | |
| 255 | // Ensure auth object passes basic validation. |
| 256 | if err := auth.Validate(); err != nil { |
| 257 | return err |
| 258 | } |
| 259 | |
| 260 | // Convert expiry date to RFC 3339 for SQLite. |
| 261 | var expiry *string |
| 262 | if auth.Expiry != nil { |
| 263 | tmp := auth.Expiry.Format(time.RFC3339) |
| 264 | expiry = &tmp |
| 265 | } |
| 266 | |
| 267 | // Execute insertion query. |
| 268 | result, err := tx.ExecContext(ctx, ` |
| 269 | INSERT INTO auths ( |
| 270 | user_id, |
| 271 | source, |
| 272 | source_id, |
| 273 | access_token, |
| 274 | refresh_token, |
| 275 | expiry, |
| 276 | created_at, |
| 277 | updated_at |
| 278 | ) |
| 279 | VALUES (?, ?, ?, ?, ?, ?, ?, ?) |
| 280 | `, |
| 281 | auth.UserID, |
| 282 | auth.Source, |
| 283 | auth.SourceID, |
| 284 | auth.AccessToken, |
| 285 | auth.RefreshToken, |
| 286 | expiry, |
| 287 | (*NullTime)(&auth.CreatedAt), |
| 288 | (*NullTime)(&auth.UpdatedAt), |
| 289 | ) |
| 290 | if err != nil { |
| 291 | return FormatError(err) |
| 292 | } |
| 293 | |
| 294 | // Update caller object to set ID. |
| 295 | if auth.ID, err = lastInsertID(result); err != nil { |
| 296 | return err |
| 297 | } |
| 298 | |
| 299 | return nil |
| 300 | } |
| 301 | |
| 302 | // updateAuth updates tokens & expiry on exist auth object. |
| 303 | // Returns new state of the auth object. |
no test coverage detected