| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | |
|
| | 4 | | internal static class LandHelper |
| | 5 | | { |
| | 6 | | public static List<Land> ConvertQueryResult(LandQueryResult queryResult, string lowerCaseAddress) |
| | 7 | | { |
| 0 | 8 | | List<Land> lands = new List<Land>(); |
| | 9 | |
|
| | 10 | | // parcels and estates that I own |
| 0 | 11 | | for (int i = 0; i < queryResult.ownerParcels.Length; i++) |
| | 12 | | { |
| 0 | 13 | | lands.Add(FromParcel(queryResult.ownerParcels[i], LandRole.OWNER)); |
| | 14 | | } |
| | 15 | |
|
| 0 | 16 | | for (int i = 0; i < queryResult.ownerEstates.Length; i++) |
| | 17 | | { |
| 0 | 18 | | lands.Add(FromEstate(queryResult.ownerEstates[i], LandRole.OWNER)); |
| | 19 | | } |
| | 20 | |
|
| | 21 | | // parcels and estates that I operate |
| 0 | 22 | | for (int i = 0; i < queryResult.updateOperatorParcels.Length; i++) |
| | 23 | | { |
| 0 | 24 | | lands.Add(FromParcel(queryResult.updateOperatorParcels[i], LandRole.OPERATOR)); |
| | 25 | | } |
| | 26 | |
|
| 0 | 27 | | for (int i = 0; i < queryResult.updateOperatorEstates.Length; i++) |
| | 28 | | { |
| 0 | 29 | | lands.Add(FromEstate(queryResult.updateOperatorEstates[i], LandRole.OPERATOR)); |
| | 30 | | } |
| | 31 | |
|
| | 32 | | // I'm operator of all the lands from addresses that gave me UpdateManager permission |
| 0 | 33 | | for (int i = 0; i < queryResult.operatorAuthorizations.Length; i++) |
| | 34 | | { |
| 0 | 35 | | var owner = queryResult.operatorAuthorizations[i].owner; |
| 0 | 36 | | for (int j = 0; j < owner.parcels.Length; j++) |
| | 37 | | { |
| 0 | 38 | | var authLand = FromParcel(owner.parcels[j], LandRole.OPERATOR); |
| 0 | 39 | | authLand.operators.Add(lowerCaseAddress); |
| | 40 | |
|
| | 41 | | // skip if already owned or operated |
| 0 | 42 | | if (!lands.Exists(l => l.id == authLand.id)) |
| | 43 | | { |
| 0 | 44 | | lands.Add(authLand); |
| | 45 | | } |
| | 46 | | } |
| | 47 | |
|
| 0 | 48 | | for (int j = 0; j < owner.estates.Length; j++) |
| | 49 | | { |
| 0 | 50 | | if (owner.estates[j].parcels.Length == 0) |
| | 51 | | continue; |
| | 52 | |
|
| 0 | 53 | | var authEstate = FromEstate(owner.estates[j], LandRole.OPERATOR); |
| 0 | 54 | | authEstate.operators.Add(lowerCaseAddress); |
| | 55 | |
|
| | 56 | | // skip if already owned or operated |
| 0 | 57 | | if (!lands.Exists(l => l.id == authEstate.id)) |
| | 58 | | { |
| 0 | 59 | | lands.Add(authEstate); |
| | 60 | | } |
| | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | return lands |
| 0 | 65 | | .Where(land => land.type == LandType.PARCEL || land.parcels.Count > 0) |
| | 66 | | .ToList(); |
| | 67 | | } |
| | 68 | |
|
| | 69 | | static Land FromParcel(ParcelFields parcel, LandRole role) |
| | 70 | | { |
| 0 | 71 | | string id = CoordsToId(parcel.x, parcel.y); |
| 0 | 72 | | Land result = new Land() |
| | 73 | | { |
| | 74 | | id = id, |
| | 75 | | name = parcel.data?.name ?? $"Parcel {id}", |
| | 76 | | type = LandType.PARCEL, |
| | 77 | | role = role, |
| | 78 | | description = parcel.data?.description, |
| | 79 | | owner = parcel.owner.address, |
| | 80 | | operators = new List<string>() |
| | 81 | | }; |
| | 82 | |
|
| 0 | 83 | | if (int.TryParse(parcel.x, out int x) && int.TryParse(parcel.y, out int y)) |
| | 84 | | { |
| 0 | 85 | | result.x = x; |
| 0 | 86 | | result.y = y; |
| 0 | 87 | | result.parcels = new List<Parcel>() { new Parcel() { id = CoordsToId(parcel.x, parcel.y), x = x, y = y } }; |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | if (!string.IsNullOrEmpty(parcel.updateOperator)) |
| | 91 | | { |
| 0 | 92 | | result.operators.Add(parcel.updateOperator); |
| | 93 | | } |
| | 94 | |
|
| 0 | 95 | | return result; |
| | 96 | | } |
| | 97 | |
|
| | 98 | | static Land FromEstate(EstateFields estate, LandRole role) |
| | 99 | | { |
| 0 | 100 | | string id = estate.id; |
| | 101 | |
|
| 0 | 102 | | Land result = new Land() |
| | 103 | | { |
| | 104 | | id = id, |
| | 105 | | name = estate.data?.name ?? $"Parcel {id}", |
| | 106 | | type = LandType.ESTATE, |
| | 107 | | role = role, |
| | 108 | | description = estate.data?.description, |
| | 109 | | size = estate.size, |
| | 110 | | parcels = new List<Parcel>(), |
| | 111 | | owner = estate.owner.address, |
| | 112 | | operators = new List<string>() |
| | 113 | | }; |
| | 114 | |
|
| 0 | 115 | | for (int i = 0; i < estate.parcels.Length; i++) |
| | 116 | | { |
| 0 | 117 | | if (!(int.TryParse(estate.parcels[i].x, out int x) && (int.TryParse(estate.parcels[i].y, out int y)))) |
| | 118 | | { |
| | 119 | | continue; |
| | 120 | | } |
| | 121 | |
|
| 0 | 122 | | result.parcels.Add(new Parcel() |
| | 123 | | { |
| | 124 | | x = x, |
| | 125 | | y = y, |
| | 126 | | id = CoordsToId(estate.parcels[i].x, estate.parcels[i].y) |
| | 127 | | }); |
| | 128 | | } |
| | 129 | |
|
| 0 | 130 | | if (!string.IsNullOrEmpty(estate.updateOperator)) |
| | 131 | | { |
| 0 | 132 | | result.operators.Add(estate.updateOperator); |
| | 133 | | } |
| | 134 | |
|
| 0 | 135 | | return result; |
| | 136 | | } |
| | 137 | |
|
| 0 | 138 | | private static string CoordsToId(string x, string y) { return $"{x},{y}"; } |
| | 139 | | } |