-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDataService.swift
executable file
·136 lines (97 loc) · 4.47 KB
/
DataService.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
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
// Created by luis castillo on 3/22/17.
// Copyright © 2017 luis castill0. All rights reserved.
//
import Foundation
import Firebase
import FirebaseAuth
import FirebaseStorage
class DataService {
static let dataService = DataService()
private var _BASE_REF = FIRDatabase.database().reference()
var BASE_REF: FIRDatabaseReference {
return _BASE_REF
}
var currentUser: FIRUser? {
return FIRAuth.auth()?.currentUser
}
var storageRef: FIRStorageReference {
return FIRStorage.storage().reference()
}
let job = Job()
let user = Users()
var fileUrl: String!
var postFileUrl: String!
var jobDate: String!
var mydescription: String!
func SignUp(username: String, email: String, password: String, data: Data) {
FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in
if let error = error {
print(error.localizedDescription)
return
}
let filePath = "profileImage/\(user!.uid)"
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"
self.storageRef.child(filePath).put(data, metadata: metadata, completion: { (metadata, error) in
if let error = error {
print(error.localizedDescription)
return
}
//let seg = LoginViewController()
self.fileUrl = metadata!.downloadURLs![0].absoluteString
let changeRequestPhoto = user!.profileChangeRequest()
changeRequestPhoto.photoURL = URL(string: self.fileUrl)
changeRequestPhoto.commitChanges(completion: { (error) in
if let error = error {
print(error.localizedDescription)
} else {
let newUser = FIRDatabase.database().reference().child("users").child((user?.uid)!)
newUser.setValue(["displayName" : "\(username)", "email" : "\(email)", "id": "\(user!.uid)",
"profileURL": "\(self.fileUrl!)", "myDescription": "\(self.mydescription)"])
}
})
})
let changeRequest = user?.profileChangeRequest()
changeRequest?.displayName = username
changeRequest?.commitChanges(completion: { (error) in
if let error = error {
print(error.localizedDescription)
return
}
})
})
}
//Gets param values from secondJPVC and posts to FB
func getData(title: String, company: String, location: String, data: Data, function: String, jobDescription: String, timestamp: String)
{
let Post = BASE_REF.child("jobPost").child((currentUser?.uid)!)
// keep track of user posts with autoId
let NewPost = Post.childByAutoId()
// get data and turn it into a string
let filePath = "jobPostImage/\(currentUser?.uid)"
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"
self.storageRef.child(filePath).put(data, metadata: metadata, completion: { (metadata, error) in
if let error = error {
print(error.localizedDescription)
return
}
self.postFileUrl = metadata!.downloadURLs![0].absoluteString
//set dictionary values in a new post with parameters of the function in firebase
NewPost.setValue(["title" : "\(title)",
"company" : "\(company)",
"location": "\(location)",
"function": "\(function)",
"jobDescription" : "\(jobDescription)",
"photoURL": "\(self.postFileUrl!)",
"timestamp": "\(timestamp)"
])
})
//send data to job model
NewPost.observe(.childAdded, with: { (snapshot) in
if let dict = snapshot.value as? [String: Any] {
self.job.setValuesForKeys(dict)
}
})
}
}