-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParty.swift
More file actions
151 lines (108 loc) · 3.88 KB
/
Party.swift
File metadata and controls
151 lines (108 loc) · 3.88 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// Party.swift
// Musivote
//
// Created by Matthew Loucks on 3/9/23.
//
import Foundation
import PostgREST
// Responsible for creating, deleting, and updating parties
class Party {
/**
Is there an active party associated with this user?
*/
static func partyExists() async -> Bool {
struct DefaultsKeys {
static let partyCode = "partyCode"
}
let partyCode = await getPartyCode()
if partyCode == nil || partyCode ?? 0 <= 0 {
return false
}
return true
}
/**
Create a party and return the party_code
*/
static func create() async -> Int? {
print("### Creating party")
let client = getSupabaseConnection()
let deviceToken: String = getDeviceToken()
// insert
struct InsertModel: Encodable, Decodable {
let device_token: String?
let party_code: Int?
}
let insertData = InsertModel(device_token: deviceToken, party_code: nil)
let query = client!.database
.from("party")
.insert(values: insertData,
returning: .representation)
.select()
do {
let response: [InsertModel] = try await query.execute().value
print("RESPONSE \(response)")
if response.count <= 0 {
return nil
}
let partyCode = response[0].party_code
return partyCode
} catch {
print("### Insert Error: \(error)")
}
return nil
}
static func getPartyCode() async -> Int? {
print("### Retrieving party code")
let client = getSupabaseConnection()
let deviceToken: String = getDeviceToken()
struct InsertModel: Encodable, Decodable {
let device_token: String
let party_code: Int?
}
let insertData: InsertModel = InsertModel(device_token: deviceToken, party_code: nil)
let query = client!.database
.from("party")
.select()
.match(query: ["device_token" : insertData.device_token])
do {
let response: [InsertModel] = try await query.execute().value
print("### Returned: \(response)")
// no verdict
if response.count <= 0 {
return nil
}
let partyCode = response[0].party_code
return partyCode
} catch {
print("### Insert Error: \(error)")
}
return nil
}
/**
Delete the current party as defined by the currently saved device token
*/
static func delete() async -> Bool {
let client = getSupabaseConnection()
let deviceToken: String = getDeviceToken()
struct InsertModel: Encodable, Decodable {
let device_token: String
let party_code: Int?
}
let insertData: InsertModel = InsertModel(device_token: deviceToken, party_code: nil)
let query = client!.database
.from("party")
.delete()
.match(query: ["device_token" : insertData.device_token])
do {
let response: [InsertModel] = try await query.execute().value
// TODO: relook at this later to actually verify it's being deleted
// Right now if we get a response, we can just assume it was deleted
// and if not, it probably doesn't exist.
return true
} catch {
print("### Insert Error: \(error)")
}
return false
}
}