189 lines
5.1 KiB
Go
189 lines
5.1 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"magmise/models"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
type User struct {
|
|
}
|
|
|
|
// "/user/:username"
|
|
func (u User) Get(w http.ResponseWriter, r *http.Request) {
|
|
username := chi.URLParam(r, "username")
|
|
if username == "" {
|
|
http.Error(w, "Missing user id", http.StatusNotFound)
|
|
return
|
|
}
|
|
user := models.User{Username: username}
|
|
if user.Read() != nil {
|
|
http.Error(w, "Not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
if err := json.NewEncoder(w).Encode(user); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Add("Content-Type", "application/json")
|
|
}
|
|
|
|
// "/user"
|
|
func (User) Create(w http.ResponseWriter, r *http.Request) {
|
|
var user models.User
|
|
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
}
|
|
|
|
if user.Create() != nil {
|
|
http.Error(w, "failed to create user", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
|
|
// // /users/application/:status/
|
|
// func (t User) Getusers(w http.ResponseWriter, r *http.Request) {
|
|
// status := vestigo.Param(r, "status")
|
|
// offset, err := strconv.ParseInt(vestigo.Param(r, "offset"), 0, 32)
|
|
// if err != nil {
|
|
// offset = 0
|
|
// }
|
|
|
|
// limit, err := strconv.ParseInt(vestigo.Param(r, "limit"), 0, 32)
|
|
// if err != nil {
|
|
// limit = 100
|
|
// }
|
|
|
|
// var user models.User
|
|
// switch status {
|
|
// case "open", "closed":
|
|
// user = models.User{Status: status}
|
|
// case "all":
|
|
// user = models.User{}
|
|
// default:
|
|
// http.Error(w, "Missing status", http.StatusNotFound)
|
|
// return
|
|
// }
|
|
|
|
// users, err := user.ReadAll(int(offset), int(limit))
|
|
// if err != nil {
|
|
// http.Error(w, "Not found", http.StatusNotFound)
|
|
// return
|
|
// }
|
|
// if err := json.NewEncoder(w).Encode(users); err != nil {
|
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
// return
|
|
// }
|
|
// w.Header().Add("Content-Type", "application/json")
|
|
// }
|
|
|
|
// // "/user/application/:userid"
|
|
// func (t User) Updateuser(w http.ResponseWriter, r *http.Request) {
|
|
// userid := vestigo.Param(r, "userid")
|
|
// if userid == "" {
|
|
// http.Error(w, "Missing user or discord id", http.StatusNotFound)
|
|
// return
|
|
// }
|
|
// log.Println(r.Body)
|
|
// var user models.User
|
|
// if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
// return
|
|
// }
|
|
// log.Println(user)
|
|
// if err := user.Update(); err != nil {
|
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
// return
|
|
// }
|
|
|
|
// }
|
|
|
|
// // "/user/application/:userid"
|
|
// func (t User) Deleteuser(w http.ResponseWriter, r *http.Request) {
|
|
// userid := vestigo.Param(r, "userid")
|
|
// if userid == "" {
|
|
// http.Error(w, "Missing user or discord id", http.StatusNotFound)
|
|
// }
|
|
// user := models.User{userId: userid}
|
|
|
|
// if user.Delete() != nil {
|
|
// http.Error(w, "failed to delete user", http.StatusInternalServerError)
|
|
// return
|
|
// }
|
|
|
|
// }
|
|
|
|
// // /user/:discordid/users/application/:status/
|
|
// func (t User) GetUserusers(w http.ResponseWriter, r *http.Request) {
|
|
// discordid, _ := strconv.ParseInt(vestigo.Param(r, "discordid"), 0, 64)
|
|
// status := vestigo.Param(r, "status")
|
|
// offset, err := strconv.ParseInt(vestigo.Param(r, "offset"), 0, 32)
|
|
// if err != nil {
|
|
// offset = 0
|
|
// }
|
|
|
|
// limit, err := strconv.ParseInt(vestigo.Param(r, "limit"), 0, 32)
|
|
// if err != nil {
|
|
// limit = 100
|
|
// }
|
|
|
|
// log.Println(discordid, status)
|
|
// var user models.User
|
|
// switch status {
|
|
// case "open", "closed":
|
|
// user = models.User{DiscordId: discordid, Status: status}
|
|
// case "all":
|
|
// user = models.User{DiscordId: discordid}
|
|
// default:
|
|
// http.Error(w, "Missing status", http.StatusNotFound)
|
|
// return
|
|
// }
|
|
|
|
// users, err := user.ReadAll(int(offset), int(limit))
|
|
// if err != nil {
|
|
// http.Error(w, "Not found", http.StatusNotFound)
|
|
// return
|
|
// }
|
|
// if err := json.NewEncoder(w).Encode(users); err != nil {
|
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
// return
|
|
// }
|
|
// w.Header().Add("Content-Type", "application/json")
|
|
// }
|
|
|
|
// // "/answer/application/:userid/:modalid"
|
|
|
|
// func (t User) AddAnswer(w http.ResponseWriter, r *http.Request) {
|
|
// userid := vestigo.Param(r, "userid")
|
|
// modalid := vestigo.Param(r, "modalid")
|
|
// if userid == "" {
|
|
// http.Error(w, "Missing user or discord id", http.StatusNotFound)
|
|
// return
|
|
// }
|
|
// b, err := ioutil.ReadAll(r.Body)
|
|
// if err != nil {
|
|
// http.Error(w, "Missing body", http.StatusNotFound)
|
|
// }
|
|
// user := models.User{userId: userid}
|
|
// if user.Read() != nil {
|
|
// http.Error(w, "Failed to read user", http.StatusInternalServerError)
|
|
// }
|
|
// for _, answer := range user.Answers {
|
|
// if answer.ModalId == modalid {
|
|
// answer.Response = string(b)
|
|
// if answer.Update() != nil {
|
|
// http.Error(w, "Failed to update answer", http.StatusInternalServerError)
|
|
// return
|
|
// }
|
|
// return
|
|
// }
|
|
// }
|
|
// answer := models.Answer{ModalId: modalid, Response: string(b)}
|
|
// user.Answers = append(user.Answers, answer)
|
|
// if user.Update() != nil {
|
|
// http.Error(w, "Failed to update user", http.StatusInternalServerError)
|
|
// }
|
|
// }
|