From f5c92ee0e93d41a1944f5e5396838525a6331a05 Mon Sep 17 00:00:00 2001 From: Hirbod Behnam Date: Thu, 28 Dec 2023 09:41:42 +0330 Subject: [PATCH] Working on some endpoints --- payment/api/api.go | 4 +-- payment/api/goods.go | 1 + payment/api/idpay.go | 35 +++++++++++++++++++++++++++ payment/cmd/payment/main.go | 16 ++++-------- payment/internal/database/database.go | 14 +++++++++++ payment/internal/database/payment.go | 12 +++++++++ payment/internal/database/pg.go | 8 +++--- payment/internal/database/types.go | 8 +++--- 8 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 payment/api/goods.go create mode 100644 payment/internal/database/database.go diff --git a/payment/api/api.go b/payment/api/api.go index 6b966238..4679e963 100644 --- a/payment/api/api.go +++ b/payment/api/api.go @@ -1,8 +1,8 @@ package api -import "gorm.io/gorm" +import db "wss-payment/internal/database" // API contains the data needed to operate the endpoints type API struct { - Database *gorm.DB + Database db.PaymentDatabase } diff --git a/payment/api/goods.go b/payment/api/goods.go new file mode 100644 index 00000000..778f64ec --- /dev/null +++ b/payment/api/goods.go @@ -0,0 +1 @@ +package api diff --git a/payment/api/idpay.go b/payment/api/idpay.go index 778f64ec..64b98c4a 100644 --- a/payment/api/idpay.go +++ b/payment/api/idpay.go @@ -1 +1,36 @@ package api + +import ( + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" + "net/http" + "wss-payment/internal/database" +) + +// GetGoods gets all goods registered in database +func (api *API) GetGoods(c *gin.Context) { + goods, err := api.Database.GetGoods() + if err != nil { + log.WithError(err).Error("cannot get goods") + c.JSON(http.StatusInternalServerError, err.Error()) + return + } + c.JSON(http.StatusOK, goods) +} + +// AddGood adds a good to server +func (api *API) AddGood(c *gin.Context) { + var body database.Good + err := c.BindJSON(&body) + if err != nil { + c.JSON(http.StatusBadRequest, err.Error()) + return + } + err = api.Database.AddGood(&body) + if err != nil { + log.WithError(err).WithField("body", body).Error("cannot insert body in database") + c.JSON(http.StatusInternalServerError, err.Error()) + return + } + c.Status(http.StatusCreated) +} diff --git a/payment/cmd/payment/main.go b/payment/cmd/payment/main.go index a2668fc6..7ced4ee6 100644 --- a/payment/cmd/payment/main.go +++ b/payment/cmd/payment/main.go @@ -5,7 +5,6 @@ import ( "github.com/gin-gonic/gin" "github.com/go-faster/errors" log "github.com/sirupsen/logrus" - "gorm.io/gorm" "net" "net/http" "os" @@ -19,13 +18,15 @@ func main() { // Create the data needed endpointApi := new(api.API) endpointApi.Database = setupDatabase() - defer closeDatabase(endpointApi.Database) + defer endpointApi.Database.Close() // Setup endpoints r := gin.New() r.Use(gin.Recovery()) + r.GET("/health", api.HealthCheck) r.POST("/create") r.GET("/status") - r.GET("/health", api.HealthCheck) + r.GET("/goods", endpointApi.GetGoods) + r.POST("/goods") // Listen srv := &http.Server{ Handler: r, @@ -43,7 +44,7 @@ func main() { } // Setup the database according to the DATABASE_URL environment variable -func setupDatabase() *gorm.DB { +func setupDatabase() db.PaymentDatabase { // Check DB url dbURL := os.Getenv("DATABASE_URL") if dbURL == "" { @@ -57,13 +58,6 @@ func setupDatabase() *gorm.DB { return database } -// Closes a gorm database -func closeDatabase(database *gorm.DB) { - if database, err := database.DB(); err == nil { - _ = database.Close() - } -} - // getListener will start a listener based on environment variables func getListener() net.Listener { // Get protocol diff --git a/payment/internal/database/database.go b/payment/internal/database/database.go new file mode 100644 index 00000000..b8e0a960 --- /dev/null +++ b/payment/internal/database/database.go @@ -0,0 +1,14 @@ +package database + +import "gorm.io/gorm" + +type PaymentDatabase struct { + db *gorm.DB +} + +// Close a gorm database +func (db PaymentDatabase) Close() { + if database, err := db.db.DB(); err == nil { + _ = database.Close() + } +} diff --git a/payment/internal/database/payment.go b/payment/internal/database/payment.go index 636bab89..6e780737 100644 --- a/payment/internal/database/payment.go +++ b/payment/internal/database/payment.go @@ -1 +1,13 @@ package database + +// GetGoods gets the list of all goods in database +func (db PaymentDatabase) GetGoods() ([]Good, error) { + var payments []Good + result := db.db.Find(&payments) + return payments, result.Error +} + +// AddGood adds a good to the table +func (db PaymentDatabase) AddGood(good *Good) error { + return db.db.Create(good).Error +} diff --git a/payment/internal/database/pg.go b/payment/internal/database/pg.go index cc6a8324..71922bdf 100644 --- a/payment/internal/database/pg.go +++ b/payment/internal/database/pg.go @@ -7,17 +7,17 @@ import ( ) // NewPostgres opens a postgres database and returns the connection -func NewPostgres(dsn string) (*gorm.DB, error) { +func NewPostgres(dsn string) (PaymentDatabase, error) { // Create database db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { - return nil, errors.Wrap(err, "cannot open database") + return PaymentDatabase{}, errors.Wrap(err, "cannot open database") } // Gorm automatically pings the database thus we can just migrate tables err = db.AutoMigrate(Good{}, Payment{}) if err != nil { - return nil, errors.Wrap(err, "cannot migrate database") + return PaymentDatabase{}, errors.Wrap(err, "cannot migrate database") } // GTG - return db, nil + return PaymentDatabase{db}, nil } diff --git a/payment/internal/database/types.go b/payment/internal/database/types.go index 66b96c39..591a7d8b 100644 --- a/payment/internal/database/types.go +++ b/payment/internal/database/types.go @@ -42,11 +42,11 @@ type Payment struct { type Good struct { // ID of this good - ID uint32 `gorm:"primarykey"` + ID uint32 `gorm:"primarykey" json:"id"` // Name of it - Name string `gorm:"unique;not null"` + Name string `gorm:"unique;not null" json:"name"` // The price of this item - Price uint64 `gorm:"not null"` + Price uint64 `gorm:"not null" json:"price"` // An optional description about this payment - Description string + Description string `json:"description"` }