Skip to content

Commit

Permalink
feat: #28 adding showAccesses flag (#29)
Browse files Browse the repository at this point in the history
* feat: #28 adding  flag

* fix: #28 fixing the variable name and return type
  • Loading branch information
dioguoliveira authored Nov 13, 2024
1 parent f63f2d7 commit 5b0aded
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
17 changes: 15 additions & 2 deletions api/shortener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"net/http"
"strconv"

"github.com/doutorfinancas/pun-sho/api/response"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -43,6 +44,7 @@ func (h *shortenerHandler) Group() *string {
// @Param id path string true "ShortLink ID"
// @Param from query string false "accesses from date 'YYYY-mm-dd'"
// @Param until query string false "accesses until date 'YYYY-mm-dd'"
// @Param show_accesses query boolean false "display accesses column"
// @Success 200 {object} entity.Shorty "response"
// @Failure 400 {object} response.FailureResponse "error"
// @Failure 404 {object} response.FailureResponse "not found"
Expand All @@ -58,7 +60,18 @@ func (h *shortenerHandler) GetLinkInformation(c *gin.Context) {
parsed := uuid.MustParse(id)
from := c.Query("from")
until := c.Query("until")
shorty, err := h.shortySvc.FindShortyByID(parsed, from, until)

showAccessesStr := c.Query("show_accesses")
showAccesses := true
if showAccessesStr != "" {
var err error
showAccesses, err = strconv.ParseBool(showAccessesStr)
if err != nil {
c.JSON(http.StatusBadRequest, "invalid value for show_accesses")
return
}
}
shorty, err := h.shortySvc.FindShortyByID(parsed, from, until, showAccesses)
if err != nil {
c.JSON(http.StatusNotFound, "shorty not found")
return
Expand Down Expand Up @@ -161,7 +174,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), "", "", true)
if err != nil {
c.JSON(http.StatusNotFound, "shorty not found")
return
Expand Down
6 changes: 6 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@
"description": "accesses until date 'YYYY-mm-dd'",
"name": "until",
"in": "query"
},
{
"type": "boolean",
"description": "display accesses column",
"name": "showAccesses",
"in": "query"
}
],
"responses": {
Expand Down
4 changes: 4 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ paths:
in: query
name: until
type: string
- description: display accesses column
in: query
name: show_accesses
type: boolean
responses:
"200":
description: response
Expand Down
2 changes: 1 addition & 1 deletion entity/shorty_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Shorty struct {
RedirectionLimit *int `json:"redirection_limit" gorm:"column:redirection_limit"`
CreatedAt *time.Time `json:"created_at" gorm:"column:created_at"`
DeletedAt *time.Time `json:"deleted_at" gorm:"column:deleted_at"`
ShortyAccesses []ShortyAccess `json:"accesses" gorm:"-"`
ShortyAccesses []ShortyAccess `json:"accesses,omitempty" gorm:"-"`
ShortLink string `json:"short_link" gorm:"-"`
Visits int `json:"visits" gorm:"-"`
RedirectCount int `json:"redirects" gorm:"-"`
Expand Down
10 changes: 7 additions & 3 deletions 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, from, until string) (*entity.Shorty, error) {
func (s *ShortyService) FindShortyByID(id uuid.UUID, from, until string, showAccesses bool) (*entity.Shorty, error) {
m := &entity.Shorty{
ID: id,
}
Expand All @@ -192,7 +192,9 @@ func (s *ShortyService) FindShortyByID(id uuid.UUID, from, until string) (*entit

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

m.ShortyAccesses = sh
if showAccesses {
m.ShortyAccesses = sh
}

m.Visits = len(sh)
m.RedirectCount = CountRedirects(sh)
Expand All @@ -202,7 +204,9 @@ func (s *ShortyService) FindShortyByID(id uuid.UUID, from, until string) (*entit

sh := s.FindAllAccessesByShortyID(id)

m.ShortyAccesses = sh
if showAccesses {
m.ShortyAccesses = sh
}

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

0 comments on commit 5b0aded

Please sign in to comment.