Skip to content

Commit

Permalink
feat: offer a public endpoint to share packing lists (#11)
Browse files Browse the repository at this point in the history
* chore: Makefile refactoring

* feat: implement a public endpoint for sharing packing lists:x

* chore: go mod tidy

---------

Co-authored-by: Angak0k <romain.broussard@mirakl.net>
  • Loading branch information
Angak0k and Angak0k authored Feb 8, 2024
1 parent 3f48907 commit d4ab8d3
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test:
go test -covermode=atomic -coverprofile=coverage.out -race ./...

api-doc:
swag init
swag init --tags \!Internal

build: test
go build
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/spec v0.20.14 // indirect
github.com/go-openapi/swag v0.22.7 // indirect
github.com/go-openapi/swag v0.22.9 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
golang.org/x/tools v0.17.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdX
github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4=
github.com/go-openapi/spec v0.20.14 h1:7CBlRnw+mtjFGlPDRZmAMnq35cRzI91xj03HVyUi/Do=
github.com/go-openapi/spec v0.20.14/go.mod h1:8EOhTpBoFiask8rrgwbLC3zmJfz4zsCUueRuPM6GNkw=
github.com/go-openapi/swag v0.22.7 h1:JWrc1uc/P9cSomxfnsFSVWoE1FW6bNbrVPmpQYpCcR8=
github.com/go-openapi/swag v0.22.7/go.mod h1:Gl91UqO+btAM0plGGxHqJcQZ1ZTy6jbmridBTsDy8A0=
github.com/go-openapi/swag v0.22.9 h1:XX2DssF+mQKM2DHsbgZK74y/zj4mo9I99+89xUmuZCE=
github.com/go-openapi/swag v0.22.9/go.mod h1:3/OXnFfnMAwBD099SwYRk7GD3xOrr1iL7d/XNLXVVwE=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func main() {
public.POST("/login", accounts.Login)
public.GET("/confirmemail", accounts.ConfirmEmail)
public.POST("/forgotpassword", accounts.ForgotPassword)
public.GET("/sharedlist/:sharing_code", packs.SharedList)

protected := router.Group("/api/v1")
protected.Use(security.JwtAuthProcessor())
Expand All @@ -81,7 +82,6 @@ func main() {
protected.POST("/myinventory", inventories.PostMyInventory)
protected.PUT("/myinventory/:id", inventories.PutMyInventoryByID)
protected.DELETE("/myinventory/:id", inventories.DeleteMyInventoryByID)

protected.POST("/importfromlighterpack", packs.ImportFromLighterPack)

private := router.Group("/api/admin")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "pack"
DROP COLUMN "sharing_code";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE "pack"
ADD COLUMN "sharing_code" TEXT NOT NULL DEFAULT '',
ADD CONSTRAINT "sharing_code_unique" UNIQUE ("sharing_code");
1 change: 1 addition & 0 deletions pkg/dataset/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Pack struct {
User_id uint `json:"user_id"`
Pack_name string `json:"pack_name"`
Pack_description string `json:"pack_description"`
Sharing_code string `json:"sharing_code"`
Created_at time.Time `json:"created_at"`
Updated_at time.Time `json:"updated_at"`
}
Expand Down
74 changes: 64 additions & 10 deletions pkg/packs/packs.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func GetPacks(c *gin.Context) {
func returnPacks() (dataset.Packs, error) {
var packs dataset.Packs

rows, err := database.Db().Query("SELECT id, user_id, pack_name, pack_description, created_at, updated_at FROM pack;")
rows, err := database.Db().Query("SELECT id, user_id, pack_name, pack_description, sharing_code, created_at, updated_at FROM pack;")
if err != nil {
return nil, err
}
Expand All @@ -60,7 +60,7 @@ func returnPacks() (dataset.Packs, error) {

for rows.Next() {
var pack dataset.Pack
err := rows.Scan(&pack.ID, &pack.User_id, &pack.Pack_name, &pack.Pack_description, &pack.Created_at, &pack.Updated_at)
err := rows.Scan(&pack.ID, &pack.User_id, &pack.Pack_name, &pack.Pack_description, &pack.Sharing_code, &pack.Created_at, &pack.Updated_at)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -111,8 +111,8 @@ func GetPackByID(c *gin.Context) {

}

// Get pack by ID
// @Summary Get pack by ID
// Get My pack by ID
// @Summary Get My pack by ID
// @Description Get pack by ID
// @Security Bearer
// @Tags Packs
Expand Down Expand Up @@ -167,8 +167,8 @@ func GetMyPackByID(c *gin.Context) {
func findPackById(id uint) (*dataset.Pack, error) {
var pack dataset.Pack

row := database.Db().QueryRow("SELECT id, user_id, pack_name, pack_description, created_at, updated_at FROM pack WHERE id = $1;", id)
err := row.Scan(&pack.ID, &pack.User_id, &pack.Pack_name, &pack.Pack_description, &pack.Created_at, &pack.Updated_at)
row := database.Db().QueryRow("SELECT id, user_id, pack_name, pack_description, sharing_code, created_at, updated_at FROM pack WHERE id = $1;", id)
err := row.Scan(&pack.ID, &pack.User_id, &pack.Pack_name, &pack.Pack_description, &pack.Sharing_code, &pack.Created_at, &pack.Updated_at)

if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Expand Down Expand Up @@ -250,13 +250,18 @@ func PostMyPack(c *gin.Context) {
}

func insertPack(p *dataset.Pack) error {
var err error
if p == nil {
return errors.New("payload is empty")
}
p.Created_at = time.Now().Truncate(time.Second)
p.Updated_at = time.Now().Truncate(time.Second)
p.Sharing_code, err = helper.GenerateRandomCode(30)
if err != nil {
return errors.New("failed to generate a sharing code")
}

err := database.Db().QueryRow("INSERT INTO pack (user_id, pack_name, pack_description, created_at, updated_at) VALUES ($1,$2,$3,$4,$5) RETURNING id;", p.User_id, p.Pack_name, p.Pack_description, p.Created_at, p.Updated_at).Scan(&p.ID)
err = database.Db().QueryRow("INSERT INTO pack (user_id, pack_name, pack_description, sharing_code, created_at, updated_at) VALUES ($1,$2,$3,$4,$5,$6) RETURNING id;", p.User_id, p.Pack_name, p.Pack_description, p.Sharing_code, p.Created_at, p.Updated_at).Scan(&p.ID)
if err != nil {
return err
}
Expand Down Expand Up @@ -1027,15 +1032,15 @@ func GetMyPacks(c *gin.Context) {
func findPacksByUserId(id uint) (*dataset.Packs, error) {
var packs dataset.Packs

rows, err := database.Db().Query("SELECT id, user_id, pack_name, pack_description, created_at, updated_at FROM pack WHERE user_id = $1;", id)
rows, err := database.Db().Query("SELECT id, user_id, pack_name, pack_description, sharing_code, created_at, updated_at FROM pack WHERE user_id = $1;", id)
if err != nil {
return nil, err
}
defer rows.Close()

for rows.Next() {
var pack dataset.Pack
err := rows.Scan(&pack.ID, &pack.User_id, &pack.Pack_name, &pack.Pack_description, &pack.Created_at, &pack.Updated_at)
err := rows.Scan(&pack.ID, &pack.User_id, &pack.Pack_name, &pack.Pack_description, &pack.Sharing_code, &pack.Created_at, &pack.Updated_at)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1172,7 +1177,7 @@ func ImportFromLighterPack(c *gin.Context) {
lighterPack = append(lighterPack, lighterPackItem)
}

// Perform your database insertion
// Perform database insertion
err = insertLighterPack(&lighterPack, user_id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -1225,3 +1230,52 @@ func insertLighterPack(lp *dataset.LighterPack, user_id uint) error {
}
return nil
}

// Get pack content for a given sharing code
// @Summary Get pack content for a given sharing code
// @Description Get pack content for a given sharing code
// @Tags Public
// @Produce json
// @Param sharing_code path string true "Sharing Code"
// @Success 200 {object} dataset.PackContents "Pack Contents"
// @Failure 404 {object} dataset.ErrorResponse "Pack not found"
// @Failure 500 {object} dataset.ErrorResponse "Internal Server Error"
// @Router /public/packs/{sharing_code} [get]
func SharedList(c *gin.Context) {
sharing_code := c.Param("sharing_code")

pack_id, err := findPackIdBySharingCode(sharing_code)
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if pack_id == 0 {
c.IndentedJSON(http.StatusNotFound, gin.H{"error": "Pack not found"})
return
}

packContents, err := returnPackContentsByPackID(pack_id)
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if packContents == nil {
c.IndentedJSON(http.StatusNotFound, gin.H{"error": "Pack not found"})
return
}

c.IndentedJSON(http.StatusOK, packContents)
}

func findPackIdBySharingCode(sharing_code string) (uint, error) {
var pack_id uint
row := database.Db().QueryRow("SELECT id FROM pack WHERE sharing_code = $1;", sharing_code)
err := row.Scan(&pack_id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return 0, nil
}
return 0, err
}
return pack_id, nil
}
Loading

0 comments on commit d4ab8d3

Please sign in to comment.