[Go][server] parse bool in query parameter (#9171)

* Add bool query in example

* Add parseBoolParameter

* test with boolean parameter

* add endpiont for testing boolean

* remove bool parameter test

Co-authored-by: Stéphane Guillemot <gmtstephane@gmail.com>
This commit is contained in:
William Cheng
2021-04-05 16:45:38 +08:00
committed by GitHub
parent c79d056935
commit 5dae270c3e
6 changed files with 115 additions and 54 deletions

View File

@@ -32,66 +32,112 @@ func (c *{{classname}}Controller) Routes() Routes {
}{{#operations}}{{#operation}} }{{#operations}}{{#operation}}
// {{nickname}} - {{{summary}}} // {{nickname}} - {{{summary}}}
func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Request) { {{#hasFormParams}} func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Request) {
{{#hasFormParams}}
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
{{/hasFormParams}}{{#hasPathParams}} {{/hasFormParams}}
params := mux.Vars(r){{/hasPathParams}}{{#hasQueryParams}} {{#hasPathParams}}
query := r.URL.Query(){{/hasQueryParams}}{{#allParams}}{{#isPathParam}}{{#isLong}} params := mux.Vars(r)
{{/hasPathParams}}
{{#hasQueryParams}}
query := r.URL.Query()
{{/hasQueryParams}}
{{#allParams}}
{{#isPathParam}}
{{#isLong}}
{{paramName}}, err := parseInt64Parameter(params["{{baseName}}"]) {{paramName}}, err := parseInt64Parameter(params["{{baseName}}"])
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
}{{/isLong}}{{#isInteger}} }
{{/isLong}}
{{#isInteger}}
{{paramName}}, err := parseInt32Parameter(params["{{baseName}}"]) {{paramName}}, err := parseInt32Parameter(params["{{baseName}}"])
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
{{/isInteger}}{{^isLong}}{{^isInteger}} {{/isInteger}}
{{paramName}} := params["{{baseName}}"]{{/isInteger}}{{/isLong}}{{/isPathParam}}{{#isQueryParam}}{{#isLong}} {{^isLong}}
{{^isInteger}}
{{paramName}} := params["{{baseName}}"]
{{/isInteger}}{{/isLong}}
{{/isPathParam}}
{{#isQueryParam}}
{{#isLong}}
{{paramName}}, err := parseInt64Parameter(query.Get("{{baseName}}")) {{paramName}}, err := parseInt64Parameter(query.Get("{{baseName}}"))
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
{{/isLong}}{{#isInteger}} {{/isLong}}
{{#isInteger}}
{{paramName}}, err := parseInt32Parameter(query.Get("{{baseName}}")) {{paramName}}, err := parseInt32Parameter(query.Get("{{baseName}}"))
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
{{/isInteger}}{{^isLong}}{{^isInteger}} {{/isInteger}}
{{paramName}} := {{#isArray}}strings.Split({{/isArray}}query.Get("{{baseName}}"){{#isArray}}, ","){{/isArray}}{{/isInteger}}{{/isLong}}{{/isQueryParam}}{{#isFormParam}}{{#isFile}} {{#isBoolean}}
{{#isArray}}{{paramName}}, err := ReadFormFilesToTempFiles(r, "{{baseName}}"){{/isArray}}{{^isArray}}{{paramName}}, err := ReadFormFileToTempFile(r, "{{baseName}}"){{/isArray}} {{paramName}}, err := parseBoolParameter(query.Get("{{paramName}}"))
if err != nil {
w.WriteHeader(500)
return
}
{{/isBoolean}}
{{^isLong}}
{{^isInteger}}
{{^isBoolean}}
{{paramName}} := {{#isArray}}strings.Split({{/isArray}}query.Get("{{paramName}}"){{#isArray}}, ","){{/isArray}}
{{/isBoolean}}
{{/isInteger}}
{{/isLong}}
{{/isQueryParam}}
{{#isFormParam}}
{{#isFile}}
{{#isArray}}
{{paramName}}, err := ReadFormFilesToTempFiles(r, "{{baseName}}"){{/isArray}}{{^isArray}}{{paramName}}, err := ReadFormFileToTempFile(r, "{{baseName}}")
{{/isArray}}
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
{{/isFile}}{{#isLong}} {{/isFile}}
{{#isLong}}
{{paramName}}, err := parseInt64Parameter( r.FormValue("{{baseName}}")) {{paramName}}, err := parseInt64Parameter( r.FormValue("{{baseName}}"))
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
{{/isLong}}{{#isInteger}} {{/isLong}}
{{#isInteger}}
{{paramName}}, err := parseInt32Parameter( r.FormValue("{{baseName}}")) {{paramName}}, err := parseInt32Parameter( r.FormValue("{{baseName}}"))
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
{{/isInteger}}{{^isFile}}{{^isLong}} {{/isInteger}}
{{paramName}} := r.FormValue("{{baseName}}"){{/isLong}}{{/isFile}}{{/isFormParam}}{{#isHeaderParam}} {{^isFile}}
{{paramName}} := r.Header.Get("{{baseName}}"){{/isHeaderParam}}{{#isBodyParam}} {{^isLong}}
{{paramName}} := r.FormValue("{{baseName}}")
{{/isLong}}
{{/isFile}}
{{/isFormParam}}
{{#isHeaderParam}}
{{paramName}} := r.Header.Get("{{baseName}}")
{{/isHeaderParam}}
{{#isBodyParam}}
{{paramName}} := &{{dataType}}{} {{paramName}} := &{{dataType}}{}
if err := json.NewDecoder(r.Body).Decode(&{{paramName}}); err != nil { if err := json.NewDecoder(r.Body).Decode(&{{paramName}}); err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
{{/isBodyParam}}{{/allParams}} {{/isBodyParam}}
{{/allParams}}
result, err := c.service.{{nickname}}(r.Context(){{#allParams}}, {{#isBodyParam}}*{{/isBodyParam}}{{paramName}}{{/allParams}}) result, err := c.service.{{nickname}}(r.Context(){{#allParams}}, {{#isBodyParam}}*{{/isBodyParam}}{{paramName}}{{/allParams}})
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {

View File

@@ -134,13 +134,12 @@ func readFileHeaderToTempFile(fileHeader *multipart.FileHeader) (*os.File, error
return file, nil return file, nil
} }
// parseInt64Parameter parses a string parameter to an int64
// parseInt64Parameter parses a sting parameter to an int64
func parseInt64Parameter(param string) (int64, error) { func parseInt64Parameter(param string) (int64, error) {
return strconv.ParseInt(param, 10, 64) return strconv.ParseInt(param, 10, 64)
} }
// parseInt32Parameter parses a sting parameter to an int32 // parseInt32Parameter parses a string parameter to an int32
func parseInt32Parameter(param string) (int32, error) { func parseInt32Parameter(param string) (int32, error) {
val, err := strconv.ParseInt(param, 10, 32) val, err := strconv.ParseInt(param, 10, 32)
if err != nil { if err != nil {
@@ -148,3 +147,12 @@ func parseInt32Parameter(param string) (int32, error) {
} }
return int32(val), nil 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
}

View File

@@ -82,13 +82,12 @@ func (c *PetApiController) Routes() Routes {
} }
// AddPet - Add a new pet to the store // 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{} pet := &Pet{}
if err := json.NewDecoder(r.Body).Decode(&pet); err != nil { if err := json.NewDecoder(r.Body).Decode(&pet); err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
result, err := c.service.AddPet(r.Context(), *pet) result, err := c.service.AddPet(r.Context(), *pet)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {
@@ -101,13 +100,14 @@ func (c *PetApiController) AddPet(w http.ResponseWriter, r *http.Request) {
} }
// DeletePet - Deletes a pet // 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) params := mux.Vars(r)
petId, err := parseInt64Parameter(params["petId"]) petId, err := parseInt64Parameter(params["petId"])
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
apiKey := r.Header.Get("api_key") apiKey := r.Header.Get("api_key")
result, err := c.service.DeletePet(r.Context(), petId, apiKey) result, err := c.service.DeletePet(r.Context(), petId, apiKey)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
@@ -121,7 +121,7 @@ func (c *PetApiController) DeletePet(w http.ResponseWriter, r *http.Request) {
} }
// FindPetsByStatus - Finds Pets by status // 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() query := r.URL.Query()
status := strings.Split(query.Get("status"), ",") status := strings.Split(query.Get("status"), ",")
result, err := c.service.FindPetsByStatus(r.Context(), status) result, err := c.service.FindPetsByStatus(r.Context(), status)
@@ -136,7 +136,7 @@ func (c *PetApiController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
} }
// FindPetsByTags - Finds Pets by tags // 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() query := r.URL.Query()
tags := strings.Split(query.Get("tags"), ",") tags := strings.Split(query.Get("tags"), ",")
result, err := c.service.FindPetsByTags(r.Context(), tags) result, err := c.service.FindPetsByTags(r.Context(), tags)
@@ -151,13 +151,14 @@ func (c *PetApiController) FindPetsByTags(w http.ResponseWriter, r *http.Request
} }
// GetPetById - Find pet by ID // 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) params := mux.Vars(r)
petId, err := parseInt64Parameter(params["petId"]) petId, err := parseInt64Parameter(params["petId"])
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
result, err := c.service.GetPetById(r.Context(), petId) result, err := c.service.GetPetById(r.Context(), petId)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {
@@ -170,13 +171,12 @@ func (c *PetApiController) GetPetById(w http.ResponseWriter, r *http.Request) {
} }
// UpdatePet - Update an existing pet // 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{} pet := &Pet{}
if err := json.NewDecoder(r.Body).Decode(&pet); err != nil { if err := json.NewDecoder(r.Body).Decode(&pet); err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
result, err := c.service.UpdatePet(r.Context(), *pet) result, err := c.service.UpdatePet(r.Context(), *pet)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {
@@ -189,19 +189,19 @@ func (c *PetApiController) UpdatePet(w http.ResponseWriter, r *http.Request) {
} }
// UpdatePetWithForm - Updates a pet in the store with form data // UpdatePetWithForm - Updates a pet in the store with form data
func (c *PetApiController) UpdatePetWithForm(w http.ResponseWriter, r *http.Request) { func (c *PetApiController) UpdatePetWithForm(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
params := mux.Vars(r) params := mux.Vars(r)
petId, err := parseInt64Parameter(params["petId"]) petId, err := parseInt64Parameter(params["petId"])
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
name := r.FormValue("name") name := r.FormValue("name")
status := r.FormValue("status") status := r.FormValue("status")
result, err := c.service.UpdatePetWithForm(r.Context(), petId, name, status) result, err := c.service.UpdatePetWithForm(r.Context(), petId, name, status)
@@ -216,26 +216,25 @@ func (c *PetApiController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ
} }
// UploadFile - uploads an image // UploadFile - uploads an image
func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) { func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
params := mux.Vars(r) params := mux.Vars(r)
petId, err := parseInt64Parameter(params["petId"]) petId, err := parseInt64Parameter(params["petId"])
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
additionalMetadata := r.FormValue("additionalMetadata") additionalMetadata := r.FormValue("additionalMetadata")
file, err := ReadFormFileToTempFile(r, "file") file, err := ReadFormFileToTempFile(r, "file")
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
result, err := c.service.UploadFile(r.Context(), petId, additionalMetadata, file) result, err := c.service.UploadFile(r.Context(), petId, additionalMetadata, file)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {

View File

@@ -58,9 +58,10 @@ func (c *StoreApiController) Routes() Routes {
} }
// DeleteOrder - Delete purchase order by ID // 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) params := mux.Vars(r)
orderId := params["orderId"] orderId := params["orderId"]
result, err := c.service.DeleteOrder(r.Context(), orderId) result, err := c.service.DeleteOrder(r.Context(), orderId)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {
@@ -73,7 +74,7 @@ func (c *StoreApiController) DeleteOrder(w http.ResponseWriter, r *http.Request)
} }
// GetInventory - Returns pet inventories by status // 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()) result, err := c.service.GetInventory(r.Context())
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {
@@ -86,13 +87,14 @@ func (c *StoreApiController) GetInventory(w http.ResponseWriter, r *http.Request
} }
// GetOrderById - Find purchase order by ID // 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) params := mux.Vars(r)
orderId, err := parseInt64Parameter(params["orderId"]) orderId, err := parseInt64Parameter(params["orderId"])
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
result, err := c.service.GetOrderById(r.Context(), orderId) result, err := c.service.GetOrderById(r.Context(), orderId)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {
@@ -105,13 +107,12 @@ func (c *StoreApiController) GetOrderById(w http.ResponseWriter, r *http.Request
} }
// PlaceOrder - Place an order for a pet // 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{} order := &Order{}
if err := json.NewDecoder(r.Body).Decode(&order); err != nil { if err := json.NewDecoder(r.Body).Decode(&order); err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
result, err := c.service.PlaceOrder(r.Context(), *order) result, err := c.service.PlaceOrder(r.Context(), *order)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {

View File

@@ -82,13 +82,12 @@ func (c *UserApiController) Routes() Routes {
} }
// CreateUser - Create user // 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{} user := &User{}
if err := json.NewDecoder(r.Body).Decode(&user); err != nil { if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
result, err := c.service.CreateUser(r.Context(), *user) result, err := c.service.CreateUser(r.Context(), *user)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {
@@ -101,13 +100,12 @@ func (c *UserApiController) CreateUser(w http.ResponseWriter, r *http.Request) {
} }
// CreateUsersWithArrayInput - Creates list of users with given input array // 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{} user := &[]User{}
if err := json.NewDecoder(r.Body).Decode(&user); err != nil { if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
result, err := c.service.CreateUsersWithArrayInput(r.Context(), *user) result, err := c.service.CreateUsersWithArrayInput(r.Context(), *user)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {
@@ -120,13 +118,12 @@ func (c *UserApiController) CreateUsersWithArrayInput(w http.ResponseWriter, r *
} }
// CreateUsersWithListInput - Creates list of users with given input array // 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{} user := &[]User{}
if err := json.NewDecoder(r.Body).Decode(&user); err != nil { if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
result, err := c.service.CreateUsersWithListInput(r.Context(), *user) result, err := c.service.CreateUsersWithListInput(r.Context(), *user)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {
@@ -139,9 +136,10 @@ func (c *UserApiController) CreateUsersWithListInput(w http.ResponseWriter, r *h
} }
// DeleteUser - Delete user // 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) params := mux.Vars(r)
username := params["username"] username := params["username"]
result, err := c.service.DeleteUser(r.Context(), username) result, err := c.service.DeleteUser(r.Context(), username)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {
@@ -154,9 +152,10 @@ func (c *UserApiController) DeleteUser(w http.ResponseWriter, r *http.Request) {
} }
// GetUserByName - Get user by user name // 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) params := mux.Vars(r)
username := params["username"] username := params["username"]
result, err := c.service.GetUserByName(r.Context(), username) result, err := c.service.GetUserByName(r.Context(), username)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {
@@ -169,7 +168,7 @@ func (c *UserApiController) GetUserByName(w http.ResponseWriter, r *http.Request
} }
// LoginUser - Logs user into the system // 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() query := r.URL.Query()
username := query.Get("username") username := query.Get("username")
password := query.Get("password") password := query.Get("password")
@@ -185,7 +184,7 @@ func (c *UserApiController) LoginUser(w http.ResponseWriter, r *http.Request) {
} }
// LogoutUser - Logs out current logged in user session // 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()) result, err := c.service.LogoutUser(r.Context())
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {
@@ -198,15 +197,15 @@ func (c *UserApiController) LogoutUser(w http.ResponseWriter, r *http.Request) {
} }
// UpdateUser - Updated user // 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) params := mux.Vars(r)
username := params["username"] username := params["username"]
user := &User{} user := &User{}
if err := json.NewDecoder(r.Body).Decode(&user); err != nil { if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
result, err := c.service.UpdateUser(r.Context(), username, *user) result, err := c.service.UpdateUser(r.Context(), username, *user)
//If an error occured, encode the error with the status code //If an error occured, encode the error with the status code
if err != nil { if err != nil {

View File

@@ -123,13 +123,12 @@ func readFileHeaderToTempFile(fileHeader *multipart.FileHeader) (*os.File, error
return file, nil return file, nil
} }
// parseInt64Parameter parses a string parameter to an int64
// parseInt64Parameter parses a sting parameter to an int64
func parseInt64Parameter(param string) (int64, error) { func parseInt64Parameter(param string) (int64, error) {
return strconv.ParseInt(param, 10, 64) return strconv.ParseInt(param, 10, 64)
} }
// parseInt32Parameter parses a sting parameter to an int32 // parseInt32Parameter parses a string parameter to an int32
func parseInt32Parameter(param string) (int32, error) { func parseInt32Parameter(param string) (int32, error) {
val, err := strconv.ParseInt(param, 10, 32) val, err := strconv.ParseInt(param, 10, 32)
if err != nil { if err != nil {
@@ -137,3 +136,12 @@ func parseInt32Parameter(param string) (int32, error) {
} }
return int32(val), nil 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
}