Adding support for getting the user list.
This commit is contained in:
parent
cd235d382d
commit
38c9fb9c57
|
@ -6,6 +6,7 @@ import (
|
||||||
|
|
||||||
type IController interface {
|
type IController interface {
|
||||||
POST(w http.ResponseWriter, r *http.Request)
|
POST(w http.ResponseWriter, r *http.Request)
|
||||||
|
GET(w http.ResponseWriter, r *http.Request)
|
||||||
}
|
}
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
IController IController
|
IController IController
|
||||||
|
@ -19,8 +20,15 @@ func (self Controller) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "POST" {
|
if r.Method == "POST" {
|
||||||
self.POST(w, r)
|
self.POST(w, r)
|
||||||
}
|
}
|
||||||
|
if r.Method == "GET" {
|
||||||
|
self.GET(w, r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Controller) POST(w http.ResponseWriter, r *http.Request) {
|
func (self Controller) POST(w http.ResponseWriter, r *http.Request) {
|
||||||
self.IController.POST(w, r)
|
self.IController.POST(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self Controller) GET(w http.ResponseWriter, r *http.Request) {
|
||||||
|
self.IController.GET(w, r)
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,12 +17,49 @@ func New() UserController {
|
||||||
return UserController{}
|
return UserController{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self UserController) GET(w http.ResponseWriter, r *http.Request) {
|
||||||
|
query := r.URL.Query()
|
||||||
|
usernames, _ := query["username"]
|
||||||
|
ids, _ := query["id"]
|
||||||
|
pages, _ := query["page"]
|
||||||
|
var page int64
|
||||||
|
if len(pages) == 0 {
|
||||||
|
page = 0
|
||||||
|
} else {
|
||||||
|
var err error
|
||||||
|
page, err = strconv.ParseInt(pages[0], 0, 64)
|
||||||
|
if err != nil {
|
||||||
|
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to parse int page."))
|
||||||
|
fmt.Fprintf(w, json_response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
user_manager := u.New(d.DB())
|
||||||
|
ids_int := make([]int64, 0)
|
||||||
|
for _, id := range ids {
|
||||||
|
id_int, err := strconv.ParseInt(id, 0, 64)
|
||||||
|
if err != nil {
|
||||||
|
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to parse int id."))
|
||||||
|
fmt.Fprintf(w, json_response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ids_int = append(ids_int, id_int)
|
||||||
|
}
|
||||||
|
users, err := user_manager.Retrieve(page, &ids_int, &usernames)
|
||||||
|
if err != nil {
|
||||||
|
json_response, _ := EncodeJSONResponse(NewResponseError(strings.Join([]string{"Unable to retrieve users ", err.Error()}, "")))
|
||||||
|
fmt.Fprintf(w, json_response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
json_response, _ := EncodeJSONResponse(users)
|
||||||
|
fmt.Fprintf(w, json_response)
|
||||||
|
}
|
||||||
|
|
||||||
func (self UserController) POST(w http.ResponseWriter, r *http.Request) {
|
func (self UserController) POST(w http.ResponseWriter, r *http.Request) {
|
||||||
var decoded_user_struct POSTUser
|
var decoded_user_struct POSTUser
|
||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
err := decoder.Decode(&decoded_user_struct)
|
err := decoder.Decode(&decoded_user_struct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
||||||
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to decode json"))
|
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to decode json"))
|
||||||
fmt.Fprintf(w, json_response)
|
fmt.Fprintf(w, json_response)
|
||||||
return
|
return
|
||||||
|
|
|
@ -3,7 +3,9 @@ package usermanager
|
||||||
import (
|
import (
|
||||||
"WWWShop/wwwshop/model/user"
|
"WWWShop/wwwshop/model/user"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserManager struct {
|
type UserManager struct {
|
||||||
|
@ -34,3 +36,73 @@ func (self UserManager) AddWithPlainPassword(username string, password string) (
|
||||||
user, err := self.Add(username, string(password_hash))
|
user, err := self.Add(username, string(password_hash))
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type QueryParameter interface{}
|
||||||
|
|
||||||
|
func (self UserManager) Retrieve(page int64, ids *[]int64, usernames *[]string) (*[]user.User, error) {
|
||||||
|
var where bool
|
||||||
|
if ids != nil && len(*ids) > 0 {
|
||||||
|
where = true
|
||||||
|
}
|
||||||
|
if usernames != nil && len(*usernames) > 0 {
|
||||||
|
where = true
|
||||||
|
}
|
||||||
|
cur_key := 1
|
||||||
|
where_string := ""
|
||||||
|
if where {
|
||||||
|
where_string = "WHERE true "
|
||||||
|
if usernames != nil && len(*usernames) > 0 {
|
||||||
|
where_string = strings.Join([]string{where_string, "AND ( false"}, "")
|
||||||
|
for range *usernames {
|
||||||
|
where_string = strings.Join([]string{where_string, fmt.Sprintf(" OR username = $%d", cur_key)}, "")
|
||||||
|
cur_key++
|
||||||
|
}
|
||||||
|
where_string = strings.Join([]string{where_string, " ) "}, "")
|
||||||
|
}
|
||||||
|
if ids != nil && len(*ids) > 0 {
|
||||||
|
where_string = strings.Join([]string{where_string, "AND ( false"}, "")
|
||||||
|
for range *ids {
|
||||||
|
where_string = strings.Join([]string{where_string, fmt.Sprintf(" OR id = $%d", cur_key)}, "")
|
||||||
|
cur_key++
|
||||||
|
}
|
||||||
|
where_string = strings.Join([]string{where_string, " ) "}, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
page = (10 * page)
|
||||||
|
page_string := fmt.Sprintf("LIMIT 10 OFFSET %d", page)
|
||||||
|
cur_key++
|
||||||
|
query_parameters := make([]interface{}, 0)
|
||||||
|
for _, username := range *usernames {
|
||||||
|
query_parameters = append(query_parameters, username)
|
||||||
|
}
|
||||||
|
for _, id := range *ids {
|
||||||
|
query_parameters = append(query_parameters, id)
|
||||||
|
}
|
||||||
|
query_string := fmt.Sprintf("SELECT id, username, password from users %s %s;", where_string, page_string)
|
||||||
|
fmt.Println(query_string)
|
||||||
|
fmt.Println(query_string)
|
||||||
|
fmt.Printf("%#v", query_parameters)
|
||||||
|
result, err := self.db.Query(
|
||||||
|
query_string,
|
||||||
|
query_parameters...,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer result.Close()
|
||||||
|
var users []user.User
|
||||||
|
for result.Next() {
|
||||||
|
var (
|
||||||
|
id_user int64
|
||||||
|
username_user string
|
||||||
|
password_user string
|
||||||
|
)
|
||||||
|
err := result.Scan(&id_user, &username_user, &password_user)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
u := user.New(id_user, username_user, password_user)
|
||||||
|
users = append(users, u)
|
||||||
|
}
|
||||||
|
return &users, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue