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
249 lines
7.3 KiB
Go
Executable file
249 lines
7.3 KiB
Go
Executable file
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/rede5/gohorsejobs/backend/internal/dto"
|
|
"github.com/rede5/gohorsejobs/backend/internal/models"
|
|
"github.com/rede5/gohorsejobs/backend/internal/services"
|
|
)
|
|
|
|
// swaggerTypes ensures swagger can resolve referenced response models.
|
|
var (
|
|
_ models.Job
|
|
_ models.JobWithCompany
|
|
)
|
|
|
|
type JobHandler struct {
|
|
Service *services.JobService
|
|
}
|
|
|
|
func NewJobHandler(service *services.JobService) *JobHandler {
|
|
return &JobHandler{Service: service}
|
|
}
|
|
|
|
// GetJobs godoc
|
|
// @Summary List all jobs
|
|
// @Description Get a paginated list of job postings with optional filters
|
|
// @Tags Jobs
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param page query int false "Page number (default: 1)"
|
|
// @Param limit query int false "Items per page (default: 10, max: 100)"
|
|
// @Param companyId query int false "Filter by company ID"
|
|
// @Param featured query bool false "Filter by featured status"
|
|
// @Param search query string false "Full-text search query"
|
|
// @Param employmentType query string false "Filter by employment type"
|
|
// @Param workMode query string false "Filter by work mode (onsite, hybrid, remote)"
|
|
// @Param location query string false "Filter by location text"
|
|
// @Param salaryMin query number false "Minimum salary filter"
|
|
// @Param salaryMax query number false "Maximum salary filter"
|
|
// @Param sortBy query string false "Sort by: date, salary, relevance"
|
|
// @Param sortOrder query string false "Sort order: asc, desc"
|
|
// @Success 200 {object} dto.PaginatedResponse
|
|
// @Failure 500 {string} string "Internal Server Error"
|
|
// @Router /api/v1/jobs [get]
|
|
func (h *JobHandler) GetJobs(w http.ResponseWriter, r *http.Request) {
|
|
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
|
|
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
|
companyID, _ := strconv.Atoi(r.URL.Query().Get("companyId"))
|
|
isFeaturedStr := r.URL.Query().Get("featured")
|
|
search := r.URL.Query().Get("search")
|
|
employmentType := r.URL.Query().Get("employmentType")
|
|
workMode := r.URL.Query().Get("workMode")
|
|
location := r.URL.Query().Get("location")
|
|
salaryMinStr := r.URL.Query().Get("salaryMin")
|
|
salaryMaxStr := r.URL.Query().Get("salaryMax")
|
|
sortBy := r.URL.Query().Get("sortBy")
|
|
sortOrder := r.URL.Query().Get("sortOrder")
|
|
|
|
filter := dto.JobFilterQuery{
|
|
PaginationQuery: dto.PaginationQuery{
|
|
Page: page,
|
|
Limit: limit,
|
|
},
|
|
SortBy: sortBy,
|
|
SortOrder: sortOrder,
|
|
}
|
|
|
|
if companyID > 0 {
|
|
filter.CompanyID = &companyID
|
|
}
|
|
if isFeaturedStr == "true" {
|
|
val := true
|
|
filter.IsFeatured = &val
|
|
}
|
|
if search != "" {
|
|
filter.Search = &search
|
|
}
|
|
if employmentType != "" {
|
|
filter.EmploymentType = &employmentType
|
|
}
|
|
if workMode != "" {
|
|
filter.WorkMode = &workMode
|
|
}
|
|
if location != "" {
|
|
filter.LocationSearch = &location
|
|
}
|
|
if salaryMinStr != "" {
|
|
if val, err := strconv.ParseFloat(salaryMinStr, 64); err == nil {
|
|
filter.SalaryMin = &val
|
|
}
|
|
}
|
|
if salaryMaxStr != "" {
|
|
if val, err := strconv.ParseFloat(salaryMaxStr, 64); err == nil {
|
|
filter.SalaryMax = &val
|
|
}
|
|
}
|
|
|
|
jobs, total, err := h.Service.GetJobs(filter)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if page == 0 {
|
|
page = 1
|
|
}
|
|
if limit == 0 {
|
|
limit = 10
|
|
}
|
|
|
|
response := dto.PaginatedResponse{
|
|
Data: jobs,
|
|
Pagination: dto.Pagination{
|
|
Page: page,
|
|
Limit: limit,
|
|
Total: total,
|
|
},
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
// CreateJob godoc
|
|
// @Summary Create a new job
|
|
// @Description Create a new job posting
|
|
// @Tags Jobs
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param job body dto.CreateJobRequest true "Job data"
|
|
// @Success 201 {object} models.Job
|
|
// @Failure 400 {string} string "Bad Request"
|
|
// @Failure 500 {string} string "Internal Server Error"
|
|
// @Router /api/v1/jobs [post]
|
|
func (h *JobHandler) CreateJob(w http.ResponseWriter, r *http.Request) {
|
|
var req dto.CreateJobRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Validate request (omitted for brevity, assume validation middleware or service validation)
|
|
|
|
job, err := h.Service.CreateJob(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(job)
|
|
}
|
|
|
|
// GetJobByID godoc
|
|
// @Summary Get job by ID
|
|
// @Description Get a single job posting by its ID
|
|
// @Tags Jobs
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Job ID"
|
|
// @Success 200 {object} models.Job
|
|
// @Failure 400 {string} string "Bad Request"
|
|
// @Failure 404 {string} string "Not Found"
|
|
// @Router /api/v1/jobs/{id} [get]
|
|
func (h *JobHandler) GetJobByID(w http.ResponseWriter, r *http.Request) {
|
|
idStr := r.PathValue("id") // Go 1.22+ routing
|
|
|
|
id, err := strconv.Atoi(idStr)
|
|
if err != nil {
|
|
http.Error(w, "Invalid job ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
job, err := h.Service.GetJobByID(id)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(job)
|
|
}
|
|
|
|
// UpdateJob godoc
|
|
// @Summary Update a job
|
|
// @Description Update an existing job posting
|
|
// @Tags Jobs
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Job ID"
|
|
// @Param job body dto.UpdateJobRequest true "Updated job data"
|
|
// @Success 200 {object} models.Job
|
|
// @Failure 400 {string} string "Bad Request"
|
|
// @Failure 500 {string} string "Internal Server Error"
|
|
// @Router /api/v1/jobs/{id} [put]
|
|
func (h *JobHandler) UpdateJob(w http.ResponseWriter, r *http.Request) {
|
|
idStr := r.PathValue("id")
|
|
id, err := strconv.Atoi(idStr)
|
|
if err != nil {
|
|
http.Error(w, "Invalid job ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var req dto.UpdateJobRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
job, err := h.Service.UpdateJob(id, req)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(job)
|
|
}
|
|
|
|
// DeleteJob godoc
|
|
// @Summary Delete a job
|
|
// @Description Delete a job posting
|
|
// @Tags Jobs
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Job ID"
|
|
// @Success 204 "No Content"
|
|
// @Failure 400 {string} string "Bad Request"
|
|
// @Failure 500 {string} string "Internal Server Error"
|
|
// @Router /api/v1/jobs/{id} [delete]
|
|
func (h *JobHandler) DeleteJob(w http.ResponseWriter, r *http.Request) {
|
|
idStr := r.PathValue("id")
|
|
id, err := strconv.Atoi(idStr)
|
|
if err != nil {
|
|
http.Error(w, "Invalid job ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if err := h.Service.DeleteJob(id); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|