doxy/Library/Sources/Internal/Controllers/ArchiveController.swift

66 lines
1.5 KiB
Swift

import Hummingbird
/// A controller that handles
struct ArchiveController<Context: RequestContext> {
// MARK: Properties
private let fileService: any FileServicing
private let folderArchives: String
// MARK: Initialisers
/// Initialises this controller.
/// - Parameters:
/// - folderArchives: <#folderArchives description#>
/// - fileService: <#fileService description#>
init(
_ folderArchives: String,
fileService: any FileServicing = FileService()
) {
self.folderArchives = folderArchives
self.fileService = fileService
}
// MARK: Functions
/// Registers the controller to a given router.
/// - Parameter router: A router to register this controller to.
func register(to router: Router<Context>) {
router.get(.archives, use: listAllArchives)
}
}
// MARK: - Helpers
private extension ArchiveController {
// MARK: Functions
@Sendable func listAllArchives(
_ request: Request,
context: Context
) async throws -> ArchiveList {
let archives = try await fileService
.listItems(in: folderArchives)
.filter { $0.hasSuffix(.suffixArchive) }
.sorted { $0 < $1 }
return .init(archives)
}
}
// MARK: - RouterPath+Constants
private extension RouterPath {
static let archives: RouterPath = .init("archives")
}
// MARK: - String+Constants
private extension String {
static let suffixArchive: String = ".doccarchive"
}