wip
This commit is contained in:
20
.vscode/launch.json
vendored
Normal file
20
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "pwa-node",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Launch Program",
|
||||||
|
"skipFiles": [
|
||||||
|
"<node_internals>/**"
|
||||||
|
],
|
||||||
|
"program": "${workspaceFolder}\\src\\services\\GetCoordinatesByAddress.ts",
|
||||||
|
"outFiles": [
|
||||||
|
"${workspaceFolder}/**/*.js"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -21,6 +21,15 @@ export class CreateUsers1617210132141 implements MigrationInterface {
|
|||||||
{
|
{
|
||||||
name: 'email',
|
name: 'email',
|
||||||
type: 'varchar',
|
type: 'varchar',
|
||||||
|
length: '255',
|
||||||
|
isUnique: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'phone_number',
|
||||||
|
type: 'varchar',
|
||||||
|
length: '14',
|
||||||
|
isUnique: true,
|
||||||
|
isNullable: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'birth_date',
|
name: 'birth_date',
|
||||||
@@ -40,6 +49,13 @@ export class CreateUsers1617210132141 implements MigrationInterface {
|
|||||||
type: 'varchar',
|
type: 'varchar',
|
||||||
isNullable: true
|
isNullable: true
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'star_rating',
|
||||||
|
type: 'numeric',
|
||||||
|
precision: 3,
|
||||||
|
scale: 2,
|
||||||
|
isNullable: true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'created_at',
|
name: 'created_at',
|
||||||
type: 'timestamp',
|
type: 'timestamp',
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ export class CreateUsersSearching1652672860580 implements MigrationInterface {
|
|||||||
name: 'longitude_to',
|
name: 'longitude_to',
|
||||||
type: 'numeric',
|
type: 'numeric',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'address_to',
|
||||||
|
type: 'varchar',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'created_at',
|
name: 'created_at',
|
||||||
type: 'timestamp',
|
type: 'timestamp',
|
||||||
|
|||||||
37
src/models/TransportOffers.ts
Normal file
37
src/models/TransportOffers.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import {
|
||||||
|
Entity,
|
||||||
|
Column,
|
||||||
|
PrimaryGeneratedColumn,
|
||||||
|
CreateDateColumn,
|
||||||
|
ManyToOne,
|
||||||
|
JoinColumn
|
||||||
|
} from 'typeorm';
|
||||||
|
|
||||||
|
import User from './User';
|
||||||
|
|
||||||
|
@Entity('transport_offers')
|
||||||
|
class UserSearching {
|
||||||
|
@PrimaryGeneratedColumn('increment')
|
||||||
|
id_offer: string;
|
||||||
|
|
||||||
|
@ManyToOne(() => User, {eager: true})
|
||||||
|
@JoinColumn({ name: 'user_id', referencedColumnName: 'id_user' })
|
||||||
|
user: User;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
latitude_from: number;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
longitude_from: number;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
latitude_to: number;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
longitude_to: number;
|
||||||
|
|
||||||
|
@CreateDateColumn()
|
||||||
|
created_at: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default UserSearching;
|
||||||
@@ -16,6 +16,9 @@ class User {
|
|||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
email: string;
|
email: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
phone_number: string;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
birth_date: Date;
|
birth_date: Date;
|
||||||
@@ -28,6 +31,9 @@ class User {
|
|||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
bio: string;
|
bio: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
star_rating: number;
|
||||||
|
|
||||||
@CreateDateColumn()
|
@CreateDateColumn()
|
||||||
created_at: Date;
|
created_at: Date;
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ class UserSearching {
|
|||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
longitude_to: number;
|
longitude_to: number;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
address_to: string;
|
||||||
|
|
||||||
@CreateDateColumn()
|
@CreateDateColumn()
|
||||||
created_at: Date;
|
created_at: Date;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
import { getRepository } from 'typeorm';
|
import { getRepository } from 'typeorm';
|
||||||
import UserSearching from '../models/UsersSearching';
|
import UserSearching from '../models/UsersSearching';
|
||||||
|
import CalculateDistanceBetweenCoords from '../services/CalculateDistanceBetweenCoords';
|
||||||
import CreateUserSearchingService from '../services/CreateUserSearchingService';
|
import CreateUserSearchingService from '../services/CreateUserSearchingService';
|
||||||
import GetCoordinatesByAddress from '../services/GetCoordinatesByAddress';
|
import GetCoordinatesByAddress from '../services/GetCoordinatesByAddress';
|
||||||
|
|
||||||
@@ -37,10 +38,10 @@ searchRoutes.post('/', async (request, response) => {
|
|||||||
|
|
||||||
const getCoordinates = new GetCoordinatesByAddress();
|
const getCoordinates = new GetCoordinatesByAddress();
|
||||||
|
|
||||||
const coordinates = await getCoordinates.execute({address_to})
|
const coordinates = await getCoordinates.execute({ address_to });
|
||||||
|
|
||||||
const latitude_to = coordinates.lat;
|
const latitude_to = coordinates[0].lat;
|
||||||
const longitude_to = coordinates.lon;
|
const longitude_to = coordinates[0].lon;
|
||||||
|
|
||||||
const createUserSearching = new CreateUserSearchingService();
|
const createUserSearching = new CreateUserSearchingService();
|
||||||
|
|
||||||
@@ -49,7 +50,7 @@ searchRoutes.post('/', async (request, response) => {
|
|||||||
latitude_from,
|
latitude_from,
|
||||||
longitude_from,
|
longitude_from,
|
||||||
latitude_to,
|
latitude_to,
|
||||||
longitude_to
|
longitude_to,
|
||||||
});
|
});
|
||||||
|
|
||||||
return response.json({ message: 'Busca de usuário criada.' });
|
return response.json({ message: 'Busca de usuário criada.' });
|
||||||
@@ -57,27 +58,25 @@ searchRoutes.post('/', async (request, response) => {
|
|||||||
|
|
||||||
export default searchRoutes;
|
export default searchRoutes;
|
||||||
|
|
||||||
//TODO: Arrumar calculo da busca no raio
|
|
||||||
//TODO: Arrumar tipo das colunas latitude e longitude que está numeric no banco mas vem como string
|
//TODO: Arrumar tipo das colunas latitude e longitude que está numeric no banco mas vem como string
|
||||||
searchRoutes.post('/inraio', async (request, response) => {
|
searchRoutes.post('/inraio', async (request, response) => {
|
||||||
const { latitude, longitude } = request.body;
|
const { latitude, longitude } = request.body;
|
||||||
const usersSearchingRepository = getRepository(UserSearching);
|
const usersSearchingRepository = getRepository(UserSearching);
|
||||||
console.log(request.body)
|
|
||||||
const searches = await usersSearchingRepository.find();
|
const searches = await usersSearchingRepository.find();
|
||||||
var searchesFiltered;
|
var searchesFiltered;
|
||||||
for(let i in searches){
|
|
||||||
searchesFiltered = searches.filter(x =>{
|
let lat1:number = +latitude;
|
||||||
let distance = (6371 * Math.acos(
|
let lng1:number = +longitude;
|
||||||
Math.cos(Math.atan(latitude)) *
|
|
||||||
Math.cos(Math.atan(x.latitude_from)) *
|
searchesFiltered = searches.filter(x => {
|
||||||
Math.cos(Math.atan(longitude) - Math.atan(x.longitude_from)) +
|
let lat2:number = +x.latitude_from;
|
||||||
Math.sin(Math.atan(latitude)) *
|
let lng2:number = +x.longitude_from;
|
||||||
Math.sin(Math.atan(x.latitude_from))
|
let distance = CalculateDistanceBetweenCoords({lat1, lng1, lat2, lng2});
|
||||||
))
|
return distance <= 2.75;
|
||||||
// console.log(distance)
|
});
|
||||||
return distance <= 0.1
|
|
||||||
})
|
return response.json({
|
||||||
}
|
allRecords: searchesFiltered,
|
||||||
// console.log(searches)
|
center: { latitude, longitude },
|
||||||
return response.json({ allRecords: searchesFiltered, center:{latitude, longitude} });
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,48 +1,98 @@
|
|||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
import { getRepository } from 'typeorm';
|
import { getRepository } from 'typeorm';
|
||||||
|
import CalculateDistanceBetweenCoords from '../services/CalculateDistanceBetweenCoords';
|
||||||
|
|
||||||
const transportesRouter = Router();
|
const transportesRouter = Router();
|
||||||
|
|
||||||
transportesRouter.get('/', async (request, response) => {
|
transportesRouter.post('/', async (request, response) => {
|
||||||
const data = [{
|
const { coordinatesFrom, coordinatesTo } = request.body;
|
||||||
"motorista": "João",
|
console.log(coordinatesFrom, coordinatesTo);
|
||||||
"valor": "R$ 10,00",
|
const data = [
|
||||||
"lugares": "2",
|
{
|
||||||
"avaliacao": "4.5",
|
motorista: 'João',
|
||||||
},{
|
valor: 'R$ 150,00',
|
||||||
"motorista": "Ricardo",
|
lugares: '2',
|
||||||
"valor": "R$ 13,00",
|
avaliacao: '4.5',
|
||||||
"lugares": "5",
|
bairros_atendidos: [{lat:-22.873432, lgn:-47.142274}],
|
||||||
"avaliacao": "4.0",
|
destinos: [{lat:-22.833645, lgn:-47.048905}],
|
||||||
},{
|
},
|
||||||
"motorista": "Luiz",
|
{
|
||||||
"valor": "R$ 12,00",
|
motorista: 'Ricardo',
|
||||||
"lugares": "1",
|
valor: 'R$ 180,00',
|
||||||
"avaliacao": "4.3",
|
lugares: '5',
|
||||||
},{
|
avaliacao: '4.0',
|
||||||
"motorista": "Marcos",
|
bairros_atendidos: [{lat:-22.873432, lgn:-47.142274}],
|
||||||
"valor": "R$ 15,00",
|
destinos: [{lat:-22.833645, lgn:-47.048905}],
|
||||||
"lugares": "6",
|
},
|
||||||
"avaliacao": "4.9",
|
{
|
||||||
},{
|
motorista: 'Luiz',
|
||||||
"motorista": "Orandi",
|
valor: 'R$ 200,00',
|
||||||
"valor": "R$ 20,00",
|
lugares: '1',
|
||||||
"lugares": "8",
|
avaliacao: '4.3',
|
||||||
"avaliacao": "5.0",
|
bairros_atendidos: [{lat:-22.873432, lgn:-47.142274}],
|
||||||
},{
|
destinos: [{lat:-22.833645, lgn:-47.048905}],
|
||||||
"motorista": "Pedro",
|
},
|
||||||
"valor": "R$ 18,00",
|
{
|
||||||
"lugares": "4",
|
motorista: 'Marcos',
|
||||||
"avaliacao": "4.1",
|
valor: 'R$ 199,00',
|
||||||
},{
|
lugares: '6',
|
||||||
"motorista": "Pericles",
|
avaliacao: '4.9',
|
||||||
"valor": "R$ 22,00",
|
bairros_atendidos: [{lat:-22.873432, lgn:-47.142274}],
|
||||||
"lugares": "19",
|
destinos: [{lat:-22.833645, lgn:-47.048905}],
|
||||||
"avaliacao": "4.5",
|
},
|
||||||
},
|
{
|
||||||
]
|
motorista: 'Orandi',
|
||||||
|
valor: 'R$ 210,00',
|
||||||
|
lugares: '8',
|
||||||
|
avaliacao: '5.0',
|
||||||
|
bairros_atendidos: [{lat:-22.873432, lgn:-47.142274}],
|
||||||
|
destinos: [{lat:-22.833645, lgn:-47.048905}],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
motorista: 'Pedro',
|
||||||
|
valor: 'R$ 189,00',
|
||||||
|
lugares: '4',
|
||||||
|
avaliacao: '4.1',
|
||||||
|
bairros_atendidos: [{lat:-22.873432, lgn:-47.142274}],
|
||||||
|
destinos: [{lat:-22.833645, lgn:-47.048905}],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
motorista: 'Pericles',
|
||||||
|
valor: 'R$ 220,00',
|
||||||
|
lugares: '19',
|
||||||
|
avaliacao: '4.5',
|
||||||
|
bairros_atendidos: [{lat:-23.873432, lgn:-47.142274}],
|
||||||
|
destinos: [{lat:-22.833645, lgn:-47.048905}],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
return response.json( data );
|
let lat_from:number = +coordinatesFrom.lat;
|
||||||
|
let lng_from:number = +coordinatesFrom.lng;
|
||||||
|
let lat_to:number = +coordinatesTo.lat;
|
||||||
|
let lng_to:number = +coordinatesTo.lng;
|
||||||
|
|
||||||
|
let transportsFiltered = data.filter(x => {
|
||||||
|
var distance = 0;
|
||||||
|
var distance2 = 0;
|
||||||
|
for (const i of x.bairros_atendidos) {
|
||||||
|
let lat2:number = +i.lat;
|
||||||
|
let lng2:number = +i.lgn;
|
||||||
|
distance = CalculateDistanceBetweenCoords({lat1:lat_from, lng1:lng_from, lat2, lng2});
|
||||||
|
if (distance <= 10) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const j of x.destinos) {
|
||||||
|
let lat2:number = +j.lat;
|
||||||
|
let lng2:number = +j.lgn;
|
||||||
|
distance2 = CalculateDistanceBetweenCoords({lat1:lat_to, lng1:lng_to, lat2, lng2});
|
||||||
|
if (distance2 <= 10) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (distance <= 10 && distance2 <= 10);
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(transportsFiltered)
|
||||||
|
return response.json(transportsFiltered);
|
||||||
});
|
});
|
||||||
|
|
||||||
export default transportesRouter;
|
export default transportesRouter;
|
||||||
|
|||||||
25
src/services/CalculateDistanceBetweenCoords.ts
Normal file
25
src/services/CalculateDistanceBetweenCoords.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
interface Request {
|
||||||
|
lat1: number,
|
||||||
|
lng1: number,
|
||||||
|
lat2: number,
|
||||||
|
lng2: number
|
||||||
|
}
|
||||||
|
|
||||||
|
function convertToRad(lat: number, lng: number) {
|
||||||
|
let latRad = lat * (Math.PI / 180);
|
||||||
|
let lngRad = lng * (Math.PI / 180);
|
||||||
|
|
||||||
|
return { latRad, lngRad };
|
||||||
|
}
|
||||||
|
|
||||||
|
function CalculateDistanceBetweenCoords({ lat1, lng1, lat2, lng2 }: Request){
|
||||||
|
let { latRad, lngRad } = convertToRad(lat1, lng1);
|
||||||
|
let { latRad: lat2Rad, lngRad: lng2Rad } = convertToRad(lat2, lng2);
|
||||||
|
|
||||||
|
let d = Math.acos(Math.sin(latRad) * Math.sin(lat2Rad) + Math.cos(latRad) * Math.cos(lat2Rad) * Math.cos(lngRad - lng2Rad)) * 6371;
|
||||||
|
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export default CalculateDistanceBetweenCoords;
|
||||||
@@ -22,5 +22,23 @@ class GetCoordinatesByAddress{
|
|||||||
return response.data;
|
return response.data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// class GetCoordinatesByAddress{
|
||||||
|
// public async execute({ address_to }: Request): Promise<any> {
|
||||||
|
// // let endereco = address_to.replace(/[^a-z0-9+ ]/gi,'')
|
||||||
|
// let endereco = address_to.replace(/[^a-z0-9+áàâãéèêíïóôõöúçñ ]/gi,'')
|
||||||
|
// // console.log(endereco)
|
||||||
|
// endereco = endereco.replace(/ /gi,'+')
|
||||||
|
// endereco = endereco.replace(/\+\+/g, '+')
|
||||||
|
// // console.log(endereco)
|
||||||
|
// const querystring = require('querystring');
|
||||||
|
// const response = await axios.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${querystring.stringify(endereco)}key=`)
|
||||||
|
// console.log(response.data)
|
||||||
|
// if (!response.data || !response.data.length) {
|
||||||
|
// throw new AppError('Não foi possível encontrar coordenadas para o endereço informado!', 400);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return response.data;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
export default GetCoordinatesByAddress;
|
export default GetCoordinatesByAddress;
|
||||||
|
|||||||
Reference in New Issue
Block a user