Cleaning up andcreating mongo configs
This commit is contained in:
parent
50039a3a11
commit
a1ab363349
9
config/mongose.js
Normal file
9
config/mongose.js
Normal file
@ -0,0 +1,9 @@
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const mongoseConfig = {
|
||||
mongodbUri: process.env.MONGODB_URI,
|
||||
};
|
||||
|
||||
export default mongoseConfig;
|
||||
@ -1,25 +1,89 @@
|
||||
<template>
|
||||
<div>
|
||||
<h1>Real Estate Listings</h1>
|
||||
<ul>
|
||||
<li v-for="listing in listings" :key="listing._id">
|
||||
<h2>{{ listing.title }}</h2>
|
||||
<p>Price: {{ listing.price }}</p>
|
||||
<p>Location: {{ listing.location }}</p>
|
||||
<p>Description: {{ listing.description }}</p>
|
||||
<a :href="listing.url">View Listing</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="container mx-auto py-8">
|
||||
<h1 class="text-3xl font-bold mb-8 text-center">Property Listings</h1>
|
||||
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
<Card
|
||||
v-for="property in properties"
|
||||
:key="property.id"
|
||||
:property="property"
|
||||
class="card"
|
||||
>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, onMounted } from 'vue';
|
||||
import { ref, onMounted } from "vue";
|
||||
|
||||
const listings = ref([]);
|
||||
const properties = ref([]);
|
||||
|
||||
onMounted(async () => {
|
||||
// const response = await $fetch('/api/listings');
|
||||
// listings.value = response;
|
||||
const fetchProperties = async () => {
|
||||
try {
|
||||
const response = await fetch("/api/house/parse", {
|
||||
method: "GET",
|
||||
});
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
properties.value = result.data;
|
||||
} else {
|
||||
console.error(result.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error fetching properties:", error);
|
||||
}
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
fetchProperties();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
h1 {
|
||||
font-size: x-large;
|
||||
font-weight: 700;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.card {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
margin: auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: top;
|
||||
align-items: center;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.blur {
|
||||
opacity: 0.6;
|
||||
filter: blur(10px);
|
||||
backdrop-filter: blur(
|
||||
10px
|
||||
); /* Propiedad experimental para algunos navegadores */
|
||||
}
|
||||
|
||||
.card .content {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
img {
|
||||
pointer-events: none;
|
||||
-webkit-user-drag: none;
|
||||
-khtml-user-drag: none;
|
||||
-moz-user-drag: none;
|
||||
-o-user-drag: none;
|
||||
user-drag: none;
|
||||
}
|
||||
|
||||
.card img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
</style>
|
||||
|
||||
@ -20,7 +20,7 @@ const properties = ref([]);
|
||||
|
||||
const fetchProperties = async () => {
|
||||
try {
|
||||
const response = await fetch("/api/house/construe", {
|
||||
const response = await fetch("/api/house/parse", {
|
||||
method: "GET",
|
||||
});
|
||||
const result = await response.json();
|
||||
|
||||
@ -25,12 +25,8 @@ const parse = async () => {
|
||||
|
||||
const initiateParse = async () => {
|
||||
try {
|
||||
const response = await fetch("/api/parse", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ id: "searchtext", title: "Test" }),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
const response = await fetch("/api/house/parse", {
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
@ -1,14 +1,8 @@
|
||||
import { House } from "../../db/mysql/db.config";
|
||||
import { ConstrueHouseCommand } from "../../application/command/house/construeHouseCommand";
|
||||
import { ConstrueHouseCommandHandler } from "../../application/commandHandler/house/construeHouseCommandHandler";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const candidateHouses = await House.findAll();
|
||||
|
||||
const construeHouseCommandHandler = await ConstrueHouseCommandHandler.create(
|
||||
ConstrueHouseCommand.create(candidateHouses)
|
||||
);
|
||||
const construeHouseCommandHandler = await ConstrueHouseCommandHandler.create();
|
||||
|
||||
return { success: true, data: construeHouseCommandHandler };
|
||||
} catch (error) {
|
||||
@ -15,7 +15,7 @@ export class ConstrueHouseCommand {
|
||||
return construeHouseCommand;
|
||||
}
|
||||
|
||||
public rawHouseList() {
|
||||
public get rawHouseList() {
|
||||
return this._rawHouseList;
|
||||
}
|
||||
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
import { Op, sequelize, House as Hou } from "../../../db/mysql/db.config";
|
||||
import { HouseRepository } from "~/server/db/mysql/repository/HouseRepository";
|
||||
import { House } from "../../../domain/house";
|
||||
import type { ConstrueHouseCommand } from "../../command/house/construeHouseCommand";
|
||||
|
||||
export class ConstrueHouseCommandHandler {
|
||||
private constructor() {}
|
||||
public static async create() {
|
||||
const houseRepository = new HouseRepository();
|
||||
const houseList = await houseRepository.findAll();
|
||||
|
||||
public static async create(construeHouseCommand: ConstrueHouseCommand) {
|
||||
const houseCandidates: House[] = [];
|
||||
let i = 0;
|
||||
|
||||
construeHouseCommand.rawHouseList().forEach((houseCandidate: object) => {
|
||||
houseList.forEach((houseCandidate: object) => {
|
||||
houseCandidates.push(
|
||||
House.create(
|
||||
`${++i}`,
|
||||
@ -27,147 +27,11 @@ export class ConstrueHouseCommandHandler {
|
||||
));
|
||||
});
|
||||
|
||||
const proposedHouses = ConstrueHouseCommandHandler.findHouseByTitleAndExactPriceAndRoomsAndBaths(houseCandidates);
|
||||
const proposedHouses = houseRepository.filterHouseByTitleAndExactPriceAndRoomsAndBaths(houseCandidates);
|
||||
|
||||
return proposedHouses;
|
||||
}
|
||||
|
||||
async execute(): Promise<void> {
|
||||
}
|
||||
|
||||
private static async findHouseByTitleAndExactPriceAndRoomsAndBaths(houseCandidates: any): Promise<String[]> {
|
||||
const mergedHouses: any[] = [];
|
||||
|
||||
// Usamos un bucle for normal para manejar bien el await dentro del ciclo
|
||||
for (let i = 0; i < houseCandidates.length; i++) {
|
||||
const houseCandidate = houseCandidates[i];
|
||||
const sanitizedTitle = houseCandidate.title.replace(/'/g, "\\'");
|
||||
|
||||
// Esperamos a que la búsqueda en la base de datos se resuelva
|
||||
const matchingHouses = await Hou.findAll({
|
||||
where: {
|
||||
[Op.and]: [
|
||||
{ price: houseCandidate.rawPrice },
|
||||
{ rooms: houseCandidate.rooms },
|
||||
{ area: houseCandidate.area },
|
||||
{ baths: houseCandidate.baths },
|
||||
sequelize.literal(`MATCH(title) AGAINST('${sanitizedTitle}' IN NATURAL LANGUAGE MODE)`)
|
||||
]
|
||||
}
|
||||
});
|
||||
|
||||
const resultHouse = ConstrueHouseCommandHandler.mergeHouseProperties(matchingHouses);
|
||||
|
||||
// Si hay coincidencias, añadimos los resultados al array de casas encontradas
|
||||
if (matchingHouses.length > 0) {
|
||||
mergedHouses.push(resultHouse);
|
||||
|
||||
// Eliminamos todas las casas que coincidan con los resultados obtenidos
|
||||
houseCandidates = houseCandidates.filter((candidate: any) => {
|
||||
return !matchingHouses.some((matchedHouse: any) => {
|
||||
// Aquí puedes definir los criterios para eliminar la casa original.
|
||||
// Por ejemplo, comparando el ID, precio, habitaciones, etc.
|
||||
return (
|
||||
candidate.rawPrice === matchedHouse.price &&
|
||||
candidate.rooms === matchedHouse.rooms &&
|
||||
candidate.area === matchedHouse.area &&
|
||||
candidate.baths === matchedHouse.baths
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// Como hemos modificado el array, restamos 1 al índice
|
||||
i = -1; // Reiniciamos el índice para volver a iterar el array actualizado
|
||||
}
|
||||
}
|
||||
|
||||
return mergedHouses;
|
||||
|
||||
// const sanitizedTitle = house.title.replace(/'/g, "\\'");
|
||||
|
||||
// const matchingHouses = Hou.findAll({
|
||||
// where: {
|
||||
// [Op.and]: [
|
||||
// {price: house.rawPrice},
|
||||
// {rooms: house.rooms},
|
||||
// {area: house.area},
|
||||
// {baths: house.baths},
|
||||
// sequelize.literal(`MATCH(title) AGAINST('${sanitizedTitle}' IN NATURAL LANGUAGE MODE)`)
|
||||
// ]
|
||||
// }
|
||||
// });
|
||||
|
||||
// return matchingHouses;
|
||||
}
|
||||
|
||||
private static mergeHouseProperties(houses: any[]): any {
|
||||
if (!Array.isArray(houses)) {
|
||||
throw new Error("El parámetro 'houses' debe ser un array.");
|
||||
}
|
||||
|
||||
const mergedHouse = {
|
||||
id: ConstrueHouseCommandHandler.getLongestString(houses.map(h => h.id)) || Math.floor(Math.random() * 100000000).toString().padStart(8, '0'),
|
||||
title: ConstrueHouseCommandHandler.getLongestString(houses.map(h => h.title)),
|
||||
url: ConstrueHouseCommandHandler.joinByComma(houses.map(h => h.url)),
|
||||
neighborhood: ConstrueHouseCommandHandler.getLongestString(houses.map(h => h.neighborhood)),
|
||||
area: houses[0]?.area || 0, // Asumo que el área es la misma en todos los duplicados
|
||||
rooms: houses[0]?.rooms || 0, // Asumo que el número de habitaciones es el mismo
|
||||
price: houses[0]?.price || 0, // Asumo que el precio es el mismo
|
||||
description: ConstrueHouseCommandHandler.getLongestString(houses.map(h => h.description)),
|
||||
pic: ConstrueHouseCommandHandler.joinByComma(houses.map(h => h.pic)),
|
||||
baths: houses[0]?.baths || 0, // Asumo que el número de baños es el mismo
|
||||
level: ConstrueHouseCommandHandler.getLongestString(houses.map(h => h.level)),
|
||||
phone: ConstrueHouseCommandHandler.joinByComma(houses.map(h => h.phone)) // Concatenar los teléfonos
|
||||
};
|
||||
|
||||
return mergedHouse;
|
||||
}
|
||||
|
||||
private static getLongestString(strings: string[]): string {
|
||||
const preSelectedString = strings
|
||||
.filter(str => str && !str.startsWith('<a href=')) // Ignora las cadenas que comienzan con '<a href='
|
||||
.reduce((longest, current) => current && current.length > longest.length ? current : longest, "");
|
||||
|
||||
const alternativeString = strings.reduce((longest, current) => current && current.length > longest.length ? current : longest, "");
|
||||
|
||||
return preSelectedString !== "" ? preSelectedString : ConstrueHouseCommandHandler.parseLinks(alternativeString);
|
||||
}
|
||||
|
||||
private static joinByComma(items: string[]): string {
|
||||
return items.filter(Boolean).join(', ');
|
||||
}
|
||||
|
||||
private static parseLinks(htmlString) {
|
||||
// Regex para identificar las etiquetas <a> con el href y el contenido interno
|
||||
const anchorTagRegex = /<a href="\/comprar\/(.*?)18.*?"[^>]*>(.*?)<\/a>/g;
|
||||
let match;
|
||||
let result = "";
|
||||
|
||||
while ((match = anchorTagRegex.exec(htmlString)) !== null) {
|
||||
let urlPart = match[1]; // La parte que queremos desde el href
|
||||
let innerText = match[2].trim(); // El texto dentro de las etiquetas <a>
|
||||
|
||||
// Si innerText está vacío, tomamos la parte de la URL
|
||||
if (!innerText) {
|
||||
result= ConstrueHouseCommandHandler.snakeToSpaceCase(urlPart);
|
||||
} else {
|
||||
result = innerText;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static snakeToSpaceCase(snakeCaseString) {
|
||||
// Reemplazar tanto los guiones bajos (_) como los guiones medios (-) por espacios
|
||||
let spaceCaseString = snakeCaseString.replace(/[_-]/g, ' ');
|
||||
|
||||
// Capitalizar la primera letra de cada palabra
|
||||
spaceCaseString = spaceCaseString.split(' ').map(word => {
|
||||
return word.charAt(0).toUpperCase() + word.slice(1);
|
||||
}).join(' ');
|
||||
|
||||
return spaceCaseString;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,7 +1,8 @@
|
||||
import mongoose from 'mongoose';
|
||||
import mongoseConfig from '~/config/mongose';
|
||||
|
||||
export default async () => {
|
||||
const config = useRuntimeConfig();
|
||||
const config = mongoseConfig;
|
||||
|
||||
await mongoose.connect(config.mongodbUri, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
|
||||
|
||||
@ -1,20 +1,20 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
const propertySchema = new mongoose.Schema({
|
||||
const houseSchema = new mongoose.Schema({
|
||||
id: { type: String, required: true, unique: true },
|
||||
title: { type: String, required: true },
|
||||
url: { type: String, required: true },
|
||||
price: { type: String, required: true },
|
||||
rooms: { type: Number, required: true },
|
||||
neighborhood: { type: String, required: true },
|
||||
area: { type: Number, required: true },
|
||||
level: { type: String, required: true },
|
||||
rooms: { type: Number, required: true },
|
||||
price: { type: Number, required: true },
|
||||
description: { type: String },
|
||||
pic: { type: String },
|
||||
baths: { type: Number, required: true },
|
||||
neighborhood: { type: String, required: true },
|
||||
level: { type: String, required: true },
|
||||
phone: { type: String, required: true },
|
||||
});
|
||||
|
||||
const Property = mongoose.model("Property", propertySchema);
|
||||
const House = mongoose.model("House", houseSchema);
|
||||
|
||||
export default Property;
|
||||
export default House;
|
||||
@ -1,18 +1,118 @@
|
||||
import { sequelize, Op, House } from "../db.config";
|
||||
import { Op, sequelize, House } from "../../../db/mysql/db.config";
|
||||
|
||||
export async function findHouseByTitleAndExactPriceAndRoomsAndBaths(house: any) {
|
||||
//const sanitizedTitle = house.title.replace(/'/g, "\\'");
|
||||
export class HouseRepository {
|
||||
|
||||
public async findAll(): Promise<string[]> {
|
||||
return await House.findAll();
|
||||
}
|
||||
|
||||
return "pollas";
|
||||
public async filterHouseByTitleAndExactPriceAndRoomsAndBaths(houseCandidates: any): Promise<string[]> {
|
||||
const mergedHouses: any[] = [];
|
||||
|
||||
const matchingHouses = await House.findAll({
|
||||
where: {
|
||||
price: house.price,
|
||||
rooms: house.rooms,
|
||||
baths: house.baths,
|
||||
[Op.and]: sequelize.literal(`MATCH(title) AGAINST('${sanitizedTitle}' IN NATURAL LANGUAGE MODE)`)
|
||||
for (let i = 0; i < houseCandidates.length; i++) {
|
||||
const houseCandidate = houseCandidates[i];
|
||||
const sanitizedTitle = houseCandidate.title.replace(/'/g, "\\'");
|
||||
|
||||
const matchingHouses = await House.findAll({
|
||||
where: {
|
||||
[Op.and]: [
|
||||
{ price: houseCandidate.rawPrice },
|
||||
{ rooms: houseCandidate.rooms },
|
||||
{ area: houseCandidate.area },
|
||||
{ baths: houseCandidate.baths },
|
||||
sequelize.literal(`MATCH(title) AGAINST('${sanitizedTitle}' IN NATURAL LANGUAGE MODE)`)
|
||||
]
|
||||
}
|
||||
});
|
||||
|
||||
const resultHouse = this.mergeHouseProperties(matchingHouses);
|
||||
|
||||
if (matchingHouses.length > 0) {
|
||||
mergedHouses.push(resultHouse);
|
||||
|
||||
houseCandidates = houseCandidates.filter((candidate: any) => {
|
||||
return !matchingHouses.some((matchedHouse: any) => {
|
||||
return (
|
||||
candidate.rawPrice === matchedHouse.price &&
|
||||
candidate.rooms === matchedHouse.rooms &&
|
||||
candidate.area === matchedHouse.area &&
|
||||
candidate.baths === matchedHouse.baths
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
i = -1;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return mergedHouses;
|
||||
}
|
||||
|
||||
private mergeHouseProperties(houses: any[]): any {
|
||||
if (!Array.isArray(houses)) {
|
||||
throw new Error("El parámetro 'houses' debe ser un array.");
|
||||
}
|
||||
|
||||
const mergedHouse = {
|
||||
//id: this.getLongestString(houses.map(h => h.id)) || Math.floor(Math.random() * 100000000).toString().padStart(8, '0'),
|
||||
title: this.getLongestString(houses.map(h => h.title)),
|
||||
url: this.joinByComma(houses.map(h => h.url)),
|
||||
neighborhood: this.getLongestString(houses.map(h => h.neighborhood)),
|
||||
area: houses[0]?.area || 0,
|
||||
rooms: houses[0]?.rooms || 0,
|
||||
price: houses[0]?.price || 0,
|
||||
description: this.getLongestString(houses.map(h => h.description)),
|
||||
pic: this.joinByComma(houses.map(h => h.pic)),
|
||||
baths: houses[0]?.baths || 0,
|
||||
level: this.getLongestString(houses.map(h => h.level)),
|
||||
phone: this.joinByComma(houses.map(h => h.phone))
|
||||
};
|
||||
|
||||
return mergedHouse;
|
||||
}
|
||||
|
||||
private getLongestString(strings: string[]): string {
|
||||
const preSelectedString = strings
|
||||
.filter(str => str && !str.startsWith('<a href='))
|
||||
.reduce((longest, current) => current && current.length > longest.length ? current : longest, "");
|
||||
|
||||
const alternativeString = strings.reduce((longest, current) => current && current.length > longest.length ? current : longest, "");
|
||||
|
||||
return preSelectedString !== "" ? preSelectedString : this.parseLinks(alternativeString);
|
||||
}
|
||||
|
||||
private joinByComma(items: string[]): string {
|
||||
return items.filter(Boolean).join(', ');
|
||||
}
|
||||
|
||||
private parseLinks(htmlString: string): string {
|
||||
const anchorTagRegex = /<a href="\/comprar\/(.*?)18.*?"[^>]*>(.*?)<\/a>/g;
|
||||
let match;
|
||||
let result = "";
|
||||
|
||||
while ((match = anchorTagRegex.exec(htmlString)) !== null) {
|
||||
const urlPart = match[1];
|
||||
const innerText = match[2].trim();
|
||||
|
||||
if (!innerText) {
|
||||
result= this.snakeToSpaceCase(urlPart);
|
||||
} else {
|
||||
result = innerText;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private snakeToSpaceCase(snakeCaseString: string): string {
|
||||
let spaceCaseString = snakeCaseString.replace(/[_-]/g, ' ');
|
||||
|
||||
spaceCaseString = spaceCaseString.split(' ').map(word => {
|
||||
return word.charAt(0).toUpperCase() + word.slice(1);
|
||||
}).join(' ');
|
||||
|
||||
return spaceCaseString;
|
||||
}
|
||||
|
||||
|
||||
return matchingHouses;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user