This commit is contained in:
79
internal/delivery/http/handlers/auth_handler.go
Normal file
79
internal/delivery/http/handlers/auth_handler.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"counter/internal/domain/entities"
|
||||
"counter/internal/usecase/auth"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AuthHandler handles authentication HTTP requests
|
||||
type AuthHandler struct {
|
||||
authService *auth.AuthService
|
||||
}
|
||||
|
||||
// NewAuthHandler creates a new authentication handler
|
||||
func NewAuthHandler(authService *auth.AuthService) *AuthHandler {
|
||||
return &AuthHandler{
|
||||
authService: authService,
|
||||
}
|
||||
}
|
||||
|
||||
// Register handles user registration
|
||||
func (h *AuthHandler) Register(c *gin.Context) {
|
||||
var req auth.RegisterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
response, err := h.authService.Register(c.Request.Context(), &req)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrUserAlreadyExists:
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Username or email already exists"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create user"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, response)
|
||||
}
|
||||
|
||||
// Login handles user login
|
||||
func (h *AuthHandler) Login(c *gin.Context) {
|
||||
var req auth.LoginRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
response, err := h.authService.Login(c.Request.Context(), &req)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrInvalidCredentials:
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Authentication failed"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// GetMe returns the current authenticated user
|
||||
func (h *AuthHandler) GetMe(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
|
||||
user, err := h.authService.GetCurrentUser(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get user"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
228
internal/delivery/http/handlers/counter_handler.go
Normal file
228
internal/delivery/http/handlers/counter_handler.go
Normal file
@@ -0,0 +1,228 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"counter/internal/domain/entities"
|
||||
"counter/internal/usecase/counter"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// CounterHandler handles counter HTTP requests
|
||||
type CounterHandler struct {
|
||||
counterService *counter.CounterService
|
||||
}
|
||||
|
||||
// NewCounterHandler creates a new counter handler
|
||||
func NewCounterHandler(counterService *counter.CounterService) *CounterHandler {
|
||||
return &CounterHandler{
|
||||
counterService: counterService,
|
||||
}
|
||||
}
|
||||
|
||||
// Create handles counter creation
|
||||
func (h *CounterHandler) Create(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
|
||||
var req counter.CreateCounterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
counter, err := h.counterService.Create(c.Request.Context(), userID, &req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create counter"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, counter)
|
||||
}
|
||||
|
||||
// Get handles retrieving a specific counter
|
||||
func (h *CounterHandler) Get(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
counterID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
|
||||
return
|
||||
}
|
||||
|
||||
counter, err := h.counterService.Get(c.Request.Context(), counterID, userID)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrCounterNotFound:
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch counter"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, counter)
|
||||
}
|
||||
|
||||
// List handles retrieving all counters for a user
|
||||
func (h *CounterHandler) List(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
search := c.Query("search")
|
||||
|
||||
counters, err := h.counterService.List(c.Request.Context(), userID, search)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch counters"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, counters)
|
||||
}
|
||||
|
||||
// Update handles counter updates
|
||||
func (h *CounterHandler) Update(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
counterID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
|
||||
return
|
||||
}
|
||||
|
||||
var req counter.UpdateCounterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
counter, err := h.counterService.Update(c.Request.Context(), counterID, userID, &req)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrCounterNotFound:
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update counter"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, counter)
|
||||
}
|
||||
|
||||
// Delete handles counter deletion
|
||||
func (h *CounterHandler) Delete(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
counterID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
|
||||
return
|
||||
}
|
||||
|
||||
err = h.counterService.Delete(c.Request.Context(), counterID, userID)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrCounterNotFound:
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete counter"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Counter deleted successfully"})
|
||||
}
|
||||
|
||||
// Increment handles counter increment/decrement
|
||||
func (h *CounterHandler) Increment(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
counterID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
|
||||
return
|
||||
}
|
||||
|
||||
var req counter.IncrementRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
entry, err := h.counterService.Increment(c.Request.Context(), counterID, userID, &req)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrCounterNotFound:
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create counter entry"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, entry)
|
||||
}
|
||||
|
||||
// GetEntries handles retrieving counter entries
|
||||
func (h *CounterHandler) GetEntries(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
counterID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
|
||||
return
|
||||
}
|
||||
|
||||
// Parse date range parameters
|
||||
var startDate, endDate *time.Time
|
||||
if startDateStr := c.Query("start_date"); startDateStr != "" {
|
||||
if parsed, err := time.Parse("2006-01-02", startDateStr); err == nil {
|
||||
startDate = &parsed
|
||||
}
|
||||
}
|
||||
if endDateStr := c.Query("end_date"); endDateStr != "" {
|
||||
if parsed, err := time.Parse("2006-01-02", endDateStr); err == nil {
|
||||
endDate = &parsed
|
||||
}
|
||||
}
|
||||
|
||||
entries, err := h.counterService.GetEntries(c.Request.Context(), counterID, userID, startDate, endDate)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrCounterNotFound:
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch counter entries"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, entries)
|
||||
}
|
||||
|
||||
// GetStats handles retrieving counter statistics
|
||||
func (h *CounterHandler) GetStats(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
counterID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
|
||||
return
|
||||
}
|
||||
|
||||
// Parse days parameter
|
||||
days := 30 // default
|
||||
if daysStr := c.Query("days"); daysStr != "" {
|
||||
if parsed, err := strconv.Atoi(daysStr); err == nil && parsed > 0 {
|
||||
days = parsed
|
||||
}
|
||||
}
|
||||
|
||||
stats, err := h.counterService.GetStats(c.Request.Context(), counterID, userID, days)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrCounterNotFound:
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch statistics"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"daily_stats": stats})
|
||||
}
|
||||
Reference in New Issue
Block a user