Skip to content

Commit

Permalink
Refactor URL handling in Redis integration and improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
SartajBhuvaji committed Nov 22, 2024
1 parent 60d0f21 commit ba2ced5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions api/shorten.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
29 changes: 20 additions & 9 deletions database/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
Expand All @@ -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{
Expand Down Expand Up @@ -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,
}

Expand All @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit ba2ced5

Please sign in to comment.