- Add validateCreateJobRequest() checking required fields (companyId, title ≥5 chars, description ≥20 chars) and enum values (employmentType, workMode, status) before hitting the DB - Catch *pq.Error in handler: check_violation (23514) and foreign_key_violation (23503) now return 400; unique_violation (23505) returns 409; other DB errors return 500 without leaking raw pq messages - Fix test fixtures: description and companyId now meet validation requirements Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
350 lines
12 KiB
Go
Executable file
350 lines
12 KiB
Go
Executable file
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/lib/pq"
|
|
"github.com/rede5/gohorsejobs/backend/internal/api/middleware"
|
|
"github.com/rede5/gohorsejobs/backend/internal/dto"
|
|
"github.com/rede5/gohorsejobs/backend/internal/models"
|
|
)
|
|
|
|
// JobServiceInterface describes the service needed by JobHandler
|
|
type JobServiceInterface interface {
|
|
GetJobs(filter dto.JobFilterQuery) ([]models.JobWithCompany, int, error)
|
|
CreateJob(req dto.CreateJobRequest, createdBy string) (*models.Job, error)
|
|
GetJobByID(id string) (*models.Job, error)
|
|
UpdateJob(id string, req dto.UpdateJobRequest) (*models.Job, error)
|
|
DeleteJob(id string) error
|
|
}
|
|
|
|
type JobHandler struct {
|
|
Service JobServiceInterface
|
|
}
|
|
|
|
func NewJobHandler(service JobServiceInterface) *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 string false "Filter by company ID"
|
|
// @Param featured query bool false "Filter by featured status"
|
|
// @Param search query string false "Full-text search query"
|
|
// @Param s query string false "Search query (Careerjet-style)"
|
|
// @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 l query string false "Location (Careerjet-style)"
|
|
// @Param salaryMin query number false "Minimum salary filter"
|
|
// @Param salaryMax query number false "Maximum salary filter"
|
|
// @Param datePosted query string false "Date posted filter (24h, 7d, 30d)"
|
|
// @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 := r.URL.Query().Get("companyId")
|
|
isFeaturedStr := r.URL.Query().Get("featured")
|
|
|
|
// Legacy and New Filter Handling
|
|
search := r.URL.Query().Get("search")
|
|
if search == "" {
|
|
search = r.URL.Query().Get("q")
|
|
}
|
|
if search == "" {
|
|
search = r.URL.Query().Get("s") // Careerjet-style alias (What)
|
|
}
|
|
|
|
employmentType := r.URL.Query().Get("employmentType")
|
|
if employmentType == "" {
|
|
employmentType = r.URL.Query().Get("type")
|
|
}
|
|
|
|
workMode := r.URL.Query().Get("workMode")
|
|
location := r.URL.Query().Get("location")
|
|
if location == "" {
|
|
location = r.URL.Query().Get("l") // Careerjet-style alias (Where)
|
|
}
|
|
salaryMinStr := r.URL.Query().Get("salaryMin")
|
|
salaryMaxStr := r.URL.Query().Get("salaryMax")
|
|
sortBy := r.URL.Query().Get("sortBy")
|
|
sortOrder := r.URL.Query().Get("sortOrder")
|
|
|
|
// Date Posted filter (Careerjet-style: 24h, 7d, 30d)
|
|
datePosted := r.URL.Query().Get("datePosted")
|
|
if datePosted == "" {
|
|
datePosted = r.URL.Query().Get("date") // Alternative alias
|
|
}
|
|
|
|
filter := dto.JobFilterQuery{
|
|
PaginationQuery: dto.PaginationQuery{
|
|
Page: page,
|
|
Limit: limit,
|
|
},
|
|
SortBy: &sortBy,
|
|
SortOrder: &sortOrder,
|
|
}
|
|
|
|
if companyID != "" {
|
|
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.Location = &location
|
|
filter.LocationSearch = &location // Map to both for compatibility
|
|
}
|
|
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
|
|
}
|
|
}
|
|
if datePosted != "" {
|
|
filter.DatePosted = &datePosted
|
|
}
|
|
|
|
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 401 {string} string "Unauthorized"
|
|
// @Failure 500 {string} string "Internal Server Error"
|
|
// @Router /api/v1/jobs [post]
|
|
func (h *JobHandler) CreateJob(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Println("[CREATE_JOB DEBUG] === CreateJob Handler Started ===")
|
|
|
|
var req dto.CreateJobRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
fmt.Printf("[CREATE_JOB ERROR] Failed to decode request body: %v\n", err)
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if err := validateCreateJobRequest(&req); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("[CREATE_JOB DEBUG] Request received: title=%s, companyId=%s, status=%s\n", req.Title, req.CompanyID, req.Status)
|
|
fmt.Printf("[CREATE_JOB DEBUG] Full request: %+v\n", req)
|
|
|
|
// Extract UserID from context
|
|
val := r.Context().Value(middleware.ContextUserID)
|
|
fmt.Printf("[CREATE_JOB DEBUG] Context UserID value: %v (type: %T)\n", val, val)
|
|
|
|
userID, ok := val.(string)
|
|
if !ok || userID == "" {
|
|
fmt.Printf("[CREATE_JOB ERROR] UserID extraction failed. ok=%v, userID='%s'\n", ok, userID)
|
|
http.Error(w, "Unauthorized: User ID missing", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("[CREATE_JOB DEBUG] UserID extracted: %s\n", userID)
|
|
fmt.Println("[CREATE_JOB DEBUG] Calling service.CreateJob...")
|
|
|
|
job, err := h.Service.CreateJob(req, userID)
|
|
if err != nil {
|
|
fmt.Printf("[CREATE_JOB ERROR] Service.CreateJob failed: %v\n", err)
|
|
var pqErr *pq.Error
|
|
if errors.As(err, &pqErr) {
|
|
switch pqErr.Code {
|
|
case "23514": // check_violation
|
|
http.Error(w, fmt.Sprintf("invalid value: %s", pqErr.Constraint), http.StatusBadRequest)
|
|
case "23503": // foreign_key_violation
|
|
http.Error(w, "referenced resource not found", http.StatusBadRequest)
|
|
case "23505": // unique_violation
|
|
http.Error(w, "duplicate entry", http.StatusConflict)
|
|
default:
|
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
|
}
|
|
return
|
|
}
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("[CREATE_JOB DEBUG] Job created successfully! ID=%s\n", job.ID)
|
|
|
|
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 string 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) {
|
|
id := r.PathValue("id")
|
|
|
|
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 string 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) {
|
|
id := r.PathValue("id")
|
|
|
|
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 string 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) {
|
|
id := r.PathValue("id")
|
|
|
|
if err := h.Service.DeleteJob(id); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
var validEmploymentTypes = map[string]bool{
|
|
"full-time": true, "part-time": true, "dispatch": true, "contract": true,
|
|
"temporary": true, "training": true, "voluntary": true, "permanent": true,
|
|
}
|
|
|
|
var validWorkModes = map[string]bool{
|
|
"onsite": true, "hybrid": true, "remote": true,
|
|
}
|
|
|
|
var validJobStatuses = map[string]bool{
|
|
"draft": true, "open": true, "closed": true, "review": true,
|
|
"published": true, "paused": true, "expired": true, "archived": true, "reported": true,
|
|
}
|
|
|
|
func validateCreateJobRequest(req *dto.CreateJobRequest) error {
|
|
if strings.TrimSpace(req.CompanyID) == "" {
|
|
return fmt.Errorf("companyId is required")
|
|
}
|
|
title := strings.TrimSpace(req.Title)
|
|
if title == "" {
|
|
return fmt.Errorf("title is required")
|
|
}
|
|
if len(title) < 5 || len(title) > 255 {
|
|
return fmt.Errorf("title must be between 5 and 255 characters")
|
|
}
|
|
if len(strings.TrimSpace(req.Description)) < 20 {
|
|
return fmt.Errorf("description must be at least 20 characters")
|
|
}
|
|
if req.EmploymentType != nil && !validEmploymentTypes[*req.EmploymentType] {
|
|
return fmt.Errorf("invalid employmentType %q, must be one of: full-time, part-time, dispatch, contract, temporary, training, voluntary, permanent", *req.EmploymentType)
|
|
}
|
|
if req.WorkMode != nil && !validWorkModes[*req.WorkMode] {
|
|
return fmt.Errorf("invalid workMode %q, must be one of: onsite, hybrid, remote", *req.WorkMode)
|
|
}
|
|
if req.Status != "" && !validJobStatuses[req.Status] {
|
|
return fmt.Errorf("invalid status %q", req.Status)
|
|
}
|
|
return nil
|
|
}
|