naise_pos/api/server.go

55 lines
1.2 KiB
Go
Raw Normal View History

2023-03-07 18:52:37 +07:00
package api
import (
2023-03-14 17:39:40 +07:00
"fmt"
2023-03-07 18:52:37 +07:00
db "git.nochill.in/nochill/naice_pos/db/sqlc"
2023-03-14 17:39:40 +07:00
"git.nochill.in/nochill/naice_pos/token"
"git.nochill.in/nochill/naice_pos/util"
2023-03-07 18:52:37 +07:00
"github.com/gin-gonic/gin"
)
type Server struct {
2023-03-14 17:39:40 +07:00
config util.Config
store db.Store
tokenMaker token.Maker
router *gin.Engine
}
func NewServer(config util.Config, store db.Store) (*Server, error) {
tokenMaker, err := token.NewPasetoMaker(config.TokenSymmetricKey)
if err != nil {
return nil, fmt.Errorf("cannot create token maker: %w", err)
}
server := &Server{
config: config,
store: store,
tokenMaker: tokenMaker,
}
server.getRoutes()
return server, nil
2023-03-07 18:52:37 +07:00
}
2023-03-14 17:39:40 +07:00
func (server *Server) getRoutes() {
2023-03-07 18:52:37 +07:00
router := gin.Default()
2023-03-14 17:39:40 +07:00
router.POST("/user/login", server.loginUser)
2023-03-14 12:04:03 +07:00
router.POST("/user/merchants", server.createUserMerchant)
2023-03-14 17:39:40 +07:00
2023-03-07 18:52:37 +07:00
router.POST("/products", server.createProduct)
2023-03-12 11:01:43 +07:00
router.PATCH("/products", server.updateProduct)
router.GET("/product/:id", server.getProduct)
2023-03-14 17:39:40 +07:00
2023-03-07 18:52:37 +07:00
router.POST("/suppliers", server.createSupplier)
2023-03-14 17:39:40 +07:00
2023-03-08 15:22:57 +07:00
router.POST("/purchase-products", server.createPurchase)
2023-03-07 18:52:37 +07:00
server.router = router
}
func (server *Server) Start(address string) error {
return server.router.Run(address)
}