forked from sopt-makers/SOPT-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] sopt-makers#159 - 출석하기 Data 및 Domain 추가
- Loading branch information
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
SOPT-iOS/Projects/Data/Sources/Repository/AttendanceRepository.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,40 @@ | ||
// | ||
// AttendanceRepository.swift | ||
// Data | ||
// | ||
// Created by 김영인 on 2023/04/16. | ||
// Copyright © 2023 SOPT-iOS. All rights reserved. | ||
// | ||
|
||
import Combine | ||
|
||
import Core | ||
import Domain | ||
import Network | ||
|
||
public class AttendanceRepository { | ||
|
||
private let attendanceService: AttendanceService | ||
private let cancelBag = CancelBag() | ||
|
||
public init(service: AttendanceService) { | ||
self.attendanceService = service | ||
} | ||
} | ||
|
||
extension AttendanceRepository: AttendanceRepositoryInterface { | ||
|
||
public func fetchLectureRound(lectureId: Int) -> AnyPublisher<Int, Error> { | ||
return self.attendanceService | ||
.fetchAttendanceRound(lectureId: lectureId) | ||
.map { $0.data.round } | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
public func postAttendance(lectureRoundId: Int, code: Int) -> AnyPublisher<Bool, Error> { | ||
return self.attendanceService | ||
.postAttendance(lectureRoundId: lectureRoundId, code: code) | ||
.map { $0.success } | ||
.eraseToAnyPublisher() | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
SOPT-iOS/Projects/Domain/Sources/RepositoryInterface/AttendanceRepositoryInterface.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,16 @@ | ||
// | ||
// AttendanceRepositoryInterface.swift | ||
// Domain | ||
// | ||
// Created by 김영인 on 2023/04/16. | ||
// Copyright © 2023 SOPT-iOS. All rights reserved. | ||
// | ||
|
||
import Combine | ||
|
||
import Core | ||
|
||
public protocol AttendanceRepositoryInterface { | ||
func fetchLectureRound(lectureId: Int) -> AnyPublisher<Int, Error> | ||
func postAttendance(lectureRoundId: Int, code: Int) -> AnyPublisher<Bool, Error> | ||
} |
64 changes: 64 additions & 0 deletions
64
SOPT-iOS/Projects/Domain/Sources/UseCase/AttendanceUseCase.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,64 @@ | ||
// | ||
// AttendanceUseCase.swift | ||
// Domain | ||
// | ||
// Created by 김영인 on 2023/04/16. | ||
// Copyright © 2023 SOPT-iOS. All rights reserved. | ||
// | ||
|
||
import Combine | ||
|
||
import Core | ||
|
||
public protocol AttendanceUseCase { | ||
func fetchLectureRound(lectureId: Int) | ||
func postAttendance(lectureRoundId: Int, code: Int) | ||
var lectureRound: PassthroughSubject<Int, Never> { get set } | ||
var attendSuccess: PassthroughSubject<Bool, Never> { get set } | ||
} | ||
|
||
public class DefaultAttendanceUseCase { | ||
|
||
private let repository: AttendanceRepositoryInterface | ||
private var cancelBag = CancelBag() | ||
|
||
public var lectureRound = PassthroughSubject<Int, Never>() | ||
public var attendSuccess = PassthroughSubject<Bool, Never>() | ||
|
||
public init(repository: AttendanceRepositoryInterface, cancelBag: CancelBag = CancelBag()) { | ||
self.repository = repository | ||
self.cancelBag = cancelBag | ||
} | ||
} | ||
|
||
extension DefaultAttendanceUseCase: AttendanceUseCase { | ||
public func fetchLectureRound(lectureId: Int) { | ||
repository.fetchLectureRound(lectureId: lectureId) | ||
.sink(receiveCompletion: { event in | ||
switch event { | ||
case .failure(let error): | ||
print("failure: fetchLectureRound \(error)") | ||
case .finished: | ||
print("completion: fetchLectureRound \(event)") | ||
} | ||
}, receiveValue: { result in | ||
self.lectureRound.send(result) | ||
}) | ||
.store(in: cancelBag) | ||
} | ||
|
||
public func postAttendance(lectureRoundId: Int, code: Int) { | ||
repository.postAttendance(lectureRoundId: lectureRoundId, code: code) | ||
.sink(receiveCompletion: { event in | ||
switch event { | ||
case .failure(let error): | ||
print("failure: postAttendance \(error)") | ||
case .finished: | ||
print("completion: postAttendance \(event)") | ||
} | ||
}, receiveValue: { result in | ||
self.attendSuccess.send(result) | ||
}) | ||
.store(in: cancelBag) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
SOPT-iOS/Projects/Modules/Network/Sources/Entity/BaseEntity.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,26 @@ | ||
// | ||
// BaseEntity.swift | ||
// Network | ||
// | ||
// Created by 김영인 on 2023/04/16. | ||
// Copyright © 2023 SOPT-iOS. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct BaseEntity<T: Decodable>: Decodable { | ||
public let success: Bool | ||
public let message: String | ||
public let data: T | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case success, message, data | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let values = try decoder.container(keyedBy: CodingKeys.self) | ||
success = try values.decode(Bool.self, forKey: .success) | ||
message = try values.decode(String.self, forKey: .message) | ||
data = try values.decodeIfPresent(T.self, forKey: .data) ?? "non-data" as! T | ||
} | ||
} |