דילוג לתוכן

פיתוח ועיצוב אתרים

637 נושאים 5.5k פוסטים

קטגוריות משנה


  • מערכות ניהול תוכן ליצירה ותחזוקה של אתרי אינטרנט

    353 3k
    353 נושאים
    3k פוסטים
    ס
    מתי יחזור האתר להתעדכן? אתר מצויין אך מזמן לא עודכן
  • פיתוח web צד לקוח וצד שרת

    284 2k
    284 נושאים
    2k פוסטים
    ע
    אני צריך לכתוב פונקציה שתקבל מודעות ממסד נתונים על פי פילטרים אופציונליים, הAI הציעו את שלהם וכל הזמן היא מושלמת, ועם זאת כל הזמן יש בה באגים שיכולים להקריס אותה. אשמח לעזרה, הפונקציה קצת מורכבת, יש את הטבלה ה"ראשית", ויש שתי טבלאות מקושרות, 1 ערים ו1 נקרא או לא נקרא. בינתיים התעסקתי רק עם הסינון + ערים, עוד לא הוספתי את הנקרא/לא נקרא, אשמח לעזרה בזה. פונקציות עזר כלליות const schemaAdvertiser = { phone: {validate: isValidIsraeliPhone, required: true}, profession: {validate: checkProfession, required: true}, min_price: {validate: checkNumber, required: false}, path: {validate: (path) => typeof path === 'string' && path.trim() !== '', required: false} }; const schemaLocation = { area: {validate: checkArea, required: false}, city: {validate: checkCity, required: false}, }; function validateParams(params, schema, checkRequired = true) { if (!params || typeof params !== 'object') { return errorResponse("Invalid parameters input"); } const cleanParams = {}; for (const key in schema) { const field = schema[key]; const value = params[key]; if (value === undefined) { if (checkRequired && field.required) { return errorResponse(`Missing required parameter: ${key}`); } continue; } if (!field.validate(value)) { return errorResponse(`Invalid value for ${key}`); } cleanParams[key] = value; } for (const key in params) { if (!schema[key]) { return errorResponse(`Unknown parameter: ${key}`); } } return successResponse(cleanParams); } function validateLocationParams(locationParams) { const locationsArray = Array.isArray(locationParams) ? locationParams : [locationParams]; let cleanLocations = []; for (const loc of locationsArray) { const validatedLocation = validateParams(loc, schemaLocation, false); if (!validatedLocation.success) return validatedLocation; if (validatedLocation.data.city && !validatedLocation.data.area) { return errorResponse("City provided without area"); } const { city, area } = validatedLocation.data; if (city && area && city !== "כל הארץ" && area !== "כל הארץ") { if (!checkCityInArea(city, area)) { return errorResponse("City does not belong to the given area"); } } cleanLocations.push(validatedLocation.data); } return successResponse(cleanLocations); } הפונקציה שלשמה התכנסנו import knex from "knex"; import {areas} from "./lists"; /** * הגדרת שדות מותרים לסינון והאופרטור שלהם */ const FILTER_CONFIG = { phone: '=', profession: '=', min_price: '>=' }; export async function findAdByFilter(filters = {}) { const { path, ...schemaSearch } = schemaAdvertiser; const validated = validateParams(filters, { ...schemaSearch, ...schemaLocation }, false); if (!validated.success) return validated; if (Object.keys(validated.data).length === 0) { return errorResponse("לא סופקו פילטרים חוקיים"); } try { const { city, area, ...activeFilters } = validated.data; const result = await knex("ads as M") .modify(q => { if (city || area) { q.join("advertiser_cities as C", "C.adId", "M.id"); } if (city) { const relatedAreas = Array.isArray(city) ? getAreasForCities(city) : [getCityArea(city)].filter(Boolean); if (Array.isArray(city)) { q.where(function() { this.whereIn("C.city", city) .orWhereIn("C.area", relatedAreas); }); } else { q.where(function() { this.where("C.city", city) .orWhere("C.area", relatedAreas[0]); }); } } else if (area) { Array.isArray(area) ? q.whereIn("C.area", area) : q.where("C.area", area); } Object.entries(FILTER_CONFIG).forEach(([field, operator]) => { const value = activeFilters[field]; if (value !== undefined && value !== null && value !== "") { Array.isArray(value) ? q.whereIn(`M.${field}`, value) : q.where(`M.${field}`, operator, value); } }); }) .select("M.*") .distinct("M.id"); return successResponse({ ads: result }); } catch (error) { console.error("[findAdByFilter] Error:", error); return errorResponse("שגיאה בשליפת המודעה לפי פילטרים"); } } function getCityArea(city) { for (const [area, cities] of Object.entries(areas)) { if (cities.includes(city)) return area; } return null; } function getAreasForCities(citiesArray) { return [...new Set(citiesArray.map(getCityArea).filter(Boolean))]; } הקוד מחולק בכל מיני מקומות, מקווה שצירפתי כל מה שצריך. טבלה ראשית: ID טלפון - phone מקצוע - profession מחיר מינימום - min_price נתיב הקלטה - path סטטוס - status ערים: ID, עיר, אזור. מודעות שנקראו: ID, טלפון לא חייבים להצמד לפונקציה הזו, העיקר שיהיה משהו שיעבוד... הAI כבר עשו לי כאב ראש...