|
func (api ProductsAPI) Create() http.HandlerFunc { |
|
return func(w http.ResponseWriter, r *http.Request) { |
|
var product models.Product |
|
|
|
if r.Method != http.MethodPost { |
|
code := http.StatusMethodNotAllowed |
|
httpError := common.NewError(common.HTTPFriendlyStatus(code), code) |
|
common.SendErrorResult(w, httpError) |
|
return |
|
} |
|
|
|
if err := json.NewDecoder(r.Body).Decode(&product); err != nil { |
|
code := http.StatusBadRequest |
|
httpError := common.NewError(common.HTTPFriendlyStatus(code), code) |
|
common.SendErrorResult(w, httpError) |
|
return |
|
} |
|
|
|
data := fmt.Sprintf("%s: Created!\n", api.Text) |
|
common.SendSuccessResult(w, data) |
|
} |
|
} |