Skip to content

Commit

Permalink
refactor to repository parttern
Browse files Browse the repository at this point in the history
  • Loading branch information
realhugn committed Sep 18, 2024
1 parent d54517a commit 1e9a09c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
2 changes: 1 addition & 1 deletion tinyURL/internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/gin-gonic/gin"
)

func SetupRoutes(router *gin.Engine, urlHandler *handlers.URLHandler) {
func SetupRoutes(router *gin.Engine, urlHandler handlers.URLHandler) {

router.POST("/shorten", urlHandler.Shorten)
router.GET("/:short_url", urlHandler.Original)
Expand Down
21 changes: 13 additions & 8 deletions tinyURL/internal/handlers/url_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ import (
"github.com/go-playground/validator/v10"
)

type URLHandler struct {
urlService *service.URLService
validator *validator.Validate
type URLHandler interface {
Shorten(c *gin.Context)
Original(c *gin.Context)
}

func NewURLHandler(urlService *service.URLService) *URLHandler {
return &URLHandler{
type urlHandler struct {
urlService service.URLService
validator validator.Validate
}

func NewURLHandler(urlService service.URLService) URLHandler {
return &urlHandler{
urlService: urlService,
validator: validator.New(),
validator: *validator.New(),
}
}

type ShortenRequest struct {
LongURL string `json:"long_url" validate:"required,url"`
}

func (h *URLHandler) Shorten(c *gin.Context) {
func (h urlHandler) Shorten(c *gin.Context) {
var input ShortenRequest

if err := c.ShouldBindJSON(&input); err != nil {
Expand All @@ -46,7 +51,7 @@ func (h *URLHandler) Shorten(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"short_url": "http://localhost:8082/" + shortURL})
}

func (h *URLHandler) Original(c *gin.Context) {
func (h urlHandler) Original(c *gin.Context) {
shortURL := c.Param("short_url")

if err := h.validator.Var(shortURL, "required,min=6,max=10"); err != nil {
Expand Down
36 changes: 30 additions & 6 deletions tinyURL/internal/repository/url_repository.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
package repository

import (
"errors"
"log"
"tinyURL/internal/models"

"gorm.io/gorm"
)

type URLRepository struct {
type URLRepository interface {
Create(url *models.URL) error
FindByOriginalURL(originalURL string) (*models.URL, error)
FindByShortURL(shortURL string) (*models.URL, error)
KeyExists(key string) bool
}

type postgresURLRepository struct {
db *gorm.DB
}

func NewURLRepository(db *gorm.DB) *URLRepository {
return &URLRepository{db: db}
func NewURLRepository(db *gorm.DB) URLRepository {
return postgresURLRepository{
db: db,
}
}

func (r *URLRepository) Create(url *models.URL) error {
func (r postgresURLRepository) Create(url *models.URL) error {
return r.db.Create(url).Error
}

func (r *URLRepository) FindByOriginalURL(originalURL string) (*models.URL, error) {
func (r postgresURLRepository) FindByOriginalURL(originalURL string) (*models.URL, error) {
var url models.URL
err := r.db.Where("original = ?", originalURL).First(&url).Error
if err != nil {
Expand All @@ -27,11 +38,24 @@ func (r *URLRepository) FindByOriginalURL(originalURL string) (*models.URL, erro
return &url, nil
}

func (r *URLRepository) FindByShortURL(shortURL string) (*models.URL, error) {
func (r postgresURLRepository) FindByShortURL(shortURL string) (*models.URL, error) {
var url models.URL
err := r.db.Where("short_url = ?", shortURL).First(&url).Error
if err != nil {
return nil, err
}
return &url, nil
}

func (r postgresURLRepository) KeyExists(key string) bool {
var url models.URL
err := r.db.Where("key = ?", key).Scan(&url).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return false
}
log.Printf("Error checking key existence: %v", err)
return false
}
return true
}
28 changes: 18 additions & 10 deletions tinyURL/internal/service/url_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@ import (
"github.com/go-playground/validator/v10"
)

type URLService struct {
repo *repository.URLRepository
shortener *shortener.Shortener
validator *validator.Validate
type URLService interface {
Shorten(originalURL string) (string, error)
Original(shortURL string) (string, error)
}

func NewURLService(repo *repository.URLRepository) *URLService {
return &URLService{
type urlService struct {
repo repository.URLRepository
shortener shortener.Shortener
validator validator.Validate
}

func NewURLService(repo repository.URLRepository) URLService {
return &urlService{
repo: repo,
shortener: shortener.New(),
validator: validator.New(),
shortener: *shortener.New(),
validator: *validator.New(),
}
}

func (s *URLService) Shorten(originalURL string) (string, error) {
func (s urlService) Shorten(originalURL string) (string, error) {
if err := s.validator.Var(originalURL, "required,url"); err != nil {
return "", errors.New("invalid URL format")
}
Expand All @@ -35,6 +40,9 @@ func (s *URLService) Shorten(originalURL string) (string, error) {
}

generatedURL := s.shortener.Generate()
for s.repo.KeyExists(generatedURL) {
generatedURL = s.shortener.Generate()
}

url := &models.URL{
ShortURL: generatedURL,
Expand All @@ -52,7 +60,7 @@ func (s *URLService) Shorten(originalURL string) (string, error) {
return generatedURL, nil
}

func (s *URLService) Original(shortURL string) (string, error) {
func (s urlService) Original(shortURL string) (string, error) {
if err := s.validator.Var(shortURL, "required,min=6,max=10"); err != nil {
return "", errors.New("invalid short URL format")
}
Expand Down
2 changes: 1 addition & 1 deletion tinyURL/tests/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration
package tests

import (
"bytes"
Expand Down

0 comments on commit 1e9a09c

Please sign in to comment.