Commit 635f2b3c authored by Eyad Hasan's avatar Eyad Hasan
Browse files

Mapping objects

Mapping the JSON keys to our objects, eventually we will have it displaying.  Soon^TM
parent 0d633222
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import Foundation import Foundation
import RealmSwift import RealmSwift
import ObjectMapper
// //
// MARK: - Data Model // MARK: - Data Model
// //
...@@ -21,13 +22,25 @@ enum Day: Int { ...@@ -21,13 +22,25 @@ enum Day: Int {
class Facility: Object { class Facility: Object {
dynamic var slug = "" dynamic var slug = ""
dynamic var facilityName = "" dynamic var facilityName = ""
let facilityLocation = Locations() var facilityLocation = Locations()
let category = Categories() var category = Categories()
let facilityTags = List<FacilityTags>() var facilityTags = List<FacilityTags>()
let mainSchedule = MainSchedule() var mainSchedule = MainSchedule()
let specialSchedule = SpecialSchedule() var specialSchedule = SpecialSchedule()
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map){
slug <- map["slug"]
facilityName <- map["facility_name"]
facilityLocation <- map["facility_location"]
category <- map["facility_category"]
facilityTags <- map["facility_product_tags"]
mainSchedule <- map["main_schedule"]
specialSchedule <- map["special_schedules"]
}
} }
...@@ -40,6 +53,19 @@ class Locations: Object { ...@@ -40,6 +53,19 @@ class Locations: Object {
dynamic var campus = "" dynamic var campus = ""
dynamic var onCampus = false dynamic var onCampus = false
required convenience init?(map: Map){
self.init()
}
func mapping(map: Map){
id <- map["id"]
created <- map["created"]
lastmodified <- map["modified"]
building <- map["building"]
address <- map["address"]
campus <- map["campus_region"]
onCampus <- map["on_campus"]
}
} }
class Categories: Object { class Categories: Object {
...@@ -48,6 +74,16 @@ class Categories: Object { ...@@ -48,6 +74,16 @@ class Categories: Object {
dynamic var modified = "" dynamic var modified = ""
dynamic var categoryName = "" dynamic var categoryName = ""
required convenience init?(map: Map){
self.init()
}
func mapping(map: Map){
id <- map["id"]
created <- map["created"]
modified <- map["modified"]
categoryName <- map["name"]
}
} }
class FacilityTags: Object { class FacilityTags: Object {
...@@ -57,7 +93,7 @@ class FacilityTags: Object { ...@@ -57,7 +93,7 @@ class FacilityTags: Object {
class MainSchedule: Object { class MainSchedule: Object {
dynamic var id = 0 dynamic var id = 0
let openTimes = List<OpenTimes>() var openTimes = List<OpenTimes>()
dynamic var lastModified = "" dynamic var lastModified = ""
dynamic var name = "" dynamic var name = ""
dynamic var validStart = "" dynamic var validStart = ""
...@@ -65,6 +101,19 @@ class MainSchedule: Object { ...@@ -65,6 +101,19 @@ class MainSchedule: Object {
dynamic var twentyFourHours = false dynamic var twentyFourHours = false
required convenience init?(map: Map){
self.init()
}
func mapping(map: Map){
id <- map["id"]
openTimes <- map["open_times"]
lastModified <- map["modified"]
name <- map["name"]
validStart <- map["valid_start"]
validEnd <- map["valid_end"]
twentyFourHours <- map["twenty_four_hours"]
}
} }
class SpecialSchedule: Object { class SpecialSchedule: Object {
......
...@@ -15,7 +15,7 @@ class SRCTNetworkController: NSObject { ...@@ -15,7 +15,7 @@ class SRCTNetworkController: NSObject {
//https://api.srct.gmu.edu/whatsopen/v2/facilities/?format=json //https://api.srct.gmu.edu/whatsopen/v2/facilities/?format=json
public static func performDownload(completion: @escaping (_ result: Array<Facility>) -> Void) { public static func performDownload(completion: @escaping (_ result: Array<Facility>) -> Void) {
let facility = Facility()
let requestURL: NSURL = NSURL(string: "https://api.srct.gmu.edu/whatsopen/v2/facilities/?format=json")! let requestURL: NSURL = NSURL(string: "https://api.srct.gmu.edu/whatsopen/v2/facilities/?format=json")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL) let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
let session = URLSession.shared let session = URLSession.shared
...@@ -31,8 +31,9 @@ class SRCTNetworkController: NSObject { ...@@ -31,8 +31,9 @@ class SRCTNetworkController: NSObject {
do{ do{
let realm = try! Realm() let realm = try! Realm()
let json = try? JSONSerialization.jsonObject(with: dataN, options: []) let json = try? JSONSerialization.jsonObject(with: dataN, options: [])
Object.init(value: "slug") try! realm.write {
realm.add(facility, update: true)
}
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment