Skip to content

Commit 0046377

Browse files
committed
feat: add delete URL functionality with cache support
1 parent e271b5b commit 0046377

5 files changed

Lines changed: 25 additions & 2 deletions

File tree

frontend/components/url-table/columns.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ export const createColumns = (onDelete?: () => void): ColumnDef<Url>[] => [
250250
},
251251
body: JSON.stringify({
252252
id: url.id,
253+
short_code: url.short_code,
253254
}),
254255
});
255256

internal/cacher/cacher_redis_url.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,17 @@ func (c *RedisCacher) GetURLFromCache(ctx context.Context, shortCode string) (*r
6565
// Return the URL information
6666
return &urlInfo, nil
6767
}
68+
69+
// DeleteURLFromCache deletes the URL information from the cache using redis
70+
func (c *RedisCacher) DeleteURLFromCache(ctx context.Context, shortCode string) error {
71+
// Delete the URL information from Redis
72+
err := c.client.Del(ctx, urlKeyPrefix+shortCode).Err()
73+
74+
// If the key does not exist, consider it successful
75+
if err == redis.Nil {
76+
return nil
77+
}
78+
79+
// If there was an error, return it
80+
return err
81+
}

internal/model/url_model.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ type GetUserShortURLsResponse struct {
3737
}
3838

3939
type DeleteUserShortURLRequest struct {
40-
// Id of the shortened URL to be deleted
40+
// Id of the shortened URL to be deleted in the database
4141
ID int64 `json:"id" validate:"required,min=1"`
42+
// Short code of the URL to be deleted in the cache
43+
ShortCode string `json:"short_code" validate:"required,alphanum,min=4,max=10"`
4244
}
4345

4446
type DeleteUserShortURLResponse struct {
4547
// Success message
4648
Message string `json:"message"`
47-
}
49+
}

internal/service/cacher_interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type Cacher interface {
99
// For URL service
1010
GetURLFromCache(ctx context.Context, shortCode string) (*repo.Url, error)
1111
StoreURLToCache(ctx context.Context, urlInfo repo.Url) error
12+
DeleteURLFromCache(ctx context.Context, shortCode string) error
1213

1314
// For User service
1415
GetEmailUsingCode(ctx context.Context, emailCode string) (*string, error)

internal/service/url_service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ func (s *URLService) GetMyURLs(ctx context.Context, req model.GetUserShortURLsRe
164164

165165
// Delete a short URL created by the user
166166
func (s *URLService) DeleteShortURL(ctx context.Context, req model.DeleteUserShortURLRequest, username string) (*model.DeleteUserShortURLResponse, error) {
167+
// Remove the URL from the cache first
168+
if err := s.cacher.DeleteURLFromCache(ctx, req.ShortCode); err != nil {
169+
return nil, err
170+
}
171+
167172
// Delete the URL from the database
168173
err := s.querier.DeleteURLFromId(ctx, repo.DeleteURLFromIdParams{
169174
ID: req.ID,

0 commit comments

Comments
 (0)