fix: return URL if it already exists
This commit is contained in:
parent
38f29e147f
commit
a3dc0b646b
3 changed files with 2 additions and 8 deletions
|
@ -50,12 +50,6 @@ func (h *handler) CreateShortURL(ctx context.Context, req *oas.CreateShortURLReq
|
||||||
LongUrl: req.URL,
|
LongUrl: req.URL,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// No row == conflict since we do a "ON CONFLICT DO NOTHING"
|
|
||||||
if errors.Is(err, pgx.ErrNoRows) {
|
|
||||||
return &oas.CreateShortURLBadRequest{
|
|
||||||
Message: oas.NewOptString("URL already exist."),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("failed to insert url: %w", err)
|
return nil, fmt.Errorf("failed to insert url: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ func (q *Queries) GetURLByHash(ctx context.Context, hash string) (Url, error) {
|
||||||
const insertURL = `-- name: InsertURL :one
|
const insertURL = `-- name: InsertURL :one
|
||||||
INSERT INTO urls (hash, long_url)
|
INSERT INTO urls (hash, long_url)
|
||||||
VALUES ($1, $2)
|
VALUES ($1, $2)
|
||||||
ON CONFLICT (hash) DO NOTHING
|
ON CONFLICT (hash) DO UPDATE SET hash = EXCLUDED.hash -- force row to be returned
|
||||||
RETURNING id, hash, long_url
|
RETURNING id, hash, long_url
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
|
@ -5,5 +5,5 @@ WHERE hash = $1;
|
||||||
-- name: InsertURL :one
|
-- name: InsertURL :one
|
||||||
INSERT INTO urls (hash, long_url)
|
INSERT INTO urls (hash, long_url)
|
||||||
VALUES ($1, $2)
|
VALUES ($1, $2)
|
||||||
ON CONFLICT (hash) DO NOTHING
|
ON CONFLICT (hash) DO UPDATE SET hash = EXCLUDED.hash -- force row to be returned
|
||||||
RETURNING *;
|
RETURNING *;
|
||||||
|
|
Loading…
Reference in a new issue