Skip to content

Commit a52d5d3

Browse files
committed
Add Booking model
1 parent 5b30ba0 commit a52d5d3

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// periphery:ignore:all
2+
import Codegen
3+
import Foundation
4+
5+
/// Represents a Booking Entity.
6+
///
7+
public struct Booking: Codable, GeneratedCopiable, Equatable, GeneratedFakeable {
8+
public let siteID: Int64
9+
public let bookingID: Int64
10+
public let allDay: Bool
11+
public let cost: String
12+
public let customerID: Int64
13+
public let dateCreated: Date
14+
public let dateModified: Date
15+
public let endDate: Date
16+
public let googleCalendarEventID: String?
17+
public let orderID: Int64
18+
public let orderItemID: Int64
19+
public let parentID: Int64
20+
public let productID: Int64
21+
public let resourceID: Int64
22+
public let startDate: Date
23+
public let statusKey: String
24+
public let localTimezone: String
25+
26+
/// Computed Properties
27+
///
28+
public var bookingStatus: BookingStatus {
29+
return BookingStatus(rawValue: statusKey)
30+
}
31+
32+
/// Booking struct initializer.
33+
///
34+
public init(siteID: Int64,
35+
bookingID: Int64,
36+
allDay: Bool,
37+
cost: String,
38+
customerID: Int64,
39+
dateCreated: Date,
40+
dateModified: Date,
41+
endDate: Date,
42+
googleCalendarEventID: String?,
43+
orderID: Int64,
44+
orderItemID: Int64,
45+
parentID: Int64,
46+
productID: Int64,
47+
resourceID: Int64,
48+
startDate: Date,
49+
statusKey: String,
50+
localTimezone: String) {
51+
self.siteID = siteID
52+
self.bookingID = bookingID
53+
self.allDay = allDay
54+
self.cost = cost
55+
self.customerID = customerID
56+
self.dateCreated = dateCreated
57+
self.dateModified = dateModified
58+
self.endDate = endDate
59+
self.googleCalendarEventID = googleCalendarEventID
60+
self.orderID = orderID
61+
self.orderItemID = orderItemID
62+
self.parentID = parentID
63+
self.productID = productID
64+
self.resourceID = resourceID
65+
self.startDate = startDate
66+
self.statusKey = statusKey
67+
self.localTimezone = localTimezone
68+
}
69+
70+
/// The public initializer for Booking.
71+
///
72+
public init(from decoder: Decoder) throws {
73+
guard let siteID = decoder.userInfo[.siteID] as? Int64 else {
74+
throw BookingDecodingError.missingSiteID
75+
}
76+
77+
let container = try decoder.container(keyedBy: CodingKeys.self)
78+
79+
let bookingID = try container.decode(Int64.self, forKey: .bookingID)
80+
let allDay = try container.decode(Bool.self, forKey: .allDay)
81+
82+
// Cost may come as string or number
83+
let cost = container.failsafeDecodeIfPresent(targetType: String.self,
84+
forKey: .cost,
85+
alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })]) ?? ""
86+
87+
let customerID = try container.decode(Int64.self, forKey: .customerID)
88+
let dateCreated = try container.decode(Date.self, forKey: .dateCreated)
89+
let dateModified = try container.decode(Date.self, forKey: .dateModified)
90+
let endDate = try container.decode(Date.self, forKey: .endDate)
91+
let googleCalendarEventID = try container.decodeIfPresent(String.self, forKey: .googleCalendarEventID)
92+
let orderID = try container.decode(Int64.self, forKey: .orderID)
93+
let orderItemID = try container.decode(Int64.self, forKey: .orderItemID)
94+
let parentID = try container.decode(Int64.self, forKey: .parentID)
95+
let productID = try container.decode(Int64.self, forKey: .productID)
96+
let resourceID = try container.decode(Int64.self, forKey: .resourceID)
97+
let startDate = try container.decode(Date.self, forKey: .startDate)
98+
let statusKey = try container.decode(String.self, forKey: .statusKey)
99+
let localTimezone = try container.decode(String.self, forKey: .localTimezone)
100+
101+
self.init(siteID: siteID,
102+
bookingID: bookingID,
103+
allDay: allDay,
104+
cost: cost,
105+
customerID: customerID,
106+
dateCreated: dateCreated,
107+
dateModified: dateModified,
108+
endDate: endDate,
109+
googleCalendarEventID: googleCalendarEventID,
110+
orderID: orderID,
111+
orderItemID: orderItemID,
112+
parentID: parentID,
113+
personCounts: personCounts,
114+
productID: productID,
115+
resourceID: resourceID,
116+
startDate: startDate,
117+
statusKey: statusKey,
118+
localTimezone: localTimezone)
119+
}
120+
121+
public func encode(to encoder: Encoder) throws {
122+
var container = encoder.container(keyedBy: CodingKeys.self)
123+
124+
try container.encode(bookingID, forKey: .bookingID)
125+
try container.encode(allDay, forKey: .allDay)
126+
try container.encode(cost, forKey: .cost)
127+
try container.encode(customerID, forKey: .customerID)
128+
try container.encode(dateCreated, forKey: .dateCreated)
129+
try container.encode(dateModified, forKey: .dateModified)
130+
try container.encode(endDate, forKey: .endDate)
131+
try container.encode(googleCalendarEventID, forKey: .googleCalendarEventID)
132+
try container.encode(orderID, forKey: .orderID)
133+
try container.encode(orderItemID, forKey: .orderItemID)
134+
try container.encode(parentID, forKey: .parentID)
135+
try container.encode(personCounts, forKey: .personCounts)
136+
try container.encode(productID, forKey: .productID)
137+
try container.encode(resourceID, forKey: .resourceID)
138+
try container.encode(startDate, forKey: .startDate)
139+
try container.encode(statusKey, forKey: .statusKey)
140+
try container.encode(localTimezone, forKey: .localTimezone)
141+
}
142+
}
143+
144+
/// Defines all of the Booking CodingKeys
145+
///
146+
private extension Booking {
147+
148+
enum CodingKeys: String, CodingKey {
149+
case bookingID = "id"
150+
case allDay = "all_day"
151+
case cost
152+
case customerID = "customer_id"
153+
case dateCreated = "date_created"
154+
case dateModified = "date_modified"
155+
case endDate = "end"
156+
case googleCalendarEventID = "google_calendar_event_id"
157+
case orderID = "order_id"
158+
case orderItemID = "order_item_id"
159+
case parentID = "parent_id"
160+
case personCounts = "person_counts"
161+
case productID = "product_id"
162+
case resourceID = "resource_id"
163+
case startDate = "start"
164+
case statusKey = "status"
165+
case localTimezone = "local_timezone"
166+
}
167+
}
168+
169+
// MARK: - Decoding Errors
170+
//
171+
enum BookingDecodingError: Error {
172+
case missingSiteID
173+
}
174+
175+
// MARK: - Supporting Types
176+
//
177+
178+
/// Represents a Booking Status.
179+
///
180+
public enum BookingStatus: String, CaseIterable {
181+
case complete = "complete"
182+
case paid = "paid"
183+
case unpaid = "unpaid"
184+
case cancelled = "cancelled"
185+
186+
public init(rawValue: String) {
187+
switch rawValue {
188+
case "complete":
189+
self = .complete
190+
case "paid":
191+
self = .paid
192+
case "unpaid":
193+
self = .unpaid
194+
case "cancelled":
195+
self = .cancelled
196+
default:
197+
self = .unpaid
198+
}
199+
}
200+
}

0 commit comments

Comments
 (0)