FloorVisualizer/internal/handler/auth_handler.go
dindang cb7e595c27 Fix lint warnings and code cleanup
- Remove duplicate calc structs, use model.CalcXxx
- Replace log.Printf with logger in recommend handler
- Remove dead code (unused var, dummy imports)
- Clean up response helpers
- Add api/ package with route registration

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-16 10:29:11 +08:00

50 lines
1.2 KiB
Go

package handler
import (
"database/sql"
"encoding/json"
"net/http"
"floorvisualizer/internal/service"
)
func Register(authSvc *service.AuthService, db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeErr(w, 405, "Method not allowed")
return
}
var input service.RegisterInput
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
writeJSON(w, 400, map[string]string{"error": "invalid request body"})
return
}
resp, err := authSvc.Register(db, input)
if err != nil {
writeJSON(w, 400, map[string]string{"error": err.Error()})
return
}
writeJSON(w, 201, resp)
}
}
func Login(authSvc *service.AuthService, db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeErr(w, 405, "Method not allowed")
return
}
var input service.LoginInput
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
writeJSON(w, 400, map[string]string{"error": "invalid request body"})
return
}
resp, err := authSvc.Login(db, input)
if err != nil {
writeJSON(w, 401, map[string]string{"error": err.Error()})
return
}
writeJSON(w, 200, resp)
}
}