Skip to content

Commit

Permalink
Merge pull request #13 from usama-tariq1/Update
Browse files Browse the repository at this point in the history
Update -> create controller with model , create middleware with cli ,…
  • Loading branch information
usama-tariq1 authored Jul 25, 2023
2 parents f2a5497 + 3978ba6 commit 6f872dc
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 9 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

#
Expand Down
31 changes: 31 additions & 0 deletions leet-gin/templates/MiddlewareTemplate.tmpl
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 3 additions & 1 deletion leet-gin/templates/RouterTemplate.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions leet-gin/templates/RouterTemplateWithController.tmpl
Original file line number Diff line number Diff line change
@@ -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)
}
31 changes: 31 additions & 0 deletions middlewares/Sample.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 3 additions & 3 deletions migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
7 changes: 5 additions & 2 deletions models/Sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 4 additions & 2 deletions routers/SampleRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6f872dc

Please sign in to comment.