Skip to content

Commit

Permalink
Merge pull request #381 from sot528/ALIS-4119
Browse files Browse the repository at this point in the history
add QV
  • Loading branch information
hayago authored Aug 21, 2019
2 parents 0971c24 + af532ab commit 4a67734
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"lint": "npx cfn-lint validate",
"build": "python make_deploy_zip.py --target 'src/handlers/labo/**/handler.py'",
"deploy": "npx sls deploy",
"bd": "npm run build && npm run deploy",
"remove": "npx sls remove",
"invoke": "npx sls invoke -f",
"cilint": "circleci config validate -c .circleci/config.yml"
Expand Down
43 changes: 42 additions & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ provider:
restApi: true
environment:
# Labo
MAJORITY_JUDGEMENT_TABLE_NAME: "${env:ALIS_APP_ID}-${self:provider.stage}-MajorityJudgment"
LABO_S3_BUCKET_NAME: ${env:ALIS_APP_ID}-${self:provider.stage}-s3-bucket
MAJORITY_JUDGEMENT_TABLE_NAME: "${env:ALIS_APP_ID}-${self:provider.stage}-MajorityJudgment"
QUADRATIC_VOTING_TABLE_NAME: "${env:ALIS_APP_ID}-${self:provider.stage}-QuadraticVoting"

# 既存システム
ARTICLE_INFO_TABLE_NAME: ${ssm:${env:ALIS_APP_ID}ssmArticleInfoTableName}
Expand Down Expand Up @@ -102,6 +103,32 @@ functions:
name: cognitoUserPool
arn: ${ssm:${env:ALIS_APP_ID}ssmCognitoUserPoolArn}

QuadraticVotingIndex:
description: "QuadraticVotingの画面を表示する。既に登録済であれば登録済画面を表示する"
handler: handler.lambda_handler
package:
artifact: ./deploy/labo_n_quadratic_voting_index.zip
events:
- http:
method: get
path: /labo/n/quadratic_voting
authorizer:
name: cognitoUserPool
arn: ${ssm:${env:ALIS_APP_ID}ssmCognitoUserPoolArn}

QuadraticVotingCreate:
description: "QuadraticVotingの投票を行う"
handler: handler.lambda_handler
package:
artifact: ./deploy/labo_n_quadratic_voting_create.zip
events:
- http:
method: post
path: /labo/n/quadratic_voting
authorizer:
name: cognitoUserPool
arn: ${ssm:${env:ALIS_APP_ID}ssmCognitoUserPoolArn}

LicenseTokenFileUploadUrl:
description: "ライセンストークンに対応するファイルのアップロード用URLを取得する"
handler: handler.lambda_handler
Expand Down Expand Up @@ -183,6 +210,20 @@ resources:
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
BillingMode: PAY_PER_REQUEST
QuadraticVotingDB:
Type: AWS::DynamoDB::Table
DeletionPolicy: 'Retain'
Properties:
TableName: "${env:ALIS_APP_ID}-${self:provider.stage}-QuadraticVoting"
AttributeDefinitions:
- AttributeName: user_id
AttributeType: S
KeySchema:
- AttributeName: user_id
KeyType: HASH
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
BillingMode: PAY_PER_REQUEST

# S3
S3BucketLabo:
Expand Down
10 changes: 10 additions & 0 deletions src/handlers/labo/n/quadratic_voting/create/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
import boto3
from quadratic_voting_create import LaboNQuadraticVotingCreate

dynamodb = boto3.resource('dynamodb')


def lambda_handler(event, context):
target = LaboNQuadraticVotingCreate(event, context, dynamodb)
return target.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
import os
import time
import math
from lambda_base import LambdaBase
from jsonschema import validate, ValidationError

options_count = 6 # 選択肢の数
credit_per_user = 100 # ユーザに付与されるクレジット(持ち越しは考慮しない)
maximum = math.sqrt(credit_per_user) # 許容される最大の値


class LaboNQuadraticVotingCreate(LambdaBase):
def get_schema(self):
opt = {
"type": "integer",
"minimum": 0,
"maximum": maximum
}

required = []
properties = {}
for i in range(options_count):
key = 'opt_' + str(i + 1)
properties[key] = opt
required.append(key)

for i in range(options_count):
properties['opt_' + str(i + 1)] = opt

return {
"type": "object",
"properties": properties,
"required": required
}

def validate_params(self):
validate(self.params, self.get_schema())

totalVotedValue = 0
for key in self.params:
totalVotedValue += self.params[key] ** 2

# 投票の合計がCreditの限界を超えている場合
if totalVotedValue > credit_per_user:
raise ValidationError('Invalid')

def exec_main_proc(self):
table = self.dynamodb.Table(os.environ['QUADRATIC_VOTING_TABLE_NAME'])

user_id = self.event['requestContext']['authorizer']['claims']['cognito:username']
if not LaboNQuadraticVotingCreate.__is_exists(table, user_id):
item = {
'user_id': user_id,
'created_at': int(time.time())
}

for key, value in self.params.items():
item[key] = value

table.put_item(
Item=item,
ConditionExpression='attribute_not_exists(user_id)'
)

return {
'statusCode': 200
}

@staticmethod
def __is_exists(table, user_id):
result = table.get_item(Key={'user_id': user_id}).get('Item')

return False if result is None else True
10 changes: 10 additions & 0 deletions src/handlers/labo/n/quadratic_voting/index/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
import boto3
from quadratic_voting_index import LaboNQuadraticVotingIndex

dynamodb = boto3.resource('dynamodb')


def lambda_handler(event, context):
target = LaboNQuadraticVotingIndex(event, context, dynamodb)
return target.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
import os
import json
from lambda_base import LambdaBase


class LaboNQuadraticVotingIndex(LambdaBase):
def get_schema(self):
pass

def validate_params(self):
pass

def exec_main_proc(self):
table = self.dynamodb.Table(os.environ['QUADRATIC_VOTING_TABLE_NAME'])

user_id = self.event['requestContext']['authorizer']['claims']['cognito:username']
exists = LaboNQuadraticVotingIndex.__is_exists(table, user_id)

return {
'statusCode': 200,
'body': json.dumps({'exists': exists})
}

@staticmethod
def __is_exists(table, user_id):
result = table.get_item(Key={'user_id': user_id}).get('Item')

return False if result is None else True

0 comments on commit 4a67734

Please sign in to comment.