lots more stuff

This commit is contained in:
2026-07-16 19:24:39 -07:00
parent a06f21a5c2
commit 228e2e99f7
22 changed files with 1605 additions and 405 deletions
-162
View File
@@ -62,165 +62,3 @@ func main() {
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"})
}