-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…ransaction Feat/#227 apply firestore transaction
- Loading branch information
Showing
25 changed files
with
325 additions
and
227 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
abstract interface class TransactionService { | ||
Future<bool> run(Function callBack); | ||
} |
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
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,22 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:weaco/core/db/transaction_service.dart'; | ||
|
||
class FirestoreService implements TransactionService { | ||
final FirebaseFirestore _fireStore; | ||
|
||
const FirestoreService({ | ||
required FirebaseFirestore fireStore, | ||
}) : _fireStore = fireStore; | ||
|
||
@override | ||
Future<bool> run(Function callBack) async { | ||
return _fireStore.runTransaction<bool>((transaction) async { | ||
return await callBack(transaction); | ||
}).then( | ||
(value) => true, | ||
onError: (e) { | ||
throw Exception('피드 업로드에 실패 하였습니다.'); | ||
}, | ||
); | ||
} | ||
} |
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
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,65 +1,100 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:weaco/core/db/transaction_service.dart'; | ||
import 'package:weaco/data/feed/data_source/remote_feed_data_source.dart'; | ||
import 'package:weaco/data/user/data_source/remote_user_profile_data_source.dart'; | ||
import 'package:weaco/domain/feed/model/feed.dart'; | ||
import 'package:weaco/domain/feed/repository/feed_repository.dart'; | ||
import 'package:weaco/domain/feed/repository/ootd_feed_repository.dart'; | ||
import 'package:weaco/domain/file/repository/file_repository.dart'; | ||
import 'package:weaco/domain/user/repository/user_profile_repository.dart'; | ||
|
||
class OotdFeedRepositoryImpl implements OotdFeedRepository { | ||
final FileRepository _fileRepository; | ||
final FeedRepository _feedRepository; | ||
final UserProfileRepository _userProfileRepository; | ||
|
||
final RemoteFeedDataSource _remoteFeedDataSource; | ||
final RemoteUserProfileDataSource _remoteUserProfileDataSource; | ||
|
||
final TransactionService _firestoreService; | ||
|
||
const OotdFeedRepositoryImpl({ | ||
required FileRepository fileRepository, | ||
required FeedRepository feedRepository, | ||
required UserProfileRepository userProfileRepository, | ||
required RemoteFeedDataSource remoteFeedDataSource, | ||
required RemoteUserProfileDataSource remoteUserProfileDataSource, | ||
required TransactionService firestoreService, | ||
}) : _fileRepository = fileRepository, | ||
_feedRepository = feedRepository, | ||
_userProfileRepository = userProfileRepository; | ||
_remoteFeedDataSource = remoteFeedDataSource, | ||
_remoteUserProfileDataSource = remoteUserProfileDataSource, | ||
_firestoreService = firestoreService; | ||
|
||
/// 피드 저장 및 수정 | ||
@override | ||
Future<bool> saveOotdFeed({required Feed feed}) async { | ||
return feed.id == null | ||
? await _save(feed: feed) | ||
: await _update(feed: feed); | ||
return _firestoreService.run((Transaction transaction) async { | ||
return feed.id == null | ||
? await _save(transaction: transaction, feed: feed) | ||
: await _update(transaction: transaction, feed: feed); | ||
}); | ||
} | ||
|
||
/// 피드 저장 | ||
Future<bool> _save({required Feed feed}) async { | ||
Future<bool> _save({ | ||
required Transaction transaction, | ||
required Feed feed, | ||
}) async { | ||
final List<String> path = await _fileRepository.saveOotdImage(); | ||
|
||
final saveResult = await _feedRepository.saveFeed( | ||
editedFeed: feed.copyWith(imagePath: path[0], thumbnailImagePath: path[1])); | ||
await _remoteFeedDataSource.saveFeed( | ||
transaction: transaction, | ||
feed: feed.copyWith(imagePath: path[0], thumbnailImagePath: path[1]), | ||
); | ||
|
||
await _updateMyFeedCount(1); | ||
await _updateMyFeedCount( | ||
transaction: transaction, | ||
count: 1, | ||
); | ||
|
||
return saveResult; | ||
return true; | ||
} | ||
|
||
/// 피드 수정 | ||
Future<bool> _update({required Feed feed}) async { | ||
final updateResult = await _feedRepository.saveFeed(editedFeed: feed); | ||
Future<bool> _update({ | ||
required Transaction transaction, | ||
required Feed feed, | ||
}) async { | ||
final updateResult = await _remoteFeedDataSource.saveFeed( | ||
transaction: transaction, | ||
feed: feed, | ||
); | ||
|
||
return updateResult; | ||
} | ||
|
||
/// 피드 삭제 | ||
@override | ||
Future<bool> removeOotdFeed({required String id}) async { | ||
await _feedRepository.deleteFeed(id: id); | ||
return _firestoreService.run((Transaction transaction) async { | ||
await _remoteFeedDataSource.deleteFeed( | ||
transaction: transaction, | ||
id: id, | ||
); | ||
|
||
await _updateMyFeedCount(-1); | ||
await _updateMyFeedCount( | ||
transaction: transaction, | ||
count: -1, | ||
); | ||
|
||
return true; | ||
return true; | ||
}); | ||
} | ||
|
||
/// 유저 피드 카운트 업데이트 | ||
Future<void> _updateMyFeedCount(int count) async { | ||
final myProfile = await _userProfileRepository.getMyProfile(); | ||
Future<void> _updateMyFeedCount({ | ||
required Transaction transaction, | ||
required int count, | ||
}) async { | ||
final myProfile = await _remoteUserProfileDataSource.getUserProfile(); | ||
|
||
_userProfileRepository.updateUserProfile( | ||
userProfile: | ||
myProfile!.copyWith(feedCount: myProfile.feedCount + count)); | ||
await _remoteUserProfileDataSource.updateUserProfile( | ||
transaction: transaction, | ||
userProfile: myProfile.copyWith(feedCount: myProfile.feedCount + count), | ||
); | ||
} | ||
} |
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.