Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #24 Adds filters for dates regardings accesses on /shorty/:id #26

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions api/shortener.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (h *shortenerHandler) Group() *string {
// @Description retrieves full information for the give shortlink
// @Param token header string false "Authorization token"
// @Param id path string true "ShortLink ID"
// @Param from query string true "accesses from date 'YYYY-mm-dd'"
// @Param until query string true "accesses until date 'YYYY-mm-dd'"
// @Success 200 {object} entity.Shorty "response"
// @Failure 400 {object} response.FailureResponse "error"
// @Failure 404 {object} response.FailureResponse "not found"
Expand All @@ -54,7 +56,9 @@ func (h *shortenerHandler) GetLinkInformation(c *gin.Context) {
)
}
parsed := uuid.MustParse(id)
shorty, err := h.shortySvc.FindShortyByID(parsed)
from := c.Query("from")
until := c.Query("until")
shorty, err := h.shortySvc.FindShortyByID(parsed, from, until)
if err != nil {
c.JSON(http.StatusNotFound, "shorty not found")
return
Expand Down Expand Up @@ -157,7 +161,7 @@ func (h *shortenerHandler) EditLink(c *gin.Context) {
return
}

shorty, err := h.shortySvc.FindShortyByID(uuid.MustParse(id))
shorty, err := h.shortySvc.FindShortyByID(uuid.MustParse(id), "", "")
if err != nil {
c.JSON(http.StatusNotFound, "shorty not found")
return
Expand Down
14 changes: 14 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@
"name": "id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "accesses from date 'YYYY-mm-dd'",
"name": "from",
"in": "query",
"required": true
},
{
"type": "string",
"description": "accesses until date 'YYYY-mm-dd'",
"name": "until",
"in": "query",
"required": true
}
],
"responses": {
Expand Down
10 changes: 10 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ paths:
name: id
required: true
type: string
- description: accesses from date 'YYYY-mm-dd'
in: query
name: from
required: true
type: string
- description: accesses until date 'YYYY-mm-dd'
in: query
name: until
required: true
type: string
responses:
"200":
description: response
Expand Down
38 changes: 37 additions & 1 deletion service/shorty.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (s *ShortyService) List(limit, offset int) ([]*entity.Shorty, error) {
return shorties, nil
}

func (s *ShortyService) FindShortyByID(id uuid.UUID) (*entity.Shorty, error) {
func (s *ShortyService) FindShortyByID(id uuid.UUID, from, until string) (*entity.Shorty, error) {
m := &entity.Shorty{
ID: id,
}
Expand All @@ -179,6 +179,27 @@ func (s *ShortyService) FindShortyByID(id uuid.UUID) (*entity.Shorty, error) {
return nil, err
}

if from != "" && until != "" {
fromTime, err := time.Parse(time.DateOnly, from)
if err != nil {
return nil, err
}

untilTime, err := time.Parse(time.DateTime, until+" 23:59:59")
if err != nil {
return nil, err
}

sh := s.FindAllAccessesByShortyIDAndDateRange(id, &fromTime, &untilTime)

m.ShortyAccesses = sh

m.Visits = len(sh)
m.RedirectCount = CountRedirects(sh)

return m, nil
}

sh := s.FindAllAccessesByShortyID(id)

m.ShortyAccesses = sh
Expand Down Expand Up @@ -216,6 +237,21 @@ func (s *ShortyService) FindAllAccessesByShortyID(id uuid.UUID) []entity.ShortyA
return sh
}

func (s *ShortyService) FindAllAccessesByShortyIDAndDateRange(
id uuid.UUID,
from,
until *time.Time,
) []entity.ShortyAccess {
var sh []entity.ShortyAccess

_ = s.shortyAccessRepository.Database.Orm.
Model(&entity.ShortyAccess{}).
Where("shorty_id = ?", id).
Where("created_at BETWEEN ? AND ?", from, until).Scan(&sh)

return sh
}

func CountRedirects(accesses []entity.ShortyAccess) int {
var redirects int
for _, access := range accesses {
Expand Down