naise_pos/api/server.go
2023-04-05 14:04:49 +07:00

67 lines
1.7 KiB
Go

package api
import (
"fmt"
"path/filepath"
db "git.nochill.in/nochill/naice_pos/db/sqlc"
"git.nochill.in/nochill/naice_pos/token"
"git.nochill.in/nochill/naice_pos/util"
"github.com/gin-gonic/gin"
)
type Server struct {
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
}
func (server *Server) getRoutes() {
router := gin.Default()
router.POST("/user/login", server.loginUser)
router.POST("/user/merchants", server.createUserMerchant)
router.POST("/user/renew_token", server.renewAccessToken)
router.Static("/public", filepath.Base("public"))
apiRoutes := router.Group("/api").Use(authMiddleware(server.tokenMaker))
apiRoutes.POST("/products", server.createProduct)
apiRoutes.PATCH("/products", server.updateProduct)
apiRoutes.GET("/products", server.listProducts)
apiRoutes.GET("/product/:id", server.getProduct)
apiRoutes.POST("/product/category", server.createProductCategory)
apiRoutes.GET("/product/categories", server.listProductCategories)
apiRoutes.PATCH("/product/category", server.updateProductCategory)
// apiRoutes.DELETE("/product/category/:id", server.)
apiRoutes.POST("/suppliers", server.createSupplier)
apiRoutes.POST("/purchase-products", server.createPurchase)
apiRoutes.POST("/sale-order", server.createSaleOrder)
server.router = router
}
func (server *Server) Start(address string) error {
return server.router.Run(address)
}