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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
|
@ -108,28 +109,41 @@ func (h *JobHandler) GetJobs(w http.ResponseWriter, r *http.Request) {
|
||||||
// @Failure 500 {string} string "Internal Server Error"
|
// @Failure 500 {string} string "Internal Server Error"
|
||||||
// @Router /api/v1/jobs [post]
|
// @Router /api/v1/jobs [post]
|
||||||
func (h *JobHandler) CreateJob(w http.ResponseWriter, r *http.Request) {
|
func (h *JobHandler) CreateJob(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Println("[CREATE_JOB DEBUG] === CreateJob Handler Started ===")
|
||||||
|
|
||||||
var req dto.CreateJobRequest
|
var req dto.CreateJobRequest
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
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)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
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
|
// Extract UserID from context
|
||||||
val := r.Context().Value(middleware.ContextUserID)
|
val := r.Context().Value(middleware.ContextUserID)
|
||||||
|
fmt.Printf("[CREATE_JOB DEBUG] Context UserID value: %v (type: %T)\n", val, val)
|
||||||
|
|
||||||
userID, ok := val.(string)
|
userID, ok := val.(string)
|
||||||
if !ok || userID == "" {
|
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)
|
http.Error(w, "Unauthorized: User ID missing", http.StatusUnauthorized)
|
||||||
return
|
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)
|
job, err := h.Service.CreateJob(req, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Printf("[CREATE_JOB ERROR] Service.CreateJob failed: %v\n", err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Printf("[CREATE_JOB DEBUG] Job created successfully! ID=%s\n", job.ID)
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
json.NewEncoder(w).Encode(job)
|
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) {
|
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 := `
|
query := `
|
||||||
INSERT INTO jobs (
|
INSERT INTO jobs (
|
||||||
company_id, created_by, title, description, salary_min, salary_max, salary_type,
|
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(),
|
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(
|
err := s.DB.QueryRow(
|
||||||
query,
|
query,
|
||||||
job.CompanyID, job.CreatedBy, job.Title, job.Description, job.SalaryMin, job.SalaryMax, job.SalaryType,
|
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)
|
).Scan(&job.ID, &job.CreatedAt, &job.UpdatedAt)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Printf("[JOB_SERVICE ERROR] INSERT query failed: %v\n", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Printf("[JOB_SERVICE DEBUG] Job created successfully! ID=%s\n", job.ID)
|
||||||
return job, nil
|
return job, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue