Skip to content

Commit

Permalink
db: use unix time to query news by date range
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederick-S committed May 17, 2020
1 parent 96d1a72 commit 3a3a78a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
7 changes: 6 additions & 1 deletion lib/model/news.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class News {

String publishedAtUtc;

int publishedAtEpoch;

News();

factory News.fromJson(Map<String, dynamic> json) {
Expand All @@ -21,6 +23,8 @@ class News {
news.body = json['body'];
news.imageUrl = json['imageUrl'];
news.publishedAtUtc = json['publishedAtUtc'];
news.publishedAtEpoch =
DateTime.parse(news.publishedAtUtc).millisecondsSinceEpoch;

return news;
}
Expand All @@ -32,7 +36,8 @@ class News {
'titleWithRuby': titleWithRuby,
'body': body,
'imageUrl': imageUrl,
'publishedAtUtc': publishedAtUtc
'publishedAtUtc': publishedAtUtc,
'publishedAtEpoch': publishedAtEpoch
};
}
}
2 changes: 1 addition & 1 deletion lib/repository/base_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class BaseRepository {
batch.execute(
'CREATE TABLE IF NOT EXISTS config(id INTEGER PRIMARY KEY, newsFetchedStartUtc TEXT, newsFetchedEndUtc TEXT)');
batch.execute(
'CREATE TABLE IF NOT EXISTS news(newsId TEXT PRIMARY KEY, title TEXT, titleWithRuby TEXT, body TEXT, imageUrl TEXT, publishedAtUtc TEXT)');
'CREATE TABLE IF NOT EXISTS news(newsId TEXT PRIMARY KEY, title TEXT, titleWithRuby TEXT, body TEXT, imageUrl TEXT, publishedAtUtc TEXT, publishedAtEpoch INTEGER)');

return batch.commit();
},
Expand Down
6 changes: 3 additions & 3 deletions lib/repository/news_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import 'package:sqflite/sqflite.dart';
import 'base_repository.dart';

class NewsRepository extends BaseRepository {
Future<List<News>> getNews(String startDate, String endDate) async {
Future<List<News>> getNews(DateTime startDate, DateTime endDate) async {
final database = await getDatabase();
final rows = await database.rawQuery(
'select * from news where publishedAtUtc >= ? and publishedAtUtc <= ?',
[startDate, endDate]);
'select * from news where publishedAtEpoch >= ? and publishedAtEpoch <= ?',
[startDate.millisecondsSinceEpoch, endDate.millisecondsSinceEpoch]);

return List.generate(rows.length, (i) {
final row = rows[i];
Expand Down
3 changes: 1 addition & 2 deletions lib/service/cached_news_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class CachedNewsService {

if (config != null && (_newsFetched(config, startDate, endDate))) {
try {
return await _newsRepository.getNews(
startDate.toIso8601String(), endDate.toIso8601String());
return await _newsRepository.getNews(startDate, endDate);
} catch (error, stackTrace) {
ErrorReporter.reportError(error, stackTrace);

Expand Down

0 comments on commit 3a3a78a

Please sign in to comment.