-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from boostcampwm-2024/feature/photoObject
[Feature] 사진 오브젝트 설계, 유즈케이스 구현
- Loading branch information
Showing
7 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// DomainError.swift | ||
// Domain | ||
// | ||
// Created by 이동현 on 11/18/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum DomainError { | ||
case cannotWriteFile | ||
case cannotCreateDirectory | ||
case cannotFindDirectory | ||
} | ||
|
||
extension DomainError: LocalizedError { | ||
public var errorDescription: String? { | ||
switch self { | ||
case .cannotWriteFile: | ||
return "파일을 쓸 수 없습니다." | ||
case .cannotCreateDirectory: | ||
return "디렉터리를 생성할 수 없습니다." | ||
case .cannotFindDirectory: | ||
return "디렉터리를 찾을 수 없습니다." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// PhotoObject.swift | ||
// Domain | ||
// | ||
// Created by 이동현 on 11/18/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public final class PhotoObject: WhiteboardObject { | ||
public let photoURL: URL | ||
|
||
public init( | ||
id: UUID, | ||
position: CGPoint, | ||
size: CGSize, | ||
photoURL: URL | ||
) { | ||
self.photoURL = photoURL | ||
super.init( | ||
id: id, | ||
position: position, | ||
size: size) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
Domain/Domain/Sources/Interface/UseCase/AddPhotoUseCaseInterface.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// AddPhotoUseCaseInterface.swift | ||
// Domain | ||
// | ||
// Created by 이동현 on 11/18/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol AddPhotoUseCaseInterface { | ||
|
||
/// 사진 오브젝트를 추가합니다. | ||
/// - Parameters: | ||
/// - imageData: 추가할 사진의 데이터 | ||
/// - position: 사진을 추가할 위치 (origin) | ||
/// - size: 사진 객체 | ||
/// - Returns: | ||
func addPhoto( | ||
imageData: Data, | ||
position: CGPoint, | ||
size: CGSize | ||
) throws -> PhotoObject | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// AddPhotoUseCase.swift | ||
// Domain | ||
// | ||
// Created by 이동현 on 11/18/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public final class AddPhotoUseCase: AddPhotoUseCaseInterface { | ||
private let photoDirectory: URL | ||
private let fileManager: FileManager | ||
|
||
public init() throws { | ||
fileManager = FileManager.default | ||
guard | ||
let documentDirectory = fileManager | ||
.urls(for: .documentDirectory, in: .userDomainMask) | ||
.first | ||
else { throw DomainError.cannotCreateDirectory } | ||
photoDirectory = documentDirectory.appending(path: "photos") | ||
|
||
if !fileManager.fileExists(atPath: photoDirectory.path()) { | ||
do { | ||
try fileManager.createDirectory(at: photoDirectory, withIntermediateDirectories: true) | ||
} catch { | ||
throw DomainError.cannotCreateDirectory | ||
} | ||
} | ||
} | ||
|
||
public func addPhoto( | ||
imageData: Data, | ||
position: CGPoint, | ||
size: CGSize | ||
) throws -> PhotoObject { | ||
let uuid = UUID() | ||
let photoname = "\(uuid.uuidString).jpg" | ||
let photoURL = photoDirectory.appending(path: photoname) | ||
|
||
do { | ||
try imageData.write(to: photoURL) | ||
} catch { | ||
throw DomainError.cannotWriteFile | ||
} | ||
|
||
let photoObject = PhotoObject( | ||
id: uuid, | ||
position: position, | ||
size: size, | ||
photoURL: photoURL) | ||
|
||
return photoObject | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// AddPhotoUseCaseTests.swift | ||
// DomainTests | ||
// | ||
// Created by 이동현 on 11/18/24. | ||
// | ||
|
||
import Domain | ||
import XCTest | ||
|
||
final class AddPhotoUseCaseTests: XCTestCase { | ||
private var useCase: AddPhotoUseCase! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
do { | ||
useCase = try AddPhotoUseCase() | ||
} catch { | ||
XCTFail("init photoUseCase should success.") | ||
} | ||
} | ||
|
||
override func tearDown() { | ||
useCase = nil | ||
super.tearDown() | ||
} | ||
|
||
// 사진 객체 생성 성공 테스트 | ||
func testAddPhotoSuccess() throws { | ||
// 준비 | ||
let dummyImageData = Data() | ||
let position = CGPoint(x: 100, y: 100) | ||
let size = CGSize(width: 200, height: 200) | ||
let photoObject: PhotoObject | ||
|
||
// 실행 | ||
do { | ||
photoObject = try useCase.addPhoto( | ||
imageData: dummyImageData, | ||
position: position, | ||
size: size) | ||
} catch { | ||
XCTFail("photoObject should not fail.") | ||
return | ||
} | ||
|
||
// 검증 | ||
XCTAssertEqual(photoObject.photoURL.lastPathComponent.suffix(4), ".jpg") | ||
XCTAssertEqual(photoObject.position, position) | ||
XCTAssertEqual(photoObject.size, size) | ||
} | ||
} |