From d977d60ae42cf93ac9ea6a6dac76a9e24c14e93e Mon Sep 17 00:00:00 2001 From: Utkarsh4517 Date: Fri, 20 Sep 2024 05:58:54 +0530 Subject: [PATCH] created funct for controllers and routes --- controllers/_controller.go | 0 controllers/generateController.go | 25 +++++++++++++++++++++++++ routes/routes.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) delete mode 100644 controllers/_controller.go create mode 100644 controllers/generateController.go diff --git a/controllers/_controller.go b/controllers/_controller.go deleted file mode 100644 index e69de29..0000000 diff --git a/controllers/generateController.go b/controllers/generateController.go new file mode 100644 index 0000000..f6aab28 --- /dev/null +++ b/controllers/generateController.go @@ -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) +} \ No newline at end of file diff --git a/routes/routes.go b/routes/routes.go index e69de29..01862fe 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -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) + +} \ No newline at end of file