Skip to content

Commit

Permalink
✨ Add 좋아요 기능 구현 [90%완성]
Browse files Browse the repository at this point in the history
좋아요 기능 구현 [90%완성] Related to : #52
  • Loading branch information
kimjingyoung committed Apr 25, 2024
1 parent 3ed2fe2 commit 4d4c04b
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.firestore
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import com.google.firebase.storage.StorageReference
Expand All @@ -16,15 +17,15 @@ import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext
import kr.co.lion.farming_customer.model.farmingLife.ActivityModel
import kr.co.lion.farming_customer.model.CropModel
import kr.co.lion.farming_customer.model.FarmModel
import kr.co.lion.farming_customer.model.farmingLife.FarmModel
import kr.co.lion.farming_customer.model.LikeModel
import kr.co.lion.farming_customer.model.CommunityPostModel
import kr.co.lion.farming_customer.model.RentalModel
import kr.co.lion.farming_customer.model.farminLifeTools.RentalModel

class LikeDao {

companion object {

companion object {
// 농산품 좋아요 리스트
suspend fun getLikeList(userIdx:Int): List<LikeModel>{
return withContext(Dispatchers.IO) {
Expand All @@ -48,19 +49,6 @@ class LikeDao {
}



// // 게시판 좋아요 리스트
// suspend fun getLikeList(userIdx:Int): List<LikeModel>{
// return withContext(Dispatchers.IO) {
// val likeListRef = Firebase.firestore.collection("LikeData")
// val query = likeListRef.whereEqualTo("like_status", 1)
// .whereEqualTo("like_user_idx", userIdx)
// .orderBy("like_idx", Query.Direction.ASCENDING)
// val querySnapshot = query.get().await()
// querySnapshot.map { it.toObject(LikeModel::class.java) }
// }
// }

// 좋아요 클릭한 게시판 데이터 가져오기
suspend fun getPostList(like_type_idx:Int): List<CommunityPostModel>{
return withContext(Dispatchers.IO) {
Expand All @@ -72,19 +60,6 @@ class LikeDao {
}
}

// // 주말농장 좋아요 리스트
// suspend fun getFarmLikeList(userIdx:Int): List<LikeModel>{
// return withContext(Dispatchers.IO) {
// val likeListRef = Firebase.firestore.collection("LikeData")
// val query = likeListRef.whereEqualTo("like_status", 1)
// .whereEqualTo("like_user_idx", userIdx)
// .whereEqualTo("like_type", 3)
// .orderBy("like_idx", Query.Direction.ASCENDING)
// val querySnapshot = query.get().await()
// querySnapshot.map { it.toObject(LikeModel::class.java) }
// }
// }

// 좋아요 클릭한 주말농장 데이터 가져오기
suspend fun getFarmList(like_type_idx:Int): List<FarmModel> {
return withContext(Dispatchers.IO) {
Expand All @@ -95,19 +70,6 @@ class LikeDao {
}
}

// // 체험활동 좋아요 리스트
// suspend fun getActivityLikeList(userIdx:Int): List<LikeModel>{
// return withContext(Dispatchers.IO) {
// val likeListRef = Firebase.firestore.collection("LikeData")
// val query = likeListRef.whereEqualTo("like_status", 1)
// .whereEqualTo("like_user_idx", userIdx)
// .whereEqualTo("like_type", 4)
// .orderBy("like_idx", Query.Direction.ASCENDING)
// val querySnapshot = query.get().await()
// querySnapshot.map { it.toObject(LikeModel::class.java) }
// }
// }

// 좋아요 클릭한 체험활동 데이터 가져오기
suspend fun getActivityList(like_type_idx:Int): List<ActivityModel> {
return withContext(Dispatchers.IO) {
Expand All @@ -118,19 +80,6 @@ class LikeDao {
}
}

// // 농기구 좋아요 리스트
// suspend fun getRentalLikeList(userIdx:Int): List<LikeModel>{
// return withContext(Dispatchers.IO) {
// val likeListRef = Firebase.firestore.collection("LikeData")
// val query = likeListRef.whereEqualTo("like_status", 1)
// .whereEqualTo("like_user_idx", userIdx)
// .whereEqualTo("like_type", 5)
// .orderBy("like_idx", Query.Direction.ASCENDING)
// val querySnapshot = query.get().await()
// querySnapshot.map { it.toObject(LikeModel::class.java) }
// }
// }

// 좋아요 클릭한 체험활동 데이터 가져오기
suspend fun getRentalList(like_type_idx:Int): List<RentalModel> {
return withContext(Dispatchers.IO) {
Expand Down Expand Up @@ -172,5 +121,53 @@ class LikeDao {

}

// 좋아요 클릭시 [ 찜 삭제 ]
suspend fun LikeListDelete(likeIdx: Int, newState: Int) {
val job1 = CoroutineScope(Dispatchers.IO).launch {
// 컬렉션에 접근할 수 있는 객체를 가져온다.
val collectionReference = com.google.firebase.Firebase.firestore.collection("LikeData")
// 컬렉션이 가지고 있는 문서들 중에 contentIdx 필드가 지정된 글 번호값하고 같은 Document 들을 가져온다.
val query = collectionReference.whereEqualTo("like_idx", likeIdx).get().await()

// 저장할 데이터를 담을 HashMap을 만들어준다.
val map = mutableMapOf<String, Any>()
map["like_status"] = newState.toLong()
// 저장한다.
// 가져온 문서 중 첫 번째 문서에 접근하여 데이터를 수정한다.
query.documents[0].reference.update(map)
}
job1.join()
}

// 좋아요 클릭시 [ 찜하기 ]
suspend fun LikeListAdd(likeType: Int, likeTypeIdx: Int, likeUserIdx: String) {
val job1 = CoroutineScope(Dispatchers.IO).launch {
var likeIdx:Int?

// 컬렉션에 접근할 수 있는 객체를 가져온다.
val collectionReference = com.google.firebase.Firebase.firestore.collection("LikeData")

// 마지막 문서 가져오기
val query = collectionReference.orderBy("like_idx").limitToLast(1).get().await()

if (query.documents.isNotEmpty()) {
val document = query.documents[0]
val lastDocument = document.id
likeIdx = lastDocument.toInt()+1
} else {
likeIdx = 1
}
val data = mutableMapOf<String, Any?>()
data["like_idx"] = likeIdx
data["like_status"] = 1
data["like_type"] = likeType
data["like_type_idx"] = likeTypeIdx
data["like_user_idx"] = likeUserIdx.toInt()

collectionReference.document(likeIdx.toString()).set(data).await()
}
job1.join()
}

}
}
Loading

0 comments on commit 4d4c04b

Please sign in to comment.