Skip to content

Commit

Permalink
feat(service.url): add service to list urls by filter for admin
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Oct 3, 2020
1 parent 8e2ced5 commit ad056f0
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions service/url/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package url

import (
"net/http"

"github.com/adhocore/urlsh/common"
"github.com/adhocore/urlsh/model"
"github.com/adhocore/urlsh/orm"
"github.com/adhocore/urlsh/request"
)

func ListUrlsFilteredFromRequest(req *http.Request) ([]model.Url, error) {
_ = req.ParseForm()

filter := request.UrlFilter{
ShortCode: req.Form.Get("short_code"),
Keyword: req.Form.Get("keyword"),
Page: req.Form.Get("page"),
}

return ListUrlsFiltered(filter)
}

func ListUrlsFiltered(filter request.UrlFilter) ([]model.Url, error) {
var urls []model.Url

limit, conn := 50, orm.Connection().Select("short_code, origin_url, hits, expires_on")
if filter.ShortCode != "" {
limit = 1
conn = conn.Where("short_code = ?", filter.ShortCode)
} else {
conn.Offset(filter.GetOffset(limit))
}

if filter.Keyword != "" {
conn = conn.
Joins("LEFT JOIN url_keywords ON url_keywords.url_id = urls.id").
Joins("LEFT JOIN keywords ON url_keywords.keyword_id = keywords.id").
Where("keyword = ?", filter.Keyword)
}

if conn.Limit(limit).Find(&urls); len(urls) == 0 {
return urls, common.ErrNoMatchingData
}

return urls, nil
}

0 comments on commit ad056f0

Please sign in to comment.