Przeglądaj źródła

Added API 'driverIdActionIdUnitsGET' and 'driverIdActionIdUnitIdGET'

Lukas Cerny 1 rok temu
rodzic
commit
b8531a1d3b

+ 28 - 0
src/main/java/cz/senslog/telemetry/database/repository/MapLogRepository.java

@@ -451,6 +451,34 @@ public class MapLogRepository implements SensLogRepository {
     }
 
     @Override
+    public Future<Unit> findUnitByIdAndDriverIdAndActionId(long unitId, int driverId, int actionId) {
+        return client.preparedQuery("SELECT u.unit_id, u.name, u.imei, u.description " +
+                        "FROM maplog.unit AS u " +
+                        "JOIN maplog.driver_to_action AS dta ON u.unit_id = dta.unit_id " +
+                        "WHERE u.unit_id = $1 AND dta.driver_id = $2 AND dta.action_id = $3")
+                .execute(Tuple.of(unitId, driverId, actionId))
+                .map(RowSet::iterator)
+                .map(iterator -> iterator.hasNext() ? ROW_TO_UNIT.apply(iterator.next()) : null)
+                .onFailure(logger::catching);
+    }
+
+    @Override
+    public Future<List<Unit>> findUnitsByDriverIdAndActionId(int driverId, int actionId) {
+        return client.preparedQuery("SELECT u.unit_id, u.name, u.imei, u.description FROM maplog.unit AS u " +
+                "JOIN maplog.driver_to_action dta on u.unit_id = dta.unit_id " +
+                "WHERE dta.driver_id = $1 AND dta.action_id = $2")
+                .execute(Tuple.of(driverId, actionId))
+                .map(rs -> StreamSupport.stream(rs.spliterator(), false)
+                        .map(row -> Unit.of(
+                                row.getLong("unit_id"),
+                                row.getString("name"),
+                                row.getString("imei"),
+                                row.getString("description")
+                        )).collect(toList()))
+                .onFailure(logger::catching);
+    }
+
+    @Override
     public Future<Map<Long, Sensor>> findSensorsByUnitIdGroupById(long unitId) {
         return findSensorsByUnitId(unitId).map(sensors -> sensors.stream()
                 .collect(Collectors.toMap(Sensor::getSensorId, Function.identity())));

+ 10 - 0
src/main/java/cz/senslog/telemetry/database/repository/MockMapLogRepository.java

@@ -211,6 +211,16 @@ public class MockMapLogRepository implements SensLogRepository {
     }
 
     @Override
+    public Future<Unit> findUnitByIdAndDriverIdAndActionId(long unitId, int driverId, int actionId) {
+        return Future.succeededFuture(Unit.of(unitId, "mock(name)", "mock(imei)", "mock(description)"));
+    }
+
+    @Override
+    public Future<List<Unit>> findUnitsByDriverIdAndActionId(int driverId, int actionId) {
+        return Future.succeededFuture(Collections.emptyList());
+    }
+
+    @Override
     public Future<List<UnitLocation>> findUnitsLocationsByCampaignId(long campaignId, int limitPerUnit, OffsetDateTime from, OffsetDateTime to, ZoneId zone, SortType sort) {
         return Future.succeededFuture(Collections.emptyList());
     }

+ 2 - 0
src/main/java/cz/senslog/telemetry/database/repository/SensLogRepository.java

@@ -27,6 +27,8 @@ public interface SensLogRepository {
     Future<List<CampaignUnit>> findUnitsByCampaignId(long campaignId);
     Future<CampaignUnit> findUnitByIdAndCampaignId(long unitId, long campaignId);
     Future<Unit> findUnitByIdAndDriverId(long unitId, int driverId);
+    Future<Unit> findUnitByIdAndDriverIdAndActionId(long unitId, int driverId, int actionId);
+    Future<List<Unit>> findUnitsByDriverIdAndActionId(int driverId, int actionId);
     Future<List<Long>> findUnitIdsByCampaignId(long campaignId);
 
     Future<List<Sensor>> allSensors();

+ 2 - 0
src/main/java/cz/senslog/telemetry/server/HttpVertxServer.java

@@ -85,6 +85,8 @@ public final class HttpVertxServer extends AbstractVerticle {
                     openAPIRouterBuilder.operation("driverIdUnitIdActionsGET").handler(apiHandler::driverIdUnitIdActionsGET);
                     openAPIRouterBuilder.operation("driverIdActionsGET").handler(apiHandler::driverIdActionsGET);
                     openAPIRouterBuilder.operation("driverIdActionIdGET").handler(apiHandler::driverIdActionIdGET);
+                    openAPIRouterBuilder.operation("driverIdActionIdUnitsGET").handler(apiHandler::driverIdActionIdUnitsGET);
+                    openAPIRouterBuilder.operation("driverIdActionIdUnitIdGET").handler(apiHandler::driverIdActionIdUnitIdGET);
 
                     Router mainRouter = openAPIRouterBuilder.createRouter();
 //                    mainRouter.route().handler(LoggerHandler.create());

+ 47 - 0
src/main/java/cz/senslog/telemetry/server/OpenAPIHandler.java

@@ -953,4 +953,51 @@ public class OpenAPIHandler {
                         )).encode())
                         .onFailure(th -> rc.fail(400, th)));
     }
+
+    public void driverIdActionIdUnitsGET(RoutingContext rc) {
+        String host =  hostURLFull(rc.request());
+
+        int driverId = Integer.parseInt(rc.pathParam("driverId"));
+        int actionId = Integer.parseInt(rc.pathParam("actionId"));
+
+        List<String> paramNavigationLinks = rc.queryParam("navigationLinks");
+        boolean navigationLinks = paramNavigationLinks.isEmpty() ? DEFAULT_NAVIGATION_LINKS : parseBoolean(paramNavigationLinks.get(0));
+
+        repo.findUnitsByDriverIdAndActionId(driverId, actionId)
+                .onSuccess(units -> rc.response().end(new JsonArray(
+                        units.stream().map(u -> (navigationLinks ? JsonObject.of(
+                                "DriverActionUnit@NavigationLink", String.format("%s/drivers/%d/actions/%d/units/%d",host, driverId, actionId, u.getUnitId())
+                        ) : JsonObject.of()).mergeIn(JsonObject.of(
+                                "unitId", u.getUnitId(),
+                                "name", u.getName(),
+                                "description", u.getDescription()
+                        ))).collect(toList())
+                ).encode()))
+                .onFailure(th -> rc.fail(400, th));
+    }
+
+    public void driverIdActionIdUnitIdGET(RoutingContext rc) {
+        String host =  hostURLFull(rc.request());
+
+        int driverId = Integer.parseInt(rc.pathParam("driverId"));
+        int actionId = Integer.parseInt(rc.pathParam("actionId"));
+        long unitId = Integer.parseInt(rc.pathParam("unitId"));
+
+        List<String> paramNavigationLinks = rc.queryParam("navigationLinks");
+        boolean navigationLinks = paramNavigationLinks.isEmpty() ? DEFAULT_NAVIGATION_LINKS : parseBoolean(paramNavigationLinks.get(0));
+
+        repo.findUnitByIdAndDriverIdAndActionId(unitId, driverId, actionId)
+                .onSuccess(u -> rc.response().end((navigationLinks ? JsonObject.of(
+                                "self@NavigationLink", String.format("%s/drivers/%d/actions/%d/units/%d", host, driverId, actionId, u.getUnitId()),
+                                "Unit@NavigationLink", String.format("%s/units/%d", host, u.getUnitId()),
+                                "DriverAction@NavigationLink", String.format("%s/drivers/%d/actions/%d", host, driverId, actionId),
+                                "Events@NavigationLink", String.format("%s/ drivers/%d/units/%d/actions/%d/events", host, driverId, u.getUnitId(), actionId)
+                        ) : JsonObject.of()).mergeIn(JsonObject.of(
+                                "unitId", u.getUnitId(),
+                                "name", u.getName(),
+                                "imei", u.getImei(),
+                                "description", u.getDescription()
+                        )).encode())
+                        .onFailure(th -> rc.fail(400, th)));
+    }
 }

+ 82 - 3
src/main/resources/openAPISpec.yaml

@@ -679,7 +679,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /drivers/{driverId}/actions/{actionId}/units:
-    get:
+    get: # done
       operationId: driverIdActionIdUnitsGET
       summary: Publish basic info about units on which the driver performed its action
       parameters:
@@ -702,6 +702,29 @@ paths:
               schema:
                 $ref: '#/components/schemas/Error'
 
+  /drivers/{driverId}/actions/{actionId}/units/{unitId}:
+    get: # done
+      operationId: driverIdActionIdUnitIdGET
+      summary: Publish detail info about the unit on which the driver performed the action
+      parameters:
+        - $ref: '#/components/parameters/driverIdParam'
+        - $ref: '#/components/parameters/actionIdParam'
+        - $ref: '#/components/parameters/unitIdParam'
+        - $ref: '#/components/parameters/navigationLinksParam'
+      responses:
+        200:
+          description: JSON Object of the unit
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/DriverActionUnitDetailInfo'
+        default:
+          description: unexpected error
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/Error'
+
   /drivers/{driverId}/units/{unitId}/actions/{actionId}:
     get:
       operationId: driverIdUnitIdActionIdGET
@@ -1917,7 +1940,7 @@ components:
           type: string
           format: uri
           x-graph-properties:
-            linkTo: driverIdUnitIdActionIdGET
+            linkTo: driverIdActionIdUnitIdGET
       properties:
         DriverActionUnit@NavigationLink:
           $ref: '#/components/schemas/DriverActionUnitBasicInfo/x-NavigationLinks/DriverActionUnit@NavigationLink'
@@ -1930,11 +1953,67 @@ components:
         description:
           type: string
       example:
-        DriverActionUnit@NavigationLink: "<domain>/drivers/42/units/25/actions/258"
+        DriverActionUnit@NavigationLink: "<domain>/drivers/42/actions/258/units/25"
         unitId: 25
         name: "Unit name"
         description: "Purpose of the Unit 25"
 
+    DriverActionUnitDetailInfo:
+      type: object
+      required:
+        - unitId
+        - name
+        - imei
+      x-NavigationLinks:
+        self@NavigationLink:
+          type: string
+          format: uri
+          x-graph-properties:
+            linkTo: driverIdActionIdUnitIdGET
+        DriverAction@NavigationLink:
+          type: string
+          format: uri
+          x-graph-properties:
+            linkTo: driverIdActionIdGET
+        Events@NavigationLink:
+          type: string
+          format: uri
+          x-graph-properties:
+            linkTo: driverIdUnitIdActionIdEventsGET
+        Unit@NavigationLink:
+          type: string
+          format: uri
+          x-graph-properties:
+            linkTo: unitIdGET
+      properties:
+        self@NavigationLink:
+          $ref: '#/components/schemas/DriverActionUnitDetailInfo/x-NavigationLinks/self@NavigationLink'
+        DriverAction@NavigationLink:
+          $ref: '#/components/schemas/DriverActionUnitDetailInfo/x-NavigationLinks/DriverAction@NavigationLink'
+        Unit@NavigationLink:
+          $ref: '#/components/schemas/DriverActionUnitDetailInfo/x-NavigationLinks/Unit@NavigationLink'
+        Events@NavigationLink:
+          $ref: '#/components/schemas/DriverActionUnitDetailInfo/x-NavigationLinks/Events@NavigationLink'
+        unitId:
+          description: Identifier of the unit
+          type: integer
+          format: int64
+        name:
+          type: string
+        imei:
+          type: string
+        description:
+          type: string
+      example:
+        self@NavigationLink: "<domain>/drivers/42/actions/258/units/25"
+        DriverAction@NavigationLink: "<domain>/drivers/42/actions/258"
+        Events@NavigationLink: "<domain>/drivers/42/units/25/actions/258/events"
+        Unit@NavigationLink: "<domain>/units/25"
+        unitId: 25
+        name: "Mobile Unit"
+        description: "Mobile Unit 25"
+        imei: "3434535323345"
+
 
     SensorBasicInfo:
       type: object