Реструктуризация файлов в проекте

This commit is contained in:
2024-11-13 07:00:14 +03:00
parent 1833040329
commit a06c893092
7 changed files with 59 additions and 39 deletions

54
dal/db_Mong.py Normal file
View File

@@ -0,0 +1,54 @@
from pymongo import MongoClient
import json
class VPNServerRepository:
def __init__(self, config_path="config.json"):
with open(config_path, "r") as file:
config = json.load(file)
self.client = MongoClient(config["mongodb_uri"])
self.db = self.client[config["database_name"]]
self.collection = self.db["vpn_servers"]
def add_server(self, server_data):
"""Добавляет новый VPN сервер в коллекцию."""
result = self.collection.insert_one(server_data)
print(f"VPN сервер добавлен с ID: {result.inserted_id}")
return result.inserted_id
def get_server(self, server_id):
"""Получает сервер VPN по его ID."""
server = self.collection.find_one({"_id": server_id})
if server:
print(f"Найден VPN сервер: {server}")
else:
print(f"VPN сервер с ID {server_id} не найден.")
return server
def update_server(self, server_id, update_data):
"""Обновляет данные VPN сервера."""
result = self.collection.update_one({"_id": server_id}, {"$set": update_data})
if result.matched_count > 0:
print(f"VPN сервер с ID {server_id} обновлен.")
else:
print(f"VPN сервер с ID {server_id} не найден.")
return result.matched_count > 0
def delete_server(self, server_id):
"""Удаляет VPN сервер по его ID."""
result = self.collection.delete_one({"_id": server_id})
if result.deleted_count > 0:
print(f"VPN сервер с ID {server_id} удален.")
else:
print(f"VPN сервер с ID {server_id} не найден.")
return result.deleted_count > 0
def list_servers(self):
"""Возвращает список всех VPN серверов."""
servers = list(self.collection.find())
print(f"Найдено {len(servers)} VPN серверов.")
return servers
def close_connection(self):
"""Закрывает подключение к базе данных MongoDB."""
self.client.close()
print("Подключение к MongoDB закрыто.")