termbox/internal/handlers/handlers.go

94 lines
3.0 KiB
Go

package handlers
import (
"net/http"
"strings"
"git.keircn.com/keiran/termbox/internal/db"
"git.keircn.com/keiran/termbox/internal/email"
"github.com/labstack/echo/v4"
)
var emailService *email.Service
func InitEmailService() {
emailService = email.NewService()
}
func HandleRoot(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}
func HandleRegister(c echo.Context) error {
var req db.CreateUserRequest
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request body"})
}
if req.Username == "" || req.Email == "" || req.Password == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Username, email, and password are required"})
}
user, err := db.CreateUser(c.Request().Context(), req)
if err != nil {
if strings.Contains(err.Error(), "duplicate") || strings.Contains(err.Error(), "unique") {
return c.JSON(http.StatusConflict, map[string]string{"error": "Username or email already exists"})
}
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to create user: " + err.Error()})
}
verificationCode, err := db.CreateVerificationCode(c.Request().Context(), user.ID)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to create verification code"})
}
err = emailService.SendVerificationCode(c.Request().Context(), user.Email, user.Username, verificationCode.Code)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to send verification email"})
}
return c.JSON(http.StatusCreated, map[string]any{
"message": "User created successfully. Please check your email for verification code.",
"user": user,
})
}
func HandleVerifyCode(c echo.Context) error {
var req db.VerifyCodeRequest
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request body"})
}
if req.Email == "" || req.Code == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Email and code are required"})
}
err := db.VerifyCode(c.Request().Context(), req.Email, req.Code)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}
return c.JSON(http.StatusOK, map[string]string{"message": "Account verified successfully"})
}
func HandleLogin(c echo.Context) error {
var req db.LoginRequest
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request body"})
}
if req.Username == "" || req.Password == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Username and password are required"})
}
user, err := db.ValidateUserCredentials(c.Request().Context(), req.Username, req.Password)
if err != nil {
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "Invalid credentials or account not verified"})
}
return c.JSON(http.StatusOK, map[string]any{
"message": "Login successful",
"user": user,
})
}