jobs-monorepo/infrastructure/http/job.go
Elshimy Ziad Magdy Taha 24b5bf1d08 microservices
2025-10-04 17:18:49 +05:00

65 lines
2 KiB
Go

package http
import (
"encoding/json"
"log/slog"
"net/http"
"github.com/jobs-scraper/internal/ports"
"github.com/jobs-scraper/internal/utils"
"github.com/gorilla/mux"
)
// JobHandler handles HTTP requests related to cars
type JobHandler struct {
jobCommands *ports.JobCommands
}
// NewVehicleHandler creates a new instance of VehicleHandler
func NewJobHandler(jobCommands *ports.JobCommands) *JobHandler {
return &JobHandler{
jobCommands: jobCommands,
}
}
// RegisterRoutes registers all routes to the router
func (h *JobHandler) RegisterRoutes(router *mux.Router) {
vehicles := router.PathPrefix("/jobs").Subrouter()
//vehicles.Use(AuthMiddleware)
vehicles.HandleFunc("", utils.Make(h.CreateJob)).Methods("POST")
// vehicles.HandleFunc("", utils.Make(h.ListVehicles)).Methods("GET")
// vehicles.HandleFunc("/{id}", utils.Make(h.GetVehicle)).Methods("GET")
// vehicles.HandleFunc("/buy", utils.Make(h.BuyVehicle)).Methods("POST")
// vehicles.HandleFunc("/sell", utils.Make(h.SellVehicle)).Methods("POST")
}
// CreateJob handles the creation of a new job
// @Summary Create a new job
// @Description Creates a new job with the provided specifications
// @Tags jobs
// @Accept json
// @Produce json
// @Param job body ports.CreateJobCommand true "Job creation data"
// @Success 201 {object} map[string]string "Successfully created job"
// @Failure 400 {object} map[string]string "Invalid request data"
// @Failure 500 {object} map[string]string "Internal server error"
// @Router /jobs [post]
func (h *JobHandler) CreateJob(w http.ResponseWriter, r *http.Request) error {
var cmd ports.CreateJobCommand
if err := json.NewDecoder(r.Body).Decode(&cmd); err != nil {
slog.Error(err.Error())
return utils.NewAPIError(http.StatusBadRequest, utils.InvalidJSON())
}
err := h.jobCommands.CreateJob.Handle(cmd)
if err != nil {
slog.Error(err.Error())
return utils.NewAPIError(http.StatusInternalServerError, err)
}
utils.WriteJSON(w, http.StatusCreated, map[string]string{"message": "Success"})
return nil
}