Skip to content

Commit

Permalink
created funct for controllers and routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Utkarsh4517 committed Sep 20, 2024
1 parent 33c859b commit d977d60
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Empty file removed controllers/<model>_controller.go
Empty file.
25 changes: 25 additions & 0 deletions controllers/generateController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package controllers

import (
"fmt"
"os"
)

func generateControllerFile(modelName string) {
controllerFile := fmt.Sprintf("./controllers/%s_controller.go", modelName)
file, _ := os.Create(controllerFile)
defer file.Close()

file.WriteString(fmt.Sprintf("package controllers\n\nimport (\n\t\"net/http\"\n\t\"github.com/gin-gonic/gin\"\n\t\"../models\"\n)\n\n"))

// Generate CRUD functions
file.WriteString(fmt.Sprintf("func Get%s(c *gin.Context) {\n\t// Get all %s\n\tvar items []models.%s\n\t// Fetch from DB (error handling to be added)\n\tc.JSON(http.StatusOK, items)\n}\n\n", modelName, modelName, modelName))

file.WriteString(fmt.Sprintf("func Create%s(c *gin.Context) {\n\t// Create %s\n\tvar item models.%s\n\t// Bind request body (error handling to be added)\n\tc.BindJSON(&item)\n\t// Save to DB\n\tc.JSON(http.StatusOK, item)\n}\n\n", modelName, modelName, modelName))

file.WriteString(fmt.Sprintf("func Update%s(c *gin.Context) {\n\t// Update %s\n\tvar item models.%s\n\t// Fetch existing item and update (error handling to be added)\n\tc.JSON(http.StatusOK, item)\n}\n\n", modelName, modelName, modelName))

file.WriteString(fmt.Sprintf("func Delete%s(c *gin.Context) {\n\t// Delete %s\n\tvar item models.%s\n\t// Perform deletion (error handling to be added)\n\tc.JSON(http.StatusOK, item)\n}\n\n", modelName, modelName, modelName))

fmt.Printf("Controller for %s created successfully!\n", modelName)
}
31 changes: 31 additions & 0 deletions routes/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package routes

import (
"fmt"
"os"
)

func generateRoutesFile(modelName string) {
routesFile := "./routes/routes.go"

if _, err := os.Stat(routesFile); os.IsNotExist(err) {
file, _ := os.Create(routesFile)
defer file.Close()

file.WriteString("package routes\n\nimport (\n\t\"github.com/gin-gonic/gin\"\n\t\"../controllers\"\n)\n\n")
file.WriteString("func SetupRouter() *gin.Engine {\n\trouter := gin.Default()\n\n")
file.WriteString("\treturn router\n}")
}

file, _ := os.OpenFile(routesFile, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
defer file.Close()

// Register routes for the current model
file.WriteString(fmt.Sprintf("\trouter.GET(\"/%s\", controllers.Get%s)\n", modelName, modelName))
file.WriteString(fmt.Sprintf("\trouter.POST(\"/%s\", controllers.Create%s)\n", modelName, modelName))
file.WriteString(fmt.Sprintf("\trouter.PUT(\"/%s/:id\", controllers.Update%s)\n", modelName, modelName))
file.WriteString(fmt.Sprintf("\trouter.DELETE(\"/%s/:id\", controllers.Delete%s)\n", modelName, modelName))

fmt.Printf("Routes for %s added successfully!\n", modelName)

}

0 comments on commit d977d60

Please sign in to comment.