-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
116 lines (95 loc) · 3.64 KB
/
app.py
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
from skilly.framework.db.src.model.entity.skilly_entity import Entity
from skilly.framework.utils.src.decorators.skilly_decorators import entity
from skilly.framework.db.src.model.orm.skilly_sql_orm import Sql
class UserService:
@classmethod
def getUser(cls, userId):
# call the repository method to get a user by its ID
user: User = UserRepository.getByUser_idEq(userId)
# check if user is null
if user is None:
return ResponseHandler.send().NOT_FOUND("No user found")
else:
return ResponseHandler(user.toJSON()).OK("User found")
@classmethod
def getUsersByNameAndMoney(cls, name, money):
# call the repository method to get a user by its name and money
users: list[User] = UserRepository.getByNameEqAndMoneyGt(name, money)
result = []
for user in users:
result.append(user.toJSON())
return ResponseHandler(result).OK("Users found")
@classmethod
def getUserAndNewParameter(cls, userId):
user: User = UserRepository.getByUser_idEq(userId)
if user is None:
return ResponseHandler.send().NOT_FOUND("No user found")
else:
# instead of calling the method toJSON(), you can
# add some parameters to it and they will be added to
# the default JSON
return ResponseHandler(user.toJSON(newParameter='Hello', ...)).OK("User found")
@classmethod
def updateName(cls, userId, newName):
# call the repository method to get a user by its name and money
user: User = UserRepository.getByUser_idEq(userId)
if user is None:
return ResponseHandler.send().NOT_FOUND("No user found with this ID")
else:
user.name = newName
user.update()
return ResponseHandler(user.toJSON()).OK("User updated")
@classmethod
def deleteUser(cls, userId):
# call the repository method to get a user by its name and money
user: User = UserRepository.getByUser_idEq(userId)
if user is None:
return ResponseHandler.send().NOT_FOUND("No user found with this ID")
else:
user.delete()
return ResponseHandler().OK("User deleted")
@classmethod
def createUser(cls, body):
# create user object
user: User = User(
body['name'],
body['surname'],
body['birthday'],
body['money']
)
user.save()
return ResponseHandler(user.toJSON()).OK("User created")
@classmethod
def joinQuery(cls, companyId):
users: list[User] = \
UserRepository
.startJoin(User, Workers)
.on("User.user_id", "Workers.user_id")
.where("Workers.company_id", "=", companyId)
.execute()
# it's going to return an array of rows, every row has two or more
# entitiy objects, depending on the amount of joined tables
# [ [User, Worker], [User, Worker] ... ]
@classmethod
def joinThreeQuery(cls, companyName):
users: list[User] = \
UserRepository
.startJoin(User, Workers)
.on("User.user_id", "Workers.user_id")
.join(Company)
.on("Workers.company_id", "Company.company_id")
.where("Company.companyName", "=", companyName)
.execute()
# it's going to return an array of rows, every row has two or more
# entitiy objects, depending on the amount of joined tables
# [ [User, Worker, Company], [User, Worker, Company] ... ]
@classmethod
def customQuery(cls, userMoney):
users: list[User] = UserRepository.manualQBN(
True, # fetchall, boolean
"SELECT COUNT(*), user.name FROM user GROUP BY money" # query
Attr("count(*)"), # attribute 1 of the returned objects
Attr("name"), # attribute 2 of the returned objects
)
# it's going to return an array of generic entitis, having the attributes you passed through Attr()
# [ GenericEntity, GenericEntity, ...]