From 3978ba688beeef2d7cf075b8be96f1efaa6100a6 Mon Sep 17 00:00:00 2001 From: leet-dino Date: Tue, 25 Jul 2023 18:17:08 +0500 Subject: [PATCH] Update -> create controller with model , create middleware with cli , auto populate router with controller --- README.md | 16 +++++++++- leet-gin/templates/MiddlewareTemplate.tmpl | 31 +++++++++++++++++++ leet-gin/templates/RouterTemplate.tmpl | 4 ++- .../RouterTemplateWithController.tmpl | 22 +++++++++++++ middlewares/Sample.go | 31 +++++++++++++++++++ migrations/migrations.go | 6 ++-- models/Sample.go | 7 +++-- routers/SampleRouter.go | 6 ++-- 8 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 leet-gin/templates/MiddlewareTemplate.tmpl create mode 100644 leet-gin/templates/RouterTemplateWithController.tmpl create mode 100644 middlewares/Sample.go diff --git a/README.md b/README.md index 34b4518..a0f0201 100644 --- a/README.md +++ b/README.md @@ -63,13 +63,27 @@ leet-astro create controller ControllerName ``` ## Create Model +create just model with query methods ```sh leet-astro create model ModalName ``` +With controller auto generated +```sh +leet-astro create model Name --controller=true +``` +With controller and router auto generated +```sh +leet-astro create model Name --controller=true --router=true +``` ## Create Router ```sh -leet-astro create router RouterName +leet-astro create router NameRouter +``` + +## Create Middleware +```sh +leet-astro create middleware Name ``` # diff --git a/leet-gin/templates/MiddlewareTemplate.tmpl b/leet-gin/templates/MiddlewareTemplate.tmpl new file mode 100644 index 0000000..a0c8aa8 --- /dev/null +++ b/leet-gin/templates/MiddlewareTemplate.tmpl @@ -0,0 +1,31 @@ +package middlewares + +import ( + "log" + "time" + + "github.com/gin-gonic/gin" +) + +func {{.Name}}() gin.HandlerFunc { + return func(c *gin.Context) { + t := time.Now() + + // Set example variable + c.Set("example", "12345") + // get the value in controller with + // example := c.MustGet("example").(string) + + // before request + + c.Next() + + // after request + latency := time.Since(t) + log.Print(latency) + + // access the status we are sending + status := c.Writer.Status() + log.Println(status) + } +} diff --git a/leet-gin/templates/RouterTemplate.tmpl b/leet-gin/templates/RouterTemplate.tmpl index 3810be2..305e7a6 100644 --- a/leet-gin/templates/RouterTemplate.tmpl +++ b/leet-gin/templates/RouterTemplate.tmpl @@ -9,9 +9,11 @@ import ( type {{ .Name }} struct { } -// var SampleController controllers.SampleController func ({{ .Name }} {{ .Name }} ) handle(router *gin.RouterGroup) { + // var SampleController controllers.SampleController + // router.Use(middlewares.Sample()) + // router.GET("", SampleController.Index) // router.POST("", SampleController.Create) // router.GET("/:id", SampleController.Read) diff --git a/leet-gin/templates/RouterTemplateWithController.tmpl b/leet-gin/templates/RouterTemplateWithController.tmpl new file mode 100644 index 0000000..844a7be --- /dev/null +++ b/leet-gin/templates/RouterTemplateWithController.tmpl @@ -0,0 +1,22 @@ +package routers + +import ( + "github.com/gin-gonic/gin" + "{{.ModName}}/controllers" + +) + +type {{ .Name }} struct { +} + + +func ({{ .Name }} {{ .Name }} ) handle(router *gin.RouterGroup) { + var {{.ControllerName}} controllers.{{.ControllerName}} + // router.Use(middlewares.Sample()) + + router.GET("", {{.ControllerName}}.Index) + router.POST("", {{.ControllerName}}.Create) + router.GET("/:id", {{.ControllerName}}.Read) + router.PATCH("/:id", {{.ControllerName}}.Update) + router.DELETE("/:id", {{.ControllerName}}.Delete) +} diff --git a/middlewares/Sample.go b/middlewares/Sample.go new file mode 100644 index 0000000..3061e15 --- /dev/null +++ b/middlewares/Sample.go @@ -0,0 +1,31 @@ +package middlewares + +import ( + "log" + "time" + + "github.com/gin-gonic/gin" +) + +func Sample() gin.HandlerFunc { + return func(c *gin.Context) { + t := time.Now() + + // Set example variable + c.Set("example", "12345") + // get the value in controller with + // example := c.MustGet("example").(string) + + // before request + + c.Next() + + // after request + latency := time.Since(t) + log.Print(latency) + + // access the status we are sending + status := c.Writer.Status() + log.Println(status) + } +} diff --git a/migrations/migrations.go b/migrations/migrations.go index 48d0fd5..456c124 100644 --- a/migrations/migrations.go +++ b/migrations/migrations.go @@ -2,17 +2,17 @@ package migrations import ( leetGin "github.com/usama-tariq1/leet-gin/helper" + "github.com/usama-tariq1/leet-gin/models" "gorm.io/gorm" ) -// var DB *gorm.DB var console leetGin.Console -// var sample models.Sample +var sample models.Sample // list models here func Migrate(DB *gorm.DB) { - // DB.AutoMigrate(sample) + DB.AutoMigrate(sample) } diff --git a/models/Sample.go b/models/Sample.go index 3badb9e..b50b260 100644 --- a/models/Sample.go +++ b/models/Sample.go @@ -45,14 +45,17 @@ func (Sample) GetList(page, limit int) ([]Sample, int64, error) { var list []Sample var totalCount int64 + // Create a query builder + query := Lamp().Model(&Sample{}) + // Retrieve the total count - if err := Lamp().Model(&Sample{}).Count(&totalCount).Error; err != nil { + if err := query.Count(&totalCount).Error; err != nil { return nil, 0, err } // Apply pagination and retrieve the list offset := (page - 1) * limit - if err := Lamp().Limit(limit).Offset(offset).Find(&list).Error; err != nil { + if err := query.Limit(limit).Offset(offset).Find(&list).Error; err != nil { return nil, 0, err } diff --git a/routers/SampleRouter.go b/routers/SampleRouter.go index 0d8fc67..027182f 100644 --- a/routers/SampleRouter.go +++ b/routers/SampleRouter.go @@ -3,14 +3,16 @@ package routers import ( "github.com/gin-gonic/gin" "github.com/usama-tariq1/leet-gin/controllers" + // "github.com/usama-tariq1/leet-gin/middlewares" ) type SampleRouter struct { } -var SampleController controllers.SampleController - func (SampleRouter SampleRouter) handle(router *gin.RouterGroup) { + var SampleController controllers.SampleController + // router.Use(middlewares.Sample()) + router.GET("", SampleController.Index) router.POST("", SampleController.Create) router.GET("/:id", SampleController.Read)