Improved upon the "listArchives(_: context: )" function for the ArchiveController controller in the library target.

This commit is contained in:
2025-03-12 23:43:19 +01:00
parent dbb7aeda1b
commit a14d388321
4 changed files with 108 additions and 25 deletions
@@ -1,6 +1,6 @@
import Hummingbird
/// A controller that handles
/// A controller type that provides information about the *DocC* archives containers.
struct ArchiveController<Context: RequestContext> {
// MARK: Properties
@@ -12,8 +12,8 @@ struct ArchiveController<Context: RequestContext> {
/// Initialises this controller.
/// - Parameters:
/// - folderArchives: <#folderArchives description#>
/// - fileService: <#fileService description#>
/// - folderArchives: A folder in the file system where the *DocC* archive contained are located.
/// - fileService: A service that interfaces with the local file system.
init(
_ folderArchives: String,
fileService: any FileServicing = FileService()
@@ -27,7 +27,7 @@ struct ArchiveController<Context: RequestContext> {
/// 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: listArchives)
}
}
@@ -38,16 +38,26 @@ private extension ArchiveController {
// MARK: Functions
@Sendable func listAllArchives(
@Sendable func listArchives(
_ 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)
) async throws (HTTPError) -> ArchiveList {
do {
let nameArchives = try await fileService
.listItems(in: folderArchives)
.filter { $0.hasSuffix(.suffixArchive) }
.map { $0.dropLast(String.suffixArchive.count) }
.map(String.init)
.sorted { $0 < $1 }
return .init(nameArchives)
} catch .folderNotFound {
throw .init(.notFound)
} catch .folderPathEmpty, .folderNotDirectory {
throw .init(.unprocessableContent)
} catch {
throw .init(.badRequest)
}
}
}