debug(backend): add console logging to job creation endpoint
- Added debug logging to job_handler.go CreateJob - Added debug logging to job_service.go CreateJob - Tracks request data, userID extraction, SQL execution, and errors
This commit is contained in:
parent
362b569c8d
commit
930c57a9c7
2 changed files with 23 additions and 1 deletions
|
|
@ -2,6 +2,7 @@ package handlers
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
|
|
@ -108,28 +109,41 @@ func (h *JobHandler) GetJobs(w http.ResponseWriter, r *http.Request) {
|
|||
// @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
|
||||
}
|
||||
|
||||
// Validate request (omitted for brevity, assume validation middleware or service validation)
|
||||
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)
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,9 @@ func NewJobService(db *sql.DB) *JobService {
|
|||
}
|
||||
|
||||
func (s *JobService) CreateJob(req dto.CreateJobRequest, createdBy string) (*models.Job, error) {
|
||||
fmt.Println("[JOB_SERVICE DEBUG] === CreateJob Started ===")
|
||||
fmt.Printf("[JOB_SERVICE DEBUG] CompanyID=%s, CreatedBy=%s, Title=%s, Status=%s\n", req.CompanyID, createdBy, req.Title, req.Status)
|
||||
|
||||
query := `
|
||||
INSERT INTO jobs (
|
||||
company_id, created_by, title, description, salary_min, salary_max, salary_type,
|
||||
|
|
@ -50,6 +53,9 @@ func (s *JobService) CreateJob(req dto.CreateJobRequest, createdBy string) (*mod
|
|||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
fmt.Println("[JOB_SERVICE DEBUG] Executing INSERT query...")
|
||||
fmt.Printf("[JOB_SERVICE DEBUG] Job struct: %+v\n", job)
|
||||
|
||||
err := s.DB.QueryRow(
|
||||
query,
|
||||
job.CompanyID, job.CreatedBy, job.Title, job.Description, job.SalaryMin, job.SalaryMax, job.SalaryType,
|
||||
|
|
@ -58,9 +64,11 @@ func (s *JobService) CreateJob(req dto.CreateJobRequest, createdBy string) (*mod
|
|||
).Scan(&job.ID, &job.CreatedAt, &job.UpdatedAt)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("[JOB_SERVICE ERROR] INSERT query failed: %v\n", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Printf("[JOB_SERVICE DEBUG] Job created successfully! ID=%s\n", job.ID)
|
||||
return job, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue