Skip to content

Commit

Permalink
Merge pull request #13 from akmamun/dev
Browse files Browse the repository at this point in the history
added examples
  • Loading branch information
akmamun authored Mar 16, 2022
2 parents 1f98b69 + dd97f06 commit 8facaf6
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/controllers/base_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package controllers

import (
"gorm.io/gorm"
)

type Controller struct {
//rep repository.Repository
DB *gorm.DB
}
Empty file removed examples/controllers/controller.go
Empty file.
26 changes: 26 additions & 0 deletions examples/controllers/create_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package controllers

import (
"gin-boilerplate/models"
"gin-boilerplate/pkg/logger"
"github.com/gin-gonic/gin"
"net/http"
)

func (base *Controller) CreateExample(ctx *gin.Context) {
example := new(models.Example)

err := ctx.ShouldBindJSON(&example)
if err != nil {
logger.Errorf("error: %v", err)
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
err = base.DB.Create(&example).Error
if err != nil {
logger.Errorf("error: %v", err)
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, &example)
}
Empty file.
Empty file.
23 changes: 23 additions & 0 deletions examples/controllers/foreign_relation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package controllers

import (
"gin-boilerplate/examples/models"
"github.com/gin-gonic/gin"
"net/http"
)

// GetNormalData get normal data if added pagination see example_controller
func (base *Controller) GetNormalData(ctx *gin.Context) {
var categories []models.Category
base.DB.Find(&categories)
ctx.JSON(http.StatusOK, gin.H{"data": categories})

}

// GetForeignRelationData Get Foreign Data with Preload
func (base *Controller) GetForeignRelationData(ctx *gin.Context) {
var articles []models.Article
base.DB.Preload("Category").Find(&articles)
ctx.JSON(http.StatusOK, &articles)

}
29 changes: 29 additions & 0 deletions examples/controllers/paginated_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package controllers

import (
"gin-boilerplate/models"
"gin-boilerplate/pkg/helpers/pagination"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)

func (base *Controller) GetPaginatedData(ctx *gin.Context) {
var example []models.Example

page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "0"))
query := ctx.DefaultQuery("query", "")

//db := base.DB.Where("")
paginateData := pagination.Pagination(&pagination.Param{
DB: base.DB, //db
Page: int64(page),
Limit: int64(limit),
//OrderBy: "id desc",
Search: query,
}, &example)

ctx.JSON(http.StatusOK, paginateData)

}
20 changes: 20 additions & 0 deletions examples/controllers/selected_fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package controllers

import (
"gin-boilerplate/examples/models"
"github.com/gin-gonic/gin"
"net/http"
)

// SelectedFiledFetch fields fetch from defining new struct
type SelectedFiledFetch struct {
ID uint `json:"id"`
Title string `json:"title"`
}

func (base *Controller) GetSelectedFieldData(ctx *gin.Context) {
var selectData []SelectedFiledFetch
base.DB.Model(&models.Article{}).Find(&selectData)
ctx.JSON(http.StatusOK, selectData)

}
17 changes: 17 additions & 0 deletions examples/models/example_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package models

import (
"time"
)

type Example struct {
Id int `json:"id"`
Data string `json:"data" binding:"required"`
CreatedAt *time.Time `json:"created_at,string,omitempty"`
UpdatedAt *time.Time `json:"updated_at_at,string,omitempty"`
}

// TableName Database Table Name of this model
func (e *Example) TableName() string {
return "examples"
}
14 changes: 14 additions & 0 deletions examples/models/foreign_key_relation_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package models

//Article table
type Article struct {
ID uint `json:"id" gorm:"primary_key"`
Title string `json:"title"`
CategoryID uint `json:"category_id"`
Category Category `json:"category"`
}

type Category struct {
ID uint `json:"id" gorm:"primary_key"`
CategoryName string `json:"category_name"`
}
11 changes: 11 additions & 0 deletions examples/models/has_many.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

// User has many CreditCards, UserID is the foreign key
type User struct {
CreditCards []CreditCard `json:"credit_cards"`
}

type CreditCard struct {
Number string
UserID uint
}

0 comments on commit 8facaf6

Please sign in to comment.