Skip to content

Commit

Permalink
feat(service.url): add service to lookup origin & inc hits by shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Oct 3, 2020
1 parent 040db1d commit 79ac77a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions service/url/client.go
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/adhocore/urlsh/orm"
"github.com/adhocore/urlsh/request"
"github.com/adhocore/urlsh/util"
"gorm.io/gorm"
)

func CreateUrlShortCodeFromRequest(req *http.Request) (string, error) {
Expand Down Expand Up @@ -40,6 +41,28 @@ func CreateUrlShortCode(input request.UrlInput) (string, error) {
return shortCode, nil
}

func LookupOriginUrl(shortCode string) (string, int) {
var urlModel model.Url

if status := orm.Connection().Where("short_code = ?", shortCode).First(&urlModel); status.RowsAffected == 0 {
return "", http.StatusNotFound
}

if !urlModel.IsActive() {
return "", http.StatusGone
}

return urlModel.OriginUrl, http.StatusFound
}

func IncrementHits(shortCode string) {
var urlModel model.Url

orm.Connection().Model(&urlModel).
Where("short_code = ?", shortCode).
UpdateColumn("hits", gorm.Expr("hits + ?", 1))
}

func allowDupeUrl() bool {
return os.Getenv("APP_ALLOW_DUPE_URL") == "1"
}
Expand Down

0 comments on commit 79ac77a

Please sign in to comment.