144 lines
3.7 KiB
Go
144 lines
3.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"math/rand"
|
|
"net/http"
|
|
|
|
"git.urec56.ru/urec/chat_back_go/internal/domain"
|
|
)
|
|
|
|
// GetUsers args: query: optional(username)
|
|
func (h *Handler) GetUsers(w http.ResponseWriter, r *http.Request) {
|
|
v := r.URL.Query()
|
|
username := v.Get("username")
|
|
|
|
users, err := h.serv.GetAll(username)
|
|
if err != nil {
|
|
h.l.Infof("[%s] getting users: %s", r.URL.Path, err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
j, _ := json.Marshal(map[string][]domain.User{"users": users}) // no error because []domain.User can`t cause any error here
|
|
|
|
if _, err = w.Write(j); err != nil {
|
|
h.l.Errorf("[%s] writing response: %s", r.URL.Path, err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
|
|
// CheckExisting args: body: optional(username), optional(email)
|
|
func (h *Handler) CheckExisting(w http.ResponseWriter, r *http.Request) {
|
|
var userFilter domain.UserFilter
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
if err := decoder.Decode(&userFilter); err != nil {
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
return
|
|
}
|
|
|
|
if err := domain.V.Struct(userFilter); err != nil {
|
|
h.l.Infof("[%s] validation: %s", r.URL.Path, err)
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
return
|
|
}
|
|
|
|
u, err := h.serv.FindOne(userFilter.Username, userFilter.Email)
|
|
if err != nil {
|
|
h.l.Infof("[%s] serv.FindOne error: %s", r.URL.Path, err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if u.ID != 0 { // zero value
|
|
w.WriteHeader(http.StatusConflict)
|
|
}
|
|
}
|
|
|
|
// CheckExistingPassword args: body: password
|
|
func (h *Handler) CheckExistingPassword(w http.ResponseWriter, _ *http.Request) {
|
|
randInt := rand.Intn(10)
|
|
if randInt > 5 {
|
|
w.WriteHeader(http.StatusConflict)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) Register(w http.ResponseWriter, r *http.Request) {
|
|
var userData domain.UserRegister
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
if err := decoder.Decode(&userData); err != nil {
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
return
|
|
}
|
|
|
|
if err := domain.V.Struct(userData); err != nil {
|
|
h.l.Infof("[%s] validation: %s", r.URL.Path, err)
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
return
|
|
}
|
|
|
|
token, err := h.serv.Register(userData)
|
|
if err != nil {
|
|
if errors.Is(err, domain.UserAlreadyExistsError) {
|
|
w.WriteHeader(http.StatusConflict)
|
|
} else {
|
|
h.l.Infof("[%s] serv.Register: %s", r.URL.Path, err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
return
|
|
}
|
|
|
|
resp, _ := json.Marshal(map[string]string{"authorization": fmt.Sprintf("Bearer %s", token)}) // no error here
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
if _, err = w.Write(resp); err != nil {
|
|
h.l.Errorf("[%s] writing response: %s", r.URL.Path, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func (h *Handler) ResendEmailVerification(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Printf("")
|
|
}
|
|
|
|
func (h *Handler) EmailVerification(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Printf("")
|
|
}
|
|
|
|
func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Printf("")
|
|
}
|
|
|
|
func (h *Handler) Get(w http.ResponseWriter, r *http.Request) {
|
|
user, ok := r.Context().Value("user").(domain.User)
|
|
if !ok {
|
|
h.l.Errorf("[%s] extracting user from ctx", r.URL.Path)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
j, _ := json.Marshal(user) // no error because domain.User can`t cause any error here
|
|
|
|
if _, err := w.Write(j); err != nil {
|
|
h.l.Errorf("[%s] writing response: %s", r.URL.Path, err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
|
|
func (h *Handler) GetAvatarsHistory(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Printf("")
|
|
}
|
|
|
|
func (h *Handler) SendConfirmationCode(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Printf("")
|
|
}
|
|
|
|
func (h *Handler) ChangeUserData(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Printf("")
|
|
}
|