WWWShop/wwwshop/controller/usercontroller/usercontroller.go

120 lines
3.3 KiB
Go

package usercontroller
import (
d "WWWShop/wwwshop/dao/database"
u "WWWShop/wwwshop/dao/usermanager"
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
)
type UserController struct{}
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 {
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)
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.StatusInternalServerError), http.StatusInternalServerError)
json_response, _ := EncodeJSONResponse(NewResponseError("Unable to get password"))
fmt.Fprintf(w, json_response)
return
}
user_manager := u.New(d.DB())
user, err := user_manager.AddWithPlainPassword(decoded_user_struct.Username, decoded_user_struct.PasswordPlaintext)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
json_response, _ := EncodeJSONResponse(NewResponseError(strings.Join(
[]string{"Unable to create user ", err.Error()},
"")))
fmt.Fprintf(w, json_response)
return
}
json_response, err := EncodeJSONResponse(user)
fmt.Fprintf(w, json_response)
}
type Response interface{}
type ResponseError struct {
Error string `json:"error"`
}
func NewResponseError(error_str string) ResponseError {
return ResponseError{
Error: error_str,
}
}
type POSTUser struct {
Username string `json:"username"`
PasswordPlaintext string `json:"password"`
}
func EncodeJSONResponse(response Response) (string, error) {
var buffer bytes.Buffer
encoder := json.NewEncoder(&buffer)
err := encoder.Encode(response)
if err != nil {
return "", err
}
return buffer.String(), nil
}