-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
161 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import dataclasses | ||
from typing import Sequence | ||
|
||
from app.entities.category.category_codes import CategoryCode | ||
from app.entities.category.categories import Category | ||
|
||
|
||
@dataclasses.dataclass | ||
class CategoryResponse: | ||
categories: Sequence[CategoryCode] | ||
categories: Sequence[Category] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import dataclasses | ||
|
||
from app.entities.category.category_codes import CategoryCode | ||
|
||
|
||
@dataclasses.dataclass | ||
class Category: | ||
code: str | ||
name: str | ||
image_url: str | ||
deep_link: str | ||
|
||
|
||
CATEGORIES: dict[CategoryCode, Category] = { | ||
CategoryCode.CHICKEN: Category( | ||
code=CategoryCode.CHICKEN, | ||
name="치킨", | ||
image_url="https://chicken.png", | ||
deep_link="app://chicken_action", | ||
), | ||
CategoryCode.BURGER: Category( | ||
code=CategoryCode.BURGER, | ||
name="버거", | ||
image_url="https://burger.png", | ||
deep_link="app://burger_action", | ||
), | ||
CategoryCode.PIZZA: Category( | ||
code=CategoryCode.PIZZA, | ||
name="피자", | ||
image_url="https://pizza.png", | ||
deep_link="app://pizza_action", | ||
), | ||
CategoryCode.SANDWICH: Category( | ||
code=CategoryCode.SANDWICH, | ||
name="샌드위치", | ||
image_url="https://sandwich.png", | ||
deep_link="app://sandwich_action", | ||
), | ||
CategoryCode.KOREAN: Category( | ||
code=CategoryCode.KOREAN, | ||
name="한식", | ||
image_url="https://korean.png", | ||
deep_link="app://korean_action", | ||
), | ||
CategoryCode.JAPANESE: Category( | ||
code=CategoryCode.JAPANESE, | ||
name="일식", | ||
image_url="https://japanese.png", | ||
deep_link="app://japanese_action", | ||
), | ||
CategoryCode.SALAD: Category( | ||
code=CategoryCode.SALAD, | ||
name="샐러드", | ||
image_url="https://salad.png", | ||
deep_link="app://salad_action", | ||
), | ||
CategoryCode.CAFE: Category( | ||
code=CategoryCode.CAFE, | ||
name="카페", | ||
image_url="https://cafe.png", | ||
deep_link="app://cafe_action", | ||
), | ||
} |
Empty file.
53 changes: 53 additions & 0 deletions
53
app/entities/collections/category_point/category_point_collection.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from dataclasses import asdict | ||
|
||
import pymongo | ||
from motor.motor_asyncio import AsyncIOMotorCollection | ||
from pymongo import ReturnDocument | ||
from pymongo.errors import DuplicateKeyError | ||
|
||
from app.entities.category.category_codes import CategoryCode | ||
from app.entities.collections.category_point.category_point_document import ( | ||
CategoryPointDocument, | ||
) | ||
from app.entities.collections.geo_json import GeoJsonPoint | ||
from app.utils.mongo import db | ||
|
||
|
||
class CategoryPointCollection: | ||
_collection = AsyncIOMotorCollection(db, "category_points") | ||
|
||
@classmethod | ||
async def set_index(cls) -> None: | ||
await cls._collection.create_index( | ||
[ | ||
("point", pymongo.GEOSPHERE), | ||
("codes", pymongo.ASCENDING), | ||
] | ||
) | ||
await cls._collection.create_index("cache_key", unique=True) | ||
|
||
@classmethod | ||
async def insert_or_replace( | ||
cls, cache_key: str, point: GeoJsonPoint, codes: tuple[CategoryCode, ...] | ||
) -> CategoryPointDocument: | ||
document_to_insert = { | ||
"cache_key": cache_key, | ||
"codes": codes, | ||
"point": asdict(point), | ||
} | ||
try: | ||
result = await cls._collection.insert_one(document_to_insert) | ||
_id = result.inserted_id | ||
except DuplicateKeyError: | ||
inserted_document = await cls._collection.find_one_and_replace( | ||
{"cache_key": cache_key}, | ||
document_to_insert, | ||
return_document=ReturnDocument.AFTER, | ||
) | ||
_id = inserted_document["_id"] | ||
return CategoryPointDocument( | ||
codes=codes, | ||
point=point, | ||
cache_key=cache_key, | ||
_id=_id, | ||
) |
12 changes: 12 additions & 0 deletions
12
app/entities/collections/category_point/category_point_document.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import dataclasses | ||
|
||
from app.entities.category.category_codes import CategoryCode | ||
from app.entities.collections.base_document import BaseDocument | ||
from app.entities.collections.geo_json import GeoJsonPoint | ||
|
||
|
||
@dataclasses.dataclass | ||
class CategoryPointDocument(BaseDocument): | ||
cache_key: str | ||
codes: tuple[CategoryCode, ...] | ||
point: GeoJsonPoint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
import asyncio | ||
|
||
from app.entities.category.categories import CATEGORIES, Category | ||
from app.entities.category.category_codes import CategoryCode | ||
from app.entities.collections.geo_json import GeoJsonPoint | ||
from app.entities.collections.shop.shop_collection import ShopCollection | ||
|
||
|
||
async def get_distinct_home_categories(longitude: float, latitude: float) -> tuple[CategoryCode, ...]: | ||
async def get_distinct_home_categories(longitude: float, latitude: float) -> tuple[Category, ...]: | ||
return tuple( | ||
code | ||
CATEGORIES[code] | ||
for code in await ShopCollection.get_distinct_category_codes_by_point_intersects( | ||
GeoJsonPoint(coordinates=[longitude, latitude]) | ||
) | ||
) | ||
|
||
|
||
async def get_home_categories_one_by_one(longitude: float, latitude: float) -> tuple[CategoryCode, ...]: | ||
async def get_home_categories_one_by_one(longitude: float, latitude: float) -> tuple[Category, ...]: | ||
li = [ | ||
ShopCollection.exists_by_category_and_point_intersects(code, GeoJsonPoint(coordinates=[longitude, latitude])) | ||
for code in CategoryCode | ||
] | ||
return tuple(code for code, exists in zip(CategoryCode, await asyncio.gather(*li)) if exists) | ||
return tuple(CATEGORIES[code] for code, exists in zip(CategoryCode, await asyncio.gather(*li)) if exists) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
25 changes: 25 additions & 0 deletions
25
app/tests/entities/collections/category_point/test_category_point_collection.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from app.entities.category.category_codes import CategoryCode | ||
from app.entities.collections.category_point.category_point_collection import ( | ||
CategoryPointCollection, | ||
) | ||
from app.entities.collections.geo_json import GeoJsonPoint | ||
|
||
|
||
async def test_insert_or_replace() -> None: | ||
# Given | ||
cache_key = "1_1" | ||
point = GeoJsonPoint(coordinates=[1, 1]) | ||
codes = (CategoryCode.CHICKEN, CategoryCode.PIZZA) | ||
|
||
# When | ||
category_point = await CategoryPointCollection.insert_or_replace(cache_key, point, codes) | ||
category_point_upserted = await CategoryPointCollection.insert_or_replace(cache_key, point, codes) | ||
|
||
# Then | ||
result = await CategoryPointCollection._collection.find_one({"_id": category_point.id}) | ||
assert category_point.cache_key == result["cache_key"] | ||
assert category_point.point.coordinates == result["point"]["coordinates"] | ||
assert category_point.codes == tuple(CategoryCode(code) for code in result["codes"]) | ||
assert category_point_upserted.cache_key == result["cache_key"] | ||
assert category_point_upserted.point.coordinates == result["point"]["coordinates"] | ||
assert category_point_upserted.codes == tuple(CategoryCode(code) for code in result["codes"]) |