naise_pos/api/server.go

67 lines
1.7 KiB
Go
Raw Permalink Normal View History

2023-03-07 18:52:37 +07:00
package api
import (
2023-03-14 17:39:40 +07:00
"fmt"
2023-04-05 14:04:49 +07:00
"path/filepath"
2023-03-14 17:39:40 +07:00
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-16 12:21:41 +07:00
router.POST("/user/renew_token", server.renewAccessToken)
2023-04-05 14:04:49 +07:00
router.Static("/public", filepath.Base("public"))
2023-03-14 17:39:40 +07:00
2023-03-15 15:00:36 +07:00
apiRoutes := router.Group("/api").Use(authMiddleware(server.tokenMaker))
2023-03-14 17:39:40 +07:00
2023-03-15 15:00:36 +07:00
apiRoutes.POST("/products", server.createProduct)
apiRoutes.PATCH("/products", server.updateProduct)
2023-03-22 12:06:09 +07:00
apiRoutes.GET("/products", server.listProducts)
2023-03-15 15:00:36 +07:00
apiRoutes.GET("/product/:id", server.getProduct)
2023-03-14 17:39:40 +07:00
2023-03-16 16:55:45 +07:00
apiRoutes.POST("/product/category", server.createProductCategory)
apiRoutes.GET("/product/categories", server.listProductCategories)
apiRoutes.PATCH("/product/category", server.updateProductCategory)
// apiRoutes.DELETE("/product/category/:id", server.)
2023-03-15 15:00:36 +07:00
apiRoutes.POST("/suppliers", server.createSupplier)
apiRoutes.POST("/purchase-products", server.createPurchase)
2023-03-07 18:52:37 +07:00
2023-03-22 12:06:09 +07:00
apiRoutes.POST("/sale-order", server.createSaleOrder)
2023-03-07 18:52:37 +07:00
server.router = router
}
func (server *Server) Start(address string) error {
return server.router.Run(address)
}