diff --git a/api/shorten.go b/api/shorten.go index 8a59d36..045d5cf 100644 --- a/api/shorten.go +++ b/api/shorten.go @@ -53,10 +53,10 @@ func ShortenURLHandler(w http.ResponseWriter, r *http.Request, redisClient *data redisClient.SetCounter("counter", counter+1) // Add the short URL to the database - err = redisClient.Set(shortURL, req.URL) + err = redisClient.Set(req.URL, shortURL) if err != nil { - http.Error(w, "Error updating counter in Reddis", http.StatusInternalServerError) + http.Error(w, "Error storing URL in Redis", http.StatusInternalServerError) return } diff --git a/database/redis.go b/database/redis.go index 5ed3e00..475d07a 100644 --- a/database/redis.go +++ b/database/redis.go @@ -2,6 +2,7 @@ package database import ( "context" + "encoding/json" "fmt" "log" "strconv" @@ -17,12 +18,22 @@ type RedisClient struct { } type URLValue struct { - URL string `json:"url"` - createdAt string `json:"created_at"` - lastAccessed string `json:"last_accessed"` + OriginalURL string `json:"url"` + CreatedAt string `json:"created_at"` + LastAccessed string `json:"last_accessed"` AccessCount int `json:"access_count"` } +// Implement the encoding.BinaryMarshaler interface +func (u URLValue) MarshalBinary() ([]byte, error) { + return json.Marshal(u) +} + +// Implement the encoding.BinaryUnmarshaler interface +func (u *URLValue) UnmarshalBinary(data []byte) error { + return json.Unmarshal(data, u) +} + // NewRedisClient initializes a new Redis client. func NewRedisClient(addr, password string, db int) *RedisClient { rdb := redis.NewClient(&redis.Options{ @@ -73,9 +84,9 @@ func (r *RedisClient) GetCounter() (int, error) { func (r *RedisClient) Set(originalURL, shortURL string) error { value := URLValue{ - URL: originalURL, - createdAt: time.Now().String(), - lastAccessed: time.Now().String(), + OriginalURL: originalURL, + CreatedAt: time.Now().String(), + LastAccessed: time.Now().String(), AccessCount: 1, } @@ -88,7 +99,7 @@ func (r *RedisClient) Set(originalURL, shortURL string) error { return nil } -// Get retrieves the value of a given key from Redis. +// Get retrieves the original URL of a given key from Redis. func (r *RedisClient) Get(shortURL string) (string, error) { var urlValue URLValue @@ -98,11 +109,11 @@ func (r *RedisClient) Get(shortURL string) (string, error) { return "", err } - urlValue.lastAccessed = time.Now().String() + urlValue.LastAccessed = time.Now().String() urlValue.AccessCount++ r.client.Set(r.ctx, shortURL, urlValue, 0).Err() - return urlValue.URL, nil + return urlValue.OriginalURL, nil } // Close closes the Redis client connection.