-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30445c4
commit f5c92ee
Showing
8 changed files
with
77 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters