-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from akmamun/dev
added examples
- Loading branch information
Showing
11 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
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,10 @@ | ||
package controllers | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Controller struct { | ||
//rep repository.Repository | ||
DB *gorm.DB | ||
} |
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,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.
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,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) | ||
|
||
} |
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,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) | ||
|
||
} |
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,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) | ||
|
||
} |
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,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" | ||
} |
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,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"` | ||
} |
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,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 | ||
} |