Skip to content

Commit a764e67

Browse files
committed
Full database version.
1 parent 181b118 commit a764e67

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed
Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
from typing import List, Optional
22

33
from guitary.data.guitar import Guitar
4+
from guitary.data import session_factory
45

56

67
def all_guitars(style: Optional[str]) -> List[Guitar]:
7-
guitars = [
8-
Guitar('AX Black', 499, '/static/img/guitars/ax-black.jpg', style='electric'),
9-
Guitar('Jet Black Electric', 599, '/static/img/guitars/jet-black-electric.jpg', style='electric'),
10-
Guitar('Weezer Classic', 1499, '/static/img/guitars/weezer-classic.jpg', style='electric'),
11-
Guitar('Acoustic Black', 1299, '/static/img/guitars/black-acoustic.jpg', style='acoustic'),
12-
Guitar('Mellow Yellow', 799, '/static/img/guitars/mellow-yellow.jpg', style='electric'),
13-
Guitar('White Vibes', 699, '/static/img/guitars/white-vibes.jpg', style='electric'),
14-
Guitar('Brush Riffs', 599, '/static/img/guitars/brushed-black-electric.jpg', style='electric'),
15-
Guitar("Nature's Song", 799, '/static/img/guitars/natures-song.jpg', style='electric'),
16-
Guitar('Electric Wood Grain', 399, '/static/img/guitars/woodgrain-electric.jpg', style='electric'),
17-
]
18-
guitars.sort(key=lambda g: g.price, reverse=True)
8+
with session_factory.create_session() as ctx:
9+
if style is None or style == 'all':
10+
guitars = ctx.session.query(Guitar).order_by(Guitar.price.desc())
11+
return list(guitars)
1912

20-
if style is None or style == 'all':
21-
return guitars
22-
23-
filtered_guitars = [
24-
g
25-
for g in guitars
26-
if g.style == style
27-
]
13+
filtered_guitars = list(ctx.session.query(Guitar)
14+
.filter(Guitar.style == style)
15+
.order_by(Guitar.price.desc()))
2816

2917
return filtered_guitars

0 commit comments

Comments
 (0)