- Video Interview system (backend + frontend) - Date Posted filter (24h, 7d, 30d) - Company filter in jobs listing - Recent searches persistence (LocalStorage) - Job Alerts with email confirmation - Favorite jobs with API - Company followers system - Careerjet URL compatibility (s/l aliases)
99 lines
2.8 KiB
Go
99 lines
2.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/rede5/gohorsejobs/backend/internal/api/middleware"
|
|
"github.com/rede5/gohorsejobs/backend/internal/models"
|
|
)
|
|
|
|
type FavoriteJobServiceInterface interface {
|
|
AddFavorite(userID, jobID string) (*models.FavoriteJob, error)
|
|
RemoveFavorite(userID, jobID string) error
|
|
GetFavorites(userID string) ([]models.FavoriteJobWithDetails, error)
|
|
IsFavorite(userID, jobID string) (bool, error)
|
|
}
|
|
|
|
type FavoriteJobHandler struct {
|
|
Service FavoriteJobServiceInterface
|
|
}
|
|
|
|
func NewFavoriteJobHandler(service FavoriteJobServiceInterface) *FavoriteJobHandler {
|
|
return &FavoriteJobHandler{Service: service}
|
|
}
|
|
|
|
func (h *FavoriteJobHandler) AddFavorite(w http.ResponseWriter, r *http.Request) {
|
|
userID, ok := r.Context().Value(middleware.ContextUserID).(string)
|
|
if !ok || userID == "" {
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
jobID := strings.TrimPrefix(r.URL.Path, "/api/v1/favorites/")
|
|
|
|
favorite, err := h.Service.AddFavorite(userID, jobID)
|
|
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(favorite)
|
|
}
|
|
|
|
func (h *FavoriteJobHandler) RemoveFavorite(w http.ResponseWriter, r *http.Request) {
|
|
userID, ok := r.Context().Value(middleware.ContextUserID).(string)
|
|
if !ok || userID == "" {
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
jobID := strings.TrimPrefix(r.URL.Path, "/api/v1/favorites/")
|
|
|
|
err := h.Service.RemoveFavorite(userID, jobID)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *FavoriteJobHandler) GetMyFavorites(w http.ResponseWriter, r *http.Request) {
|
|
userID, ok := r.Context().Value(middleware.ContextUserID).(string)
|
|
if !ok || userID == "" {
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
favorites, err := h.Service.GetFavorites(userID)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(favorites)
|
|
}
|
|
|
|
func (h *FavoriteJobHandler) CheckFavorite(w http.ResponseWriter, r *http.Request) {
|
|
userID, ok := r.Context().Value(middleware.ContextUserID).(string)
|
|
if !ok || userID == "" {
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
jobID := strings.TrimPrefix(r.URL.Path, "/api/v1/favorites/")
|
|
|
|
isFavorite, err := h.Service.IsFavorite(userID, jobID)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]bool{"isFavorite": isFavorite})
|
|
}
|