Commit 9c6d57e2 authored by Thomas Frion's avatar Thomas Frion

Merge branch 'dev'

parents 7862fddc 1c7639fe
<div class="main-container">
<app-sidebar-left class="left-menu"></app-sidebar-left>
<app-map></app-map>
</div>
<!-- NOT DONE YET:
<app-sidebar-right class='right-menu'></app-sidebar-right> #}
<app-coord-map></app-coord-map>
-->
</div>
\ No newline at end of file
......@@ -7,20 +7,13 @@ import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AppFrontComponent } from './app-front/app-front.component';
import { AuthComponent} from './auth-component/auth-component.component';
import { CoordMapComponent } from './coord-map/coord-map.component';
import { ErrorPageComponent } from './error-page/error-page.component';
import { MapComponent } from './map/map.component';
import { SidebarLeftComponent } from './sidebar-left/sidebar-left.component';
import { SidebarRightComponent } from './sidebar-right/sidebar-right.component';
import { LogoutComponent } from './logout/logout.component';
import { AuthService } from './services/auth.service';
import { AuthGuard } from './services/auth-guard.service';
import { ServiceApi } from './services/api/clients/service.api';
import { TokenApi } from './services/api/clients/token.api';
import { UnitApi } from './services/api/clients/unit.api';
import { UserApi } from './services/api/clients/user.api';
import { WebcamApi } from './services/api/clients/webcam.api';
import { AuthGuard } from './services/auth-guard.service';
import { PghmApiModule } from './pghm-api/pghm-api.module'
@NgModule({
......@@ -28,27 +21,20 @@ import { WebcamApi } from './services/api/clients/webcam.api';
AppComponent,
MapComponent,
SidebarLeftComponent,
CoordMapComponent,
AuthComponent,
AppFrontComponent,
ErrorPageComponent,
SidebarRightComponent,
LogoutComponent
],
imports: [
BrowserModule,
AppRoutingModule,
ReactiveFormsModule,
HttpClientModule
HttpClientModule,
PghmApiModule
],
providers: [
AuthService,
AuthGuard,
ServiceApi,
TokenApi,
UnitApi,
UserApi,
WebcamApi
AuthGuard,
],
bootstrap: [AppComponent]
})
......
......@@ -3,7 +3,7 @@ import { FormBuilder, FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { AuthService } from '../services/auth.service';
import { AuthService } from 'src/app/pghm-api/auth.service';
/**
* @AuthComponent:
......
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CoordMapComponent } from './coord-map.component';
describe('CoordMapComponent', () => {
let component: CoordMapComponent;
let fixture: ComponentFixture<CoordMapComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CoordMapComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CoordMapComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { Component, OnInit } from '@angular/core';
/**
* @CoordMapComponent:
* Component to display the coordinates of the pointer on the map,
* they are displayed at the bottom-roght of the scree
*/
@Component({
selector: 'app-coord-map',
templateUrl: './coord-map.component.html',
styleUrls: ['./coord-map.component.scss']
})
export class CoordMapComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '../services/auth.service';
import { AuthService } from 'src/app/pghm-api/auth.service';
@Component({
selector: 'app-logout',
......
This diff is collapsed.
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { AuthGuard } from '../auth-guard.service';
import { AuthGuard } from 'src/app/services/auth-guard.service';
import { environment } from 'src/environments/environment';
/**
......@@ -21,12 +21,6 @@ export abstract class ApiService<T> {
this.defaultHeaders = new HttpHeaders({
'Content-Type':'application/json',
});
if(authGuard !== undefined){
this.authGuard.getToken().then((token) => {
this.defaultHeaders = this.defaultHeaders.append('Authorization', 'Bearer ' + token);
});
}
}
/**
......@@ -34,10 +28,12 @@ export abstract class ApiService<T> {
* @param resource The resource name. Ex: .../myResources/
* @param headers HttpHeaders a custom HTTP header
*/
protected get(resource : string, headers? : HttpHeaders) : Promise<T[]> {
protected async get(resource : string, headers? : HttpHeaders) : Promise<T[]> {
if(headers !== undefined)
this.defaultHeaders = headers;
else
await this.authHeader();
return this.http.get<T[]>(
this.baseUrl + resource,
{headers: this.defaultHeaders}
......@@ -49,9 +45,11 @@ export abstract class ApiService<T> {
* @param resource The resource name. Ex: .../myResources/id-of-my-resource
* @param headers HttpHeaders a custom HTTP header
*/
protected read(resource : string, headers? : HttpHeaders) : Promise<T> {
protected async read(resource : string, headers? : HttpHeaders) : Promise<T> {
if(headers !== undefined)
this.defaultHeaders = headers;
else
await this.authHeader();
return this.http.get<T>(
this.baseUrl + resource,
......@@ -65,9 +63,11 @@ export abstract class ApiService<T> {
* @param params Element to create in the database
* @param headers HttpHeaders a custom HTTP header
*/
protected create(resource : string, params : any, headers? : HttpHeaders) : Promise<T>{
protected async create(resource : string, params : any, headers? : HttpHeaders) : Promise<T>{
if(headers !== undefined)
this.defaultHeaders = headers;
else
await this.authHeader();
return this.http.post<T>(
this.baseUrl + resource,
......@@ -82,9 +82,11 @@ export abstract class ApiService<T> {
* @param params Element to save
* @param headers HttpHeaders a custom HTTP header
*/
protected update(resource : string, params : any, headers? : HttpHeaders) : Promise<T>{
protected async update(resource : string, params : any, headers? : HttpHeaders) : Promise<T>{
if(headers !== undefined)
this.defaultHeaders = headers;
else
await this.authHeader();
return this.http.put<T>(
this.baseUrl + resource,
......@@ -118,4 +120,8 @@ export abstract class ApiService<T> {
return res;
}
protected async authHeader() {
this.defaultHeaders = this.defaultHeaders.set('Authorization', 'Bearer ' + await this.authGuard.getToken());
}
}
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 GendUnitService extends ApiService<models.GendUnit> implements IClient<models.GendUnit> {
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
super(http, authGuard);
}
public all(headers?: HttpHeaders): Promise<models.GendUnit[]> {
return this.get('/services/', headers);
}
public one(selector: any, headers?: HttpHeaders): Promise<models.GendUnit> {
return this.read('/services/' + selector, headers);
}
public save(elem: models.GendUnit, headers?: HttpHeaders): Promise<models.GendUnit> {
let result : Promise<models.GendUnit>;
if(elem.id !== undefined){
result = this.update('/services/' + elem.id, JSON.stringify(elem), headers);
} else {
result = this.create('/services/', JSON.stringify(elem), headers);
}
return result;
}
public remove(elem: models.GendUnit, headers?: HttpHeaders): boolean {
return this.delete('/services/' + elem.id, headers);
}
public async getInBoundaries(boundaries: any, headers?: HttpHeaders): Promise<models.GendUnit[]>{
if(headers !== undefined)
this.defaultHeaders = headers;
else
await this.authHeader();
return this.http.post<models.GendUnit[]>(
this.baseUrl + '/units/gendarmerie/boundaries/',
JSON.stringify(boundaries),
{headers: this.defaultHeaders}
).toPromise();
}
}
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
/**
* IApiService defines the behavior of an API client
*/
export interface IApiService<T> {
export interface IClient<T> {
/**
* Returns all registered T-type elements
* @param headers HttpHeaders a custom HTTP header
......
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as models from '../models/models';
import { ApiService } from '../api.service';
import { IApiService } from '../api-client.interface';
import { AuthGuard } from '../../auth-guard.service';;
@Injectable()
export class ServiceApi extends ApiService<models.Service> implements IApiService<models.Service>{
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
import { ApiService } from '../api';
import { IClient } from './iclient';
import { AuthGuard } from 'src/app/services/auth-guard.service';
@Injectable({
providedIn: 'root'
})
export class ServiceService extends ApiService<models.IService> implements IClient<models.IService> {
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
super(http, authGuard);
}
public all(headers?: HttpHeaders): Promise<models.Service[]> {
public all(headers?: HttpHeaders): Promise<models.IService[]> {
return this.get('/services/', headers);
}
public one(selector: any, headers?: HttpHeaders): Promise<models.Service> {
public one(selector: any, headers?: HttpHeaders): Promise<models.IService> {
return this.read('/services/' + selector, headers);
}
public save(elem: models.Service, headers?: HttpHeaders): Promise<models.Service> {
let result : Promise<models.Service>;
public save(elem: models.Service, headers?: HttpHeaders): Promise<models.IService> {
let result : Promise<models.IService>;
if(elem.id !== undefined){
result = this.update('/services/' + elem.id, JSON.stringify(elem), headers);
......@@ -36,4 +37,4 @@ export class ServiceApi extends ApiService<models.Service> implements IApiServic
public remove(elem: models.Service, headers?: HttpHeaders): boolean {
return this.delete('/services/' + elem.id, headers);
}
}
\ No newline at end of file
}
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as models from '../models/models';
import { ApiService } from '../api.service';
@Injectable()
export class TokenApi extends ApiService<models.Token> {
import { ApiService } from '../api';
constructor(protected http: HttpClient) {
@Injectable({
providedIn: 'root'
})
export class TokenService extends ApiService<models.Token> {
constructor(protected http: HttpClient) {
super(http, undefined);
this.defaultHeaders = new HttpHeaders({
......@@ -19,8 +20,12 @@ export class TokenApi extends ApiService<models.Token> {
if(user.username === undefined || user.password === undefined){
throw new Error("user must contains username and password properties.");
}
return this.create('/token/', JSON.stringify(user), headers);
return this.http.post<models.Token>(
this.baseUrl + '/token/',
user,
{headers: headers},
).toPromise();
}
public async refresh(token : models.Token, headers?:HttpHeaders) : Promise<models.TokenRefresh> {
......@@ -48,5 +53,4 @@ export class TokenApi extends ApiService<models.Token> {
{headers: this.defaultHeaders}
).toPromise();
}
}
\ No newline at end of file
}
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as models from '../models/models';
import { ApiService } from '../api.service';
import { IApiService } from '../api-client.interface';
import { AuthGuard } from '../../auth-guard.service';
@Injectable()
export class UnitApi extends ApiService<models.Unit> implements IApiService<models.Unit>{
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
import { ApiService } from '../api';
import { IClient } from './iclient';
import { AuthGuard } from 'src/app/services/auth-guard.service';
@Injectable({
providedIn: 'root'
})
export class UnitService extends ApiService<models.Unit> implements IClient<models.Unit> {
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
super(http, authGuard);
}
......@@ -37,5 +37,4 @@ export class UnitApi extends ApiService<models.Unit> implements IApiService<mode
public remove(elem: models.Unit, headers?: HttpHeaders): boolean {
return this.delete('/units/'+elem.id, headers);
}
}
\ No newline at end of file
}
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as models from '../models/models';
import { ApiService } from '../api.service';
import { IApiService } from '../api-client.interface';
import { AuthGuard } from '../../auth-guard.service';
@Injectable()
export class UserApi extends ApiService<models.PghmUser> implements IApiService<models.PghmUser>{
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
import { ApiService } from '../api';
import { IClient } from './iclient';
import { AuthGuard } from 'src/app/services/auth-guard.service';
@Injectable({
providedIn: 'root'
})
export class UserService extends ApiService<models.PghmUser> implements IClient<models.PghmUser>{
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
super(http, authGuard);
}
......@@ -37,4 +37,4 @@ export class UserApi extends ApiService<models.PghmUser> implements IApiService<
public remove(elem: models.PghmUser, headers?: HttpHeaders): boolean {
return this.delete('/users/' + elem.id, headers);
}
}
\ No newline at end of file
}
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 WebcamService extends ApiService<models.Webcam> implements IClient<models.Webcam> {
constructor(protected http: HttpClient, protected authGuard : AuthGuard) {
super(http, authGuard);
}
public all(headers?: HttpHeaders): Promise<models.Webcam[]> {
return this.get('/webcams/', headers);
}
public one(selector: any, headers?: HttpHeaders): Promise<models.Webcam> {
return this.read('/webcams/'+selector, headers);
}
public save(elem: models.Webcam, headers?: HttpHeaders): Promise<models.Webcam> {
let res : Promise<models.Webcam>;
if(elem.id !== undefined){
res = this.update('/webcas/'+elem.id, JSON.stringify(elem), headers);
} else {
res = this.create('/webcas/', JSON.stringify(elem), headers);
}
return res;
}
public remove(elem: models.Webcam, headers?: HttpHeaders): boolean {
return this.delete('/webcams/' + elem.id, headers);
}
}
......@@ -12,11 +12,44 @@ export interface ServiceSettings{
* @interface :
* Model representing an external service
*/
export interface Service {
export interface IService {
readonly id?: number,
readonly serviceSettings?: ServiceSettings[],
name: string,
baseUrl: string,
key?: string,
desc?: string
}
export class Service {
readonly id?: number;
readonly serviceSettings?: ServiceSettings[];
name: string;
baseUrl: string;
key?: string;
desc?: string;
constructor(contract: IService){
this.id = contract.id;
this.serviceSettings = contract.serviceSettings;
this.name = contract.name;
this.baseUrl = contract.baseUrl;
this.key = contract.key;
this.desc = contract.desc;
}
public getSetting(name: string) : ServiceSettings {
let i = 0;
let setting : ServiceSettings = null;
while(i < this.serviceSettings.length && setting == null){
if(this.serviceSettings[i].name == name){
setting = this.serviceSettings[i];
}
i++;
}
return setting;
}
}
\ No newline at end of file
......@@ -29,7 +29,7 @@ export interface UnitSetting{
export interface Unit{
readonly id?: number,
code: string,
nom: string,
name: string,
email: string,
geom: string,
dept: string,
......@@ -48,7 +48,7 @@ export class UnitModel {
constructor(unit : Unit) {
this.id = unit.id;
this.code = unit.code;
this.name = unit.nom;
this.name = unit.name;
this.email = unit.email;
this.geom = unit.geom;
this.dept = unit.dept;
......@@ -69,4 +69,14 @@ export class UnitModel {
return setting;
}
}
export interface GendUnit{
readonly id?: number,
code: string,
name: string,
geom: string,
dept: string,
addr: string,
phone: string,
}
\ No newline at end of file
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { AuthComponent } from '../auth-component/auth-component.component';
import { environment } from '../../environments/environment';
import { TokenApi } from './api/clients/token.api';
import { AuthComponent } from 'src/app/auth-component/auth-component.component';
import { environment } from 'src/environments/environment';
import { CookieService } from 'src/app/services/cookie.service';
import { TokenService } from './api/clients/token.service';
import { Token } from './api/models/token';
import { CookieService } from './cookie.service';
/**
* @Service :
*
......@@ -13,7 +13,9 @@ import { CookieService } from './cookie.service';
* Get the JWT generated by the API and save it in localStorage.
* If the API returns an error, the service will show the error message on the login page.
*/
@Injectable()
@Injectable({
providedIn: 'root'
})
export class AuthService {
private readonly tokenName = {
......@@ -27,7 +29,7 @@ export class AuthService {
constructor(
private router : Router,
private tokenApi : TokenApi,
private tokenApi : TokenService,
) {}
/**
......@@ -74,6 +76,8 @@ export class AuthService {
}
);
},(err) => {
console.log(err);
authComp.error = err.error.detail;
document.getElementById('loader').style.display = "none";
});
......@@ -114,6 +118,7 @@ export class AuthService {
newToken.access,
{minutes: this.accessTokenLifeTime}
);
return newToken.access;
}
} else
......
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AuthService } from './auth.service';
import { GendUnitService } from './api/clients/gendunit.service';
import { ServiceService } from './api/clients/service.service'
import { TokenService } from './api/clients/token.service'
import { UnitService } from './api/clients/unit.service'
import { UserService } from './api/clients/user.service'
import { WebcamService } from './api/clients/webcam.service'
@NgModule({
declarations: [],
imports: [
CommonModule
],
providers: [
AuthService,
GendUnitService,
ServiceService,
TokenService,
UnitService,
UserService,
WebcamService
]
})
export class PghmApiModule { }
import { TestBed } from '@angular/core/testing';
import { ApiService } from './api.service';
describe('ApiService', () => {
let service: ApiService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ApiService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});