-
Notifications
You must be signed in to change notification settings - Fork 0
[Feature] Firestore Link CRUD 구현 #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
|
|
||
| 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? | ||
| } |
There was a problem hiding this comment.
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 프로퍼티는 생성 시점을 나타내기 위해 기본 값을 현재 날짜로 넣어주었습니다.
| /// 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) | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async로 작성된 setData(from:) 메서드가 존재하지 않았기 때문에 콜백함수를 비동기 함수로 전환시켰습니다
|
|
||
| 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 | ||
| ) | ||
| } | ||
| } |
There was a problem hiding this comment.
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() 메서드를 정의했습니다.
| 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 | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요 로직은 레파지토리로 이전되는게 좋을 것 같아요
| var linkDTO = LinkDTO(link) | ||
| linkDTO.id = linkDocRef.documentID |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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(문맥)안에 있느냐에 따라 완전히 다름



#️⃣ 연관된 이슈
📝 작업 내용
🎨 스크린샷
💬 추가 설명
특정 조건에 해당하는 링크 리스트 가져오기
원래는 Firebase에서 즐겨찾기한 링크 불러오기, 읽지 않은 링크 불러오기 로직을 추가로 구현하려고 했지만, 앱 진입 시 전체 링크 데이터를 먼저 불러오기 때문에 이 데이터를 기반으로 로컬에서 필터링하는 방식으로 구현했습니다.
다만, 저장된 링크가 많아질 경우 전체 데이터를 한번에 불러오는 것이 부담될 수 있기 때문에 이후에는 페이징이 필요할 것으로 예상됩니다.
이와 함께 특정 조건(즐겨찾기, 읽지 않음 등)에 해당하는 링크만 가져오는 서버 쿼리 방식도 필요할 것으로 예상됩니다.
그 이유는, 초기 로딩 시 제한된 범위의 데이터만 불러오고 이를 기반으로 필터링하게 되면, 실제로 조건에 해당하는 모든 링크가 아닌, 현재 로컬에 존재하는 일부 데이터만 보여지게 되기 때문입니다.