import { getDb, ObjectId, City } from "../index"; import { Collection, Filter, ObjectId as MongoObjectId } from "mongodb"; export const getCitiesCollection = (): Collection => { return getDb().collection("cities"); }; export const findCities = async ( filter: Filter = {}, ): Promise => { return await getCitiesCollection().find(filter).toArray(); }; export const getCityById = async (cityId: string): Promise => { const city = await getCitiesCollection().findOne({ _id: new ObjectId(cityId), }); return city ? city : null; }; export const createCity = async ( cityData: Omit, ): Promise => { const city: Omit = { ...cityData, createdAt: new Date(), updatedAt: new Date(), }; const { insertedId } = await getCitiesCollection().insertOne(city as City); return insertedId; }; export const updateCity = async ( cityId: string, data: Partial>, ): Promise => { const result = await getCitiesCollection().findOneAndUpdate( { _id: new ObjectId(cityId) }, { $set: { ...data, updatedAt: new Date() } }, { returnDocument: "after" }, ); return result || null; }; export const deleteCity = async (cityId: string): Promise => { const result = await getCitiesCollection().deleteOne({ _id: new ObjectId(cityId), }); return result.deletedCount > 0; };