created dummy API routes, passing API test

This commit is contained in:
2026-07-16 14:01:39 -07:00
parent 0d952d7221
commit ea34be4e1b
3 changed files with 343 additions and 1 deletions
+178
View File
@@ -15,11 +15,13 @@ func main() {
DB = db
router := gin.Default()
router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
router.GET("/db-check", func(c *gin.Context) {
if err := DB.Ping(); err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"status": "unreachable", "error": err.Error()})
@@ -27,5 +29,181 @@ func main() {
}
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
// User routes
router.POST("/users", createUserHandler)
router.GET("/users/:id", getUserHandler)
router.PUT("/users/:id", updateUserHandler)
router.DELETE("/users/:id", deleteUserHandler)
// Object routes
router.POST("/objects", createObjectHandler)
router.GET("/objects/:id", getObjectHandler)
router.GET("/users/:id/objects", getObjectsByUserHandler)
router.PUT("/objects/:id", updateObjectHandler)
router.DELETE("/objects/:id", deleteObjectHandler)
router.Run() // listens on 0.0.0.0:8080 by default
}
// createUserRequest is the expected body for POST /users.
type createUserRequest struct {
UserID string `json:"userId" binding:"required"`
RootObjectID string `json:"rootObjectId" binding:"required"`
}
func createUserHandler(c *gin.Context) {
var req createUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := CreateUser(req.UserID, req.RootObjectID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{"status": "created"})
}
func getUserHandler(c *gin.Context) {
id := c.Param("id")
user, err := GetUser(id)
if err != nil {
if err.Error() == "user not found" {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, user)
}
// updateUserRequest is the expected body for PUT /users/:id.
type updateUserRequest struct {
RootObjectID string `json:"rootObjectId" binding:"required"`
}
func updateUserHandler(c *gin.Context) {
id := c.Param("id")
var req updateUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := UpdateUser(id, req.RootObjectID); err != nil {
if err.Error() == "user not found" {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "updated"})
}
func deleteUserHandler(c *gin.Context) {
id := c.Param("id")
if err := DeleteUser(id); err != nil {
if err.Error() == "user not found" {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
}
// createObjectRequest is the expected body for POST /objects.
type createObjectRequest struct {
ObjectID string `json:"objectId" binding:"required"`
UserID string `json:"userId" binding:"required"`
ObjectData string `json:"objectData" binding:"required"`
}
func createObjectHandler(c *gin.Context) {
var req createObjectRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := CreateObject(req.ObjectID, req.UserID, req.ObjectData); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{"status": "created"})
}
func getObjectHandler(c *gin.Context) {
id := c.Param("id")
obj, err := GetObject(id)
if err != nil {
if err.Error() == "object not found" {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, obj)
}
func getObjectsByUserHandler(c *gin.Context) {
id := c.Param("id")
objects, err := GetObjectsByUser(id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, objects)
}
// updateObjectRequest is the expected body for PUT /objects/:id.
type updateObjectRequest struct {
ObjectData string `json:"objectData" binding:"required"`
}
func updateObjectHandler(c *gin.Context) {
id := c.Param("id")
var req updateObjectRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := UpdateObject(id, req.ObjectData); err != nil {
if err.Error() == "object not found" {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "updated"})
}
func deleteObjectHandler(c *gin.Context) {
id := c.Param("id")
if err := DeleteObject(id); err != nil {
if err.Error() == "object not found" {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
}