gohorsejobs/backend/internal/handlers/application_handler.go
Tiago Yamamoto 9ee9f6855c feat: implementar múltiplas features
Backend:
- Password reset flow (forgot/reset endpoints, tokens table)
- Profile management (PUT /users/me, skills, experience, education)
- Tickets system (CRUD, messages, stats)
- Activity logs (list, stats)
- Document validator (CNPJ, CPF, EIN support)
- Input sanitizer (XSS prevention)
- Full-text search em vagas (plainto_tsquery)
- Filtros avançados (location, salary, workMode)
- Ordenação (date, salary, relevance)

Frontend:
- Forgot/Reset password pages
- Candidate profile edit page
- Sanitize utilities (sanitize.ts)

Backoffice:
- TicketsModule proxy
- ActivityLogsModule proxy
- Dockerfile otimizado (multi-stage, non-root, healthcheck)

Migrations:
- 013: Profile fields to users
- 014: Password reset tokens
- 015: Tickets table
- 016: Activity logs table
2025-12-27 11:19:47 -03:00

165 lines
5.3 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"strconv"
"github.com/rede5/gohorsejobs/backend/internal/api/middleware"
"github.com/rede5/gohorsejobs/backend/internal/dto"
"github.com/rede5/gohorsejobs/backend/internal/services"
)
type ApplicationHandler struct {
Service *services.ApplicationService
}
func NewApplicationHandler(service *services.ApplicationService) *ApplicationHandler {
return &ApplicationHandler{Service: service}
}
// CreateApplication submits a new job application
// @Summary Create Application
// @Description Submit a new job application for a posting
// @Tags Applications
// @Accept json
// @Produce json
// @Param application body dto.CreateApplicationRequest true "Application data"
// @Success 201 {object} models.Application
// @Failure 400 {string} string "Bad Request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /api/v1/applications [post]
func (h *ApplicationHandler) CreateApplication(w http.ResponseWriter, r *http.Request) {
var req dto.CreateApplicationRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
app, err := h.Service.CreateApplication(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(app)
}
// GetApplications lists applications for a job
// @Summary List Applications
// @Description List all applications for a job
// @Tags Applications
// @Accept json
// @Produce json
// @Param jobId query int true "Filter applications by job ID"
// @Success 200 {array} models.Application
// @Failure 400 {string} string "Bad Request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /api/v1/applications [get]
func (h *ApplicationHandler) GetApplications(w http.ResponseWriter, r *http.Request) {
// For now, simple get by Job ID query param
jobIDStr := r.URL.Query().Get("jobId")
if jobIDStr == "" {
http.Error(w, "jobId is required", http.StatusBadRequest)
return
}
jobID, err := strconv.Atoi(jobIDStr)
if err != nil {
http.Error(w, "Invalid jobId", http.StatusBadRequest)
return
}
apps, err := h.Service.GetApplications(jobID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(apps)
}
// GetApplicationByID returns a single application by ID
// @Summary Get Application
// @Description Retrieve a job application by its ID
// @Tags Applications
// @Accept json
// @Produce json
// @Param id path int true "Application ID"
// @Success 200 {object} models.Application
// @Failure 400 {string} string "Bad Request"
// @Failure 404 {string} string "Not Found"
// @Router /api/v1/applications/{id} [get]
func (h *ApplicationHandler) GetApplicationByID(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "Invalid application ID", http.StatusBadRequest)
return
}
app, err := h.Service.GetApplicationByID(id)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(app)
}
// UpdateApplicationStatus updates the status of an application
// @Summary Update Application Status
// @Description Update the status of a job application
// @Tags Applications
// @Accept json
// @Produce json
// @Param id path int true "Application ID"
// @Param body body dto.UpdateApplicationStatusRequest true "Status update"
// @Success 200 {object} models.Application
// @Failure 400 {string} string "Bad Request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /api/v1/applications/{id}/status [put]
func (h *ApplicationHandler) UpdateApplicationStatus(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "Invalid application ID", http.StatusBadRequest)
return
}
var req dto.UpdateApplicationStatusRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
app, err := h.Service.UpdateApplicationStatus(id, req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(app)
}
// ListUserApplications lists applications for the logged in user
func (h *ApplicationHandler) ListUserApplications(w http.ResponseWriter, r *http.Request) {
userID, ok := r.Context().Value(middleware.ContextUserID).(string) // Corrected Key
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
apps, err := h.Service.ListUserApplications(userID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(apps)
}