Skip to content

Conversation

@ljdongz
Copy link
Collaborator

@ljdongz ljdongz commented Apr 21, 2025

#️⃣ 연관된 이슈

📝 작업 내용

  • Link 데이터를 Firestore에 저장하기 위한 CRUD 로직을 구현했습니다.

🎨 스크린샷

💬 추가 설명

특정 조건에 해당하는 링크 리스트 가져오기

원래는 Firebase에서 즐겨찾기한 링크 불러오기, 읽지 않은 링크 불러오기 로직을 추가로 구현하려고 했지만, 앱 진입 시 전체 링크 데이터를 먼저 불러오기 때문에 이 데이터를 기반으로 로컬에서 필터링하는 방식으로 구현했습니다.

다만, 저장된 링크가 많아질 경우 전체 데이터를 한번에 불러오는 것이 부담될 수 있기 때문에 이후에는 페이징이 필요할 것으로 예상됩니다.

이와 함께 특정 조건(즐겨찾기, 읽지 않음 등)에 해당하는 링크만 가져오는 서버 쿼리 방식도 필요할 것으로 예상됩니다.
그 이유는, 초기 로딩 시 제한된 범위의 데이터만 불러오고 이를 기반으로 필터링하게 되면, 실제로 조건에 해당하는 모든 링크가 아닌, 현재 로컬에 존재하는 일부 데이터만 보여지게 되기 때문입니다.

@ljdongz ljdongz requested a review from f-lab-barry April 21, 2025 07:35
@ljdongz ljdongz self-assigned this Apr 21, 2025
@ljdongz ljdongz linked an issue Apr 21, 2025 that may be closed by this pull request
@sonarqubecloud
Copy link

Comment on lines +9 to +19

struct Link {
var id: String = ""
var url: String
var title: String?
var thumbnailUrl: String?
var faviconUrl: String?
var createdBy: Date = .now
var lastAccessedAt: Date?
var folderId: String?
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id 프로퍼티를 옵셔널타입 대신 기본값을 설정해 주었습니다.
link를 firebase에 추가한 시점에 id값이 생성되고, 그 이후부터는 필수적으로 id가 존재하기 때문에 옵셔널이 아닌 기본값을 넣어주었습니다.
createdBy 프로퍼티는 생성 시점을 나타내기 위해 기본 값을 현재 날짜로 넣어주었습니다.

Comment on lines +27 to +38
/// 3. Firestore에 추가
try await withCheckedThrowingContinuation { (continuation: VoidCheckedContinuation) in
do {
try linkDocRef.setData(from: linkDTO) { error in
if let error { continuation.resume(throwing: error) }
else { continuation.resume(returning: ()) }
}
} catch {
continuation.resume(throwing: error)
}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async로 작성된 setData(from:) 메서드가 존재하지 않았기 때문에 콜백함수를 비동기 함수로 전환시켰습니다

Comment on lines +9 to +43

struct LinkDTO: Codable {
var id: String
var url: String
var title: String?
var thumbnailUrl: String?
var faviconUrl: String?
var createdBy: Date
var lastAccessedAt: Date?
var folderId: String?

init(_ link: Link) {
self.id = link.id
self.url = link.url
self.title = link.title
self.thumbnailUrl = link.thumbnailUrl
self.faviconUrl = link.faviconUrl
self.createdBy = link.createdBy
self.lastAccessedAt = link.lastAccessedAt
self.folderId = link.folderId
}

func toEntity() -> Link {
Link(
id: self.id,
url: self.url,
title: self.title,
thumbnailUrl: self.thumbnailUrl,
faviconUrl: self.faviconUrl,
createdBy: self.createdBy,
lastAccessedAt: self.lastAccessedAt,
folderId: self.folderId
)
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entity -> DTO 타입 변환, DTO -> Entity 타입 변환을 DTO 타입을 통해서만 이루어질 수 있도록 init 메서드와 toEntity() 메서드를 정의했습니다.

Comment on lines +20 to +29
init(_ link: Link) {
self.id = link.id
self.url = link.url
self.title = link.title
self.thumbnailUrl = link.thumbnailUrl
self.faviconUrl = link.faviconUrl
self.createdBy = link.createdBy
self.lastAccessedAt = link.lastAccessedAt
self.folderId = link.folderId
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요 로직은 레파지토리로 이전되는게 좋을 것 같아요

Comment on lines +24 to +25
var linkDTO = LinkDTO(link)
linkDTO.id = linkDocRef.documentID

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makeDTO 메서드 내부에서 처리하도록 변경하면 좋을 것 같아요~

/// 1. Link 문서 참조 생성
// TODO: 후에 testUser를 실제 로그인 된 유저로 변경 예정
let linkDocRef = db
.collection("users").document("testUser")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id를 파라미터로 받도록 수정


import Foundation

struct Link {
Copy link

@f-lab-barry f-lab-barry Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct WriteLink {
  var url: String
  var title: String?
  var thumbnailUrl: String?
  var faviconUrl: String?
  var lastAccessedAt: Date?
  var folderId: String?
}

create(from writeLink: WriteLink) -> Link {
  let doc = "users/testUser/links".document()
  makeDTO(writeLink)
}

같은 도메인 어떤 context(문맥)안에 있느냐에 따라 완전히 다름

@ljdongz ljdongz merged commit 0a9955b into develop Apr 28, 2025
2 checks passed
@ljdongz ljdongz deleted the feature/#8 branch April 28, 2025 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Firestore Link CRUD 구현

3 participants