Skip to content

Commit

Permalink
[Feat] sopt-makers#159 - 출석하기 Data 및 Domain 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
0inn committed Apr 17, 2023
1 parent cb3faf4 commit 0139507
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
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()
}
}
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 SOPT-iOS/Projects/Domain/Sources/UseCase/AttendanceUseCase.swift
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 SOPT-iOS/Projects/Modules/Network/Sources/Entity/BaseEntity.swift
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
}
}

0 comments on commit 0139507

Please sign in to comment.