feat: Add authentication DTOs and setup API routes for user and activity management

This commit is contained in:
2025-03-10 21:02:41 +00:00
parent aa5c7e77fc
commit 558ee70c21
10 changed files with 1043 additions and 17 deletions
+85
View File
@@ -0,0 +1,85 @@
package utils
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Response is a standardized API response structure
type Response struct {
Success bool `json:"success"`
Data interface{} `json:"data,omitempty"`
Error *ErrorInfo `json:"error,omitempty"`
}
// ErrorInfo contains detailed error information
type ErrorInfo struct {
Code string `json:"code"`
Message string `json:"message"`
}
// ErrorResponse codes
const (
ErrorCodeValidation = "VALIDATION_ERROR"
ErrorCodeNotFound = "NOT_FOUND"
ErrorCodeUnauthorized = "UNAUTHORIZED"
ErrorCodeForbidden = "FORBIDDEN"
ErrorCodeInternal = "INTERNAL_ERROR"
ErrorCodeBadRequest = "BAD_REQUEST"
ErrorCodeConflict = "CONFLICT"
)
// SuccessResponse sends a successful response with data
func SuccessResponse(c *gin.Context, statusCode int, data interface{}) {
c.JSON(statusCode, Response{
Success: true,
Data: data,
})
}
// ErrorResponse sends an error response
func ErrorResponse(c *gin.Context, statusCode int, errorCode string, message string) {
c.JSON(statusCode, Response{
Success: false,
Error: &ErrorInfo{
Code: errorCode,
Message: message,
},
})
}
// BadRequestResponse sends a 400 Bad Request response
func BadRequestResponse(c *gin.Context, message string) {
ErrorResponse(c, http.StatusBadRequest, ErrorCodeBadRequest, message)
}
// ValidationErrorResponse sends a 400 Bad Request response for validation errors
func ValidationErrorResponse(c *gin.Context, message string) {
ErrorResponse(c, http.StatusBadRequest, ErrorCodeValidation, message)
}
// NotFoundResponse sends a 404 Not Found response
func NotFoundResponse(c *gin.Context, message string) {
ErrorResponse(c, http.StatusNotFound, ErrorCodeNotFound, message)
}
// UnauthorizedResponse sends a 401 Unauthorized response
func UnauthorizedResponse(c *gin.Context, message string) {
ErrorResponse(c, http.StatusUnauthorized, ErrorCodeUnauthorized, message)
}
// ForbiddenResponse sends a 403 Forbidden response
func ForbiddenResponse(c *gin.Context, message string) {
ErrorResponse(c, http.StatusForbidden, ErrorCodeForbidden, message)
}
// InternalErrorResponse sends a 500 Internal Server Error response
func InternalErrorResponse(c *gin.Context, message string) {
ErrorResponse(c, http.StatusInternalServerError, ErrorCodeInternal, message)
}
// ConflictResponse sends a 409 Conflict response
func ConflictResponse(c *gin.Context, message string) {
ErrorResponse(c, http.StatusConflict, ErrorCodeConflict, message)
}
+71
View File
@@ -0,0 +1,71 @@
package utils
// This file contains type definitions for Swagger documentation
// SwaggerULID is a string representation of ULID for Swagger
type SwaggerULID string
// SwaggerTime is a string representation of time.Time for Swagger
type SwaggerTime string
// ActivityResponse is a Swagger representation of ActivityDto
type ActivityResponse struct {
ID SwaggerULID `json:"id" example:"01H1VECTJQXS1RVWJT6QG3QJCJ"`
CreatedAt SwaggerTime `json:"createdAt" example:"2023-01-01T12:00:00Z"`
UpdatedAt SwaggerTime `json:"updatedAt" example:"2023-01-01T12:00:00Z"`
Name string `json:"name" example:"Development"`
BillingRate float64 `json:"billingRate" example:"100.0"`
}
// UserResponse is a Swagger representation of UserDto
type UserResponse struct {
ID SwaggerULID `json:"id" example:"01H1VECTJQXS1RVWJT6QG3QJCJ"`
CreatedAt SwaggerTime `json:"createdAt" example:"2023-01-01T12:00:00Z"`
UpdatedAt SwaggerTime `json:"updatedAt" example:"2023-01-01T12:00:00Z"`
Email string `json:"email" example:"user@example.com"`
Role string `json:"role" example:"admin"`
CompanyID int `json:"companyId" example:"1"`
HourlyRate float64 `json:"hourlyRate" example:"50.0"`
}
// ActivityCreateRequest is a Swagger representation of ActivityCreateDto
type ActivityCreateRequest struct {
Name string `json:"name" example:"Development"`
BillingRate float64 `json:"billingRate" example:"100.0"`
}
// ActivityUpdateRequest is a Swagger representation of ActivityUpdateDto
type ActivityUpdateRequest struct {
Name *string `json:"name,omitempty" example:"Development"`
BillingRate *float64 `json:"billingRate,omitempty" example:"100.0"`
}
// UserCreateRequest is a Swagger representation of UserCreateDto
type UserCreateRequest struct {
Email string `json:"email" example:"user@example.com"`
Password string `json:"password" example:"SecurePassword123!"`
Role string `json:"role" example:"admin"`
CompanyID int `json:"companyId" example:"1"`
HourlyRate float64 `json:"hourlyRate" example:"50.0"`
}
// UserUpdateRequest is a Swagger representation of UserUpdateDto
type UserUpdateRequest struct {
Email *string `json:"email,omitempty" example:"user@example.com"`
Password *string `json:"password,omitempty" example:"SecurePassword123!"`
Role *string `json:"role,omitempty" example:"admin"`
CompanyID *int `json:"companyId,omitempty" example:"1"`
HourlyRate *float64 `json:"hourlyRate,omitempty" example:"50.0"`
}
// LoginRequest is a Swagger representation of LoginDto
type LoginRequest struct {
Email string `json:"email" example:"user@example.com"`
Password string `json:"password" example:"SecurePassword123!"`
}
// TokenResponse is a Swagger representation of TokenDto
type TokenResponse struct {
Token string `json:"token" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."`
User UserResponse `json:"user"`
}