Skip to content

Commit

Permalink
Merge pull request #91 from vapor/modify-patch
Browse files Browse the repository at this point in the history
modify now works
  • Loading branch information
loganwright authored Sep 12, 2016
2 parents 63b01c4 + d446efe commit fec5165
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
31 changes: 16 additions & 15 deletions Sources/Fluent/Entity/Entity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public protocol Entity: Preparation, NodeConvertible {
*/
var id: Node? { get set }

/**
Whether or not entity was retrieved from database.

This value shouldn't be interacted w/ external users
w/o explicit knowledge.

General implementation should just be `var exists = false`
*/
var exists: Bool { get set }

/**
Called before the entity will be created.
*/
Expand Down Expand Up @@ -49,9 +59,7 @@ public protocol Entity: Preparation, NodeConvertible {
func didDelete()
}

//MARK: Defaults

var existanceStorage: [String: Bool] = [:]
// MARK: Defaults

extension Entity {
/**
Expand All @@ -66,20 +74,13 @@ extension Entity {
return String(describing: self).lowercased()
}

private var address: String {
mutating get {
var id = ""
withUnsafePointer(to: &self) { id = "\($0)"}
return id
}
}

var exists: Bool {
mutating get {
return existanceStorage[address] ?? false
// FIXME: Default for 0.11 to maintain compatibility. Remove for 0.12
public var exists: Bool {
get {
return true
}
set {
existanceStorage[address] = newValue
print("Exists not stored")
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions Sources/Fluent/Query/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,7 @@ extension QueryRepresentable {
let query = try makeQuery()
let data = try model.makeNode()

if let id = model.id, model.exists {
let _ = try filter(
query.database.driver.idKey,
.equals,
id
)
if let _ = model.id, model.exists {
model.willUpdate()
try modify(data)
model.didUpdate()
Expand Down Expand Up @@ -249,6 +244,10 @@ extension QueryRepresentable {
query.action = .modify
query.data = serialized

let idKey = query.database.driver.idKey
if let id = serialized?[idKey] {
_ = try filter(idKey, id)
}
try query.run()
}
}
Expand Down
1 change: 1 addition & 0 deletions Tests/FluentTests/Utilities/Atom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ struct Atom: Entity {
var id: Node?
var name: String
var groupId: Node?
var exists: Bool = false

init(name: String, id: Node? = nil) {
self.id = id
Expand Down

0 comments on commit fec5165

Please sign in to comment.