From 7699c1b883ff3d4dc12326b48fc38f769898f7fd Mon Sep 17 00:00:00 2001 From: emkartal1 Date: Sat, 2 Dec 2023 15:30:30 +0100 Subject: [PATCH] add Capsule collection --- Sources/justMUSIC/lib/model/Capsule.dart | 34 ++++++++++++++++ .../lib/services/CapsuleService.dart | 33 ++++++++++++++++ .../justMUSIC/lib/services/MusicService.dart | 2 +- .../justMUSIC/lib/services/PostService.dart | 39 +++++-------------- .../lib/view_model/PostViewModel.dart | 5 ++- 5 files changed, 81 insertions(+), 32 deletions(-) create mode 100644 Sources/justMUSIC/lib/model/Capsule.dart create mode 100644 Sources/justMUSIC/lib/services/CapsuleService.dart diff --git a/Sources/justMUSIC/lib/model/Capsule.dart b/Sources/justMUSIC/lib/model/Capsule.dart new file mode 100644 index 0000000..69b39e3 --- /dev/null +++ b/Sources/justMUSIC/lib/model/Capsule.dart @@ -0,0 +1,34 @@ +import 'package:tuple/tuple.dart'; + +import 'Music.dart'; + +class Post { + final String _id; + late Music _music; + Tuple2 _location; + DateTime _date; + + // Constructor + Post(this._id, this._location, this._date); + + //Getters and setters + String get id => _id; + + Music get music => _music; + + set music(Music value) { + _music = value; + } + + Tuple2 get location => _location; + + set location(Tuple2 value) { + _location = value; + } + + DateTime get date => _date; + + set date(DateTime value) { + _date = value; + } +} diff --git a/Sources/justMUSIC/lib/services/CapsuleService.dart b/Sources/justMUSIC/lib/services/CapsuleService.dart new file mode 100644 index 0000000..95ff4e8 --- /dev/null +++ b/Sources/justMUSIC/lib/services/CapsuleService.dart @@ -0,0 +1,33 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; + +class CapsuleService { + Future> recapSevenDays(String id) async { + List recapList = []; + + DateTime sevenDaysAgo = DateTime.now().subtract(Duration(days: 6)); + + QuerySnapshot> response = await FirebaseFirestore + .instance + .collection("capsules") + .where("user_id", isEqualTo: id) + .get(); + + List?> capsuleList = response.docs + .map((DocumentSnapshot> doc) => doc.data()) + .toList(); + + for (int i = 0; i < 7; i++) { + DateTime date = sevenDaysAgo.add(Duration(days: i)); + bool capsuleExists = capsuleList.any((post) => + post?["date"] != null && + post?["date"].toDate().year == date.year && + post?["date"].toDate().month == date.month && + post?["date"].toDate().day == date.day); + + recapList.add(capsuleExists); + } + + return recapList; + } + +} diff --git a/Sources/justMUSIC/lib/services/MusicService.dart b/Sources/justMUSIC/lib/services/MusicService.dart index 09d1cf9..684571e 100644 --- a/Sources/justMUSIC/lib/services/MusicService.dart +++ b/Sources/justMUSIC/lib/services/MusicService.dart @@ -37,7 +37,7 @@ class MusicService { List> capsules = []; var querySnapshot = await FirebaseFirestore.instance - .collection('posts') + .collection('capsules') .where('user_id', isEqualTo: idUser) .where('date', isGreaterThanOrEqualTo: DateTime(year, month)) .where('date', isLessThan: DateTime(year, month + 1)) diff --git a/Sources/justMUSIC/lib/services/PostService.dart b/Sources/justMUSIC/lib/services/PostService.dart index a180f40..d7fab40 100644 --- a/Sources/justMUSIC/lib/services/PostService.dart +++ b/Sources/justMUSIC/lib/services/PostService.dart @@ -23,6 +23,15 @@ class PostService { var userRef = MyApp.db.collection("users").doc(id); + var capsule = { + "user_id": id, + "date": DateTime.now(), + "place": [location?.item1, location?.item2], + "song_id": idMusic, + }; + + await MyApp.db.collection("capsules").doc(postAdd.id).set(capsule); + await MyApp.db.runTransaction((transaction) async { var userSnapshot = await transaction.get(userRef); if (userSnapshot.exists) { @@ -122,6 +131,7 @@ class PostService { return response.docs; } + Future getAvailable(String idUser) async { DateTime today = DateTime.now(); @@ -141,35 +151,6 @@ class PostService { return !isTodayAvailable; } - Future> recapSevenDays(String id) async { - List recapList = []; - - DateTime sevenDaysAgo = DateTime.now().subtract(Duration(days: 6)); - - QuerySnapshot> response = await FirebaseFirestore - .instance - .collection("posts") - .where("user_id", isEqualTo: id) - .get(); - - List?> postList = response.docs - .map((DocumentSnapshot> doc) => doc.data()) - .toList(); - - for (int i = 0; i < 7; i++) { - DateTime date = sevenDaysAgo.add(Duration(days: i)); - bool postExists = postList.any((post) => - post?["date"] != null && - post?["date"].toDate().year == date.year && - post?["date"].toDate().month == date.month && - post?["date"].toDate().day == date.day); - - recapList.add(postExists); - } - - return recapList; - } - Future> getLikesByPostId(String id) async { var response = await FirebaseFirestore.instance.collection("posts").doc(id).get(); diff --git a/Sources/justMUSIC/lib/view_model/PostViewModel.dart b/Sources/justMUSIC/lib/view_model/PostViewModel.dart index 061f7d3..b49fa38 100644 --- a/Sources/justMUSIC/lib/view_model/PostViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/PostViewModel.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'package:justmusic/model/Post.dart'; +import 'package:justmusic/services/CapsuleService.dart'; import 'package:justmusic/services/PostService.dart'; import 'package:tuple/tuple.dart'; @@ -14,6 +15,7 @@ class PostViewModel { var lastPostFriend; var lastPostDiscovery; final PostService _postService = PostService(); + final CapsuleService _capsuleService = CapsuleService(); // Constructor PostViewModel(); @@ -114,7 +116,7 @@ class PostViewModel { Future> recapSevenDays(String id) async { try { - return await _postService.recapSevenDays(id); + return await _capsuleService.recapSevenDays(id); } catch (e) { print(e); rethrow; @@ -145,7 +147,6 @@ class PostViewModel { print(bool); return bool; } catch (e) { - print("haaaaaaaaa"); rethrow; } }