Skip to content

Commit

Permalink
remove get and post peer apis
Browse files Browse the repository at this point in the history
  • Loading branch information
Intizar-T committed Jun 26, 2024
1 parent 6fe877d commit c555380
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 86 deletions.
32 changes: 0 additions & 32 deletions node/pkg/boot/peer/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,6 @@ type PeerInsertModel struct {
Url string `db:"url" json:"url" validate:"required"`
}

func insert(c *fiber.Ctx) error {
payload := new(PeerInsertModel)
if err := c.BodyParser(payload); err != nil {
log.Error().Err(err).Msg("Failed to parse request")
return c.Status(fiber.StatusBadRequest).SendString("Failed to parse request")
}

validate := validator.New()
if err := validate.Struct(payload); err != nil {
log.Error().Err(err).Msg("Failed to validate request")
return c.Status(fiber.StatusBadRequest).SendString("Failed to validate request")
}

result, err := db.QueryRow[PeerModel](c.Context(), InsertPeer, map[string]any{
"url": payload.Url})
if err != nil {
log.Error().Err(err).Msg("Failed to execute insert query")
return c.Status(fiber.StatusInternalServerError).SendString("Failed to execute insert query")
}

return c.JSON(result)
}

func get(c *fiber.Ctx) error {
result, err := db.QueryRows[PeerModel](c.Context(), GetPeer, nil)
if err != nil {
log.Error().Err(err).Msg("Failed to execute get query")
return c.Status(fiber.StatusInternalServerError).SendString("Failed to execute get query")
}
return c.JSON(result)
}

func sync(c *fiber.Ctx) error {
payload := new(PeerInsertModel)
if err := c.BodyParser(payload); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions node/pkg/boot/peer/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ func Routes(router fiber.Router) {
peer := router.Group("/peer")

peer.Post("/sync", sync)
peer.Post("", insert)
peer.Get("", get)
}
52 changes: 0 additions & 52 deletions node/pkg/boot/tests/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestPeerInsert(t *testing.T) {
ctx := context.Background()
cleanup, testItems, err := setup(ctx)
if err != nil {
t.Fatalf("error setting up test: %v", err)
}
defer cleanup()

mockPeer1 := peer.PeerInsertModel{
Url: "/ip4/100.78.175.63/udp/10002/quic-v1/p2p/12D3KooWLT1Pp1EN1G4waBShMWgr67acueYfnrWMtkpsUbAt59Lj",
}

readResultBefore, err := adminTests.GetRequest[[]peer.PeerModel](testItems.app, "/api/v1/peer", nil)
if err != nil {
t.Fatalf("error getting peers before: %v", err)
}

insertResult, err := adminTests.PostRequest[peer.PeerModel](testItems.app, "/api/v1/peer", mockPeer1)
if err != nil {
t.Fatalf("error inserting peer: %v", err)
}
assert.Equal(t, insertResult.Url, mockPeer1.Url)

readResultAfter, err := adminTests.GetRequest[[]peer.PeerModel](testItems.app, "/api/v1/peer", nil)
if err != nil {
t.Fatalf("error getting peers after: %v", err)
}

assert.Greaterf(t, len(readResultAfter), len(readResultBefore), "expected to have more peers after insertion")

//cleanup
_, err = db.QueryRow[peer.PeerModel](ctx, peer.DeletePeerById, map[string]any{"id": insertResult.ID})
if err != nil {
t.Fatalf("error cleaning up test: %v", err)
}
}

func TestPeerGet(t *testing.T) {
ctx := context.Background()
cleanup, testItems, err := setup(ctx)
if err != nil {
t.Fatalf("error setting up test: %v", err)
}
defer cleanup()

readResult, err := adminTests.GetRequest[[]peer.PeerModel](testItems.app, "/api/v1/peer", nil)
if err != nil {
t.Fatalf("error getting peers: %v", err)
}
assert.Greater(t, len(readResult), 0, "expected to have at least one peer")
}

func TestSync(t *testing.T) {
ctx := context.Background()
cleanup, testItems, err := setup(ctx)
Expand Down

0 comments on commit c555380

Please sign in to comment.