Created
October 31, 2019 07:30
-
-
Save YordiLorenzo/2830c92dcfbe516a4aea28896d26fcd5 to your computer and use it in GitHub Desktop.
Job Entity mapping in iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Job.swift | |
// Temper | |
// | |
// Created by Said Rehouni on 24/07/2017. | |
// Copyright © 2017 Temper B.V. All rights reserved. | |
// | |
import CoreLocation | |
import ObjectMapper | |
public class Job: ModelType, JobSearchViewModelItem, Hashable { | |
public static func == (lhs: Job, rhs: Job) -> Bool { | |
lhs.jobCategory?.description == rhs.jobCategory?.description | |
} | |
public func hash(into hasher: inout Hasher) { | |
hasher.combine(jobCategory?.description) | |
} | |
/** | |
* Only provided by JobIndex / JobSearch /shifts response | |
*/ | |
public var type: JobSearchItemType = .job | |
public var id: String? | |
public var title: String? | |
public var key: String? | |
public var slug: String? | |
public var date: String? | |
public var location: CLLocationCoordinate2D? | |
public var distance: Float? | |
public var maxPossibleEarningsHour: Float? | |
public var maxPossibleEarningsTotal: Float? | |
/** | |
* JobIndex only provides (id, name, photos, description). No includes possible | |
*/ | |
public var client: Client? | |
/** | |
* JobIndex only provides (description, icon_path, slug). No includes possible | |
*/ | |
public var jobCategory: JobCategory? | |
public var openPositions: Int? | |
public var newMatchesCount: Int? | |
/** | |
* JobIndex provides a photo on the job level | |
*/ | |
public var photo: String? | |
// public var url: String? is array now | |
public var extraBriefing: String? | |
public var tips: Bool? | |
public var cancellationPolicy: Int? | |
public var dressCode: String? | |
public var skills: [SkillModel] = [] | |
public var appearances: [SkillModel] = [] | |
public var acceptedMatchesCount: Int? | |
public var shifts: [Shift] = [] | |
public var openShifts: JobShiftPaginationModel? | |
public var workShifts: JobShiftPaginationModel? | |
public var finishedShifts: JobShiftPaginationModel? | |
public var allowsFactoring: Bool? | |
public var negotiationAllowed: Bool? | |
public var reportAtAddress: Address? | |
public var factoringPaymentTermInDays: Int? | |
public init() {} | |
public init(input: JobResource) { | |
self.id = input.id | |
self.key = input.key | |
self.title = input.title | |
self.slug = input.slug | |
self.date = input.date | |
self.extraBriefing = input.extraBriefing | |
if let location = input.location { | |
self.location = CLLocationCoordinate2D(input: location) | |
} | |
self.distance = input.distance?.floatValue | |
// self.url = input.url is array now | |
self.maxPossibleEarningsHour = input.maxPossibleEarningsHour?.floatValue | |
self.maxPossibleEarningsTotal = input.maxPossibleEarningsTotal?.floatValue | |
if let jobCategory = input.jobCategory { | |
self.jobCategory = JobCategory(input: jobCategory) | |
} | |
self.openPositions = input.openPositions | |
self.acceptedMatchesCount = input.acceptedMatchesCount?.intValue | |
self.newMatchesCount = input.newMatchesCount?.intValue | |
for shift in input.shifts { | |
self.shifts.append(Shift(input: shift)) | |
} | |
if let openShifts = input.openShifts { | |
self.openShifts = JobShiftPaginationModel(input: openShifts) | |
} | |
if let finishedShifts = input.finishedShifts { | |
self.finishedShifts = JobShiftPaginationModel(input: finishedShifts) | |
} | |
if let workShifts = input.workShifts { | |
self.workShifts = JobShiftPaginationModel(input: workShifts) | |
} | |
if let client = input.client { | |
self.client = Client(input: client) | |
} | |
for skill in input.skills { | |
self.skills.append(SkillModel(input: skill)) | |
} | |
for appearance in input.appearances { | |
self.appearances.append(SkillModel(input: appearance)) | |
} | |
self.cancellationPolicy = input.cancellationPolicy | |
self.dressCode = input.dressCode | |
self.allowsFactoring = input.allowsFactoring | |
self.negotiationAllowed = input.negotiationAllowed | |
self.factoringPaymentTermInDays = input.factoringPaymentTermInDays | |
self.photo = input.photo | |
if let address = input.reportAtAddress { | |
self.reportAtAddress = Address(input: address) | |
} | |
self.tips = input.tips | |
if self.maxPossibleEarningsHour == nil { | |
let highestEarningShift = self.openShifts?.shifts?.max(by: { (lhs, rhs) -> Bool in | |
(lhs.earningsPerHour ?? 0.0) < (rhs.earningsPerHour ?? 0.0) | |
}) | |
self.maxPossibleEarningsHour = highestEarningShift?.earningsPerHour | |
} | |
} | |
public func isEnding(in time: TimeInterval) -> Bool? { | |
let now = Date().convertTo(region: defaultRegion) | |
let soon = Date().addingTimeInterval(time).convertTo(region: defaultRegion) | |
guard let jobDate = self.date?.toDate(dateFormat: .yyyy_MM_dd__HH_mm_ss)?.convertTo(region: defaultRegion) else { | |
return nil | |
} | |
return jobDate >= now && jobDate <= soon | |
} | |
} | |
public class JobCategory: ModelType { | |
public var description: String? | |
public var iconPath: String? | |
public var slug: String? | |
public init(input: JobIndexJobCategoryResource) { | |
self.description = input.description | |
self.iconPath = input.iconPath | |
self.slug = input.slug | |
} | |
} | |
extension V3 { | |
struct Job: Mappable, ModelType { | |
var id: String? | |
var title: String? | |
var extraBriefing: String? | |
var dressCode: String? | |
var tips: Bool? | |
var tipsString: String? | |
var slug: String? | |
var archived_at: String? | |
var links: JobLinks? | |
init?(map: Map) {} | |
mutating func mapping(map: Map) { | |
id <- map["data.id"] | |
title <- map["data.title"] | |
extraBriefing <- map["data.extra_briefing"] | |
dressCode <- map["data.dress_code"] | |
tipsString <- map["data.tips"] | |
tips = tipsString == "yes" | |
slug <- map["data.slug"] | |
archived_at <- map["data.archived_at"] | |
links <- map["data.links"] | |
} | |
} | |
struct JobLinks: Mappable { | |
var `self`: String? | |
var client: String? | |
var jobCategory: String? | |
var skills: String? | |
var appearances: String? | |
var reportAtAddress: String? | |
init?(map: Map) {} | |
mutating func mapping(map: Map) { | |
self.self <- map["self"] | |
client <- map["client"] | |
jobCategory <- map["job_category"] | |
skills <- map["skills"] | |
appearances <- map["appearances"] | |
reportAtAddress <- map["report_at_address"] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment