def exec_main_proc(self): user_id = self.event['requestContext']['authorizer']['claims'][ 'cognito:username'] comment_liked_user_table = self.dynamodb.Table( os.environ['COMMENT_LIKED_USER_TABLE_NAME']) query_params = { 'IndexName': 'article_id-index', 'KeyConditionExpression': Key('article_id').eq(self.params['article_id']), } result = DBUtil.query_all_items(comment_liked_user_table, query_params) comment_ids = [ liked_user['comment_id'] for liked_user in result if liked_user['user_id'] == user_id ] return { 'statusCode': 200, 'body': json.dumps({'comment_ids': comment_ids}) }
def test_query_all_items_with_no_limit(self): article_pv_user_table = self.dynamodb.Table(os.environ['ARTICLE_PV_USER_TABLE_NAME']) query_params = { 'IndexName': 'target_date-sort_key-index', 'KeyConditionExpression': Key('target_date').eq('2018-05-01') } response = DBUtil.query_all_items(article_pv_user_table, query_params) self.assertEqual(len(response), 4)
def test_query_all_items_with_limit(self): article_pv_user_table = self.dynamodb.Table(os.environ['ARTICLE_PV_USER_TABLE_NAME']) # ユースケースとしては1MBを超え、レスポンスにLastEvaluatedKeyが付与されて返ってくる場合だが # Limitを付与した際も同じレスポンスなのでLimitで代用している query_params = { 'IndexName': 'target_date-sort_key-index', 'KeyConditionExpression': Key('target_date').eq('2018-05-01'), 'Limit': 1 } response = DBUtil.query_all_items(article_pv_user_table, query_params) self.assertEqual(len(response), 4)
def exec_main_proc(self): user_id = self.event['requestContext']['authorizer']['claims']['cognito:username'] paid_articles_table = self.dynamodb.Table(os.environ['PAID_ARTICLES_TABLE_NAME']) query_params = { 'IndexName': 'user_id-sort_key-index', 'KeyConditionExpression': Key('user_id').eq(user_id) } result = DBUtil.query_all_items(paid_articles_table, query_params) article_ids = [paid_article['article_id'] for paid_article in result if paid_article['user_id'] == user_id and paid_article['status'] == 'done'] return { 'statusCode': 200, 'body': json.dumps({'article_ids': article_ids}) }
def __get_token_send_value_today(self, user_id): # 今日日付の文字列を取得 target_date = time.strftime('%Y-%m-%d', time.gmtime(int(time.time()))) # 今日日付に紐づく出金データを全て取得する token_send_table = self.dynamodb.Table( os.environ['TOKEN_SEND_TABLE_NAME']) query_params = { 'IndexName': 'target_date-user_id-index', 'KeyConditionExpression': Key('target_date').eq(target_date) & Key('user_id').eq(user_id) } result = DBUtil.query_all_items(token_send_table, query_params) # 今日日付の出金額の合計を算出(doing の状態も含める) # todo: 当 API 処理完了時に send_status が doing となっていたデータについては更新されない状態となっている。 # 別途 batch 処理等で doing 状態のデータを done or fail に更新させる必要がある。 return sum([ Decimal(i.get('send_value')) for i in result if i.get('send_status') in ['done', 'doing'] ])
def exec_main_proc(self): user_id = self.event['requestContext']['authorizer']['claims'][ 'cognito:username'] token_distribution_table = self.dynamodb.Table( os.environ['TOKEN_DISTRIBUTION_TABLE_NAME']) query_params = { 'IndexName': 'user_id-sort_key-index', 'KeyConditionExpression': Key('user_id').eq(user_id) } items = DBUtil.query_all_items(token_distribution_table, query_params) result = {'article': 0, 'like': 0, 'tip': 0, 'bonus': 0} for item in items: result[item['distribution_type']] += item['quantity'] return { 'statusCode': 200, 'body': json.dumps(result, cls=DecimalEncoder) }