diff --git a/db/db.go b/db/db.go index abd66b7..7f20745 100644 --- a/db/db.go +++ b/db/db.go @@ -15,13 +15,14 @@ import ( type Table struct { DynamoDbClient *dynamodb.Client TableName string + IndexName string } type News struct { Id int `json:"id"` Title string `json:"title"` Text string `json:"text"` - Timestamp int `json:"time"` + CreatedAt int `json:"time"` Author string `json:"by"` Url string `json:"url"` Score int `json:"score"` @@ -45,15 +46,15 @@ func CreateTable(basics Table) (*types.TableDescription, error) { AttributeName: aws.String("Id"), KeyType: types.KeyTypeHash, }, { - AttributeName: aws.String("Title"), + AttributeName: aws.String("CreatedAt"), KeyType: types.KeyTypeRange, }}, AttributeDefinitions: []types.AttributeDefinition{{ AttributeName: aws.String("Id"), AttributeType: types.ScalarAttributeTypeN, }, { - AttributeName: aws.String("Title"), - AttributeType: types.ScalarAttributeTypeS, + AttributeName: aws.String("CreatedAt"), + AttributeType: types.ScalarAttributeTypeN, }}, TableName: aws.String(basics.TableName), ProvisionedThroughput: &types.ProvisionedThroughput{ @@ -115,3 +116,30 @@ func AddNewsBatch(basics Table, news []News, batchSize int) (int, error) { return written, err } + +func ReadTodayNews(basics Table, timeStart int, timeEnd int) ([]News, error) { + var news []News + params, err := attributevalue.MarshalList([]interface{}{timeStart, timeEnd}) + if err != nil { + panic(err) + } + + response, err := basics.DynamoDbClient.ExecuteStatement(context.TODO(), &dynamodb.ExecuteStatementInput{ + Statement: aws.String( + fmt.Sprintf("SELECT * FROM \"%v\" WHERE CreatedAt>? AND CreatedAt