-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created funct for controllers and routes
- Loading branch information
1 parent
33c859b
commit d977d60
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,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) | ||
} |
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,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) | ||
|
||
} |