Skip to content

Commit

Permalink
arreglos
Browse files Browse the repository at this point in the history
  • Loading branch information
Jviejo committed Nov 30, 2024
1 parent e7e48b6 commit 2d48d62
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MONGODB_URI=mongodb://localhost:27017
MONGODB_DB=casa
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

# environment variables
.env
30 changes: 17 additions & 13 deletions src/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,58 @@
'use server'

import { connectToDatabase } from '@/lib/mongodb';
import { connectToDatabase, getDbName } from '@/lib/mongodb';
import { House } from '@/types/house';
import { ObjectId } from 'mongodb';

export async function getHouses() {
export async function getHouses(): Promise<House[]> {
const client = await connectToDatabase();
const collection = client.db('casa').collection('houses');
return collection.find({}).toArray();
const collection = client.db(getDbName()).collection('houses');
const houses = await collection.find({}).toArray();
return houses as unknown as House[];
}

export async function addHouse(house: House) {
const client = await connectToDatabase();
const collection = client.db('casa').collection('houses');
const collection = client.db(getDbName()).collection('houses');

// Calcular la puntuación
const score = calculateScore(house);
const houseWithScore = { ...house, score };

const result = await collection.insertOne(houseWithScore);
const { _id, ...houseWithoutId } = houseWithScore;
console.log(_id);
const result = await collection.insertOne(houseWithoutId);
return result;
}

export async function updateHouse(id: string, house: House) {
const client = await connectToDatabase();
const collection = client.db('casa').collection('houses');
const collection = client.db(getDbName()).collection('houses');

const score = calculateScore(house);
const houseWithScore = { ...house, score };
const { ...houseWithoutId } = { ...house, score };

const result = await collection.updateOne(
{ _id: new ObjectId(id) },
{ $set: houseWithScore }
{ $set: houseWithoutId }
);
return result;
}

export async function deleteHouse(id: string) {
const client = await connectToDatabase();
const collection = client.db('casa').collection('houses');
const collection = client.db(getDbName()).collection('houses');

const result = await collection.deleteOne({ _id: new ObjectId(id) });
return result;
}

export async function getHouse(id: string) {
export async function getHouse(id: string): Promise<House | null> {
const client = await connectToDatabase();
const collection = client.db('casa').collection('houses');
const collection = client.db(getDbName()).collection('houses');

return collection.findOne({ _id: new ObjectId(id) });
const house = await collection.findOne({ _id: new ObjectId(id) });
return house as House | null;
}

function calculateScore(house: House): number {
Expand Down
3 changes: 1 addition & 2 deletions src/app/addHouse/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import { useRouter } from 'next/navigation';
import HouseForm from '@/components/house/HouseForm';
import { House } from '@/types/house';

export default function AddHousePage() {
const router = useRouter();

const handleSave = async (house: House) => {
const handleSave = async () => {
router.push('/');
};

Expand Down
2 changes: 1 addition & 1 deletion src/app/editHouse/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function EditHousePage({ params }: { params: { id: string } }) {
loadHouse();
}, [params.id]);

const handleSave = async (house: House) => {
const handleSave = async () => {
router.push('/');
};

Expand Down
13 changes: 13 additions & 0 deletions src/components/HouseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ export default function HouseForm({ house, onClose, onSave }: HouseFormProps) {
mobileSignal: 0,
noiseLevel: 0,
hasBalcony: false,
customLighting: false,
ergonomicFurniture: false,
professionalSetup: false,
relaxationAreas: false,
screenReflectionFree: false,
plantsSpace: false,
electricalPower: false,
fiberOptic: false,
internetBackup: false,
neighborWorkStyle: false,
nearbyCoworking: false,
privateVideocalls: false,
storageSpace: false,
});

const handleSubmit = async (e: React.FormEvent) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/ui/calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ function Calendar({
...classNames,
}}
components={{
IconLeft: ({ ...props }) => <ChevronLeft className="h-4 w-4" />,
IconRight: ({ ...props }) => <ChevronRight className="h-4 w-4" />,
IconLeft: ({ }) => <ChevronLeft className="h-4 w-4" />,
IconRight: ({ }) => <ChevronRight className="h-4 w-4" />,
}}
{...props}
/>
Expand Down
16 changes: 12 additions & 4 deletions src/lib/mongodb.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { MongoClient } from 'mongodb';

const MONGODB_URI = 'mongodb://localhost:27017';
const MONGODB_DB = 'casa';
const MONGODB_URI = process.env.MONGODB_URI;
const MONGODB_DB = process.env.MONGODB_DB;

if (!MONGODB_URI) {
throw new Error('Please define the MONGODB_URI environment variable');
throw new Error('Please define the MONGODB_URI environment variable inside .env');
}

if (!MONGODB_DB) {
throw new Error('Please define the MONGODB_DB environment variable inside .env');
}

let cachedClient: MongoClient | null = null;
Expand All @@ -14,7 +18,11 @@ export async function connectToDatabase() {
return cachedClient;
}

const client = await MongoClient.connect(MONGODB_URI);
const client = await MongoClient.connect(process.env.MONGODB_URI as string);
cachedClient = client;
return client;
}

export function getDbName() {
return MONGODB_DB;
}

0 comments on commit 2d48d62

Please sign in to comment.