mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-18 18:47:07 +00:00
Merge remote-tracking branch 'origin/master' into 5.2.x
This commit is contained in:
@@ -24,7 +24,7 @@ type PetApiController struct {
|
||||
|
||||
// NewPetApiController creates a default api controller
|
||||
func NewPetApiController(s PetApiServicer) Router {
|
||||
return &PetApiController{ service: s }
|
||||
return &PetApiController{service: s}
|
||||
}
|
||||
|
||||
// Routes returns all of the api route for the PetApiController
|
||||
@@ -82,167 +82,165 @@ func (c *PetApiController) Routes() Routes {
|
||||
}
|
||||
|
||||
// AddPet - Add a new pet to the store
|
||||
func (c *PetApiController) AddPet(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *PetApiController) AddPet(w http.ResponseWriter, r *http.Request) {
|
||||
pet := &Pet{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&pet); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.service.AddPet(r.Context(), *pet)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// DeletePet - Deletes a pet
|
||||
func (c *PetApiController) DeletePet(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *PetApiController) DeletePet(w http.ResponseWriter, r *http.Request) {
|
||||
params := mux.Vars(r)
|
||||
petId, err := parseInt64Parameter(params["petId"])
|
||||
petId, err := parseInt64Parameter(params["petId"], true)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
apiKey := r.Header.Get("api_key")
|
||||
result, err := c.service.DeletePet(r.Context(), petId, apiKey)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// FindPetsByStatus - Finds Pets by status
|
||||
func (c *PetApiController) FindPetsByStatus(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *PetApiController) FindPetsByStatus(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
status := strings.Split(query.Get("status"), ",")
|
||||
result, err := c.service.FindPetsByStatus(r.Context(), status)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// FindPetsByTags - Finds Pets by tags
|
||||
func (c *PetApiController) FindPetsByTags(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *PetApiController) FindPetsByTags(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
tags := strings.Split(query.Get("tags"), ",")
|
||||
result, err := c.service.FindPetsByTags(r.Context(), tags)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetPetById - Find pet by ID
|
||||
func (c *PetApiController) GetPetById(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *PetApiController) GetPetById(w http.ResponseWriter, r *http.Request) {
|
||||
params := mux.Vars(r)
|
||||
petId, err := parseInt64Parameter(params["petId"])
|
||||
petId, err := parseInt64Parameter(params["petId"], true)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.service.GetPetById(r.Context(), petId)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UpdatePet - Update an existing pet
|
||||
func (c *PetApiController) UpdatePet(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *PetApiController) UpdatePet(w http.ResponseWriter, r *http.Request) {
|
||||
pet := &Pet{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&pet); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.service.UpdatePet(r.Context(), *pet)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UpdatePetWithForm - Updates a pet in the store with form data
|
||||
func (c *PetApiController) UpdatePetWithForm(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
func (c *PetApiController) UpdatePetWithForm(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
params := mux.Vars(r)
|
||||
petId, err := parseInt64Parameter(params["petId"])
|
||||
petId, err := parseInt64Parameter(params["petId"], true)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
name := r.FormValue("name")
|
||||
status := r.FormValue("status")
|
||||
|
||||
name := r.FormValue("name")
|
||||
status := r.FormValue("status")
|
||||
result, err := c.service.UpdatePetWithForm(r.Context(), petId, name, status)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UploadFile - uploads an image
|
||||
func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseMultipartForm(32 << 20); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
params := mux.Vars(r)
|
||||
petId, err := parseInt64Parameter(params["petId"])
|
||||
petId, err := parseInt64Parameter(params["petId"], true)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
additionalMetadata := r.FormValue("additionalMetadata")
|
||||
|
||||
additionalMetadata := r.FormValue("additionalMetadata")
|
||||
|
||||
file, err := ReadFormFileToTempFile(r, "file")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.service.UploadFile(r.Context(), petId, additionalMetadata, file)
|
||||
//If an error occured, encode the error with the status code
|
||||
result, err := c.service.UploadFile(r.Context(), petId, additionalMetadata, file)
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ type StoreApiController struct {
|
||||
|
||||
// NewStoreApiController creates a default api controller
|
||||
func NewStoreApiController(s StoreApiServicer) Router {
|
||||
return &StoreApiController{ service: s }
|
||||
return &StoreApiController{service: s}
|
||||
}
|
||||
|
||||
// Routes returns all of the api route for the StoreApiController
|
||||
@@ -58,67 +58,68 @@ func (c *StoreApiController) Routes() Routes {
|
||||
}
|
||||
|
||||
// DeleteOrder - Delete purchase order by ID
|
||||
func (c *StoreApiController) DeleteOrder(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *StoreApiController) DeleteOrder(w http.ResponseWriter, r *http.Request) {
|
||||
params := mux.Vars(r)
|
||||
orderId := params["orderId"]
|
||||
|
||||
result, err := c.service.DeleteOrder(r.Context(), orderId)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetInventory - Returns pet inventories by status
|
||||
func (c *StoreApiController) GetInventory(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *StoreApiController) GetInventory(w http.ResponseWriter, r *http.Request) {
|
||||
result, err := c.service.GetInventory(r.Context())
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetOrderById - Find purchase order by ID
|
||||
func (c *StoreApiController) GetOrderById(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *StoreApiController) GetOrderById(w http.ResponseWriter, r *http.Request) {
|
||||
params := mux.Vars(r)
|
||||
orderId, err := parseInt64Parameter(params["orderId"])
|
||||
orderId, err := parseInt64Parameter(params["orderId"], true)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.service.GetOrderById(r.Context(), orderId)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// PlaceOrder - Place an order for a pet
|
||||
func (c *StoreApiController) PlaceOrder(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *StoreApiController) PlaceOrder(w http.ResponseWriter, r *http.Request) {
|
||||
order := &Order{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&order); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.service.PlaceOrder(r.Context(), *order)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ type UserApiController struct {
|
||||
|
||||
// NewUserApiController creates a default api controller
|
||||
func NewUserApiController(s UserApiServicer) Router {
|
||||
return &UserApiController{ service: s }
|
||||
return &UserApiController{service: s}
|
||||
}
|
||||
|
||||
// Routes returns all of the api route for the UserApiController
|
||||
@@ -82,138 +82,137 @@ func (c *UserApiController) Routes() Routes {
|
||||
}
|
||||
|
||||
// CreateUser - Create user
|
||||
func (c *UserApiController) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *UserApiController) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
user := &User{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.service.CreateUser(r.Context(), *user)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// CreateUsersWithArrayInput - Creates list of users with given input array
|
||||
func (c *UserApiController) CreateUsersWithArrayInput(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *UserApiController) CreateUsersWithArrayInput(w http.ResponseWriter, r *http.Request) {
|
||||
user := &[]User{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.service.CreateUsersWithArrayInput(r.Context(), *user)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// CreateUsersWithListInput - Creates list of users with given input array
|
||||
func (c *UserApiController) CreateUsersWithListInput(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *UserApiController) CreateUsersWithListInput(w http.ResponseWriter, r *http.Request) {
|
||||
user := &[]User{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.service.CreateUsersWithListInput(r.Context(), *user)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// DeleteUser - Delete user
|
||||
func (c *UserApiController) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *UserApiController) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
params := mux.Vars(r)
|
||||
username := params["username"]
|
||||
|
||||
result, err := c.service.DeleteUser(r.Context(), username)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetUserByName - Get user by user name
|
||||
func (c *UserApiController) GetUserByName(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *UserApiController) GetUserByName(w http.ResponseWriter, r *http.Request) {
|
||||
params := mux.Vars(r)
|
||||
username := params["username"]
|
||||
|
||||
result, err := c.service.GetUserByName(r.Context(), username)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// LoginUser - Logs user into the system
|
||||
func (c *UserApiController) LoginUser(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *UserApiController) LoginUser(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
username := query.Get("username")
|
||||
password := query.Get("password")
|
||||
result, err := c.service.LoginUser(r.Context(), username, password)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// LogoutUser - Logs out current logged in user session
|
||||
func (c *UserApiController) LogoutUser(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *UserApiController) LogoutUser(w http.ResponseWriter, r *http.Request) {
|
||||
result, err := c.service.LogoutUser(r.Context())
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UpdateUser - Updated user
|
||||
func (c *UserApiController) UpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *UserApiController) UpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
params := mux.Vars(r)
|
||||
username := params["username"]
|
||||
|
||||
user := &User{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.service.UpdateUser(r.Context(), username, *user)
|
||||
//If an error occured, encode the error with the status code
|
||||
// If an error occurred, encode the error with the status code
|
||||
if err != nil {
|
||||
EncodeJSONResponse(err.Error(), &result.Code, w)
|
||||
EncodeJSONResponse(err.Error(), &result.Code, result.Headers, w)
|
||||
return
|
||||
}
|
||||
//If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, w)
|
||||
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,18 @@ package petstoreserver
|
||||
|
||||
//Response return a ImplResponse struct filled
|
||||
func Response(code int, body interface{}) ImplResponse {
|
||||
return ImplResponse{Code: code, Body: body}
|
||||
return ImplResponse {
|
||||
Code: code,
|
||||
Headers: nil,
|
||||
Body: body,
|
||||
}
|
||||
}
|
||||
|
||||
//ResponseWithHeaders return a ImplResponse struct filled, including headers
|
||||
func ResponseWithHeaders(code int, headers map[string][]string, body interface{}) ImplResponse {
|
||||
return ImplResponse {
|
||||
Code: code,
|
||||
Headers: headers,
|
||||
Body: body,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,5 +12,6 @@ package petstoreserver
|
||||
//Implementation response defines an error code with the associated body
|
||||
type ImplResponse struct {
|
||||
Code int
|
||||
Headers map[string][]string
|
||||
Body interface{}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,19 +11,21 @@ package petstoreserver
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/gorilla/mux"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"github.com/gorilla/mux"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// A Route defines the parameters for an api endpoint
|
||||
type Route struct {
|
||||
Name string
|
||||
Method string
|
||||
Pattern string
|
||||
Name string
|
||||
Method string
|
||||
Pattern string
|
||||
HandlerFunc http.HandlerFunc
|
||||
}
|
||||
|
||||
@@ -35,6 +37,8 @@ type Router interface {
|
||||
Routes() Routes
|
||||
}
|
||||
|
||||
const errMsgRequiredMissing = "required parameter is missing"
|
||||
|
||||
// NewRouter creates a new router for any number of api routers
|
||||
func NewRouter(routers ...Router) *mux.Router {
|
||||
router := mux.NewRouter().StrictSlash(true)
|
||||
@@ -56,7 +60,7 @@ func NewRouter(routers ...Router) *mux.Router {
|
||||
}
|
||||
|
||||
// EncodeJSONResponse uses the json encoder to write an interface to the http response with an optional status code
|
||||
func EncodeJSONResponse(i interface{}, status *int, w http.ResponseWriter) error {
|
||||
func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string, w http.ResponseWriter) error {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
if status != nil {
|
||||
w.WriteHeader(*status)
|
||||
@@ -123,17 +127,91 @@ func readFileHeaderToTempFile(fileHeader *multipart.FileHeader) (*os.File, error
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// parseInt64Parameter parses a string parameter to an int64.
|
||||
func parseInt64Parameter(param string, required bool) (int64, error) {
|
||||
if param == "" {
|
||||
if required {
|
||||
return 0, errors.New(errMsgRequiredMissing)
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// parseInt64Parameter parses a sting parameter to an int64
|
||||
func parseInt64Parameter(param string) (int64, error) {
|
||||
return strconv.ParseInt(param, 10, 64)
|
||||
}
|
||||
|
||||
// parseInt32Parameter parses a sting parameter to an int32
|
||||
func parseInt32Parameter(param string) (int32, error) {
|
||||
// parseInt32Parameter parses a string parameter to an int32.
|
||||
func parseInt32Parameter(param string, required bool) (int32, error) {
|
||||
if param == "" {
|
||||
if required {
|
||||
return 0, errors.New(errMsgRequiredMissing)
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
val, err := strconv.ParseInt(param, 10, 32)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return int32(val), nil
|
||||
}
|
||||
|
||||
// parseBoolParameter parses a string parameter to a bool
|
||||
func parseBoolParameter(param string) (bool, error) {
|
||||
val, err := strconv.ParseBool(param)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return bool(val), nil
|
||||
}
|
||||
|
||||
// parseInt64ArrayParameter parses a string parameter containing array of values to []int64.
|
||||
func parseInt64ArrayParameter(param, delim string, required bool) ([]int64, error) {
|
||||
if param == "" {
|
||||
if required {
|
||||
return nil, errors.New(errMsgRequiredMissing)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
str := strings.Split(param, delim)
|
||||
ints := make([]int64, len(str))
|
||||
|
||||
for i, s := range str {
|
||||
if v, err := strconv.ParseInt(s, 10, 64); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
ints[i] = v
|
||||
}
|
||||
}
|
||||
|
||||
return ints, nil
|
||||
}
|
||||
|
||||
// parseInt32ArrayParameter parses a string parameter containing array of values to []int32.
|
||||
func parseInt32ArrayParameter(param, delim string, required bool) ([]int32, error) {
|
||||
if param == "" {
|
||||
if required {
|
||||
return nil, errors.New(errMsgRequiredMissing)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
str := strings.Split(param, delim)
|
||||
ints := make([]int32, len(str))
|
||||
|
||||
for i, s := range str {
|
||||
if v, err := strconv.ParseInt(s, 10, 32); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
ints[i] = int32(v)
|
||||
}
|
||||
}
|
||||
|
||||
return ints, nil
|
||||
}
|
||||
Reference in New Issue
Block a user