[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

@@ -82,13 +82,12 @@ 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 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
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 err != nil {
@@ -120,13 +118,12 @@ func (c *UserApiController) CreateUsersWithArrayInput(w http.ResponseWriter, r *
}
// 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 err != nil {
@@ -139,9 +136,10 @@ func (c *UserApiController) CreateUsersWithListInput(w http.ResponseWriter, r *h
}
// 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 err != nil {
@@ -154,9 +152,10 @@ func (c *UserApiController) DeleteUser(w http.ResponseWriter, r *http.Request) {
}
// 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 err != nil {
@@ -169,7 +168,7 @@ func (c *UserApiController) GetUserByName(w http.ResponseWriter, r *http.Request
}
// 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")
@@ -185,7 +184,7 @@ func (c *UserApiController) LoginUser(w http.ResponseWriter, r *http.Request) {
}
// 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 err != nil {
@@ -198,15 +197,15 @@ func (c *UserApiController) LogoutUser(w http.ResponseWriter, r *http.Request) {
}
// 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 err != nil {