-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.py
More file actions
119 lines (98 loc) · 2.72 KB
/
services.py
File metadata and controls
119 lines (98 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
This file contains all services for the application.
"""
import json
from typing import List, Union
from fastapi import Depends, status, HTTPException
from models import BlipColor, BlipModel, Marker, PedModel, Weapon
def get_model(model_name: str, filters) -> Union[List[BlipModel], List[BlipColor], List[Marker], List[PedModel], List[Weapon]]:
"""
Get model from JSON file.
"""
try:
with open(f"data/{model_name}.json", "r", encoding="utf-8") as f:
data = json.load(f)
except FileNotFoundError:
data = {"error": "Model not found"}
if filters:
data = [model for model in data if all(key in model and model[key] == value for key, value in filters.items())]
if len(data) <= 0:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="No model founded")
return data
def get_model_with_id(id: int = None):
"""
Get a model with identifier.
"""
filters = {}
if id is not None:
filters["id"] = id
return filters
def get_model_with_name(name: str = None):
"""
Get a model with name.
"""
filters = {}
if name is not None:
filters["name"] = name
return filters
def get_model_with_hash(hash: str = None):
"""
Get a model with hash.
"""
filters = {}
if hash is not None:
filters["hash"] = hash
return filters
def get_model_with_type(type: str = None):
"""
Get a model with type.
"""
filters = {}
if type is not None:
filters["type"] = type
return filters
def get_blip_colors(filters = Depends(get_model_with_id)) -> List[BlipColor]:
"""
Get filtered or not blip colors.
"""
return get_model("blip_colors", filters)
def get_blip_models(filters = Depends(get_model_with_id)) -> List[BlipModel]:
"""
Get filtered or not blip models.
"""
return get_model("blip_models", filters)
def get_markers(filters = Depends(get_model_with_id)) -> List[Marker]:
"""
Get filtered or not markers.
"""
return get_model("markers", filters)
def get_ped_models(
name_filter = Depends(get_model_with_name),
hash_filter = Depends(get_model_with_hash),
) -> List[PedModel]:
"""
Get filtered or not ped models.
"""
return get_model(
"ped_models",
{
**name_filter,
**hash_filter,
},
)
def get_weapons(
name_filter = Depends(get_model_with_name),
hash_filter = Depends(get_model_with_hash),
type_filter = Depends(get_model_with_type),
) -> List[Weapon]:
"""
Get filtered or not weapons.
"""
return get_model(
"weapons",
{
**name_filter,
**hash_filter,
**type_filter,
},
)