示例#1
0
def create_offering(offering: Offering):
    query = '''
    insert into offering (
        id,
        created_at,
        updated_at,
        user_account_id,
        category,
        subcategory,
        quantity_kg,
        price_per_kg_cfa_cents
    )
    values (
        :id,
        :created_at,
        :updated_at,
        :user_account_id,
        :category,
        :subcategory,
        :quantity_kg,
        :price_per_kg_cfa_cents
    )
    returning *;
    '''
    engine.execute(sqla.text(query), asdict(offering))
示例#2
0
def retrieve_offering_audios(
        filters: OfferingAudioFilters) -> List[OfferingAudio]:

    params: OfferingAudioFilters = {
        'lang': filters['lang'],
        'category': filters['category'],
        'subcategory': filters['subcategory'],
        'min_quantity': filters.get('min_quantity', 0.0),
        'max_quantity': filters.get('max_quantity', 9999.0),
    }

    print(params)

    query = '''
    select *
    from offering_audio
    where offering_id in (
        select id
        from offering
        where quantity_kg >= :min_quantity
        and quantity_kg <= :max_quantity
        and category = lower(:category)
        and lower(subcategory) = lower(:subcategory)
    ) and lang = :lang;
    '''
    result = engine.execute(sqla.text(query), params).fetchall()
    return [from_result(a, OfferingAudio) for a in result]
示例#3
0
def create_user(data: User) -> User:
    query = '''
    insert into user_account (
        id,
        created_at,
        updated_at,
        name,
        user_group,
        email,
        phone_number,
        password
    )
    values (
        :id,
        :created_at,
        :updated_at,
        :name,
        :user_group,
        lower(:email),
        :phone_number,
        :password
    )
    returning *;
    '''
    result = engine.execute(sqla.text(query), asdict(data))\
        .first()
    return from_result(result, User)  # type: ignore
示例#4
0
def retrieve_offerings(filters: OfferingFilters) -> List[Offering]:

    params: OfferingFilters = {
        'category': filters['category'],
        'subcategory': filters['subcategory'],
        'min_quantity': filters.get('min_quantity', 0.0),
        'max_quantity': filters.get('max_quantity', 9999.0),
    }

    query = '''
    select *
    from offering o1
    where o1.created_at = (
        select max(created_at)
        from offering o2
        where o2.category = o1.category
        and o2.subcategory = o1.subcategory
        and o2.user_account_id = o1.user_account_id
        and o1.category = :category
        and o1.subcategory = :subcategory
        and o1.quantity_kg >= :min_quantity
        and o1.quantity_kg <= :max_quantity
    );
    '''
    result = engine.execute(sqla.text(query), params).fetchall()

    return [from_result(a, Offering) for a in result]
示例#5
0
def update_user(id: str, data: UserData) -> User:
    def _parse_args() -> str:
        out = ''
        if 'name' in data:
            out += 'name = :name,\n'
        if 'user_group' in data:
            out += 'user_group = :user_group,\n'
        if 'email' in data:
            out += 'email = :email,\n'
        if 'phone_number' in data:
            out += 'phone_number = :phone_number,\n'
        if 'is_admin' in data:
            out += 'is_admin = :is_admin,\n'
        if 'password' in data:
            out += 'password = :password,\n'
        return out

    query = f'''
    update user_account
    set {_parse_args()}
    updated_at = :updated_at
    where id = :id
    returning *;
    '''

    params: Mapping[str, Any] = {**data, 'updated_at': get_cursor()}

    result = engine.execute(sqla.text(query), params)\
        .first()

    return from_result(result, User)  # type: ignore
示例#6
0
def retrieve_offering_audios_by_id(ids: List[str]) -> List[OfferingAudio]:
    query = '''
    select *
    from offering_audio
    where offering_id = any(:ids);
    '''
    audios = engine.execute(sqla.text(query), {'ids': ids}).fetchall()
    return [from_result(a, OfferingAudio) for a in audios]
示例#7
0
def retrieve_users_by_id(ids: List[str]) -> List[User]:
    query = '''
    select *
    from user_account
    where id = any(:ids);
    '''
    results = engine.execute(sqla.text(query), {'ids': ids})\
        .fetchall()
    return [from_result(u, User) for u in results]
示例#8
0
def retrieve_user_by_phone_number(phone_number: str) -> Optional[User]:
    query = '''
    select *
    from user_account
    where phone_number = :phone_number;
    '''
    result = engine.execute(sqla.text(query), {'phone_number': phone_number})\
        .first()
    return from_result(result, User) if result else None
示例#9
0
def retrieve_user_by_email(email: str) -> Optional[User]:
    query = '''
    select *
    from user_account
    where email = :email;
    '''
    result = engine.execute(sqla.text(query), {'email': email})\
        .first()
    return from_result(result, User) if result else None
示例#10
0
def retrieve_user_by_id(id: str) -> Optional[User]:
    query = '''
    select *
    from user_account
    where id = :id;
    '''
    result = engine.execute(sqla.text(query), {'id': id})\
        .first()
    return from_result(result, User) if result else None
示例#11
0
def create_offering_audio(offering_audio: OfferingAudio):
    query = '''
    insert into offering_audio (
        id,
        created_at,
        updated_at,
        offering_id,
        lang,
        path
    )
    values (
        :id,
        :created_at,
        :updated_at,
        :offering_id,
        :lang,
        :path
    )
    returning *;
    '''
    engine.execute(sqla.text(query), asdict(offering_audio))
示例#12
0
def retrieve_latest_offerings() -> List[Offering]:
    query = '''
    select *
    from offering o1
    where o1.created_at = (
        select max(created_at)
        from offering o2
        where o2.category = o1.category
        and o2.subcategory = o1.subcategory
        and o2.user_account_id = o1.user_account_id
    );
    '''
    offerings = engine.execute(sqla.text(query)).fetchall()
    return [from_result(o, Offering) for o in offerings]
示例#13
0
def delete_user(id: str):
    query = '''
    delete from user_account
    where id = :id;
    '''
    engine.execute(sqla.text(query), {'id': id})