Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Erhan Yakut committed Mar 24, 2019
1 parent c704af5 commit 1e90fba
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ database:
port: "5432"
```

## Default Models
UGin has two models (Post and Tag) as boilerplate to show relational database useage.

**/model/post-model.go** content:
```
type Post struct {
gorm.Model
Name string `json:"Name" gorm:"type:varchar(255)"`
Description string `json:"Description" gorm:"type:text"`
Tags []Tag // One-To-Many relationship (has many - use Tag's UserID as foreign key)
}
type Tag struct {
gorm.Model
PostID uint `gorm:"index"` // Foreign key (belongs to)
Name string `json:"Name" gorm:"type:varchar(255)"`
Description string `json:"Description" gorm:"type:text"`
}
```

**Query parameters:**
```
/posts/?limit=2
/posts/?offset=0
/posts/?name=Third
/posts/?description=My
/posts/?order=name|asc
```

## Filtering, Search and Pagination
UGin has it's own filtering, search and pagination system. You just need to use these parameters.

Expand Down
11 changes: 11 additions & 0 deletions controller/post-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var err error
// Post struct alias
type Post = model.Post

// Tag struct alias
type Tag = model.Tag

// Data is mainle generated for filtering and pagination
type Data struct {
Total int64
Expand All @@ -28,11 +31,19 @@ func GetPost(c *gin.Context) {
db = include.GetDB()
id := c.Params.ByName("id")
var post Post
var tags []Tag

if err := db.Where("id = ? ", id).First(&post).Error; err != nil {

c.AbortWithStatus(404)
fmt.Println(err)

} else {

db.Model(&post).Related(&tags)
// SELECT * FROM "tags" WHERE ("post_id" = 1)

post.Tags = tags
c.JSON(200, post)
}
}
Expand Down
1 change: 1 addition & 0 deletions include/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func InitDB() *gorm.DB {

}

db.LogMode(true)
DB = db

return DB
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {

db = include.InitDB()
defer db.Close()
db.AutoMigrate(&model.Post{})
db.AutoMigrate(&model.Post{}, &model.Tag{})

router := gin.Default()
router.Use(include.CORS())
Expand Down
27 changes: 21 additions & 6 deletions model/post-model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,32 @@ import (

type Post struct {
gorm.Model
Name string `json:"Name" gorm:"type:varchar(200)"`
Name string `json:"Name" gorm:"type:varchar(255)"`
Description string `json:"Description" gorm:"type:text"`
Tags []Tag // One-To-Many relationship (has many - use Tag's UserID as foreign key)
}

type Tag struct {
gorm.Model
PostID uint `gorm:"index"` // Foreign key (belongs to)
Name string `json:"Name" gorm:"type:varchar(255)"`
Description string `json:"Description" gorm:"type:text"`
}

// You can send this data to API /posts/ endpoint with POST method to create dummy content
/*
{
"ID": 1,
"CreatedAt": "2019-03-10T23:04:15.78791+03:00",
"UpdatedAt": "2019-03-10T23:04:15.78791+03:00",
"DeletedAt": null,
"Name": "Hello World",
"Description": "This is your first post"
"Description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
"Tags": [
{
"Name": "lorem",
"Description": "This is lorem tag"
},
{
"Name": "ipsum",
"Description": "This is ipsum tag"
}
]
}
*/
Binary file added ugin.db
Binary file not shown.

0 comments on commit 1e90fba

Please sign in to comment.