wip
This commit is contained in:
@@ -3,4 +3,6 @@
|
||||
`npm install`
|
||||
`npm run typeorm migration:run`
|
||||
|
||||
`yarn dev:server`
|
||||
`yarn dev:server`
|
||||
|
||||
`yarn typeorm migration:create -n CreateUser`
|
||||
@@ -9,6 +9,7 @@
|
||||
"@types/express": "^4.17.11",
|
||||
"@types/jsonwebtoken": "^8.5.1",
|
||||
"@types/uuid": "^8.3.0",
|
||||
"axios": "^0.27.2",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.17.1",
|
||||
|
||||
@@ -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;
|
||||
51
yarn.lock
51
yarn.lock
@@ -164,6 +164,19 @@ array-flatten@1.1.1:
|
||||
resolved "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz"
|
||||
integrity sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=
|
||||
|
||||
asynckit@^0.4.0:
|
||||
version "0.4.0"
|
||||
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
|
||||
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==
|
||||
|
||||
axios@^0.27.2:
|
||||
version "0.27.2"
|
||||
resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
|
||||
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
|
||||
dependencies:
|
||||
follow-redirects "^1.14.9"
|
||||
form-data "^4.0.0"
|
||||
|
||||
balanced-match@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz"
|
||||
@@ -312,6 +325,13 @@ color-name@~1.1.4:
|
||||
resolved "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz"
|
||||
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==
|
||||
|
||||
combined-stream@^1.0.8:
|
||||
version "1.0.8"
|
||||
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
|
||||
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
|
||||
dependencies:
|
||||
delayed-stream "~1.0.0"
|
||||
|
||||
concat-map@0.0.1:
|
||||
version "0.0.1"
|
||||
resolved "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz"
|
||||
@@ -386,6 +406,11 @@ decamelize@^1.1.2:
|
||||
resolved "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz"
|
||||
integrity sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=
|
||||
|
||||
delayed-stream@~1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
|
||||
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=
|
||||
|
||||
depd@~1.1.2:
|
||||
version "1.1.2"
|
||||
resolved "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz"
|
||||
@@ -526,6 +551,20 @@ find-up@^1.0.0:
|
||||
path-exists "^2.0.0"
|
||||
pinkie-promise "^2.0.0"
|
||||
|
||||
follow-redirects@^1.14.9:
|
||||
version "1.15.0"
|
||||
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.0.tgz#06441868281c86d0dda4ad8bdaead2d02dca89d4"
|
||||
integrity sha512-aExlJShTV4qOUOL7yF1U5tvLCB0xQuudbf6toyYA0E/acBNw71mvjFTnLaRp50aQaYocMR0a/RMMBIHeZnGyjQ==
|
||||
|
||||
form-data@^4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
|
||||
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
|
||||
dependencies:
|
||||
asynckit "^0.4.0"
|
||||
combined-stream "^1.0.8"
|
||||
mime-types "^2.1.12"
|
||||
|
||||
forwarded@~0.1.2:
|
||||
version "0.1.2"
|
||||
resolved "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz"
|
||||
@@ -846,6 +885,18 @@ mime-db@1.47.0:
|
||||
resolved "https://registry.npmjs.org/mime-db/-/mime-db-1.47.0.tgz"
|
||||
integrity sha512-QBmA/G2y+IfeS4oktet3qRZ+P5kPhCKRXxXnQEudYqUaEioAU1/Lq2us3D/t1Jfo4hE9REQPrbB7K5sOczJVIw==
|
||||
|
||||
mime-db@1.52.0:
|
||||
version "1.52.0"
|
||||
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
|
||||
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==
|
||||
|
||||
mime-types@^2.1.12:
|
||||
version "2.1.35"
|
||||
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
|
||||
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
|
||||
dependencies:
|
||||
mime-db "1.52.0"
|
||||
|
||||
mime-types@~2.1.24:
|
||||
version "2.1.30"
|
||||
resolved "https://registry.npmjs.org/mime-types/-/mime-types-2.1.30.tgz"
|
||||
|
||||
Reference in New Issue
Block a user