app.backbone.services.config_service

 1from typing import List
 2from app.backbone.database.db_service import DbService
 3from app.backbone.entities.config import Config
 4from app.backbone.services.operation_result import OperationResult
 5
 6class ConfigService:
 7    def __init__(self):
 8        self.db_service = DbService()
 9
10    def get_all(self) -> List[Config]:
11        with self.db_service.get_database() as db:
12            configs = self.db_service.get_all(db, Config)
13
14            return configs
15
16    def get_by_name(self, name):
17        with self.db_service.get_database() as db:
18            config = self.db_service.get_by_filter(db, Config, Name=name)
19            return config
20
21    def add_or_update(self, name:str, value:str):
22        with self.db_service.get_database() as db:
23            config = self.db_service.get_by_filter(db, Config, Name=name)  
24
25            # Si existe se modifica
26            if config:
27                new_config = Config(Id=config.Id, Name=name, Value=value)
28                strategy = self.db_service.update(db, Config, new_config)
29                result = OperationResult(ok=True, message=None, item=strategy)
30                return result
31            
32            # Sino se agrega
33            new_config = Config(Name=name, Value=value)
34            strategy = self.db_service.create(db, new_config)
35            result = OperationResult(ok=True, message=None, item=strategy)
36
37            return result
38
39    def load_default_values():
40        pass
class ConfigService:
 7class ConfigService:
 8    def __init__(self):
 9        self.db_service = DbService()
10
11    def get_all(self) -> List[Config]:
12        with self.db_service.get_database() as db:
13            configs = self.db_service.get_all(db, Config)
14
15            return configs
16
17    def get_by_name(self, name):
18        with self.db_service.get_database() as db:
19            config = self.db_service.get_by_filter(db, Config, Name=name)
20            return config
21
22    def add_or_update(self, name:str, value:str):
23        with self.db_service.get_database() as db:
24            config = self.db_service.get_by_filter(db, Config, Name=name)  
25
26            # Si existe se modifica
27            if config:
28                new_config = Config(Id=config.Id, Name=name, Value=value)
29                strategy = self.db_service.update(db, Config, new_config)
30                result = OperationResult(ok=True, message=None, item=strategy)
31                return result
32            
33            # Sino se agrega
34            new_config = Config(Name=name, Value=value)
35            strategy = self.db_service.create(db, new_config)
36            result = OperationResult(ok=True, message=None, item=strategy)
37
38            return result
39
40    def load_default_values():
41        pass
db_service
def get_all(self) -> List[app.backbone.entities.config.Config]:
11    def get_all(self) -> List[Config]:
12        with self.db_service.get_database() as db:
13            configs = self.db_service.get_all(db, Config)
14
15            return configs
def get_by_name(self, name):
17    def get_by_name(self, name):
18        with self.db_service.get_database() as db:
19            config = self.db_service.get_by_filter(db, Config, Name=name)
20            return config
def add_or_update(self, name: str, value: str):
22    def add_or_update(self, name:str, value:str):
23        with self.db_service.get_database() as db:
24            config = self.db_service.get_by_filter(db, Config, Name=name)  
25
26            # Si existe se modifica
27            if config:
28                new_config = Config(Id=config.Id, Name=name, Value=value)
29                strategy = self.db_service.update(db, Config, new_config)
30                result = OperationResult(ok=True, message=None, item=strategy)
31                return result
32            
33            # Sino se agrega
34            new_config = Config(Name=name, Value=value)
35            strategy = self.db_service.create(db, new_config)
36            result = OperationResult(ok=True, message=None, item=strategy)
37
38            return result
def load_default_values():
40    def load_default_values():
41        pass