-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserPage.swift
70 lines (56 loc) · 2.82 KB
/
UserPage.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
//
// UserPage.swift
// Salisbury_Book_Exchange
//
// Created by Dean Kromer on 12/6/17.
// Copyright © 2017 Dean Kromer. All rights reserved.
//
import UIKit
import Firebase
class UserPage: UIViewController{
//----------------------------------------------------------------------
//--------------------VARIABLES-----------------------------------------
var ref: DatabaseReference!
var handle:DatabaseHandle?
var books = [Book]()
var myList:[String] = []
//-----------------------------------------------------------------------//
//------------------MAIN-------------------------------------------------//
override func viewDidLoad() {
}
//------------------------------------------------------------------------
//------------------ADD ITEM TO FIREBASE DATABASE-------------------------
@IBAction func addItemButton(_ sender: Any) {
ref = Database.database().reference()
let uid = Auth.auth().currentUser?.uid
let alert = UIAlertController(title: "Add Book", message: "Enter Book Details", preferredStyle: .alert)
alert.addTextField { (title) in title.placeholder = "Enter Title"}
alert.addTextField { (price) in price.placeholder = "Enter Price"}
alert.addTextField { (condition) in condition.placeholder = "Enter Condition"}
alert.addTextField { (isbn) in isbn.placeholder = "Enter ISBN #"}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Add Book", style: .default, handler: { (action) in
let titleText = alert.textFields![0]
let priceText = alert.textFields![1]
let conditionText = alert.textFields![2]
let isbnText = alert.textFields![3]
let key = self.ref.child("Books").childByAutoId().key
let book1 = ["title": titleText.text!, "price": priceText.text!, "condition": conditionText.text!, "ISBN": isbnText.text!, "user ID" : uid!, "book ID": key]
let book2 = ["title": titleText.text!, "book ID" : key]
//Add 2 references of book to DB (Books and Users)//
self.ref.child("Books").child(key).setValue(book1)
self.ref.child("Users").child(uid!).child("Selling").childByAutoId().setValue(book2)
}))
present(alert, animated: true, completion: nil)
}
//---------------------- SIGN OUT ----------------------------------------------------------
@IBAction func signoutButton(_ sender: Any) {
do {
try Auth.auth().signOut()
self.performSegue(withIdentifier: "LoggedOut", sender: self)
dismiss(animated: true, completion: nil)
} catch {
print("There was a problem logging out")
}
}
}