Adding support for getting the user list.
This commit is contained in:
parent
cd235d382d
commit
38c9fb9c57
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
type IController interface {
|
||||
POST(w http.ResponseWriter, r *http.Request)
|
||||
GET(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
type Controller struct {
|
||||
IController IController
|
||||
|
@ -19,8 +20,15 @@ func (self Controller) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
if r.Method == "POST" {
|
||||
self.POST(w, r)
|
||||
}
|
||||
if r.Method == "GET" {
|
||||
self.GET(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (self Controller) POST(w http.ResponseWriter, r *http.Request) {
|
||||
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"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -16,24 +17,61 @@ func New() 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) {
|
||||
var decoded_user_struct POSTUser
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&decoded_user_struct)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to decode json"))
|
||||
fmt.Fprintf(w, json_response)
|
||||
return
|
||||
}
|
||||
if decoded_user_struct.Username == "" {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to get username"))
|
||||
fmt.Fprintf(w, json_response)
|
||||
return
|
||||
}
|
||||
if decoded_user_struct.PasswordPlaintext == "" {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to get password"))
|
||||
fmt.Fprintf(w, json_response)
|
||||
|
|
|
@ -3,7 +3,9 @@ package usermanager
|
|||
import (
|
||||
"WWWShop/wwwshop/model/user"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type UserManager struct {
|
||||
|
@ -15,14 +17,14 @@ func New(db *sql.DB) UserManager {
|
|||
}
|
||||
|
||||
func (self UserManager) Add(username string, password_hash string) (*user.User, error) {
|
||||
var id int64
|
||||
var id int64
|
||||
|
||||
result := self.db.QueryRow("INSERT INTO users(username, password) VALUES ($1, $2) RETURNING id", username, password_hash)
|
||||
err := result.Scan(&id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user := user.New(id, username, password_hash)
|
||||
user := user.New(id, username, password_hash)
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
|
@ -32,5 +34,75 @@ func (self UserManager) AddWithPlainPassword(username string, password string) (
|
|||
return nil, err
|
||||
}
|
||||
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