|
1 | 1 | from typing import List, Optional
|
2 | 2 |
|
3 | 3 | from guitary.data.guitar import Guitar
|
| 4 | +from guitary.data import session_factory |
4 | 5 |
|
5 | 6 |
|
6 | 7 | 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) |
19 | 12 |
|
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())) |
28 | 16 |
|
29 | 17 | return filtered_guitars
|
0 commit comments