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',
|
||||
type: 'varchar',
|
||||
length: '255',
|
||||
isUnique: true,
|
||||
},
|
||||
{
|
||||
name: 'phone_number',
|
||||
type: 'varchar',
|
||||
length: '14',
|
||||
isUnique: true,
|
||||
isNullable: true,
|
||||
},
|
||||
{
|
||||
name: 'birth_date',
|
||||
@@ -40,6 +49,13 @@ export class CreateUsers1617210132141 implements MigrationInterface {
|
||||
type: 'varchar',
|
||||
isNullable: true
|
||||
},
|
||||
{
|
||||
name: 'star_rating',
|
||||
type: 'numeric',
|
||||
precision: 3,
|
||||
scale: 2,
|
||||
isNullable: true
|
||||
},
|
||||
{
|
||||
name: 'created_at',
|
||||
type: 'timestamp',
|
||||
|
||||
@@ -34,6 +34,10 @@ export class CreateUsersSearching1652672860580 implements MigrationInterface {
|
||||
name: 'longitude_to',
|
||||
type: 'numeric',
|
||||
},
|
||||
{
|
||||
name: 'address_to',
|
||||
type: 'varchar',
|
||||
},
|
||||
{
|
||||
name: 'created_at',
|
||||
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()
|
||||
email: string;
|
||||
|
||||
@Column()
|
||||
phone_number: string;
|
||||
|
||||
@Column()
|
||||
birth_date: Date;
|
||||
@@ -28,6 +31,9 @@ class User {
|
||||
|
||||
@Column()
|
||||
bio: string;
|
||||
|
||||
@Column()
|
||||
star_rating: number;
|
||||
|
||||
@CreateDateColumn()
|
||||
created_at: Date;
|
||||
|
||||
@@ -29,6 +29,9 @@ class UserSearching {
|
||||
|
||||
@Column()
|
||||
longitude_to: number;
|
||||
|
||||
@Column()
|
||||
address_to: string;
|
||||
|
||||
@CreateDateColumn()
|
||||
created_at: Date;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Router } from 'express';
|
||||
import { getRepository } from 'typeorm';
|
||||
import UserSearching from '../models/UsersSearching';
|
||||
import CalculateDistanceBetweenCoords from '../services/CalculateDistanceBetweenCoords';
|
||||
import CreateUserSearchingService from '../services/CreateUserSearchingService';
|
||||
import GetCoordinatesByAddress from '../services/GetCoordinatesByAddress';
|
||||
|
||||
@@ -37,10 +38,10 @@ searchRoutes.post('/', async (request, response) => {
|
||||
|
||||
const getCoordinates = new GetCoordinatesByAddress();
|
||||
|
||||
const coordinates = await getCoordinates.execute({address_to})
|
||||
const coordinates = await getCoordinates.execute({ address_to });
|
||||
|
||||
const latitude_to = coordinates.lat;
|
||||
const longitude_to = coordinates.lon;
|
||||
const latitude_to = coordinates[0].lat;
|
||||
const longitude_to = coordinates[0].lon;
|
||||
|
||||
const createUserSearching = new CreateUserSearchingService();
|
||||
|
||||
@@ -49,7 +50,7 @@ searchRoutes.post('/', async (request, response) => {
|
||||
latitude_from,
|
||||
longitude_from,
|
||||
latitude_to,
|
||||
longitude_to
|
||||
longitude_to,
|
||||
});
|
||||
|
||||
return response.json({ message: 'Busca de usuário criada.' });
|
||||
@@ -57,27 +58,25 @@ searchRoutes.post('/', async (request, response) => {
|
||||
|
||||
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
|
||||
searchRoutes.post('/inraio', async (request, response) => {
|
||||
const { latitude, longitude } = request.body;
|
||||
const usersSearchingRepository = getRepository(UserSearching);
|
||||
console.log(request.body)
|
||||
const searches = await usersSearchingRepository.find();
|
||||
var searchesFiltered;
|
||||
for(let i in searches){
|
||||
searchesFiltered = searches.filter(x =>{
|
||||
let distance = (6371 * Math.acos(
|
||||
Math.cos(Math.atan(latitude)) *
|
||||
Math.cos(Math.atan(x.latitude_from)) *
|
||||
Math.cos(Math.atan(longitude) - Math.atan(x.longitude_from)) +
|
||||
Math.sin(Math.atan(latitude)) *
|
||||
Math.sin(Math.atan(x.latitude_from))
|
||||
))
|
||||
// console.log(distance)
|
||||
return distance <= 0.1
|
||||
})
|
||||
}
|
||||
// console.log(searches)
|
||||
return response.json({ allRecords: searchesFiltered, center:{latitude, longitude} });
|
||||
});
|
||||
|
||||
let lat1:number = +latitude;
|
||||
let lng1:number = +longitude;
|
||||
|
||||
searchesFiltered = searches.filter(x => {
|
||||
let lat2:number = +x.latitude_from;
|
||||
let lng2:number = +x.longitude_from;
|
||||
let distance = CalculateDistanceBetweenCoords({lat1, lng1, lat2, lng2});
|
||||
return distance <= 2.75;
|
||||
});
|
||||
|
||||
return response.json({
|
||||
allRecords: searchesFiltered,
|
||||
center: { latitude, longitude },
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,48 +1,98 @@
|
||||
import { Router } from 'express';
|
||||
import { getRepository } from 'typeorm';
|
||||
import CalculateDistanceBetweenCoords from '../services/CalculateDistanceBetweenCoords';
|
||||
|
||||
const transportesRouter = Router();
|
||||
|
||||
transportesRouter.get('/', async (request, response) => {
|
||||
const data = [{
|
||||
"motorista": "João",
|
||||
"valor": "R$ 10,00",
|
||||
"lugares": "2",
|
||||
"avaliacao": "4.5",
|
||||
},{
|
||||
"motorista": "Ricardo",
|
||||
"valor": "R$ 13,00",
|
||||
"lugares": "5",
|
||||
"avaliacao": "4.0",
|
||||
},{
|
||||
"motorista": "Luiz",
|
||||
"valor": "R$ 12,00",
|
||||
"lugares": "1",
|
||||
"avaliacao": "4.3",
|
||||
},{
|
||||
"motorista": "Marcos",
|
||||
"valor": "R$ 15,00",
|
||||
"lugares": "6",
|
||||
"avaliacao": "4.9",
|
||||
},{
|
||||
"motorista": "Orandi",
|
||||
"valor": "R$ 20,00",
|
||||
"lugares": "8",
|
||||
"avaliacao": "5.0",
|
||||
},{
|
||||
"motorista": "Pedro",
|
||||
"valor": "R$ 18,00",
|
||||
"lugares": "4",
|
||||
"avaliacao": "4.1",
|
||||
},{
|
||||
"motorista": "Pericles",
|
||||
"valor": "R$ 22,00",
|
||||
"lugares": "19",
|
||||
"avaliacao": "4.5",
|
||||
},
|
||||
]
|
||||
transportesRouter.post('/', async (request, response) => {
|
||||
const { coordinatesFrom, coordinatesTo } = request.body;
|
||||
console.log(coordinatesFrom, coordinatesTo);
|
||||
const data = [
|
||||
{
|
||||
motorista: 'João',
|
||||
valor: 'R$ 150,00',
|
||||
lugares: '2',
|
||||
avaliacao: '4.5',
|
||||
bairros_atendidos: [{lat:-22.873432, lgn:-47.142274}],
|
||||
destinos: [{lat:-22.833645, lgn:-47.048905}],
|
||||
},
|
||||
{
|
||||
motorista: 'Ricardo',
|
||||
valor: 'R$ 180,00',
|
||||
lugares: '5',
|
||||
avaliacao: '4.0',
|
||||
bairros_atendidos: [{lat:-22.873432, lgn:-47.142274}],
|
||||
destinos: [{lat:-22.833645, lgn:-47.048905}],
|
||||
},
|
||||
{
|
||||
motorista: 'Luiz',
|
||||
valor: 'R$ 200,00',
|
||||
lugares: '1',
|
||||
avaliacao: '4.3',
|
||||
bairros_atendidos: [{lat:-22.873432, lgn:-47.142274}],
|
||||
destinos: [{lat:-22.833645, lgn:-47.048905}],
|
||||
},
|
||||
{
|
||||
motorista: 'Marcos',
|
||||
valor: 'R$ 199,00',
|
||||
lugares: '6',
|
||||
avaliacao: '4.9',
|
||||
bairros_atendidos: [{lat:-22.873432, lgn:-47.142274}],
|
||||
destinos: [{lat:-22.833645, lgn:-47.048905}],
|
||||
},
|
||||
{
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
// 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;
|
||||
|
||||
Reference in New Issue
Block a user