This commit is contained in:
Fran Jurmanović
2021-05-06 20:09:29 +02:00
parent 3a5138eb83
commit 20894ee42e
18 changed files with 197 additions and 28 deletions

View File

@@ -0,0 +1,14 @@
package api
import (
"github.com/gin-gonic/gin"
"github.com/go-pg/pg/v10"
)
func Init(s *gin.Engine, db *pg.DB) {
Routes(s, db)
}
type API struct {
Api string `json:"api"`
}

15
pkg/api/routes.go Normal file
View File

@@ -0,0 +1,15 @@
package api
import (
"wallet-api/pkg/controllers"
"wallet-api/pkg/services"
"github.com/gin-gonic/gin"
"github.com/go-pg/pg/v10"
)
func Routes(s *gin.Engine, db *pg.DB) {
apiService := services.ApiService{Db: db}
apiController := controllers.ApiController{ApiService: &apiService}
apiController.Init(s)
}

21
pkg/controllers/api.go Normal file
View File

@@ -0,0 +1,21 @@
package controllers
import (
"wallet-api/pkg/services"
"github.com/gin-gonic/gin"
)
type ApiController struct {
ApiService *services.ApiService
}
func (ac *ApiController) Init(s *gin.Engine) {
apiGroup := s.Group("/api")
apiGroup.GET("", ac.getFirst)
}
func (ac *ApiController) getFirst(c *gin.Context) {
apiModel := ac.ApiService.GetFirst()
c.JSON(200, apiModel)
}

13
pkg/migrate/migrate.go Normal file
View File

@@ -0,0 +1,13 @@
package migrate
import (
"wallet-api/pkg/migrate/migrations"
"github.com/go-pg/pg/v10"
)
func Start(conn *pg.DB) {
apiMigration := migrations.ApiMigration{Db: conn}
apiMigration.Create()
}

View File

@@ -0,0 +1,31 @@
package migrations
import (
"fmt"
"log"
"wallet-api/pkg/models"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
)
type ApiMigration struct {
Db *pg.DB
}
func (am *ApiMigration) Create() {
models := []interface{}{
(*models.ApiModel)(nil),
}
for _, model := range models {
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
IfNotExists: true,
})
if err != nil {
log.Printf("Error Creating Table: %s", err)
} else {
fmt.Println("Table created successfully")
}
}
}

6
pkg/models/api.go Normal file
View File

@@ -0,0 +1,6 @@
package models
type ApiModel struct {
tableName struct{} `pg:"api,alias:api"`
Api string `json:"api"`
}

17
pkg/services/api.go Normal file
View File

@@ -0,0 +1,17 @@
package services
import (
"wallet-api/pkg/models"
"github.com/go-pg/pg/v10"
)
type ApiService struct {
Db *pg.DB
}
func (as *ApiService) GetFirst() models.ApiModel {
apiModel := models.ApiModel{Api: "Works"}
as.Db.Model(&apiModel).First()
return apiModel
}

View File

@@ -4,6 +4,6 @@ import "log"
func CheckError(err error) {
if err != nil {
log.Fatalf("Unable to execute the query. %v", err)
log.Fatalf("Error occured. %v", err)
}
}

View File

@@ -1,5 +1 @@
package configs
const (
DbConfig = ""
)

17
pkg/utl/db/db.go Normal file
View File

@@ -0,0 +1,17 @@
package db
import (
"fmt"
"wallet-api/pkg/utl/common"
"github.com/go-pg/pg/v10"
)
func CreateConnection(dbUrl string) *pg.DB {
opt, err := pg.ParseURL(dbUrl)
common.CheckError(err)
conn := pg.Connect(opt)
fmt.Println("Successfully connected!")
return conn
}

View File

@@ -1,20 +0,0 @@
package pg
import (
"fmt"
"os"
"wallet-api/pkg/utl/common"
"github.com/go-pg/pg/v10"
)
func CreateConnection() func() *pg.DB {
opt, err := pg.ParseURL(os.Getenv("DATABASE_URL"))
common.CheckError(err)
db := pg.Connect(opt)
fmt.Println("Successfully connected!")
return func() *pg.DB {
return db
}
}

View File

@@ -6,8 +6,7 @@ import (
"github.com/gin-gonic/gin"
)
func Start() *gin.Engine {
r := gin.Default()
func Start(r *gin.Engine) *gin.Engine {
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",