wip
This commit is contained in:
@@ -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;
|
||||
Reference in New Issue
Block a user