[order] remove param, always add new meal as last
This commit is contained in:
		
							parent
							
								
									c858fe2c96
								
							
						
					
					
						commit
						1eabac6d9f
					
				
					 6 changed files with 13 additions and 16 deletions
				
			
		| 
						 | 
					@ -22,9 +22,7 @@ class CreateMeal(AuthorizedController):
 | 
				
			||||||
                raise HTTPException(status_code=404, detail="not found")
 | 
					                raise HTTPException(status_code=404, detail="not found")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                meal = await DBMeal.create(
 | 
					                meal = await DBMeal.create(session, content.diary_id, content.name)
 | 
				
			||||||
                    session, content.diary_id, content.order, content.name
 | 
					 | 
				
			||||||
                )
 | 
					 | 
				
			||||||
                return Meal.from_orm(meal)
 | 
					                return Meal.from_orm(meal)
 | 
				
			||||||
            except AssertionError as e:
 | 
					            except AssertionError as e:
 | 
				
			||||||
                raise HTTPException(status_code=400, detail=e.args[0])
 | 
					                raise HTTPException(status_code=400, detail=e.args[0])
 | 
				
			||||||
| 
						 | 
					@ -75,7 +73,7 @@ class CreateMealFromPreset(AuthorizedController):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                meal = await DBMeal.create_from_preset(
 | 
					                meal = await DBMeal.create_from_preset(
 | 
				
			||||||
                    session, content.diary_id, content.order, content.name, preset
 | 
					                    session, content.diary_id, content.name, preset
 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
                return Meal.from_orm(meal)
 | 
					                return Meal.from_orm(meal)
 | 
				
			||||||
            except AssertionError as e:
 | 
					            except AssertionError as e:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -65,13 +65,14 @@ class Meal(Base, CommonMixin):
 | 
				
			||||||
        cls,
 | 
					        cls,
 | 
				
			||||||
        session: AsyncSession,
 | 
					        session: AsyncSession,
 | 
				
			||||||
        diary_id: int,
 | 
					        diary_id: int,
 | 
				
			||||||
        order: int = 0,
 | 
					 | 
				
			||||||
        name: Optional[str] = None,
 | 
					        name: Optional[str] = None,
 | 
				
			||||||
    ) -> "Meal":
 | 
					    ) -> "Meal":
 | 
				
			||||||
        # check if order already exists in diary
 | 
					        # check if order already exists in diary
 | 
				
			||||||
        query = select(cls).where(cls.diary_id == diary_id).where(cls.order == order)
 | 
					        query = (
 | 
				
			||||||
 | 
					            select(cls.order).where(cls.diary_id == diary_id).order_by(cls.order.desc())
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
        existing_meal = await session.scalar(query)
 | 
					        existing_meal = await session.scalar(query)
 | 
				
			||||||
        assert existing_meal is None, "order already exists in diary"
 | 
					        order = existing_meal + 1 if existing_meal else 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if name is None:
 | 
					        if name is None:
 | 
				
			||||||
            name = f"Meal {order}"
 | 
					            name = f"Meal {order}"
 | 
				
			||||||
| 
						 | 
					@ -93,14 +94,15 @@ class Meal(Base, CommonMixin):
 | 
				
			||||||
        cls,
 | 
					        cls,
 | 
				
			||||||
        session: AsyncSession,
 | 
					        session: AsyncSession,
 | 
				
			||||||
        diary_id: int,
 | 
					        diary_id: int,
 | 
				
			||||||
        order: int,
 | 
					 | 
				
			||||||
        name: Optional[str],
 | 
					        name: Optional[str],
 | 
				
			||||||
        preset: Preset,
 | 
					        preset: Preset,
 | 
				
			||||||
    ) -> "Meal":
 | 
					    ) -> "Meal":
 | 
				
			||||||
        # check if order already exists in diary
 | 
					        # check if order already exists in diary
 | 
				
			||||||
        query = select(cls).where(cls.diary_id == diary_id).where(cls.order == order)
 | 
					        query = (
 | 
				
			||||||
 | 
					            select(cls.order).where(cls.diary_id == diary_id).order_by(cls.order.desc())
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
        existing_meal = await session.scalar(query)
 | 
					        existing_meal = await session.scalar(query)
 | 
				
			||||||
        assert existing_meal is None, "order already exists in diary"
 | 
					        order = existing_meal + 1 if existing_meal else 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if name is None:
 | 
					        if name is None:
 | 
				
			||||||
            name = preset.name or f"Meal {order}"
 | 
					            name = preset.name or f"Meal {order}"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -33,9 +33,7 @@ class Product(Base, CommonMixin):
 | 
				
			||||||
        if q:
 | 
					        if q:
 | 
				
			||||||
            q_list = q.split()
 | 
					            q_list = q.split()
 | 
				
			||||||
            for qq in q_list:
 | 
					            for qq in q_list:
 | 
				
			||||||
                query = query.filter(
 | 
					                query = query.filter(cls.name.ilike(f"%{qq.lower()}%"))
 | 
				
			||||||
                    cls.name.ilike(f"%{qq.lower()}%")
 | 
					 | 
				
			||||||
                )
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        query = query.offset(offset).limit(limit)
 | 
					        query = query.offset(offset).limit(limit)
 | 
				
			||||||
        stream = await session.stream_scalars(query.order_by(cls.id))
 | 
					        stream = await session.stream_scalars(query.order_by(cls.id))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -25,7 +25,6 @@ class CreateMealPayload(BaseModel):
 | 
				
			||||||
    """CreateMealPayload."""
 | 
					    """CreateMealPayload."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    name: Optional[str]
 | 
					    name: Optional[str]
 | 
				
			||||||
    order: int
 | 
					 | 
				
			||||||
    diary_id: int
 | 
					    diary_id: int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,6 +38,5 @@ class CreateMealFromPresetPayload(BaseModel):
 | 
				
			||||||
    """CreateMealPayload."""
 | 
					    """CreateMealPayload."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    name: Optional[str]
 | 
					    name: Optional[str]
 | 
				
			||||||
    order: int
 | 
					 | 
				
			||||||
    diary_id: int
 | 
					    diary_id: int
 | 
				
			||||||
    preset_id: int
 | 
					    preset_id: int
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,3 +6,4 @@ uvicorn[standard]
 | 
				
			||||||
python-jose[cryptography]
 | 
					python-jose[cryptography]
 | 
				
			||||||
passlib[bcrypt]
 | 
					passlib[bcrypt]
 | 
				
			||||||
fastapi-users
 | 
					fastapi-users
 | 
				
			||||||
 | 
					pytest
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										2
									
								
								test.sh
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								test.sh
									
									
									
									
									
								
							| 
						 | 
					@ -54,7 +54,7 @@ ${DC} exec api bash -c "python -m fooder --create-tables"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# finally run tests
 | 
					# finally run tests
 | 
				
			||||||
set -xe
 | 
					set -xe
 | 
				
			||||||
pytest fooder -sv -k "${TESTS}"
 | 
					python -m pytest fooder -sv -k "${TESTS}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# clean up after tests
 | 
					# clean up after tests
 | 
				
			||||||
echo "cleaning up..."
 | 
					echo "cleaning up..."
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue