-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create test apis + db update + create instance
- Loading branch information
1 parent
7f5ddc8
commit 3c0bd3b
Showing
28 changed files
with
780 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/celestiaorg/knuu/internal/api/v1/services" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func (h *TestHandler) CreateInstance(c *gin.Context) { | ||
user, err := getUserFromContext(c) | ||
if err != nil { | ||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) | ||
return | ||
} | ||
|
||
var input services.Instance | ||
if err := c.ShouldBindJSON(&input); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input"}) | ||
return | ||
} | ||
|
||
err = h.testService.CreateInstance(c.Request.Context(), user.ID, &input) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
c.JSON(http.StatusCreated, gin.H{"message": "Instance created successfully"}) | ||
} | ||
|
||
func (h *TestHandler) GetInstance(c *gin.Context) { | ||
user, err := getUserFromContext(c) | ||
if err != nil { | ||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) | ||
return | ||
} | ||
|
||
instance, err := h.testService.GetInstance(c.Request.Context(), user.ID, c.Param("scope"), c.Param("instance_name")) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, instance) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/celestiaorg/knuu/internal/api/v1/services" | ||
"github.com/celestiaorg/knuu/internal/database/models" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type TestHandler struct { | ||
testService *services.TestService | ||
} | ||
|
||
func NewTestHandler(ts *services.TestService) *TestHandler { | ||
return &TestHandler{testService: ts} | ||
} | ||
|
||
func (h *TestHandler) CreateTest(c *gin.Context) { | ||
user, err := getUserFromContext(c) | ||
if err != nil { | ||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) | ||
return | ||
} | ||
|
||
var input models.Test | ||
if err := c.ShouldBindJSON(&input); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input"}) | ||
return | ||
} | ||
|
||
input.UserID = user.ID | ||
if err := h.testService.Create(c.Request.Context(), &input); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
c.JSON(http.StatusCreated, gin.H{"message": "Test created successfully"}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package handlers | ||
|
||
// Users can request for new tokens with some permissions | ||
// A user can have multiple tokens with different permissions | ||
// A token can be revoked by the user or by the admin |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/celestiaorg/knuu/internal/api/v1/middleware" | ||
"github.com/celestiaorg/knuu/internal/database/models" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func getUserFromContext(c *gin.Context) (*models.User, error) { | ||
user, ok := c.Get(middleware.UserContextKey) | ||
if !ok { | ||
return nil, errors.New("user not found in context") | ||
} | ||
authUser, ok := user.(*models.User) | ||
if !ok { | ||
return nil, errors.New("invalid user data in context") | ||
} | ||
return authUser, nil | ||
} |
Oops, something went wrong.