async def _get_history_loan_row_id(id: str, loans_id: str): _id = validate_object_id(id) loans_id = validate_object_id(loans_id) row = await db.history_loans.find_one({"_id": _id, "loans_id": loans_id}) if row: return fix_id(row) else: raise HTTPException(status_code=404, detail="row not found")
async def get_vps_or_404(id: str): _id = validate_object_id(id) vps = await db.mcDB.find_one({'_id': _id}) if vps: return fix_id(vps) else: raise HTTPException(status_code=404, detail='name error!')
async def _get_user(id: str): _id = validate_object_id(id) user = await db.users.find_one({"_id": _id}) if user: return fix_id(user) else: raise HTTPException(status_code=404, detail="User not found")
async def _get_pet_or_404(id: str): _id = validate_object_id(id) pet = await db.petDB.find_one({"_id": _id}) if pet: return fix_pet_id(pet) else: raise HTTPException(status_code=404, detail="Pet not found")
async def _get_client(id: str): _id = validate_object_id(id) client = await db.clients.find_one({"_id": _id}) if client: return fix_id(client) else: raise HTTPException(status_code=404, detail="Client not found")
async def _get_loan(id: str): _id = validate_object_id(id) loan = await db.loans.find_one({"_id": _id}) if loan: return fix_id(loan) else: raise HTTPException(status_code=404, detail="Loan not found")
async def _get_telegram_or_404(id: str): _id = validate_object_id(id) telegram = await db.Telegram.find_one({"_id": _id}) if telegram: return fix_telegram_id(telegram) else: raise HTTPException(status_code=404, detail="telegram not found")
async def telegram_signin(id: str, code: int = None, password: str = None): phone = await _get_telegram_or_404(id) auth_key = await signIn(phone['phone_number'], code, phone['phone_code_hash'], password) await db.Telegram.update_one( {"_id": validate_object_id(id)}, {"$set": {"auth_key": auth_key}} ) return {'message': 'Success!! sign in into telegram'}
async def update_pet(id: str, petData: PetBaseUpdateRequest): petData = petData.dict() petData = {k: v for k, v in petData.items() if v is not None} if 'petBirthDate' in petData: petData['petBirthDate'] = petData['petBirthDate'].strftime("%d-%b-%Y") pet_op = await db.petDB.update_one({"_id": validate_object_id(id)}, {"$set": petData}) if pet_op.modified_count: return await _get_pet_or_404(id) else: raise HTTPException(status_code=304)
async def delete_pet_by_id(id: str): await _get_pet_or_404(id) pet_op = await db.petDB.delete_one({"_id": validate_object_id(id)}) if pet_op.deleted_count: return {"status": f"deleted count: {pet_op.deleted_count}"}