Skip to content

Commit a5bf9fa

Browse files
added the gin with rest-apis
1 parent 3cba8ff commit a5bf9fa

File tree

10 files changed

+806
-0
lines changed

10 files changed

+806
-0
lines changed

.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DB_HOST = localhost
2+
DB_PORT = 5432
3+
DB_USER = postgres
4+
DB_PASSWORD = password
5+
DB_NAME = gin-rest-api
6+

controllers/user.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
15+
err := models.GetAllUsers(&user)
16+
if err != nil {
17+
c.AbortWithStatus(http.StatusNotFound)
18+
} else {
19+
c.JSON(http.StatusOK, user)
20+
}
21+
}
22+
23+
// CreateUser ... Create User
24+
func CreateUser(c *gin.Context) {
25+
var user models.User
26+
27+
if err := c.BindJSON(&user); err != nil {
28+
fmt.Println("failed to bind the json", err.Error())
29+
return
30+
}
31+
32+
err := models.CreateUser(&user)
33+
if err != nil {
34+
fmt.Println(err.Error())
35+
c.AbortWithStatus(http.StatusNotFound)
36+
} else {
37+
c.JSON(http.StatusOK, user)
38+
}
39+
}
40+
41+
// GetUserByID ... Get the user by id
42+
func GetUserByID(c *gin.Context) {
43+
var user models.User
44+
45+
id := c.Params.ByName("id")
46+
47+
err := models.GetUserByID(&user, id)
48+
if err != nil {
49+
c.AbortWithStatus(http.StatusNotFound)
50+
} else {
51+
c.JSON(http.StatusOK, user)
52+
}
53+
}
54+
55+
// UpdateUser ... Update the user information
56+
func UpdateUser(c *gin.Context) {
57+
var user models.User
58+
59+
id := c.Params.ByName("id")
60+
61+
err := models.GetUserByID(&user, id)
62+
if err != nil {
63+
c.JSON(http.StatusNotFound, user)
64+
}
65+
66+
if err := c.BindJSON(&user); err != nil {
67+
fmt.Println("failed to bind the json", err.Error())
68+
return
69+
}
70+
71+
err = models.UpdateUser(&user, id)
72+
if err != nil {
73+
c.AbortWithStatus(http.StatusNotFound)
74+
} else {
75+
c.JSON(http.StatusOK, user)
76+
}
77+
}
78+
79+
// DeleteUser ... Delete the user
80+
func DeleteUser(c *gin.Context) {
81+
var user models.User
82+
83+
id := c.Params.ByName("id")
84+
85+
err := models.DeleteUser(&user, id)
86+
if err != nil {
87+
c.AbortWithStatus(http.StatusNotFound)
88+
} else {
89+
c.JSON(http.StatusOK, gin.H{"id" + id: "is deleted"})
90+
}
91+
}

database/database.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package database
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/helper"
8+
"gorm.io/driver/postgres"
9+
"gorm.io/gorm"
10+
)
11+
12+
var DB *gorm.DB
13+
14+
func ConnectToDB() error {
15+
err := helper.Configure(".env")
16+
if err != nil {
17+
fmt.Println("error is loading env file")
18+
return err
19+
}
20+
21+
host := os.Getenv("DB_HOST")
22+
port := os.Getenv("DB_PORT")
23+
password := os.Getenv("DB_PASSWORD")
24+
dbname := os.Getenv("DB_NAME")
25+
user := os.Getenv("DB_USER")
26+
27+
connectionURI := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s", user, password, host, port, dbname)
28+
29+
//connecting to postgres-SQL
30+
connection, err := gorm.Open(postgres.Open(connectionURI), &gorm.Config{})
31+
if err != nil {
32+
panic("could not connect to DB!")
33+
}
34+
35+
DB = connection
36+
37+
return nil
38+
}

go.mod

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

go.sum

Lines changed: 506 additions & 0 deletions
Large diffs are not rendered by default.

helper/helpers.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package helper
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/joho/godotenv"
7+
)
8+
9+
func Configure(filename string) error {
10+
err := godotenv.Load(filename)
11+
if err != nil {
12+
fmt.Printf("error at loading %s file", filename)
13+
return err
14+
}
15+
16+
return nil
17+
}

main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/database"
7+
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/models"
8+
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/routes"
9+
)
10+
11+
func main() {
12+
if err := database.ConnectToDB(); err != nil {
13+
fmt.Println("Failed to connect to DB", err)
14+
return
15+
}
16+
17+
database.DB.AutoMigrate(&models.User{})
18+
19+
r := routes.SetupRouter()
20+
21+
//running
22+
r.Run()
23+
}

models/user.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package models
2+
3+
import (
4+
"github.com/golang-mitrah/gin-RestAPI-postgres-orm/database"
5+
_ "gorm.io/driver/postgres"
6+
)
7+
8+
// GetAllUsers Fetch all user data
9+
func GetAllUsers(user *[]User) (err error) {
10+
if err = database.DB.Find(user).Error; err != nil {
11+
return err
12+
}
13+
14+
return nil
15+
}
16+
17+
// CreateUser ... Insert New data
18+
func CreateUser(user *User) (err error) {
19+
if err = database.DB.Create(user).Error; err != nil {
20+
return err
21+
}
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+
32+
return nil
33+
}
34+
35+
// UpdateUser ... Update user
36+
func UpdateUser(user *User, id string) (err error) {
37+
if err = database.DB.Save(user).Error; err != nil {
38+
return err
39+
}
40+
41+
return nil
42+
}
43+
44+
// DeleteUser ... Delete user
45+
func DeleteUser(user *User, id string) (err error) {
46+
if err = database.DB.Where("id = ?", id).Delete(user).Error; err != nil {
47+
return err
48+
}
49+
50+
return nil
51+
}

models/usermodel.go

Lines changed: 13 additions & 0 deletions
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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
13+
grp1 := r.Group("/user-api")
14+
{
15+
grp1.GET("user", controllers.GetUsers)
16+
grp1.POST("user", controllers.CreateUser)
17+
grp1.GET("user/:id", controllers.GetUserByID)
18+
grp1.PUT("user/:id", controllers.UpdateUser)
19+
grp1.DELETE("user/:id", controllers.DeleteUser)
20+
}
21+
22+
return r
23+
}

0 commit comments

Comments
 (0)