def _seek_client(license: str, id: str): seek = _seek(license) id = id.strip().lower() if not ObjectId.is_valid(id): raise_bad_request("Invalid client ID") seek["_id"] = ObjectId(id) return seek
async def create_license(data: LicenseCreate): ''' Membuat license dan sekaligus user admin (owner). `publishDate`: `(yyyy-mm-dd)`. ''' logging.info(">>> " + __name__ + ":create") license = await crud.find_one(data.slug) if license: raise_bad_request( 'License with this slug is already exists in the system.') return await crud.insert_one(data)
async def update(slug: str, id: str, data: ClientUpdate): logging.info(">>> " + __name__ + ":update") props = delete_empty_keys(data) if len(props) == 0: raise_bad_request("No data supplied.") collection = get_collection(DOCUMENT_TYPE) seek = {"license": slug, "_id": ObjectId(id)} client = await collection.find_one_and_update( seek, {"$set": fields_in_update(props)}, return_document=ReturnDocument.AFTER) if client: return client raise_server_error(ERROR_MONGODB_UPDATE)
async def update_one(term: str, data: BaseModel): logging.info(">>> " + __name__ + ":update_one") props = delete_empty_keys(data) logging.info(props) if len(props) == 0: raise_bad_request("No data supplied.") collection = get_collection(DOCUMENT_TYPE) seek = seek_by_term(term) rs = await collection.find_one_and_update( seek, {"$set": fields_in_update(props)}, return_document=ReturnDocument.AFTER) return rs if rs else raise_server_error(ERROR_MONGODB_UPDATE)
async def insert_one(data: UserCreate): logging.info(">>> " + __name__ + ":insert_one") # Check license valid = await is_license_valid(data.license) if not valid: raise_bad_request("License is not valid") collection = get_collection(DOCUMENT_TYPE) hashed_password = get_password_hash(data.password) model = UserInDB(**data.dict(), hashed_password=hashed_password) props = fields_in_create(model) try: rs = await collection.insert_one(props) if rs.inserted_id: user = await collection.find_one({"_id": rs.inserted_id}) return user except Exception as e: logging.info(e) raise_server_error(str(e))