async def make_favorite(req: RequestForFavorite, database: Database = Depends(get_connection)): select_user_query = users.select().where(users.columns.id == req.user_id) user = await database.fetch_one(select_user_query) select_target_query = users.select().where( users.columns.id == req.target_user_id) target_user = await database.fetch_one(select_target_query) insert_query = favorites.insert() values = {"user_id": user.id, "target_user_id": target_user.id} await database.execute(insert_query, values) return {"result": "connect success"}
async def make_friends(req: RequestForMakeFriends, database: Database = Depends(get_connection)): query = users.select().where(users.columns.id == req.user_id) user1 = await database.fetch_one(query) query = users.select().where(users.columns.id == req.target_user_id) user2 = await database.fetch_one(query) query = friends.insert() values1 = {"user_1_id": user1.id, "user_2_id": user2.id} values2 = {"user_1_id": user2.id, "user_2_id": user1.id} await database.execute(query, values1) await database.execute(query, values2) return {"result": "connect success"}
async def post(street, house, title, content, activity): lat, lon = get_coord(street, house) values = { "city": get_city(), "street": street, "house": house, "lat": lat, "long": lon } location_id = await database.execute(query=location_create, values=values) # TODO get current user user_query = users.select() user = await database.fetch_one(query=user_query) user_id = dict(user)["id"] values = { "creator": user_id, "title": title, "content": content, "location_id": int(location_id), "activities_id": int(activity) } event = await database.execute(query=event_create, values=values) values = {"location_id": int(event), "activities_id": int(user_id)} return await database.execute(query=event_user_create, values=values)
async def login_user(req: RequestForLogin, database: Database = Depends(get_connection)): query = users.select().where(users.columns.email == req.email) user = await database.fetch_one(query) user = dict(user) if hashlib.sha256(req.password.encode( 'utf-8')).hexdigest() != user['hashed_password']: raise Exception('パスワードが違います') return user
async def _users(request: Request): query = users.select() result = await database.fetch_all(query=query) result = [dict(r) for r in result] return templates.TemplateResponse('users.html', context={ 'request': request, 'result': result })
async def users_create(user: UserCreate, database: Database = Depends(get_connection)): # validatorは省略 select_query = users.select().where(users.columns.user_id == user.user_id) user_data = await database.fetch_one(select_query) if user_data is not None: raise ValueError("This id is already registered.") query = users.insert() values = get_users_insert_dict(user) ret = await database.execute(query, values) return {**user.dict()}
async def users_update(user: UserUpdate, database: Database = Depends(get_connection)): # validatorは省略 select_query = users.select().where(users.columns.id == user.id) user_data = await database.fetch_one(select_query) query = users.update().where(users.columns.id == user.id) for k, v in user.dict().items(): if v == None and hasattr(user_data, k): setattr(user, k, getattr(user_data, k)) values = get_users_insert_dict(user) values['status_update_at'] = user_data.status_update_at if user.status != user_data.status: tdatetime = datetime.now() tstr = tdatetime.strftime('%Y/%m/%d') values['status_update_at'] = tstr # 自動invite select_invite_query = f'select * from user_chat_rooms where user_id = {user.id} and valid = 0' chat_rooms_data = await database.fetch_all(select_invite_query) if not len(chat_rooms_data): select_friend_query = f'select users.* from users left join friends on users.id = friends.user_1_id where friends.user_2_id = {user.id} and status = {user.status} and users.id != {user.id}' friend = await database.fetch_one(select_friend_query) if friend: friend = dict(friend) insert_query = chat_rooms.insert() chat_room_value = {"deleted": 0} await database.execute(insert_query, chat_room_value) select_query = "select * from chat_rooms order by id desc limit 1" chat_room_data = await database.fetch_one(select_query) chat_room_id = getattr(chat_room_data, "id") invite_insert_query = user_chat_rooms.insert() values1 = { "user_id": user.id, "chat_room_id": chat_room_id, "valid": 0 } values2 = { "user_id": friend['id'], "chat_room_id": chat_room_id, "valid": 0 } await database.execute(invite_insert_query, values1) await database.execute(invite_insert_query, values2) ret = await database.execute(query, values) return values
async def users_findone(user_id: str, database: Database = Depends(get_connection)): query = users.select().where(users.columns.user_id == user_id) return await database.fetch_one(query)
async def users_findall(request: Request, database: Database = Depends(get_connection)): query = users.select() return await database.fetch_all(query)
async def get_recommend(id: int, database: Database = Depends(get_connection)): select_user_query = users.select().where(users.columns.id == id) user = await database.fetch_one(select_user_query) query = f"select users.* from users left join friends on users.id = friends.user_1_id where friends.user_2_id = {id} and status = {user.status} and users.id != {id}" return await database.fetch_all(query)