-
Notifications
You must be signed in to change notification settings - Fork 1
/
Event.swift
100 lines (82 loc) · 2.32 KB
/
Event.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// Event.swift
// PSix
//
// Created by Turzion, Avihu on 4/15/15.
// Copyright (c) 2015 PayPal. All rights reserved.
//
import Foundation
import Parse
class Event: PFObject, PFSubclassing, Comparable {
static func findOrCreateBlocking(fbId: String) -> Event {
let findEventQuery = PFQuery(className: parseClassName()).whereKey("fbId", equalTo: fbId)
if let event = findEventQuery.getFirstObject() as? Event {
return event
}
return Event(fbId)
}
convenience init(_ fbId: String) {
self.init()
self.fbId = fbId
}
override static func initialize() {
var onceToken: dispatch_once_t = 0
dispatch_once(&onceToken) {
self.registerSubclass()
}
}
static func parseClassName() -> String {
return "Event"
}
@NSManaged var fbId: String
@NSManaged var ownerFbId: String
@NSManaged var amountPerAttendee: Int
var name: String?
var eventDescription: String?
var location: String?
var startTime: NSDate?
var endTime: NSDate?
var coverImageUrl: NSURL?
lazy var paymentsRelation: PFRelation = self.relationForKey("payments")
var guests = RSVPs()
var attending: RSVPs {
return RSVP.filter(guests, whoAre: .Attending)
}
var tentative: RSVPs {
return RSVP.filter(guests, whoAre: .Tentative)
}
var declined: RSVPs {
return RSVP.filter(guests, whoAre: .NotGoing)
}
var moneyCollected: Int? {
if let payments = paymentsRelation.query()?.findObjects() as? Payments {
return payments.map { $0["amount"] as! Int }.reduce(0, combine: +)
}
return nil
}
var totalMoneyToCollect: Int? {
if amountPerAttendee > 0 {
return RSVP.filter(guests, whoAre: .Attending).count * amountPerAttendee
}
return nil
}
}
// MARK: Comparable
func <(lhs: Event, rhs: Event) -> Bool {
if let lhsStartTime = lhs.startTime,
let rhsStartTime = rhs.startTime {
return lhsStartTime.compare(rhsStartTime) == NSComparisonResult.OrderedAscending
} else if lhs.startTime == nil && rhs.startTime != nil {
return true
}
return false
}
func ==(lhs: Event, rhs: Event) -> Bool {
if let lhsStartTime = lhs.startTime,
let rhsStartTime = rhs.startTime {
return lhsStartTime.compare(rhsStartTime) == NSComparisonResult.OrderedSame
}
return false
}
// MARK: typealiasing
typealias Events = [Event]