diff --git a/Sources/LilliputWeb/Pages/Editor/EditObjectPage.swift b/Sources/LilliputWeb/Pages/Editor/EditObjectPage.swift index d57e962..9da431d 100644 --- a/Sources/LilliputWeb/Pages/Editor/EditObjectPage.swift +++ b/Sources/LilliputWeb/Pages/Editor/EditObjectPage.swift @@ -10,16 +10,6 @@ import Vapor struct EditObjectPage: LeafPage { let session: EditSession - init(game: GameConfiguration, objectID: String) { - let driver = BasicDriver() - let engine = Engine(driver: driver) - engine.load(url: game.url) - engine.setup() - - let object = engine.object(withID: objectID) - session = EditSession(for: object) - } - func meta(for user: User?) -> PageMetadata { let title = "Object: \(session.title)" let description = "Object \(session.id) - \(session.title)." @@ -55,5 +45,11 @@ extension Definition { return EditGroup(title: "Description", properties: properties) } + + func update(fromEditSubmission properties: [String:String]) { + for (key, value) in properties { + print("\(key): \(value)") + } + } } diff --git a/Sources/LilliputWeb/Pages/Editor/EditorController.swift b/Sources/LilliputWeb/Pages/Editor/EditorController.swift index 5553030..63635d2 100644 --- a/Sources/LilliputWeb/Pages/Editor/EditorController.swift +++ b/Sources/LilliputWeb/Pages/Editor/EditorController.swift @@ -4,6 +4,7 @@ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- import Fluent +import Lilliput import Vapor enum EditorError: String, DebuggableError { @@ -68,7 +69,8 @@ struct EditorController: RouteCollection { } let objectID = try req.parameters.require("object", as: String.self) - let page = EditObjectPage(game: req.application.game, objectID: objectID) + let session = editSessionForObject(withID: objectID, game: req.application.game) + let page = EditObjectPage(session: session) return req.render(page, user: loggedInUser) } @@ -77,19 +79,33 @@ struct EditorController: RouteCollection { return req.eventLoop.makeFailedFuture(AdminError.notAdmin) } - let response = try req.content.decode(AdminUserPage.FormData.self) - - let userID = try req.parameters.require("user", as: UUID.self) - let user = User.query(on: req.db).filter(\.$id == userID).first() - return user - .unwrap(or: AdminError.unknownUser) - .map { (updatedUser: User) -> EventLoopFuture in - updatedUser.name = response.name - updatedUser.email = response.email - updatedUser.roles = response.roles - - return updatedUser.save(on: req.db) - } - .thenRedirect(with: req, to: .adminIndex) + let properties = try req.content.decode([String:String].self) + let objectID = try req.parameters.require("object", as: String.self) + let game = req.application.game + updateObject(withID: objectID, properties: properties, game: game) + let session = editSessionForObject(withID: objectID, game: game) + let page = EditObjectPage(session: session) + return req.render(page, user: loggedInUser) + } + + func editSessionForObject(withID objectID: String, game: GameConfiguration) -> EditSession { + let driver = BasicDriver() + let engine = Engine(driver: driver) + engine.load(url: game.url) + engine.setup() + + let object = engine.object(withID: objectID) + let session = EditSession(for: object) + return session + } + + func updateObject(withID objectID: String, properties: [String:String], game: GameConfiguration) { + let driver = BasicDriver() + let engine = Engine(driver: driver) + engine.load(url: game.url) + engine.setup() + + let object = engine.object(withID: objectID) + object.definition.update(fromEditSubmission: properties) } }