mirror of
https://github.com/MizuchiLabs/mantrae.git
synced 2026-04-24 17:30:04 -05:00
121 lines
3.1 KiB
Go
121 lines
3.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/MizuchiLabs/mantrae/internal/config"
|
|
"github.com/MizuchiLabs/mantrae/internal/db"
|
|
"github.com/MizuchiLabs/mantrae/internal/util"
|
|
)
|
|
|
|
func ListUsers(a *config.App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
q := a.Conn.GetQuery()
|
|
users, err := q.ListUsers(r.Context())
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(users)
|
|
}
|
|
}
|
|
|
|
func GetUser(a *config.App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
q := a.Conn.GetQuery()
|
|
user_id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
user, err := q.GetUser(r.Context(), user_id)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(user)
|
|
}
|
|
}
|
|
|
|
func CreateUser(a *config.App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
q := a.Conn.GetQuery()
|
|
var user db.CreateUserParams
|
|
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if err := q.CreateUser(r.Context(), user); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
util.Broadcast <- util.EventMessage{
|
|
Type: util.EventTypeCreate,
|
|
Message: "user",
|
|
}
|
|
w.WriteHeader(http.StatusCreated)
|
|
}
|
|
}
|
|
|
|
func UpdateUser(a *config.App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
q := a.Conn.GetQuery()
|
|
var user db.UpdateUserParams
|
|
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
dbUser, err := q.GetUser(r.Context(), user.ID)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if user.Password == "" {
|
|
user.Password = dbUser.Password
|
|
} else {
|
|
hashedPassword, err := util.HashPassword(user.Password)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
user.Password = hashedPassword
|
|
}
|
|
if err := q.UpdateUser(r.Context(), user); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
util.Broadcast <- util.EventMessage{
|
|
Type: util.EventTypeUpdate,
|
|
Message: "user",
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
func DeleteUser(a *config.App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
q := a.Conn.GetQuery()
|
|
user_id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if err := q.DeleteUser(r.Context(), user_id); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
util.Broadcast <- util.EventMessage{
|
|
Type: util.EventTypeDelete,
|
|
Message: "user",
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
}
|