-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVehiclesExample.cs
More file actions
43 lines (35 loc) · 1.34 KB
/
VehiclesExample.cs
File metadata and controls
43 lines (35 loc) · 1.34 KB
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
namespace Example
{
using System;
using StarWarsApiCSharp;
public class VehiclesExample : IExecutor
{
public void Execute()
{
IRepository<Vehicle> vehicleRepository = new Repository<Vehicle>();
IRepository<Film> filmRepository = new Repository<Film>();
int vehicleId = 8;
Vehicle vehicle = vehicleRepository.GetById(vehicleId);
if (vehicle != null && vehicle.Films.Count > 0)
{
Console.WriteLine("Vehicle {0} has {1} films:", vehicle.Name, vehicle.Films.Count);
foreach (var film in vehicle.Films)
{
int filmId = this.GetFilmId(film);
//// getting related items should be done manual
Film relatedFilm = filmRepository.GetById(filmId);
Console.WriteLine(relatedFilm.Title);
}
}
}
//// Helper method for extract id from URL.
private int GetFilmId(string filmUrl)
{
//// filmUrl = https://swapi.dev/api/films/<Id>/
//// TODO: will not work if the Id has two digits.
int indexOfId = filmUrl.Length - 2;
int result = int.Parse(filmUrl[indexOfId].ToString());
return result;
}
}
}