-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 32f0154
Showing
9 changed files
with
731 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,18 @@ | ||
# **Gin GoORM REST api** | ||
|
||
Clone the git repo - `git clone git@github.com:golang-mitrah/gin-RestAPI-postgres-orm.git` - or [download it](https://github.com/golang-mitrah/gin-RestAPI-postgres-orm/zipball/master). | ||
|
||
## **Contributors** | ||
|
||
Gin GoORM REST api is authored by **[GoLang Mitrah](https://www.MitrahSoft.com/)** and everyone is welcome to contribute. | ||
|
||
## **Problems** | ||
|
||
If you experience any problems with Gin GoORM REST api API wrapper please: | ||
|
||
* [submit a ticket to our issue tracker](https://github.com/golang-mitrah/gin-RestAPI-postgres-orm/issues) | ||
* fix the error yourself and send us a pull request | ||
|
||
## **Social Media** | ||
|
||
You'll find us on [Twitter](https://twitter.com/MitrahSoft) and [Facebook](http://www.facebook.com/MitrahSoft) |
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,74 @@ | ||
package controllers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/models" | ||
) | ||
|
||
//GetUsers ... Get all users | ||
func GetUsers(c *gin.Context) { | ||
var user []models.User | ||
err := models.GetAllUsers(&user) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusNotFound) | ||
} else { | ||
c.JSON(http.StatusOK, user) | ||
} | ||
} | ||
|
||
//CreateUser ... Create User | ||
func CreateUser(c *gin.Context) { | ||
var user models.User | ||
c.BindJSON(&user) | ||
err := models.CreateUser(&user) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
c.AbortWithStatus(http.StatusNotFound) | ||
} else { | ||
c.JSON(http.StatusOK, user) | ||
} | ||
} | ||
|
||
//GetUserByID ... Get the user by id | ||
func GetUserByID(c *gin.Context) { | ||
id := c.Params.ByName("id") | ||
var user models.User | ||
err := models.GetUserByID(&user, id) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusNotFound) | ||
} else { | ||
c.JSON(http.StatusOK, user) | ||
} | ||
} | ||
|
||
//UpdateUser ... Update the user information | ||
func UpdateUser(c *gin.Context) { | ||
var user models.User | ||
id := c.Params.ByName("id") | ||
err := models.GetUserByID(&user, id) | ||
if err != nil { | ||
c.JSON(http.StatusNotFound, user) | ||
} | ||
c.BindJSON(&user) | ||
err = models.UpdateUser(&user, id) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusNotFound) | ||
} else { | ||
c.JSON(http.StatusOK, user) | ||
} | ||
} | ||
|
||
//DeleteUser ... Delete the user | ||
func DeleteUser(c *gin.Context) { | ||
var user models.User | ||
id := c.Params.ByName("id") | ||
err := models.DeleteUser(&user, id) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusNotFound) | ||
} else { | ||
c.JSON(http.StatusOK, gin.H{"id" + id: "is deleted"}) | ||
} | ||
} |
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,19 @@ | ||
package database | ||
|
||
import ( | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var DB *gorm.DB | ||
|
||
func ConnectToDB() { | ||
var err error | ||
connection, err := gorm.Open(postgres.Open("host=localhost user=sm dbname=gin_goorm_rest port=5432 password="), &gorm.Config{}) | ||
if err != nil { | ||
panic("could not connect to DB!") | ||
} | ||
|
||
DB = connection | ||
|
||
} |
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,34 @@ | ||
module github.com/golang-mitrah/gin-RestAPI-postgres-orm | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/gin-contrib/sse v0.1.0 // indirect | ||
github.com/gin-gonic/gin v1.7.4 // indirect | ||
github.com/go-playground/locales v0.13.0 // indirect | ||
github.com/go-playground/universal-translator v0.17.0 // indirect | ||
github.com/go-playground/validator/v10 v10.4.1 // indirect | ||
github.com/golang/protobuf v1.3.3 // indirect | ||
github.com/jackc/chunkreader/v2 v2.0.1 // indirect | ||
github.com/jackc/pgconn v1.8.1 // indirect | ||
github.com/jackc/pgio v1.0.0 // indirect | ||
github.com/jackc/pgpassfile v1.0.0 // indirect | ||
github.com/jackc/pgproto3/v2 v2.0.6 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect | ||
github.com/jackc/pgtype v1.7.0 // indirect | ||
github.com/jackc/pgx/v4 v4.11.0 // indirect | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.2 // indirect | ||
github.com/json-iterator/go v1.1.9 // indirect | ||
github.com/leodido/go-urn v1.2.0 // indirect | ||
github.com/mattn/go-isatty v0.0.12 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.1 // indirect | ||
github.com/ugorji/go/codec v1.1.7 // indirect | ||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect | ||
golang.org/x/text v0.3.3 // indirect | ||
gopkg.in/yaml.v2 v2.2.8 // indirect | ||
gorm.io/driver/postgres v1.1.0 // indirect | ||
gorm.io/gorm v1.21.14 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
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 main | ||
|
||
import ( | ||
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/database" | ||
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/models" | ||
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/routes" | ||
) | ||
|
||
func main() { | ||
|
||
database.ConnectToDB() | ||
database.DB.AutoMigrate(&models.User{}) | ||
|
||
r := routes.SetupRouter() | ||
//running | ||
r.Run() | ||
} |
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,45 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/database" | ||
_ "gorm.io/driver/postgres" | ||
) | ||
|
||
//GetAllUsers Fetch all user data | ||
func GetAllUsers(user *[]User) (err error) { | ||
if err = database.DB.Find(user).Error; err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
//CreateUser ... Insert New data | ||
func CreateUser(user *User) (err error) { | ||
if err = database.DB.Create(user).Error; err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
//GetUserByID ... Fetch only one user by Id | ||
func GetUserByID(user *User, id string) (err error) { | ||
if err = database.DB.Where("id = ?", id).First(user).Error; err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
//UpdateUser ... Update user | ||
func UpdateUser(user *User, id string) (err error) { | ||
fmt.Println(user) | ||
database.DB.Save(user) | ||
return nil | ||
} | ||
|
||
//DeleteUser ... Delete user | ||
func DeleteUser(user *User, id string) (err error) { | ||
database.DB.Where("id = ?", id).Delete(user) | ||
return nil | ||
} |
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,13 @@ | ||
package models | ||
|
||
type User struct { | ||
Id uint `json:"id"` | ||
Name string `json:"name"` | ||
Email string `json:"email"` | ||
Phone string `json:"phone"` | ||
Address string `json:"address"` | ||
} | ||
|
||
func (b *User) TableName() string { | ||
return "user" | ||
} |
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,21 @@ | ||
package routes | ||
|
||
import ( | ||
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/controllers" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
//SetupRouter ... Configure routes | ||
func SetupRouter() *gin.Engine { | ||
r := gin.Default() | ||
grp1 := r.Group("/user-api") | ||
{ | ||
grp1.GET("user", controllers.GetUsers) | ||
grp1.POST("user", controllers.CreateUser) | ||
grp1.GET("user/:id", controllers.GetUserByID) | ||
grp1.PUT("user/:id", controllers.UpdateUser) | ||
grp1.DELETE("user/:id", controllers.DeleteUser) | ||
} | ||
return r | ||
} |