Skip to content

Commit

Permalink
♻️ Repository 프로토콜을 Domain 레이어로 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
SwiftyJunnos committed Dec 27, 2023
1 parent c2fb4aa commit ed64471
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Combine
import Foundation
import MusicKit

import MSData
import MSDomain
import MSLogger

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,7 @@ import MSNetworking
import MSPersistentStorage
import MSUserDefaults

public protocol JourneyRepository: Persistable {

func fetchIsRecording() -> Bool
mutating func updateIsRecording(_ isRecording: Bool) -> Bool
func fetchRecordingJourneyID() -> String?
func fetchRecordingJourney(forID id: String) -> RecordingJourney?
func fetchJourneyList(userID: UUID,
minCoordinate: Coordinate,
maxCoordinate: Coordinate) async -> Result<[Journey], Error>
mutating func startJourney(at coordinate: Coordinate, userID: UUID) async -> Result<RecordingJourney, Error>
mutating func endJourney(_ journey: Journey) async -> Result<String, Error>
func recordJourney(journeyID: String, at coordinates: [Coordinate]) async -> Result<RecordingJourney, Error>
mutating func deleteJourney(_ journey: RecordingJourney, userID: UUID) async -> Result<String, Error>

}

public struct JourneyRepositoryImplementation: JourneyRepository {
public struct JourneyRepositoryImplementation: JourneyRepository, Persistable {

// MARK: - Properties

Expand Down Expand Up @@ -131,8 +115,8 @@ public struct JourneyRepositoryImplementation: JourneyRepository {
spots: [],
coordinates: [responseDTO.coordinate.toDomain()])

self.saveToLocal(value: recordingJourney.id)
self.saveToLocal(value: recordingJourney.startTimestamp)
self.saveToLocal(recordingJourney.id, at: self.storage)
self.saveToLocal(recordingJourney.startTimestamp, at: self.storage)

self.recordingJourneyID = recordingJourney.id
self.isRecording = true
Expand Down Expand Up @@ -166,7 +150,9 @@ public struct JourneyRepositoryImplementation: JourneyRepository {
spots: [],
coordinates: coordinates)

responseDTO.coordinates.forEach { self.saveToLocal(value: $0) }
responseDTO.coordinates.forEach {
self.saveToLocal($0, at: self.storage)
}

return .success(recordingJourney)
case .failure(let error):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ import MSDomain
import MSLogger
import MSPersistentStorage

public protocol Persistable {

var storage: MSPersistentStorage { get }

@discardableResult
func saveToLocal(value: Codable) -> Bool
func loadJourneyFromLocal() -> RecordingJourney?

}

// MARK: - KeyStorage

private struct KeyStorage {
Expand All @@ -37,9 +27,9 @@ private struct KeyStorage {
extension Persistable {

@discardableResult
public func saveToLocal(value: Codable) -> Bool {
public func saveToLocal(_ value: Codable, at storage: MSPersistentStorage) -> Bool {
let key = UUID().uuidString
self.storage.set(value: value, forKey: key)
storage.set(value: value, forKey: key)

switch value {
case is String:
Expand All @@ -53,7 +43,7 @@ extension Persistable {
if KeyStorage.startTimestamp == nil {
KeyStorage.startTimestamp = key
} else {
MSLogger.make(category: .persistable).debug("start tamp는 하나의 값만 저장할 수 있습니다.")
MSLogger.make(category: .persistable).debug("start timestamp는 하나의 값만 저장할 수 있습니다.")
return false
}
case is SpotDTO:
Expand All @@ -67,46 +57,46 @@ extension Persistable {
return true
}

public func loadJourneyFromLocal() -> RecordingJourney? {
guard let id = self.loadID(),
let startTimestamp = self.loadStartTimeStamp() else {
public func loadJourney(from storage: MSPersistentStorage) -> RecordingJourney? {
guard let id = self.loadID(from: storage),
let startTimestamp = self.loadStartTimeStamp(from: storage) else {
return nil
}
return RecordingJourney(id: id,
startTimestamp: startTimestamp,
spots: self.loadSpots(),
coordinates: self.loadCoordinates())
spots: self.loadSpots(from: storage),
coordinates: self.loadCoordinates(from: storage))
}

func loadStartTimeStamp() -> Date? {
func loadStartTimeStamp(from storage: MSPersistentStorage) -> Date? {
guard let startTimestampKey = KeyStorage.startTimestamp,
let startTimestamp = self.storage.get(Date.self, forKey: startTimestampKey)
let startTimestamp = storage.get(Date.self, forKey: startTimestampKey)
else {
MSLogger.make(category: .persistable).debug("id 또는 startTimestamp가 저장되지 않았습니다.")
return nil
}
return startTimestamp
}

func loadID() -> String? {
func loadID(from storage: MSPersistentStorage) -> String? {
guard let idKey = KeyStorage.id,
let id = self.storage.get(String.self, forKey: idKey) else {
let id = storage.get(String.self, forKey: idKey) else {
MSLogger.make(category: .persistable).debug("id 또는 startTimestamp가 저장되지 않았습니다.")
return nil
}
return id
}

func loadSpots() -> [Spot] {
func loadSpots(from storage: MSPersistentStorage) -> [Spot] {
return KeyStorage.spots.compactMap { spotKey in
let spotDTO = self.storage.get(SpotDTO.self, forKey: spotKey)
let spotDTO = storage.get(SpotDTO.self, forKey: spotKey)
return spotDTO?.toDomain()
}
}

func loadCoordinates() -> [Coordinate] {
func loadCoordinates(from storage: MSPersistentStorage) -> [Coordinate] {
return KeyStorage.coordinates.compactMap { coordinateKey in
let coordinateDTO = self.storage.get(CoordinateDTO.self, forKey: coordinateKey)
let coordinateDTO = storage.get(CoordinateDTO.self, forKey: coordinateKey)
return coordinateDTO?.toDomain()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,10 @@
import Foundation
import MusicKit

import MSDomain
import MSLogger
import MSNetworking

public protocol SongRepository {

func fetchSong(withID id: String) async -> Result<Song, Error>
func fetchSongList(with term: String) async -> Result<MusicItemCollection<Song>, Error>
@available(iOS 16.0, *)
func fetchSongListByRank() async -> Result<MusicItemCollection<Song>, Error>

}

public struct SongRepositoryImplementation: SongRepository {

// MARK: - Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ import MSNetworking
import MSLogger
import MSPersistentStorage

public protocol SpotRepository: Persistable {

func fetchRecordingSpots() async -> Result<[Spot], Error>
func upload(spot: CreateSpotRequestDTO) async -> Result<Spot, Error>

}

public struct SpotRepositoryImplementation: SpotRepository {
public struct SpotRepositoryImplementation: SpotRepository, Persistable {

// MARK: - Properties

Expand Down Expand Up @@ -74,7 +67,7 @@ public struct SpotRepositoryImplementation: SpotRepository {
#if DEBUG
MSLogger.make(category: .network).debug("성공적으로 업로드하였습니다.")
#endif
self.saveToLocal(value: spot)
self.saveToLocal(spot, at: self.storage)
return .success(spot.toDomain())
case .failure(let error):
MSLogger.make(category: .network).error("\(error): 업로드에 실패하였습니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@

import Foundation

import MSDomain
import MSKeychainStorage
import MSLogger
import MSNetworking

public protocol UserRepository {

func createUser() async -> Result<UUID, Error>
func storeUUID(_ userID: UUID) throws -> UUID
func fetchUUID() -> UUID?

}

public struct UserRepositoryImplementation: UserRepository {

// MARK: - Properties
Expand Down
24 changes: 24 additions & 0 deletions iOS/MSDomain/Sources/MSDomain/Repository/JourneyRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// JourneyRepository.swift
// MSDomain
//
// Created by 이창준 on 2023.12.24.
//

import Foundation

public protocol JourneyRepository {

func fetchIsRecording() -> Bool
mutating func updateIsRecording(_ isRecording: Bool) -> Bool
func fetchRecordingJourneyID() -> String?
func fetchRecordingJourney(forID id: String) -> RecordingJourney?
func fetchJourneyList(userID: UUID,
minCoordinate: Coordinate,
maxCoordinate: Coordinate) async -> Result<[Journey], Error>
mutating func startJourney(at coordinate: Coordinate, userID: UUID) async -> Result<RecordingJourney, Error>
mutating func endJourney(_ journey: Journey) async -> Result<String, Error>
func recordJourney(journeyID: String, at coordinates: [Coordinate]) async -> Result<RecordingJourney, Error>
mutating func deleteJourney(_ journey: RecordingJourney, userID: UUID) async -> Result<String, Error>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Persistable.swift
// MSDomain
//
// Created by 이창준 on 2023.12.26.
//

import Foundation

public protocol Persistable {

@discardableResult
func saveToLocal(_ value: Codable, at storage: MSPersistentStorage) -> Bool
func loadJourney() -> RecordingJourney?

}
18 changes: 18 additions & 0 deletions iOS/MSDomain/Sources/MSDomain/Repository/SongRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// SongRepository.swift
// MSDomain
//
// Created by 이창준 on 2023.12.24.
//

import Foundation
import MusicKit

public protocol SongRepository {

func fetchSong(withID id: String) async -> Result<Song, Error>
func fetchSongList(with term: String) async -> Result<MusicItemCollection<Song>, Error>
@available(iOS 16.0, *)
func fetchSongListByRank() async -> Result<MusicItemCollection<Song>, Error>

}
15 changes: 15 additions & 0 deletions iOS/MSDomain/Sources/MSDomain/Repository/SpotRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// SpotRepository.swift
// MSDomain
//
// Created by 이창준 on 2023.12.24.
//

import Foundation

public protocol SpotRepository {

func fetchRecordingSpots() async -> Result<[Spot], Error>
func upload(spot: Spot) async -> Result<Spot, Error>

}
16 changes: 16 additions & 0 deletions iOS/MSDomain/Sources/MSDomain/Repository/UserRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// UserRepository.swift
// MSDomain
//
// Created by 이창준 on 2023.12.24.
//

import Foundation

public protocol UserRepository {

func createUser() async -> Result<UUID, Error>
func storeUUID(_ userID: UUID) throws -> UUID
func fetchUUID() -> UUID?

}

0 comments on commit ed64471

Please sign in to comment.