Skip to content

Commit

Permalink
soft_delete (#8)
Browse files Browse the repository at this point in the history
* delete endpoint added
  • Loading branch information
fmgAndre authored Apr 14, 2023
1 parent b1db786 commit ce7dcc0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
23 changes: 17 additions & 6 deletions api/shortener.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (h *shortenerHandler) Routes(rg *gin.RouterGroup) {
rg.GET("/:id", h.GetLinkInformation)
rg.GET("", h.ListLinks)
rg.POST("", h.CreateLink)
rg.DELETE("", h.RemoveLink)
rg.DELETE("/:id", h.RemoveLink)
}

func (h *shortenerHandler) Group() *string {
Expand Down Expand Up @@ -97,9 +97,20 @@ func (h *shortenerHandler) CreateLink(c *gin.Context) {
}

func (h *shortenerHandler) RemoveLink(c *gin.Context) {
// @TODO Implement me!
c.JSON(
http.StatusNotImplemented,
NewErrorResponse("nope, not yet. Try again later, boss"),
)
id := c.Param("id")
if id == "" {
c.JSON(
http.StatusBadRequest,
NewErrorResponse("no id provided"),
)
}
parsed := uuid.MustParse(id)
err := h.service.DeleteShortyByUUID(parsed)
if err != nil {
c.JSON(
http.StatusBadRequest,
NewErrorResponse("kaput, no delete"),
)
}
c.JSON(http.StatusOK, nil)
}
11 changes: 11 additions & 0 deletions entity/shorty_repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package entity

import (
"time"

"github.com/google/uuid"
"go.uber.org/zap"

"github.com/doutorfinancas/pun-sho/database"
Expand Down Expand Up @@ -33,3 +36,11 @@ func (r *ShortyRepository) List(limit, offset int) ([]*Shorty, error) {

return rows, nil
}

func (r *ShortyRepository) Delete(id uuid.UUID) error {
m := Shorty{ID: id}
return r.Database.Orm.Model(m).
Where("id = ?", id).
Update("deleted_at", time.Now()).
Error
}
4 changes: 4 additions & 0 deletions service/shorty.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,7 @@ func (s *ShortyService) FindShortyByPublicID(publicID string) (*entity.Shorty, e

return m, nil
}

func (s *ShortyService) DeleteShortyByUUID(id uuid.UUID) error {
return s.shortyRepository.Delete(id)
}

0 comments on commit ce7dcc0

Please sign in to comment.