Skip to content

Commit

Permalink
Working on some endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
HirbodBehnam committed Dec 28, 2023
1 parent 30445c4 commit f5c92ee
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 21 deletions.
4 changes: 2 additions & 2 deletions payment/api/api.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions payment/api/goods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package api
35 changes: 35 additions & 0 deletions payment/api/idpay.go
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 5 additions & 11 deletions payment/cmd/payment/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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 == "" {
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions payment/internal/database/database.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
12 changes: 12 additions & 0 deletions payment/internal/database/payment.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 4 additions & 4 deletions payment/internal/database/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions payment/internal/database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit f5c92ee

Please sign in to comment.