Commit 3ea2a854 authored by Thomas Frion's avatar Thomas Frion

[UPDF] Add policeApi client

parent 07af8a90
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as models from '../models/models';
import { ApiService } from '../api';
import { IClient } from './iclient';
import { AuthGuard } from 'src/app/services/auth-guard.service';
@Injectable({
providedIn: 'root'
})
export class PoliceService extends ApiService<models.PoliceUnit> implements IClient<models.PoliceUnit> {
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
super(http, authGuard);
}
public all(headers?: HttpHeaders): Promise<models.PoliceUnit[]> {
return this.get('/units/police', headers);
}
public one(selector: any, headers?: HttpHeaders): Promise<models.PoliceUnit> {
return this.read('/units/police'+selector, headers);
}
public save(elem: models.PoliceUnit, headers?: HttpHeaders): Promise<models.PoliceUnit> {
let res : Promise<models.PoliceUnit>;
if(elem.id !== undefined){
res = this.update('/units/police'+elem.id, JSON.stringify(elem), headers);
} else {
res = this.create('/units/police', JSON.stringify(elem), headers);
}
return res;
}
public remove(elem: models.PoliceUnit, headers?: HttpHeaders): boolean {
return this.delete('/units/police'+elem.id, headers);
}
public async getInBoundaries(boundaries: any, headers?: HttpHeaders): Promise<models.PoliceUnit[]>{
if(headers !== undefined)
this.defaultHeaders = headers;
else
await this.authHeader();
return this.http.post<models.PoliceUnit[]>(
this.baseUrl + '/units/police/boundaries/',
JSON.stringify(boundaries),
{headers: this.defaultHeaders}
).toPromise();
}
}
......@@ -79,4 +79,14 @@ export interface GendUnit{
dept: string,
addr: string,
phone: string,
}
export interface PoliceUnit{
readonly id?: number,
code: string,
name: string,
geom: string,
dept: string,
addr: string,
phone: string,
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ import { CommonModule } from '@angular/common';
import { AuthService } from './auth.service';
import { GendUnitService } from './api/clients/gendunit.service';
import { PoliceService } from './api/clients/police.service';
import { ServiceService } from './api/clients/service.service'
import { TokenService } from './api/clients/token.service'
import { UnitService } from './api/clients/unit.service'
......@@ -17,6 +18,7 @@ import { WebcamService } from './api/clients/webcam.service'
providers: [
AuthService,
GendUnitService,
PoliceService,
ServiceService,
TokenService,
UnitService,
......
......@@ -13,4 +13,8 @@ urlpatterns = [
path('units/gendarmerie/<int:pk>', views.GendUnitDetail.as_view()),
path('units/gendarmerie/<slug:code>', views.GendUnitDetailCode.as_view()),
path('units/gendarmerie/boundaries/', views.GendUnitBoundaries.as_view()),
path('units/police', views.PoliceList.as_view()),
path('units/police/<int:pk>', views.PoliceUnitDetail.as_view()),
path('units/police/boundaries/', views.PoliceUnitBoundaries.as_view()),
]
\ No newline at end of file
......@@ -255,3 +255,84 @@ class GendUnitBoundaries(APIView):
return Response(serializer.data)
except GendUnit.DoesNotExist:
raise Http404
"""
=== PoliceUnit view: List
"""
class PoliceList(APIView):
@swagger_auto_schema(
responses={200: PoliceUnitSerializer(many=True)},
)
def get(self, request, format=None):
units = PoliceUnit.objects.all()
serializer = PoliceUnitSerializer(units, many = True)
return Response(serializer.data)
@swagger_auto_schema(request_body=PoliceUnitSerializer,)
def post(self, request, format=None):
serializer = PoliceUnitSerializer(data = request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
"""
=== PoliceUnit view: Detail
"""
class PoliceUnitDetail(APIView):
def get_object(self, pk):
try:
return PoliceUnit.objects.get(pk=pk)
except GendUnit.DoesNotExist:
raise Http404
@swagger_auto_schema(
responses={200: PoliceUnitSerializer},
)
def get(self, request, pk, format=None):
unit = self.get_object(pk)
serializer = PoliceUnitSerializer(unit)
return Response(serializer.data)
@swagger_auto_schema(request_body=PoliceUnitSerializer,)
def put(self, request, pk, format=None):
unit = self.get_object(pk)
serializer = PoliceUnitSerializer(unit, data = request.data)
if serializer.is_valid(): # If sent data are correct, we update the user
serializer.save()
# We return sent data, the HTTP code is 200
return Response(user_serializer.data)
# We return all catched errors whith the HTTP code 400
return Response(user_serializer.errors, status = 400)
@swagger_auto_schema(
responses={
status.HTTP_204_NO_CONTENT: openapi.Response(
description="This should not crash (response object with no schema)"
)
}
)
def delete(self, request, pk, format=None):
unit = self.get_object(pk)
# We return with HTTP code 204
return Response(status = 204)
"""
=== PoliceUnit view: Get units inside boundaries
"""
class PoliceUnitBoundaries(APIView):
def post(self, request, format=None):
x_min = request.data.get('x_min')
y_min = request.data.get('y_min')
x_max = request.data.get('x_max')
y_max = request.data.get('y_max')
area = Polygon.from_bbox((x_min, y_min, x_max, y_max))
try:
units = PoliceUnit.objects.filter(geom__contained = area)
serializer = PoliceUnitSerializer(units, many = True)
return Response(serializer.data)
except GendUnit.DoesNotExist:
raise Http404
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment