wip
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import {MigrationInterface, QueryRunner, Table} from "typeorm";
|
||||
|
||||
export class CreateUsersSearching1652672860580 implements MigrationInterface {
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.createTable(
|
||||
new Table({
|
||||
name: 'users_searching',
|
||||
columns: [
|
||||
{
|
||||
name: 'id_search',
|
||||
type: 'integer',
|
||||
isPrimary: true,
|
||||
isGenerated: true,
|
||||
generationStrategy: 'increment',
|
||||
},
|
||||
{
|
||||
name: 'user_id',
|
||||
type: 'uuid'
|
||||
},
|
||||
{
|
||||
name: 'latitude_from',
|
||||
type: 'numeric',
|
||||
},
|
||||
{
|
||||
name: 'longitude_from',
|
||||
type: 'numeric',
|
||||
},
|
||||
{
|
||||
name: 'latitude_to',
|
||||
type: 'numeric',
|
||||
},
|
||||
{
|
||||
name: 'longitude_to',
|
||||
type: 'numeric',
|
||||
},
|
||||
{
|
||||
name: 'created_at',
|
||||
type: 'timestamp',
|
||||
default: 'now()',
|
||||
}
|
||||
],
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.dropTable('users_searching');
|
||||
}
|
||||
|
||||
}
|
||||
37
src/models/UsersSearching.ts
Normal file
37
src/models/UsersSearching.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import {
|
||||
Entity,
|
||||
Column,
|
||||
PrimaryGeneratedColumn,
|
||||
CreateDateColumn,
|
||||
ManyToOne,
|
||||
JoinColumn
|
||||
} from 'typeorm';
|
||||
|
||||
import User from './User';
|
||||
|
||||
@Entity('users_searching')
|
||||
class UserSearching {
|
||||
@PrimaryGeneratedColumn('increment')
|
||||
id_search: 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;
|
||||
@@ -1,11 +1,15 @@
|
||||
import { Router } from 'express';
|
||||
import searchRoutes from './search.routes';
|
||||
|
||||
import sessionsRouter from './sessions.routes';
|
||||
import transportesRouter from './transportes.routes';
|
||||
import usersRouter from './users.routes';
|
||||
|
||||
const routes = Router();
|
||||
|
||||
routes.use('/users', usersRouter);
|
||||
routes.use('/sessions', sessionsRouter);
|
||||
routes.use('/search', searchRoutes);
|
||||
routes.use('/transportes', transportesRouter);
|
||||
|
||||
export default routes;
|
||||
|
||||
83
src/routes/search.routes.ts
Normal file
83
src/routes/search.routes.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { Router } from 'express';
|
||||
import { getRepository } from 'typeorm';
|
||||
import UserSearching from '../models/UsersSearching';
|
||||
import CreateUserSearchingService from '../services/CreateUserSearchingService';
|
||||
import GetCoordinatesByAddress from '../services/GetCoordinatesByAddress';
|
||||
|
||||
const searchRoutes = Router();
|
||||
|
||||
interface userWithoutSensitiveInfo {
|
||||
id_user: string;
|
||||
name: string;
|
||||
email: string;
|
||||
avatar_image: string;
|
||||
}
|
||||
|
||||
searchRoutes.get('/list', async (request, response) => {
|
||||
const usersSearchingRepository = getRepository(UserSearching);
|
||||
|
||||
const searches = await usersSearchingRepository.find();
|
||||
|
||||
// let usersWithoutSensitiveInfo: userWithoutSensitiveInfo[] = [];
|
||||
|
||||
// searches.map(user => {
|
||||
// usersWithoutSensitiveInfo.push({
|
||||
// id_user: user.id_user,
|
||||
// name: user.name,
|
||||
// email: user.email,
|
||||
// avatar_image: user.avatar_image,
|
||||
// });
|
||||
// });
|
||||
|
||||
return response.json({ data: searches });
|
||||
});
|
||||
|
||||
searchRoutes.post('/', async (request, response) => {
|
||||
const { id_user, latitude_from, longitude_from, address_to } = request.body;
|
||||
|
||||
const getCoordinates = new GetCoordinatesByAddress();
|
||||
|
||||
const coordinates = await getCoordinates.execute({address_to})
|
||||
|
||||
const latitude_to = coordinates.lat;
|
||||
const longitude_to = coordinates.lon;
|
||||
|
||||
const createUserSearching = new CreateUserSearchingService();
|
||||
|
||||
const search = await createUserSearching.execute({
|
||||
id_user,
|
||||
latitude_from,
|
||||
longitude_from,
|
||||
latitude_to,
|
||||
longitude_to
|
||||
});
|
||||
|
||||
return response.json({ message: 'Busca de usuário criada.' });
|
||||
});
|
||||
|
||||
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} });
|
||||
});
|
||||
48
src/routes/transportes.routes.ts
Normal file
48
src/routes/transportes.routes.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Router } from 'express';
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
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",
|
||||
},
|
||||
]
|
||||
|
||||
return response.json( data );
|
||||
});
|
||||
|
||||
export default transportesRouter;
|
||||
38
src/services/CreateUserSearchingService.ts
Normal file
38
src/services/CreateUserSearchingService.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { getRepository } from 'typeorm';
|
||||
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
import User from '../models/User';
|
||||
import UserSearching from '../models/UsersSearching';
|
||||
|
||||
interface Request {
|
||||
id_user: string;
|
||||
latitude_from: string;
|
||||
longitude_from: string;
|
||||
latitude_to: string;
|
||||
longitude_to: string;
|
||||
}
|
||||
|
||||
class CreateUserSearchingService {
|
||||
public async execute({ id_user, latitude_from, longitude_from, latitude_to, longitude_to }: Request): Promise<UserSearching> {
|
||||
const usersRepository = getRepository(User);
|
||||
const usersSearchingRepository = getRepository(UserSearching);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id_user },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError('Usuário inválido!', 200);
|
||||
}
|
||||
|
||||
const search = usersSearchingRepository.create({
|
||||
user, latitude_from, longitude_from, latitude_to, longitude_to
|
||||
});
|
||||
await usersSearchingRepository.save(search);
|
||||
|
||||
return search;
|
||||
}
|
||||
}
|
||||
|
||||
export default CreateUserSearchingService;
|
||||
20
src/services/GetAddressByCoordinates.ts
Normal file
20
src/services/GetAddressByCoordinates.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import axios from 'axios';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
interface Request {
|
||||
latitude: string;
|
||||
longitude: string;
|
||||
}
|
||||
|
||||
class GetAddressByCoordinates {
|
||||
public async execute({ latitude, longitude }: Request): Promise<JSON> {
|
||||
const response = await axios.get(`https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=${latitude}&lon=${longitude}`)
|
||||
|
||||
if (!response.data) {
|
||||
throw new AppError('Não foi possível encontrar o endereço para a coordenada informada!', 200);
|
||||
}
|
||||
return response.data;
|
||||
}
|
||||
}
|
||||
|
||||
export default GetAddressByCoordinates;
|
||||
26
src/services/GetCoordinatesByAddress.ts
Normal file
26
src/services/GetCoordinatesByAddress.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import axios from 'axios';
|
||||
import AppError from '../errors/AppError';
|
||||
|
||||
interface Request {
|
||||
address_to: string;
|
||||
}
|
||||
|
||||
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 response = await axios.get(`https://nominatim.openstreetmap.org/?addressdetails=1&q=${endereco}&format=json&limit=1`)
|
||||
if (!response.data || !response.data.length) {
|
||||
throw new AppError('Não foi possível encontrar coordenadas para o endereço informado!', 400);
|
||||
}
|
||||
console.log(response.data)
|
||||
return response.data;
|
||||
}
|
||||
}
|
||||
|
||||
export default GetCoordinatesByAddress;
|
||||
Reference in New Issue
Block a user