uniqueSlug returns base if no row with that slug exists in table; otherwise appends -2, -3, ... until it finds an unused one.
(db *sql.DB, table, base string)
| 329 | // uniqueSlug returns base if no row with that slug exists in table; |
| 330 | // otherwise appends -2, -3, ... until it finds an unused one. |
| 331 | func uniqueSlug(db *sql.DB, table, base string) (string, error) { |
| 332 | slug := base |
| 333 | n := 2 |
| 334 | for { |
| 335 | var exists int |
| 336 | // nolint:gosec — table name is hardcoded ("projects" or "tasks"). |
| 337 | q := "SELECT 1 FROM " + table + " WHERE slug = ?" |
| 338 | err := db.QueryRow(q, slug).Scan(&exists) |
| 339 | if errors.Is(err, sql.ErrNoRows) { |
| 340 | return slug, nil |
| 341 | } |
| 342 | if err != nil { |
| 343 | return "", err |
| 344 | } |
| 345 | slug = fmt.Sprintf("%s-%d", base, n) |
| 346 | n++ |
| 347 | if n > 1000 { |
| 348 | return "", fmt.Errorf("slug %q: too many collisions", base) |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | func isValidPriority(p string) bool { |
| 354 | return p == "high" || p == "medium" || p == "low" |
no outgoing calls
no test coverage detected