first commit
This commit is contained in:
84
services/product/routes.go
Normal file
84
services/product/routes.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package product
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gorilla/mux"
|
||||
"sismedika.com/sas/westone/types"
|
||||
"sismedika.com/sas/westone/utils"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
store types.ProductStore
|
||||
userStore types.UserStore
|
||||
}
|
||||
|
||||
func NewHandler(store types.ProductStore, userStore types.UserStore) *Handler {
|
||||
return &Handler{store: store, userStore: userStore}
|
||||
}
|
||||
|
||||
func (h *Handler) RegisterRoutes(router *mux.Router) {
|
||||
router.HandleFunc("/products", h.handleGetProducts).Methods(http.MethodGet)
|
||||
router.HandleFunc("/products/{productID}", h.handleGetProduct).Methods(http.MethodGet)
|
||||
|
||||
// admin routes
|
||||
// router.HandleFunc("/products", auth.WithJWTAuth(h.handleCreateProduct)).Methods(http.MethodPost)
|
||||
}
|
||||
|
||||
func (h *Handler) handleGetProducts(w http.ResponseWriter, r *http.Request) {
|
||||
products, err := h.store.GetProducts()
|
||||
if err != nil {
|
||||
utils.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
utils.WriteJSON(w, http.StatusOK, products)
|
||||
}
|
||||
|
||||
func (h *Handler) handleGetProduct(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
str, ok := vars["productID"]
|
||||
if !ok {
|
||||
utils.WriteError(w, http.StatusBadRequest, fmt.Errorf("missing product ID"))
|
||||
return
|
||||
}
|
||||
|
||||
productID, err := strconv.Atoi(str)
|
||||
if err != nil {
|
||||
utils.WriteError(w, http.StatusBadRequest, fmt.Errorf("invalid product ID"))
|
||||
return
|
||||
}
|
||||
|
||||
product, err := h.store.GetProductByID(productID)
|
||||
if err != nil {
|
||||
utils.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
utils.WriteJSON(w, http.StatusOK, product)
|
||||
}
|
||||
|
||||
func (h *Handler) handleCreateProduct(w http.ResponseWriter, r *http.Request) {
|
||||
var product types.CreateProductPayload
|
||||
if err := utils.ParseJSON(r, &product); err != nil {
|
||||
utils.WriteError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := utils.Validate.Struct(product); err != nil {
|
||||
errors := err.(validator.ValidationErrors)
|
||||
utils.WriteError(w, http.StatusBadRequest, fmt.Errorf("invalid payload: %v", errors))
|
||||
return
|
||||
}
|
||||
|
||||
err := h.store.CreateProduct(product)
|
||||
if err != nil {
|
||||
utils.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
utils.WriteJSON(w, http.StatusCreated, product)
|
||||
}
|
||||
157
services/product/routes_test.go
Normal file
157
services/product/routes_test.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package product
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"sismedika.com/sas/westone/types"
|
||||
)
|
||||
|
||||
func TestProductServiceHandlers(t *testing.T) {
|
||||
productStore := &mockProductStore{}
|
||||
userStore := &mockUserStore{}
|
||||
handler := NewHandler(productStore, userStore)
|
||||
|
||||
t.Run("should handle get products", func(t *testing.T) {
|
||||
req, err := http.NewRequest(http.MethodGet, "/products", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := mux.NewRouter()
|
||||
|
||||
router.HandleFunc("/products", handler.handleGetProducts).Methods(http.MethodGet)
|
||||
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusOK, rr.Code)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should fail if the product ID is not a number", func(t *testing.T) {
|
||||
req, err := http.NewRequest(http.MethodGet, "/products/abc", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := mux.NewRouter()
|
||||
|
||||
router.HandleFunc("/products/{productID}", handler.handleGetProduct).Methods(http.MethodGet)
|
||||
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusBadRequest, rr.Code)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should handle get product by ID", func(t *testing.T) {
|
||||
req, err := http.NewRequest(http.MethodGet, "/products/42", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := mux.NewRouter()
|
||||
|
||||
router.HandleFunc("/products/{productID}", handler.handleGetProduct).Methods(http.MethodGet)
|
||||
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusOK, rr.Code)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should fail creating a product if the payload is missing", func(t *testing.T) {
|
||||
req, err := http.NewRequest(http.MethodPost, "/products", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := mux.NewRouter()
|
||||
|
||||
router.HandleFunc("/products", handler.handleCreateProduct).Methods(http.MethodPost)
|
||||
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusBadRequest, rr.Code)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should handle creating a product", func(t *testing.T) {
|
||||
payload := types.CreateProductPayload{
|
||||
Name: "test",
|
||||
Price: 100,
|
||||
Image: "test.jpg",
|
||||
Description: "test description",
|
||||
Quantity: 10,
|
||||
}
|
||||
|
||||
marshalled, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, "/products", bytes.NewBuffer(marshalled))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
router := mux.NewRouter()
|
||||
|
||||
router.HandleFunc("/products", handler.handleCreateProduct).Methods(http.MethodPost)
|
||||
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusCreated {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusCreated, rr.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type mockProductStore struct{}
|
||||
|
||||
func (m *mockProductStore) GetProductByID(productID int) (*types.Product, error) {
|
||||
return &types.Product{}, nil
|
||||
}
|
||||
|
||||
func (m *mockProductStore) GetProducts() ([]*types.Product, error) {
|
||||
return []*types.Product{}, nil
|
||||
}
|
||||
|
||||
func (m *mockProductStore) CreateProduct(product types.CreateProductPayload) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockProductStore) UpdateProduct(product types.Product) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockProductStore) GetProductsByID(ids []int) ([]types.Product, error) {
|
||||
return []types.Product{}, nil
|
||||
}
|
||||
|
||||
type mockUserStore struct{}
|
||||
|
||||
func (m *mockUserStore) GetUserByID(userID int) (*types.User, error) {
|
||||
return &types.User{}, nil
|
||||
}
|
||||
|
||||
func (m *mockUserStore) CreateUser(user types.User) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockUserStore) SignIn(email string, password string) (*types.User, error) {
|
||||
return &types.User{}, nil
|
||||
}
|
||||
101
services/product/store.go
Normal file
101
services/product/store.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package product
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"sismedika.com/sas/westone/types"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
func NewStore(db *sqlx.DB) *Store {
|
||||
return &Store{db: db}
|
||||
}
|
||||
|
||||
func (s *Store) GetProductByID(productID int) (*types.Product, error) {
|
||||
product := new(types.Product)
|
||||
query := "SELECT * FROM products WHERE id = ?"
|
||||
|
||||
// sqlx.Get is used for fetching a single record and scanning it into the struct
|
||||
if err := s.db.Get(product, query, productID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return product, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetProductsByID(productIDs []int) ([]types.Product, error) {
|
||||
query, args, err := sqlx.In("SELECT * FROM products WHERE id IN (?)", productIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Rebind the query for the specific database driver (e.g., mysql uses "?", postgres uses "$1", etc.)
|
||||
query = s.db.Rebind(query)
|
||||
|
||||
var products []types.Product
|
||||
err = s.db.Select(&products, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return products, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetProducts() ([]*types.Product, error) {
|
||||
var products []*types.Product
|
||||
query := "SELECT * FROM products"
|
||||
|
||||
// sqlx.Select is used to fetch multiple rows and map them into a slice of structs
|
||||
if err := s.db.Select(&products, query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return products, nil
|
||||
}
|
||||
|
||||
func (s *Store) CreateProduct(product types.CreateProductPayload) error {
|
||||
query := `INSERT INTO products (name, price, image, description, quantity)
|
||||
VALUES (:name, :price, :image, :description, :quantity)`
|
||||
|
||||
// Using sqlx.NamedExec to map struct fields to named parameters in the query
|
||||
_, err := s.db.NamedExec(query, product)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
func (s *Store) UpdateProduct(product types.Product) error {
|
||||
// Start a new transaction
|
||||
tx, err := s.db.BeginTxx(context.Background(), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Defer rollback to ensure transaction is rolled back in case of an error
|
||||
defer func() {
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
query := `UPDATE products SET name = :name, price = :price, image = :image,
|
||||
description = :description, quantity = :quantity WHERE id = :id`
|
||||
|
||||
// Using tx.NamedExec to execute the update within the transaction
|
||||
_, err = tx.NamedExec(query, product)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Commit the transaction if everything succeeded
|
||||
if err = tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user