Implemented the ArchiveController controller in the library target.

This commit is contained in:
2025-03-12 08:32:12 +01:00
parent eb72aebdf1
commit dbb7aeda1b
4 changed files with 135 additions and 3 deletions
@@ -1,11 +1,33 @@
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)
router.get(.archives, use: listAllArchives)
}
}
@@ -19,8 +41,25 @@ private extension ArchiveController {
@Sendable func listAllArchives(
_ request: Request,
context: Context
) async throws -> HTTPResponse.Status {
.ok
) 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"
}