Skip to content

Commit

Permalink
handlers for user
Browse files Browse the repository at this point in the history
  • Loading branch information
Prrromanssss committed Apr 18, 2024
1 parent 96a1300 commit d52f6db
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/cmd/orchestrator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func main() {

v1Router.Use(middleware.RequestID)
v1Router.Use(mwlogger.New(log))
v1Router.Use(middleware.URLFormat)

// Expression endpoints
v1Router.Post("/expressions", handlers.HandlerCreateExpression(
Expand Down
89 changes: 89 additions & 0 deletions backend/internal/http-server/handlers/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package handlers

import (
"encoding/json"
"fmt"
"log/slog"
"net/http"

daeev1 "github.com/Prrromanssss/DAEE-fullstack/internal/protos/gen/go/daee"
"github.com/Prrromanssss/DAEE-fullstack/internal/storage"
)

// HandlerLoginUser is a http.Handler to login user.
func HandlerLoginUser(
log *slog.Logger,
dbCfg *storage.Storage,
grpcClient daeev1.AuthClient,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
const fn = "handlers.HandlerLoginUser"

log := log.With(
slog.String("fn", fn),
)

type parametrs struct {
Email string `json:"email"`
Password string `json:"password"`
}

decoder := json.NewDecoder(r.Body)
params := parametrs{}
err := decoder.Decode(&params)
if err != nil {
respondWithError(log, w, 400, fmt.Sprintf("error parsing JSON: %v", err))
return
}

loginResponse, err := grpcClient.Login(r.Context(), &daeev1.LoginRequest{
Email: params.Email,
Password: params.Password,
})
if err != nil {
respondWithError(log, w, 400, fmt.Sprintf("can't login user: %v", err))
return
}

respondWithJson(log, w, 200, loginResponse)
}
}

// HandlerRegisterNewUser is a http.Handler to register new user.
func HandlerRegisterNewUser(
log *slog.Logger,
dbCfg *storage.Storage,
grpcClient daeev1.AuthClient,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
const fn = "handlers.HandlerLoginUser"

log := log.With(
slog.String("fn", fn),
)

type parametrs struct {
Email string `json:"email"`
Password string `json:"password"`
}

decoder := json.NewDecoder(r.Body)
params := parametrs{}
err := decoder.Decode(&params)
if err != nil {
respondWithError(log, w, 400, fmt.Sprintf("error parsing JSON: %v", err))
return
}

registerResponse, err := grpcClient.Register(r.Context(), &daeev1.RegisterRequest{
Email: params.Email,
Password: params.Password,
})
if err != nil {
respondWithError(log, w, 400, fmt.Sprintf("can't register new user: %v", err))
return
}

respondWithJson(log, w, 200, registerResponse)
}
}

0 comments on commit d52f6db

Please sign in to comment.