Skip to content
This repository was archived by the owner on Jul 5, 2021. It is now read-only.

Latest commit

 

History

History
134 lines (101 loc) · 4.49 KB

File metadata and controls

134 lines (101 loc) · 4.49 KB

OData Query Examples

This guide contains example code for you to get started with writing your own queries for the Travel Expense service.

The dataService in each example is expected to be the sample Travel Expense service. In an app generated by the Assistant, this is exposed as a property on the AppDelegate class. To access this more conveniently, the following lines can be placed at the top of your view controller classes:

lazy var appDelegate = UIApplication.shared.delegate as! AppDelegate
lazy var dataService = appDelegate.travelexpense!

The code in each of these examples can be used with any of the proxy classes. The data service provides fetch methods for each type, and any proxy class instance can be passed into the createEntity, updateEntity, and/or deleteEntity methods.

Getting a list of reports

  • Assume a property called reports of type [ExpenseReportItem].
// Order the results by the end date of the expense report, descending.
let query = DataQuery().orderBy(ExpenseReportItem.reportend, .descending)

// Fetch all expense reports, using the query parameters defined above.
dataService.fetchExpenseReports(matching: query) { [weak self] reports, error in

    // Make sure no errors occurred.
    guard let reports = reports else {
        NSLog("Error: %@", error!.localizedDescription)
        return
    }
    
    // Set the `reports` property and re-display the list.
    self?.reports = reports
    self?.tableView.reloadData()
}

Getting the list of expenses for a report

When displaying the details for a selected report, you will likely want to display the list of associated expense items.

  • Assume a property called report of type ExpenseReportItem.
  • Assume a property called expenses of type [ExpenseItem].
// Filter the results by the selected report ID, automatically retrieving
// associated records for the payment type and attachments, and ordering
// by the expense date, ascending.
let reportID = report.reportid!
let query = DataQuery().filter(ExpenseItem.reportid == reportID)
                       .expand(ExpenseItem.paymentType, ExpenseItem.attachments)
                       .orderBy(ExpenseItem.itemdate)

// Fetch all expense items, using the query parameters defined above.
dataService.fetchExpenseItems(matching: query) { [weak self] expenses, error in

    // Make sure no errors occurred.
    guard let expenses = expenses else {
        NSLog("Error: %@", error!.localizedDescription)
        return
    }
    
    // Set the `expenses` property and re-display the list.
    self?.expenses = expenses
    self?.tableView.reloadData()
}

Creating a new expense

  • Assume a property called report of type ExpenseReportItem.
  • Assume a property called newExpense of type ExpenseItem.
  • Assume the properties of newExpense have been filled in from the UI.
// Associate the expense with the parent report.
newExpense.reportid = report.reportid

// Create a new record for the expense.
dataService.createEntity(newExpense) { error in
    // Make sure no errors occurred.
    if let error = error {
        NSLog("Error: %@", error.localizedDescription)
    }
}

Updating an existing expense

  • Assume a property called existingExpense of type ExpenseItem.
dataService.updateEntity(existingExpense) { error in
    // Make sure no errors occurred.
    if let error = error {
        NSLog("Error: %@", error.localizedDescription)
    }
}

Deleting an existing expense

  • Assume a property called existingExpense of type ExpenseItem.
dataService.deleteEntity(existingExpense) { error in
    // Make sure no errors occurred.
    if let error = error {
        NSLog("Error: %@", error.localizedDescription)
    }
}

OData entity to proxy class mapping

For reference, here are the OData entities that are part of the Travel Expense service data model, with the names of the proxy classes that have been generated.

Expense data

OData Entity Proxy Class name
Expense Report ExpenseReportItem
Report Status ReportStatus
Expense Item ExpenseItem
Currency Currency
Expense Type Expense
Payment Type Payment

Trip and Reservation data

OData Entity Proxy Class name
Trip TripItem
Reservation Item ReservationItem
Reservation Type Reservation