Skip to content

Commit 32f0154

Browse files
committed
Initial commit
0 parents  commit 32f0154

File tree

9 files changed

+731
-0
lines changed

9 files changed

+731
-0
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# **Gin GoORM REST api**
2+
3+
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).
4+
5+
## **Contributors**
6+
7+
Gin GoORM REST api is authored by **[GoLang Mitrah](https://www.MitrahSoft.com/)** and everyone is welcome to contribute.
8+
9+
## **Problems**
10+
11+
If you experience any problems with Gin GoORM REST api API wrapper please:
12+
13+
* [submit a ticket to our issue tracker](https://github.com/golang-mitrah/gin-RestAPI-postgres-orm/issues)
14+
* fix the error yourself and send us a pull request
15+
16+
## **Social Media**
17+
18+
You'll find us on [Twitter](https://twitter.com/MitrahSoft) and [Facebook](http://www.facebook.com/MitrahSoft)

controllers/user.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package controllers
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/gin-gonic/gin"
8+
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/models"
9+
)
10+
11+
//GetUsers ... Get all users
12+
func GetUsers(c *gin.Context) {
13+
var user []models.User
14+
err := models.GetAllUsers(&user)
15+
if err != nil {
16+
c.AbortWithStatus(http.StatusNotFound)
17+
} else {
18+
c.JSON(http.StatusOK, user)
19+
}
20+
}
21+
22+
//CreateUser ... Create User
23+
func CreateUser(c *gin.Context) {
24+
var user models.User
25+
c.BindJSON(&user)
26+
err := models.CreateUser(&user)
27+
if err != nil {
28+
fmt.Println(err.Error())
29+
c.AbortWithStatus(http.StatusNotFound)
30+
} else {
31+
c.JSON(http.StatusOK, user)
32+
}
33+
}
34+
35+
//GetUserByID ... Get the user by id
36+
func GetUserByID(c *gin.Context) {
37+
id := c.Params.ByName("id")
38+
var user models.User
39+
err := models.GetUserByID(&user, id)
40+
if err != nil {
41+
c.AbortWithStatus(http.StatusNotFound)
42+
} else {
43+
c.JSON(http.StatusOK, user)
44+
}
45+
}
46+
47+
//UpdateUser ... Update the user information
48+
func UpdateUser(c *gin.Context) {
49+
var user models.User
50+
id := c.Params.ByName("id")
51+
err := models.GetUserByID(&user, id)
52+
if err != nil {
53+
c.JSON(http.StatusNotFound, user)
54+
}
55+
c.BindJSON(&user)
56+
err = models.UpdateUser(&user, id)
57+
if err != nil {
58+
c.AbortWithStatus(http.StatusNotFound)
59+
} else {
60+
c.JSON(http.StatusOK, user)
61+
}
62+
}
63+
64+
//DeleteUser ... Delete the user
65+
func DeleteUser(c *gin.Context) {
66+
var user models.User
67+
id := c.Params.ByName("id")
68+
err := models.DeleteUser(&user, id)
69+
if err != nil {
70+
c.AbortWithStatus(http.StatusNotFound)
71+
} else {
72+
c.JSON(http.StatusOK, gin.H{"id" + id: "is deleted"})
73+
}
74+
}

database/database.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package database
2+
3+
import (
4+
"gorm.io/driver/postgres"
5+
"gorm.io/gorm"
6+
)
7+
8+
var DB *gorm.DB
9+
10+
func ConnectToDB() {
11+
var err error
12+
connection, err := gorm.Open(postgres.Open("host=localhost user=sm dbname=gin_goorm_rest port=5432 password="), &gorm.Config{})
13+
if err != nil {
14+
panic("could not connect to DB!")
15+
}
16+
17+
DB = connection
18+
19+
}

go.mod

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module github.com/golang-mitrah/gin-RestAPI-postgres-orm
2+
3+
go 1.17
4+
5+
require (
6+
github.com/gin-contrib/sse v0.1.0 // indirect
7+
github.com/gin-gonic/gin v1.7.4 // indirect
8+
github.com/go-playground/locales v0.13.0 // indirect
9+
github.com/go-playground/universal-translator v0.17.0 // indirect
10+
github.com/go-playground/validator/v10 v10.4.1 // indirect
11+
github.com/golang/protobuf v1.3.3 // indirect
12+
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
13+
github.com/jackc/pgconn v1.8.1 // indirect
14+
github.com/jackc/pgio v1.0.0 // indirect
15+
github.com/jackc/pgpassfile v1.0.0 // indirect
16+
github.com/jackc/pgproto3/v2 v2.0.6 // indirect
17+
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
18+
github.com/jackc/pgtype v1.7.0 // indirect
19+
github.com/jackc/pgx/v4 v4.11.0 // indirect
20+
github.com/jinzhu/inflection v1.0.0 // indirect
21+
github.com/jinzhu/now v1.1.2 // indirect
22+
github.com/json-iterator/go v1.1.9 // indirect
23+
github.com/leodido/go-urn v1.2.0 // indirect
24+
github.com/mattn/go-isatty v0.0.12 // indirect
25+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
26+
github.com/modern-go/reflect2 v1.0.1 // indirect
27+
github.com/ugorji/go/codec v1.1.7 // indirect
28+
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
29+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
30+
golang.org/x/text v0.3.3 // indirect
31+
gopkg.in/yaml.v2 v2.2.8 // indirect
32+
gorm.io/driver/postgres v1.1.0 // indirect
33+
gorm.io/gorm v1.21.14 // indirect
34+
)

go.sum

+490
Large diffs are not rendered by default.

main.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/database"
5+
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/models"
6+
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/routes"
7+
)
8+
9+
func main() {
10+
11+
database.ConnectToDB()
12+
database.DB.AutoMigrate(&models.User{})
13+
14+
r := routes.SetupRouter()
15+
//running
16+
r.Run()
17+
}

models/user.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package models
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/database"
7+
_ "gorm.io/driver/postgres"
8+
)
9+
10+
//GetAllUsers Fetch all user data
11+
func GetAllUsers(user *[]User) (err error) {
12+
if err = database.DB.Find(user).Error; err != nil {
13+
return err
14+
}
15+
return nil
16+
}
17+
18+
//CreateUser ... Insert New data
19+
func CreateUser(user *User) (err error) {
20+
if err = database.DB.Create(user).Error; err != nil {
21+
return err
22+
}
23+
return nil
24+
}
25+
26+
//GetUserByID ... Fetch only one user by Id
27+
func GetUserByID(user *User, id string) (err error) {
28+
if err = database.DB.Where("id = ?", id).First(user).Error; err != nil {
29+
return err
30+
}
31+
return nil
32+
}
33+
34+
//UpdateUser ... Update user
35+
func UpdateUser(user *User, id string) (err error) {
36+
fmt.Println(user)
37+
database.DB.Save(user)
38+
return nil
39+
}
40+
41+
//DeleteUser ... Delete user
42+
func DeleteUser(user *User, id string) (err error) {
43+
database.DB.Where("id = ?", id).Delete(user)
44+
return nil
45+
}

models/usermodel.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package models
2+
3+
type User struct {
4+
Id uint `json:"id"`
5+
Name string `json:"name"`
6+
Email string `json:"email"`
7+
Phone string `json:"phone"`
8+
Address string `json:"address"`
9+
}
10+
11+
func (b *User) TableName() string {
12+
return "user"
13+
}

routes/route.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package routes
2+
3+
import (
4+
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/controllers"
5+
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
//SetupRouter ... Configure routes
10+
func SetupRouter() *gin.Engine {
11+
r := gin.Default()
12+
grp1 := r.Group("/user-api")
13+
{
14+
grp1.GET("user", controllers.GetUsers)
15+
grp1.POST("user", controllers.CreateUser)
16+
grp1.GET("user/:id", controllers.GetUserByID)
17+
grp1.PUT("user/:id", controllers.UpdateUser)
18+
grp1.DELETE("user/:id", controllers.DeleteUser)
19+
}
20+
return r
21+
}

0 commit comments

Comments
 (0)