mirror of
https://github.com/rjNemo/go-microservices-tuto
synced 2026-06-12 13:26:45 +00:00
⌫ enable delete product
This commit is contained in:
parent
b6b2e94ace
commit
e9f20850ef
7 changed files with 146 additions and 36 deletions
|
|
@ -39,6 +39,16 @@ func UpdateProduct(id int, p *models.Product) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteProduct removes a Product idntified by its id
|
||||||
|
func DeleteProduct(id int) error {
|
||||||
|
idx, _, err := findProduct(id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
productList = append(productList[0:idx], productList[idx+1:]...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// ErrorProductNotFound its thrown when the product is not found
|
// ErrorProductNotFound its thrown when the product is not found
|
||||||
var ErrorProductNotFound = fmt.Errorf("Product not found")
|
var ErrorProductNotFound = fmt.Errorf("Product not found")
|
||||||
|
|
||||||
|
|
|
||||||
34
products/handlers/delete.go
Normal file
34
products/handlers/delete.go
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/rjNemo/go-micro/products/data"
|
||||||
|
)
|
||||||
|
|
||||||
|
// swagger:route DELETE /products/{id} products product
|
||||||
|
// Deletes a product
|
||||||
|
// responses:
|
||||||
|
// 200: productResponse
|
||||||
|
|
||||||
|
// DeleteProduct edit product identified by id
|
||||||
|
func (p *Products) DeleteProduct(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
id, _ := strconv.Atoi(vars["id"])
|
||||||
|
|
||||||
|
p.logger.Println("Handle 'DELETE' request", id)
|
||||||
|
|
||||||
|
err := data.DeleteProduct(id)
|
||||||
|
if err == data.ErrorProductNotFound {
|
||||||
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
errMsg := fmt.Sprintf("something went wrong: %s", err.Error())
|
||||||
|
http.Error(w, errMsg, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
23
products/handlers/post.go
Normal file
23
products/handlers/post.go
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/rjNemo/go-micro/products/data"
|
||||||
|
"github.com/rjNemo/go-micro/products/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
// swagger:route POST /products products product
|
||||||
|
// Creates a product
|
||||||
|
// responses:
|
||||||
|
// 201: productResponse
|
||||||
|
|
||||||
|
// AddProduct reads request body and creates new product
|
||||||
|
func (p *Products) AddProduct(w http.ResponseWriter, r *http.Request) {
|
||||||
|
p.logger.Println("Handle 'POST' request")
|
||||||
|
// get product from the request
|
||||||
|
newProd := r.Context().Value(KeyProduct{}).(*models.Product) // cast into a Product
|
||||||
|
|
||||||
|
p.logger.Printf("product: %#v", newProd)
|
||||||
|
data.AddProduct(newProd)
|
||||||
|
}
|
||||||
|
|
@ -19,10 +19,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"github.com/rjNemo/go-micro/products/data"
|
|
||||||
"github.com/rjNemo/go-micro/products/models"
|
"github.com/rjNemo/go-micro/products/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -39,43 +36,19 @@ type productsResponse struct {
|
||||||
Body []models.Product
|
Body []models.Product
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// product in the response. For go-swagger
|
||||||
|
// swagger:response productResponse
|
||||||
|
type productResponse struct {
|
||||||
|
// One product in the datastore
|
||||||
|
// in: body
|
||||||
|
Body models.Product
|
||||||
|
}
|
||||||
|
|
||||||
// New creates a Products handler
|
// New creates a Products handler
|
||||||
func New(logger *log.Logger) *Products {
|
func New(logger *log.Logger) *Products {
|
||||||
return &Products{logger: logger}
|
return &Products{logger: logger}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddProduct reads request body and creates new product
|
|
||||||
func (p *Products) AddProduct(w http.ResponseWriter, r *http.Request) {
|
|
||||||
p.logger.Println("Handle 'POST' request")
|
|
||||||
// get product from the request
|
|
||||||
newProd := r.Context().Value(KeyProduct{}).(*models.Product) // cast into a Product
|
|
||||||
|
|
||||||
p.logger.Printf("product: %#v", newProd)
|
|
||||||
data.AddProduct(newProd)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateProduct edit product identified by id
|
|
||||||
func (p *Products) UpdateProduct(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
id, _ := strconv.Atoi(vars["id"])
|
|
||||||
|
|
||||||
p.logger.Println("Handle 'PUT' request", id)
|
|
||||||
// get product from the request
|
|
||||||
newProd := r.Context().Value(KeyProduct{}).(*models.Product) // cast into a Product
|
|
||||||
|
|
||||||
p.logger.Printf("product: %#v", newProd)
|
|
||||||
err := data.UpdateProduct(id, newProd)
|
|
||||||
if err == data.ErrorProductNotFound {
|
|
||||||
http.Error(w, err.Error(), http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
errMsg := fmt.Sprintf("something went wrong: %s", err.Error())
|
|
||||||
http.Error(w, errMsg, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// KeyProduct is a key used to pass validated product to handler
|
// KeyProduct is a key used to pass validated product to handler
|
||||||
type KeyProduct struct{}
|
type KeyProduct struct{}
|
||||||
|
|
||||||
|
|
|
||||||
38
products/handlers/put.go
Normal file
38
products/handlers/put.go
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/rjNemo/go-micro/products/data"
|
||||||
|
"github.com/rjNemo/go-micro/products/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
// swagger:route PUT /products/{id} products product
|
||||||
|
// Updates a product
|
||||||
|
// responses:
|
||||||
|
// 204: productResponse
|
||||||
|
|
||||||
|
// UpdateProduct edit product identified by id
|
||||||
|
func (p *Products) UpdateProduct(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
id, _ := strconv.Atoi(vars["id"])
|
||||||
|
|
||||||
|
p.logger.Println("Handle 'PUT' request", id)
|
||||||
|
// get product from the request
|
||||||
|
newProd := r.Context().Value(KeyProduct{}).(*models.Product) // cast into a Product
|
||||||
|
|
||||||
|
p.logger.Printf("product: %#v", newProd)
|
||||||
|
err := data.UpdateProduct(id, newProd)
|
||||||
|
if err == data.ErrorProductNotFound {
|
||||||
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
errMsg := fmt.Sprintf("something went wrong: %s", err.Error())
|
||||||
|
http.Error(w, errMsg, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -20,7 +20,10 @@ func (p *Products) RegisterRoutes(r *mux.Router) {
|
||||||
putRouter := r.Methods(http.MethodPut).Subrouter()
|
putRouter := r.Methods(http.MethodPut).Subrouter()
|
||||||
putRouter.HandleFunc("/{id:[0-9]+}", p.UpdateProduct)
|
putRouter.HandleFunc("/{id:[0-9]+}", p.UpdateProduct)
|
||||||
putRouter.Use(p.ProductValidationMiddleware)
|
putRouter.Use(p.ProductValidationMiddleware)
|
||||||
|
// DELETE
|
||||||
|
deleteRouter := r.Methods(http.MethodDelete).Subrouter()
|
||||||
|
deleteRouter.HandleFunc("/{id:[0-9]+}", p.DeleteProduct)
|
||||||
|
// swagger docs
|
||||||
opts := middleware.RedocOpts{SpecURL: "/swagger.yaml"}
|
opts := middleware.RedocOpts{SpecURL: "/swagger.yaml"}
|
||||||
swaggerHandler := middleware.Redoc(opts, nil)
|
swaggerHandler := middleware.Redoc(opts, nil)
|
||||||
getRouter.Handle("/docs", swaggerHandler)
|
getRouter.Handle("/docs", swaggerHandler)
|
||||||
|
|
|
||||||
29
swagger.yaml
29
swagger.yaml
|
|
@ -38,9 +38,38 @@ paths:
|
||||||
$ref: '#/responses/productsResponse'
|
$ref: '#/responses/productsResponse'
|
||||||
tags:
|
tags:
|
||||||
- products
|
- products
|
||||||
|
post:
|
||||||
|
description: Creates a product
|
||||||
|
operationId: product
|
||||||
|
responses:
|
||||||
|
"201":
|
||||||
|
$ref: '#/responses/productResponse'
|
||||||
|
tags:
|
||||||
|
- products
|
||||||
|
/products/{id}:
|
||||||
|
delete:
|
||||||
|
description: Deletes a product
|
||||||
|
operationId: product
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
$ref: '#/responses/productResponse'
|
||||||
|
tags:
|
||||||
|
- products
|
||||||
|
put:
|
||||||
|
description: Updates a product
|
||||||
|
operationId: product
|
||||||
|
responses:
|
||||||
|
"204":
|
||||||
|
$ref: '#/responses/productResponse'
|
||||||
|
tags:
|
||||||
|
- products
|
||||||
produces:
|
produces:
|
||||||
- application/json
|
- application/json
|
||||||
responses:
|
responses:
|
||||||
|
productResponse:
|
||||||
|
description: product in the response. For go-swagger
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/Product'
|
||||||
productsResponse:
|
productsResponse:
|
||||||
description: list of products in the response. For go-swagger
|
description: list of products in the response. For go-swagger
|
||||||
schema:
|
schema:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue