|
| 1 | +import logfire |
1 | 2 | from sqlmodel import Session, select |
2 | 3 |
|
3 | 4 | from solesearch_api.models.sneaker import Sneaker |
4 | 5 | from solesearch_api.tasks import app |
| 6 | +from solesearch_api.db import engine |
| 7 | + |
| 8 | +logfire.configure(service_name="worker") |
5 | 9 |
|
6 | 10 |
|
7 | 11 | @app.task(name="create_or_update_sneaker") |
8 | 12 | def create_or_update_sneaker( |
9 | | - session: Session, |
10 | 13 | sneaker: Sneaker, |
11 | 14 | ): |
12 | | - """ |
13 | | - Create a new sneaker or update an existing one with the same SKU and brand. |
14 | | -
|
15 | | - This function centralizes the logic for handling sneaker uniqueness based on SKU and brand. |
16 | | - It will update all fields of an existing sneaker with the new data, and handle relationships |
17 | | - like links, images, sizes, and prices. |
| 15 | + with Session(engine) as session: |
| 16 | + """ |
| 17 | + Create a new sneaker or update an existing one with the same SKU and brand. |
18 | 18 |
|
19 | | - Args: |
20 | | - session: SQLModel session |
21 | | - sneaker: The sneaker object to create or update |
| 19 | + This function centralizes the logic for handling sneaker uniqueness based on SKU and brand. |
| 20 | + It will update all fields of an existing sneaker with the new data, and handle relationships |
| 21 | + like links, images, sizes, and prices. |
22 | 22 |
|
23 | | - Returns: |
24 | | - The created or updated sneaker object |
25 | | - """ |
26 | | - # Check if sneaker already exists |
27 | | - statement = select(Sneaker).where( |
28 | | - Sneaker.sku == sneaker.sku, |
29 | | - Sneaker.brand == sneaker.brand, |
30 | | - ) |
31 | | - existing_sneaker = session.exec(statement).first() |
| 23 | + Args: |
| 24 | + - sneaker: The sneaker object to create or update |
32 | 25 |
|
33 | | - if existing_sneaker: |
34 | | - # Update existing sneaker with new information |
35 | | - existing_sneaker.name = sneaker.name or existing_sneaker.name |
36 | | - existing_sneaker.parent_sku = sneaker.parent_sku or existing_sneaker.parent_sku |
37 | | - existing_sneaker.audience = sneaker.audience or existing_sneaker.audience |
38 | | - existing_sneaker.release_date = ( |
39 | | - sneaker.release_date or existing_sneaker.release_date |
40 | | - ) |
41 | | - existing_sneaker.retail_price = ( |
42 | | - sneaker.retail_price or existing_sneaker.retail_price |
| 26 | + Returns: |
| 27 | + - The created or updated sneaker object |
| 28 | + """ |
| 29 | + # Check if sneaker already exists with eager loading of relationships |
| 30 | + statement = select(Sneaker).where( |
| 31 | + Sneaker.sku == sneaker.sku, |
| 32 | + Sneaker.brand == sneaker.brand, |
43 | 33 | ) |
44 | | - existing_sneaker.colorway = sneaker.colorway or existing_sneaker.colorway |
45 | | - existing_sneaker.description = ( |
46 | | - sneaker.description or existing_sneaker.description |
47 | | - ) |
48 | | - existing_sneaker.source = existing_sneaker.source or sneaker.source |
49 | | - existing_sneaker.stockx_id = existing_sneaker.stockx_id or sneaker.stockx_id |
50 | | - existing_sneaker.stadium_goods_id = ( |
51 | | - existing_sneaker.stadium_goods_id or sneaker.stadium_goods_id |
52 | | - ) |
53 | | - |
54 | | - # Add any new links |
55 | | - existing_link_urls = [link.url for link in existing_sneaker.links] |
56 | | - for link in sneaker.links: |
57 | | - if link.url not in existing_link_urls: |
58 | | - existing_sneaker.links.append(link) |
| 34 | + existing_sneaker = session.exec(statement).first() |
| 35 | + if existing_sneaker is None: |
| 36 | + logfire.info( |
| 37 | + "Sneaker not found, creating new sneaker", |
| 38 | + sneaker=sneaker, |
| 39 | + ) |
| 40 | + # Just add the new sneaker if it doesn't exist already |
| 41 | + result = sneaker |
| 42 | + else: |
| 43 | + logfire.info( |
| 44 | + "Sneaker found, updating existing sneaker", |
| 45 | + sneaker=sneaker, |
| 46 | + existing_sneaker=existing_sneaker, |
| 47 | + ) |
| 48 | + # Update existing sneaker with new information |
| 49 | + existing_sneaker.name = sneaker.name or existing_sneaker.name |
| 50 | + existing_sneaker.parent_sku = ( |
| 51 | + sneaker.parent_sku or existing_sneaker.parent_sku |
| 52 | + ) |
| 53 | + existing_sneaker.audience = sneaker.audience or existing_sneaker.audience |
| 54 | + existing_sneaker.release_date = ( |
| 55 | + sneaker.release_date or existing_sneaker.release_date |
| 56 | + ) |
| 57 | + existing_sneaker.retail_price = ( |
| 58 | + sneaker.retail_price or existing_sneaker.retail_price |
| 59 | + ) |
| 60 | + existing_sneaker.colorway = sneaker.colorway or existing_sneaker.colorway |
| 61 | + existing_sneaker.description = ( |
| 62 | + sneaker.description or existing_sneaker.description |
| 63 | + ) |
| 64 | + existing_sneaker.source = existing_sneaker.source or sneaker.source |
| 65 | + existing_sneaker.stockx_id = existing_sneaker.stockx_id or sneaker.stockx_id |
| 66 | + existing_sneaker.stadium_goods_id = ( |
| 67 | + existing_sneaker.stadium_goods_id or sneaker.stadium_goods_id |
| 68 | + ) |
59 | 69 |
|
60 | | - # Add any new images |
61 | | - existing_image_urls = [image.url for image in existing_sneaker.images] |
62 | | - for image in sneaker.images: |
63 | | - if image.url not in existing_image_urls: |
64 | | - existing_sneaker.images.append(image) |
| 70 | + # Add any new links |
| 71 | + existing_link_urls = {link.url for link in existing_sneaker.links} |
| 72 | + for link in sneaker.links: |
| 73 | + if link.url not in existing_link_urls: |
| 74 | + existing_sneaker.links.append(link) |
65 | 75 |
|
66 | | - # Add any new sizes |
67 | | - existing_size_values = [size.value for size in existing_sneaker.sizes] |
68 | | - for size in sneaker.sizes: |
69 | | - if size.value not in existing_size_values: |
70 | | - existing_sneaker.sizes.append(size) |
| 76 | + # Add any new images |
| 77 | + existing_image_urls = {image.url for image in existing_sneaker.images} |
| 78 | + for image in sneaker.images: |
| 79 | + if image.url not in existing_image_urls: |
| 80 | + session.add(image) |
| 81 | + existing_sneaker.images.append(image) |
71 | 82 |
|
72 | | - return existing_sneaker |
73 | | - else: |
74 | | - # Just add the new sneaker if it doesn't exist already |
75 | | - session.add(sneaker) |
76 | | - return sneaker |
| 83 | + # Add any new sizes |
| 84 | + existing_size_values = {size.value for size in existing_sneaker.sizes} |
| 85 | + for size in sneaker.sizes: |
| 86 | + if size.value not in existing_size_values: |
| 87 | + session.add(size) |
| 88 | + existing_sneaker.sizes.append(size) |
| 89 | + result = existing_sneaker |
| 90 | + session.add(result) |
| 91 | + session.commit() |
| 92 | + return result |
0 commit comments