-
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
Changes from all commits
1f379f6
1fb0b87
4734f18
5c5b9f9
064bdc6
87e6ec1
2d06212
e8c53f4
457da0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // | ||
| // LinkDTO.swift | ||
| // Mark-In | ||
| // | ||
| // Created by 이정동 on 4/20/25. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| 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 | ||
| ) | ||
| } | ||
| } | ||
|
Comment on lines
+9
to
+43
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Entity -> DTO 타입 변환, DTO -> Entity 타입 변환을 DTO 타입을 통해서만 이루어질 수 있도록 init 메서드와 toEntity() 메서드를 정의했습니다. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // | ||
| // LinkRepositoryImpl.swift | ||
| // Mark-In | ||
| // | ||
| // Created by 이정동 on 4/21/25. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| import FirebaseFirestore | ||
|
|
||
| struct LinkRepositoryImpl: LinkRepository { | ||
|
|
||
| typealias VoidCheckedContinuation = CheckedContinuation<Void, any Error> | ||
|
|
||
| private let db = Firestore.firestore() | ||
|
|
||
| func createLink(_ link: Link) async throws -> Link { | ||
| /// 1. Link 문서 참조 생성 | ||
| // TODO: 후에 testUser를 실제 로그인 된 유저로 변경 예정 | ||
| let linkDocRef = db.collection("users/testUser/links").document() | ||
|
|
||
| /// 2. Entity를 DTO로 변환 및 새로 생성된 문서 ID를 프로퍼티에 저장 | ||
| var linkDTO = LinkDTO(link) | ||
| linkDTO.id = linkDocRef.documentID | ||
|
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makeDTO 메서드 내부에서 처리하도록 변경하면 좋을 것 같아요~ |
||
|
|
||
| /// 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) | ||
| } | ||
| } | ||
|
|
||
|
Comment on lines
+27
to
+38
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. async로 작성된 setData(from:) 메서드가 존재하지 않았기 때문에 콜백함수를 비동기 함수로 전환시켰습니다 |
||
| /// 4. DocumentID 업데이트 된 LinkEntity로 리턴 | ||
| return linkDTO.toEntity() | ||
| } | ||
|
|
||
| func fetchAllLinks() async throws -> [Link] { | ||
| /// 1. Links 컬렉션 참조 생성 | ||
| // TODO: 후에 testUser를 실제 로그인 된 유저로 변경 예정 | ||
| let linkColRef = db.collection("users/testUser/links") | ||
|
|
||
| /// 2. 컬렉션의 모든 문서 가져오기 | ||
| let snapshot = try await linkColRef.getDocuments() | ||
|
|
||
| /// 3. 문서를 DTO로 변환 후 다시 Entity로 변환 | ||
| return snapshot.documents.compactMap { | ||
| try? $0.data(as: LinkDTO.self).toEntity() | ||
| } | ||
| } | ||
|
|
||
| func updateLink(_ link: Link) async throws { | ||
| /// 1. Link 문서 참조 생성 | ||
| // TODO: 후에 testUser를 실제 로그인 된 유저로 변경 예정 | ||
| let linkDocRef = db.document("users/testUser/links/\(link.id)") | ||
|
|
||
| /// 2. Entity를 DTO로 변환 | ||
| let linkDTO = LinkDTO(link) | ||
|
|
||
| /// 3. 업데이트 | ||
| 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) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func deleteLink(_ link: Link) async throws { | ||
| /// 1. Link 문서 참조 생성 | ||
| // TODO: 후에 testUser를 실제 로그인 된 유저로 변경 예정 | ||
| let linkDocRef = db.document("users/testUser/links/\(link.id)") | ||
|
|
||
| /// 2. Link 삭제 | ||
| try await linkDocRef.delete() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // | ||
| // Link.swift | ||
| // Mark-In | ||
| // | ||
| // Created by 이정동 on 4/20/25. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| struct Link { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 같은 도메인 어떤 context(문맥)안에 있느냐에 따라 완전히 다름 |
||
| 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? | ||
| } | ||
|
Comment on lines
+9
to
+19
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. id 프로퍼티를 옵셔널타입 대신 기본값을 설정해 주었습니다. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // | ||
| // LinkRepository.swift | ||
| // Mark-In | ||
| // | ||
| // Created by 이정동 on 4/20/25. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| protocol LinkRepository { | ||
| func createLink(_ link: Link) async throws -> Link | ||
| func fetchAllLinks() async throws -> [Link] | ||
| func updateLink(_ link: Link) async throws | ||
| func deleteLink(_ link: Link) async throws | ||
| } |
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.
요 로직은 레파지토리로 이전되는게 좋을 것 같아요