naise_pos/api/server.go

57 lines
1.3 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-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)
apiRoutes.GET("/product/:id", server.getProduct)
2023-03-14 17:39:40 +07:00
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
server.router = router
}
func (server *Server) Start(address string) error {
return server.router.Run(address)
}