Products app
This commit is contained in:
5
products/app/api/dto.go
Normal file
5
products/app/api/dto.go
Normal file
@ -0,0 +1,5 @@
|
||||
package api
|
||||
|
||||
type PingDto struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
37
products/app/api/helpers.go
Normal file
37
products/app/api/helpers.go
Normal file
@ -0,0 +1,37 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func isError(c *gin.Context, err error) bool {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"details": err.Error(),
|
||||
})
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func raiseError(c *gin.Context, errCode int, message string) {
|
||||
c.AbortWithStatusJSON(errCode, gin.H{
|
||||
"details": message,
|
||||
})
|
||||
}
|
||||
|
||||
func raiseBadRequestError(c *gin.Context, message string) {
|
||||
raiseError(c, http.StatusBadRequest, message)
|
||||
}
|
||||
|
||||
func raiseNotFoundError(c *gin.Context, message string) {
|
||||
raiseError(c, http.StatusNotFound, message)
|
||||
}
|
||||
|
||||
func raiseInternalError(c *gin.Context, message string) {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
|
||||
"details": "Internal server error. We will we will fix it!",
|
||||
})
|
||||
}
|
||||
7
products/app/api/response.go
Normal file
7
products/app/api/response.go
Normal file
@ -0,0 +1,7 @@
|
||||
package api
|
||||
|
||||
import "products/app/db"
|
||||
|
||||
type GetProductsResponse struct {
|
||||
Products []db.Product `json:"products"`
|
||||
}
|
||||
75
products/app/api/router.go
Normal file
75
products/app/api/router.go
Normal file
@ -0,0 +1,75 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"products/app/db"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const HOST = "0.0.0.0"
|
||||
const PORT = 3000
|
||||
|
||||
func handlePing(c *gin.Context) {
|
||||
c.JSON(
|
||||
http.StatusOK,
|
||||
PingDto{
|
||||
Message: "Pong!",
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func handleGetProducts(dbConn *gorm.DB) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
products := db.GetProducts(dbConn)
|
||||
|
||||
c.JSON(
|
||||
http.StatusOK,
|
||||
GetProductsResponse{
|
||||
Products: *products,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetProduct(dbConn *gorm.DB) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
productId, err := strconv.Atoi(c.Param("productId"))
|
||||
if err != nil {
|
||||
raiseBadRequestError(c, "Invalid productId parameter")
|
||||
return
|
||||
}
|
||||
|
||||
product, err := db.GetProduct(dbConn, productId)
|
||||
if err != nil {
|
||||
raiseNotFoundError(c, "Product not found")
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(
|
||||
http.StatusOK,
|
||||
product,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func initRouter(dbConn *gorm.DB) *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
router := gin.Default()
|
||||
|
||||
router.GET("/ping", handlePing)
|
||||
router.GET("/", handleGetProducts(dbConn))
|
||||
router.GET("/:productId", handleGetProduct(dbConn))
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func Serve(dbConn *gorm.DB) {
|
||||
serverAddr := fmt.Sprintf("%s:%d", HOST, PORT)
|
||||
|
||||
router := initRouter(dbConn)
|
||||
router.Run(serverAddr)
|
||||
}
|
||||
24
products/app/cfg/config.go
Normal file
24
products/app/cfg/config.go
Normal file
@ -0,0 +1,24 @@
|
||||
package cfg
|
||||
|
||||
import (
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
)
|
||||
|
||||
type configStruct struct {
|
||||
DbHost string `default:"localhost"`
|
||||
DbPort int `default:"55432"`
|
||||
DbName string `default:"komponiranje"`
|
||||
DbUser string `default:"pero"`
|
||||
DbPassword string `default:"pero.000"`
|
||||
}
|
||||
|
||||
const ENV_PREFIX = ""
|
||||
|
||||
var Config configStruct
|
||||
|
||||
func init() {
|
||||
err := envconfig.Process(ENV_PREFIX, &Config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
30
products/app/db/db.go
Normal file
30
products/app/db/db.go
Normal file
@ -0,0 +1,30 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"products/app/cfg"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
gormLogger "gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func ConnectDb() *gorm.DB {
|
||||
var connectionString = fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=disable",
|
||||
cfg.Config.DbUser,
|
||||
cfg.Config.DbPassword,
|
||||
cfg.Config.DbHost,
|
||||
cfg.Config.DbPort,
|
||||
cfg.Config.DbName,
|
||||
)
|
||||
|
||||
var err error
|
||||
dbConn, err := gorm.Open(postgres.Open(connectionString), &gorm.Config{
|
||||
Logger: gormLogger.Default.LogMode(gormLogger.Info),
|
||||
})
|
||||
if err != nil {
|
||||
panic("Error connecting to database: " + err.Error())
|
||||
}
|
||||
|
||||
return dbConn
|
||||
}
|
||||
12
products/app/db/models.go
Normal file
12
products/app/db/models.go
Normal file
@ -0,0 +1,12 @@
|
||||
package db
|
||||
|
||||
type Product struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Image string `json:"image"`
|
||||
}
|
||||
|
||||
func (m *Product) TableName() string {
|
||||
return "products"
|
||||
}
|
||||
22
products/app/db/repository.go
Normal file
22
products/app/db/repository.go
Normal file
@ -0,0 +1,22 @@
|
||||
package db
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
func GetProducts(dbConn *gorm.DB) *[]Product {
|
||||
var products []Product
|
||||
|
||||
dbConn.Order("name").Find(&products)
|
||||
|
||||
return &products
|
||||
}
|
||||
|
||||
func GetProduct(dbConn *gorm.DB, id int) (*Product, error) {
|
||||
var product Product
|
||||
|
||||
result := dbConn.Order("name").Where("id = ?", id).First(&product)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
return &product, nil
|
||||
}
|
||||
11
products/app/main.go
Normal file
11
products/app/main.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"products/app/api"
|
||||
"products/app/db"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dbConn := db.ConnectDb()
|
||||
api.Serve(dbConn)
|
||||
}
|
||||
Reference in New Issue
Block a user