-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
4,055 additions
and
2,503 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,27 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:intl/intl.dart'; | ||
import 'package:nakdong_river/domain/measurement.dart'; | ||
import 'package:nakdong_river/domain/position.dart'; | ||
import 'package:nakdong_river/domain/repository.dart'; | ||
import 'package:nakdong_river/domain/measurement_repository.dart'; | ||
|
||
class RepositoryFirebaseImpl extends Repository { | ||
class RepositoryFirebaseImpl extends MeasurementRepository { | ||
static FirebaseFirestore instance = FirebaseFirestore.instance; | ||
|
||
@override | ||
Future<List<String>> getDepths(Position position) async { | ||
var response = await instance.collection(position.code).get(); | ||
return response.docs.map((e) => e.id).toList(); | ||
} | ||
|
||
@override | ||
Future<List<Measurement>> getRecentData( | ||
Position position, String depth) async { | ||
final today = DateTime.now(); | ||
String formattedDate = DateFormat('yyyy-MM-dd').format(today); | ||
Future<List<Measurement>> getRecentData(Position position) async { | ||
List<Measurement> dateList = []; | ||
|
||
var response = await instance | ||
.collection(position.code) | ||
.doc(depth) | ||
.collection(formattedDate) | ||
.orderBy('msmtTm', descending: true) | ||
.collectionGroup("data") | ||
.where("mesure_location_code", isEqualTo: position.code) | ||
.where("mesure_salinity", isNotEqualTo: 0) | ||
.orderBy("mesure_date", descending: true) | ||
.limit(10) | ||
.get(); | ||
|
||
for (final doc in response.docs) { | ||
dateList.add(Measurement.fromFireStore(doc.data(), position, depth)); | ||
dateList.add(Measurement.fromMap(doc.data())); | ||
} | ||
|
||
if (response.docs.length < 10) { | ||
formattedDate = DateFormat('yyyy-MM-dd') | ||
.format(today.subtract(const Duration(days: 1))); | ||
response = await instance | ||
.collection(position.code) | ||
.doc(depth) | ||
.collection(formattedDate) | ||
.orderBy('msmtTm', descending: true) | ||
.limit(10 - response.docs.length) | ||
.get(); | ||
|
||
for (final doc in response.docs) { | ||
dateList.add(Measurement.fromFireStore(doc.data(), position, depth)); | ||
} | ||
} | ||
return dateList; | ||
} | ||
|
||
@override | ||
Future<Measurement> getRecentDataOne(Position position, String depth) async { | ||
final today = DateTime.now(); | ||
String formattedDate = DateFormat('yyyy-MM-dd').format(today); | ||
|
||
var response = await instance | ||
.collection(position.code) | ||
.doc(depth) | ||
.collection(formattedDate) | ||
.orderBy('msmtTm', descending: true) | ||
.limit(1) | ||
.get(); | ||
|
||
if (response.docs.isEmpty) { | ||
formattedDate = DateFormat('yyyy-MM-dd') | ||
.format(today.subtract(const Duration(days: 1))); | ||
response = await instance | ||
.collection(position.code) | ||
.doc(depth) | ||
.collection(formattedDate) | ||
.orderBy('msmtTm', descending: true) | ||
.limit(1) | ||
.get(); | ||
} | ||
|
||
return Measurement.fromFireStore( | ||
response.docs.first.data(), position, depth); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,47 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:nakdong_river/domain/position.dart'; | ||
|
||
/// 측정 데이터 모델 | ||
class Measurement { | ||
final Position position; | ||
final String depth; | ||
final Timestamp time; | ||
final num salinity; | ||
final num temperature; | ||
final Timestamp date; | ||
final num depth; | ||
final String location; | ||
final String locationCode; | ||
final double salinity; | ||
final double temperature; | ||
|
||
const Measurement({ | ||
Measurement({ | ||
required this.date, | ||
required this.depth, | ||
required this.time, | ||
required this.position, | ||
required this.location, | ||
required this.locationCode, | ||
required this.salinity, | ||
required this.temperature, | ||
}); | ||
|
||
/// FireStore에서 받아온 데이터를 Measurement로 변환한다. | ||
static Measurement fromFireStore( | ||
Map<String, dynamic> json, Position position, String depth) { | ||
factory Measurement.fromMap(Map<String, dynamic> map) { | ||
return Measurement( | ||
depth: depth, | ||
position: position, | ||
time: json['msmtTm'], | ||
salinity: json['saln'], | ||
temperature: json['wtep'], | ||
date: map['mesure_date'], | ||
depth: map['mesure_depths'], | ||
location: map['mesure_location'], | ||
locationCode: map['mesure_location_code'], | ||
salinity: map['mesure_salinity'], | ||
temperature: map['mesure_temperature'], | ||
); | ||
} | ||
|
||
Map<String, dynamic> toMap() { | ||
return { | ||
'mesure_date': date, | ||
'mesure_depths': depth, | ||
'mesure_location': location, | ||
'mesure_location_code': locationCode, | ||
'mesure_salinity': salinity, | ||
'mesure_temperature': temperature, | ||
}; | ||
} | ||
|
||
@override | ||
String toString() { | ||
return 'Measurement{position: ${position.name}, depth: $depth, time: ${time.toDate().toString()}, salinity: $salinity, temperature: $temperature}'; | ||
return 'Measurement{date: $date, depth: $depth, location: $location, locationCode: $locationCode, salinity: $salinity, temperature: $temperature}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import 'package:nakdong_river/domain/measurement.dart'; | ||
import 'package:nakdong_river/domain/position.dart'; | ||
|
||
abstract class MeasurementRepository { | ||
/// 저장되어 있는 데이터 중 가장 최근의 10개의 데이터를 반환한다. | ||
Future<List<Measurement>> getRecentData(Position position); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.