Skip to content

Commit

Permalink
rename key to id
Browse files Browse the repository at this point in the history
  • Loading branch information
SusanDoggie committed Apr 27, 2020
1 parent 66a4487 commit 04f02be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
17 changes: 8 additions & 9 deletions Sources/Fluent/Fluent+Sessions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,29 @@ private struct DatabaseSessions: SessionDriver {

func createSession(_ data: SessionData, for request: Request) -> EventLoopFuture<SessionID> {
let id = self.generateID()
return SessionRecord(key: id, data: data)
return SessionRecord(id: id, data: data)
.create(on: request.db(self.databaseID))
.map { id }
}

func readSession(_ sessionID: SessionID, for request: Request) -> EventLoopFuture<SessionData?> {
SessionRecord.query(on: request.db(self.databaseID))
.filter(\.$key == sessionID)
.filter(\.$id == sessionID)
.first()
.map { $0?.data }
}

func updateSession(_ sessionID: SessionID, to data: SessionData, for request: Request) -> EventLoopFuture<SessionID> {
SessionRecord.query(on: request.db(self.databaseID))
.filter(\.$key == sessionID)
.filter(\.$id == sessionID)
.set(\.$data, to: data)
.update()
.map { sessionID }
}

func deleteSession(_ sessionID: SessionID, for request: Request) -> EventLoopFuture<Void> {
SessionRecord.query(on: request.db(self.databaseID))
.filter(\.$key == sessionID)
.filter(\.$id == sessionID)
.delete()
}

Expand Down Expand Up @@ -114,7 +114,6 @@ public final class SessionRecord: Model {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("_fluent_sessions")
.id()
.field("key", .string, .required)
.field("data", .json, .required)
.create()
}
Expand All @@ -128,16 +127,16 @@ public final class SessionRecord: Model {
_Migration()
}

@ID(key: "key")
public var key: SessionID?
@ID()
public var id: SessionID?

@Field(key: "data")
public var data: SessionData

public init() { }

public init(key: SessionID, data: SessionData) {
self.key = key
public init(id: SessionID, data: SessionData) {
self.id = id
self.data = data
}
}
3 changes: 1 addition & 2 deletions Tests/FluentTests/SessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ final class SessionTests: XCTestCase {
// Add single query output with session data for session read.
test.append([
TestOutput([
"id": UUID(),
"key": SessionID(string: sessionID!),
"id": SessionID(string: sessionID!),
"data": SessionData(["name": "vapor"])
])
])
Expand Down

0 comments on commit 04f02be

Please sign in to comment.