Skip to content

Commit

Permalink
use callAsFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
Haitec committed Oct 30, 2023
1 parent ef343fb commit 6c37bcb
Show file tree
Hide file tree
Showing 13 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

public protocol AddTodoUseCase {
func execute(todo: Todo) async -> Result<Todo, Error>
func callAsFunction(todo: Todo) async -> Result<Todo, Error>
}

public class DefaultAddTodoUseCase: AddTodoUseCase {
Expand All @@ -18,7 +18,7 @@ public class DefaultAddTodoUseCase: AddTodoUseCase {
self.repository = repository
}

public func execute(todo: Todo) async -> Result<Todo, Error> {
public func callAsFunction(todo: Todo) async -> Result<Todo, Error> {
await repository.add(todo: todo)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

public protocol CompleteTodoUseCase {
func execute(id: Int) async -> Result<Todo, Error>
func callAsFunction(id: Int) async -> Result<Todo, Error>
}

public class DefaultCompleteTodoUseCase: CompleteTodoUseCase {
Expand All @@ -18,7 +18,7 @@ public class DefaultCompleteTodoUseCase: CompleteTodoUseCase {
self.repository = repository
}

public func execute(id: Int) async -> Result<Todo, Error> {
public func callAsFunction(id: Int) async -> Result<Todo, Error> {
await repository.complete(id: id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

public protocol GetAllTodosUseCase {
func execute() async -> Result<[Todo], Error>
func callAsFunction() async -> Result<[Todo], Error>
}

public class DefaultGetAllTodosUseCase: GetAllTodosUseCase {
Expand All @@ -18,7 +18,7 @@ public class DefaultGetAllTodosUseCase: GetAllTodosUseCase {
self.repository = repository
}

public func execute() async -> Result<[Todo], Error> {
public func callAsFunction() async -> Result<[Todo], Error> {
await repository.todos()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class DefaultAddTodoUseCaseTests: XCTestCase {
let sut = DefaultAddTodoUseCase(repository: repository)

// Act
let result = try await sut.execute(todo: expected).get()
let result = try await sut(todo: expected).get()

// Assert
XCTAssertEqual(result, expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class DefaultCompleteTodoUseCaseTests: XCTestCase {
let sut = DefaultCompleteTodoUseCase(repository: repository)

// Act
let result = try await sut.execute(id: expected.id!).get()
let result = try await sut(id: expected.id!).get()

// Assert
XCTAssertEqual(result, expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class DefaultGetAllTodosUseCaseTests: XCTestCase {
let sut = DefaultGetAllTodosUseCase(repository: repository)

// Act
let result = try await sut.execute().get()
let result = try await sut().get()

// Assert
XCTAssertEqual(result, expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import DomainLayer
import Foundation

final class PreviewAddTodoUseCase: AddTodoUseCase {
func execute(todo: Todo) async -> Result<Todo, Error> {
func callAsFunction(todo: Todo) async -> Result<Todo, Error> {
fatalError()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import DomainLayer
import Foundation

final class PreviewCompleteTodoUseCase: CompleteTodoUseCase {
func execute(id: Int) async -> Result<Todo, Error> {
func callAsFunction(id: Int) async -> Result<Todo, Error> {
fatalError()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import DomainLayer
import Foundation

final class PreviewGetAllTodosUseCase: GetAllTodosUseCase {
func execute() async -> Result<[Todo], Error> {
func callAsFunction() async -> Result<[Todo], Error> {
.success([
.init(id: 1, title: "Preview #1", completed: false),
.init(id: 2, title: "Preview #2", completed: true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class TodoListViewModel: ObservableObject {
@MainActor
@Sendable
public func loadTodos() async {
_ = await getAllTodosUseCase.execute()
_ = await getAllTodosUseCase()
.map {
self.todos = $0
}
Expand All @@ -45,7 +45,7 @@ public class TodoListViewModel: ObservableObject {
addNewTodoIsDisabled = true

let todo = Todo(title: newTodoText)
let result = await addTodoUseCase.execute(todo: todo)
let result = await addTodoUseCase(todo: todo)
if case .success(let newTodo) = result {
todos.append(newTodo)
newTodoText = ""
Expand All @@ -56,7 +56,7 @@ public class TodoListViewModel: ObservableObject {

@MainActor
public func completeTodo(id: Int) async {
let result = await completeTodoUseCase.execute(id: id)
let result = await completeTodoUseCase(id: id)
if case .success(let completedTodo) = result,
let index = todos.firstIndex(where: { $0.id == completedTodo.id }) {
todos.remove(at: index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class MockAddTodoUseCase: AddTodoUseCase {
self.result = result
}

func execute(todo: Todo) async -> Result<Todo, Error> {
func callAsFunction(todo: Todo) async -> Result<Todo, Error> {
executeCalled = true
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class MockCompleteTodoUseCase: CompleteTodoUseCase {
self.result = result
}

func execute(id: Int) async -> Result<Todo, Error> {
func callAsFunction(id: Int) async -> Result<Todo, Error> {
executeCalledId = id
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class MockGetAllTodosUseCase: GetAllTodosUseCase {
self.result = result
}

func execute() async -> Result<[Todo], Error> {
func callAsFunction() async -> Result<[Todo], Error> {
executeCalled = true
return result
}
Expand Down

0 comments on commit 6c37bcb

Please sign in to comment.