Skip to content

Commit

Permalink
refactor: modifica estrategia de listagem por params
Browse files Browse the repository at this point in the history
agora a responsabilidade de saber quais parametros devem ser aplicados na filtragem esta com o service
  • Loading branch information
ARJOM committed Oct 6, 2021
1 parent 9e5b6f6 commit 2c3053b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
7 changes: 7 additions & 0 deletions src/interfaces/RealEstateQueryInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface RealEstateInterface{
cidade: string
menorValor: number
maiorValor: number
}

export default RealEstateInterface;
21 changes: 5 additions & 16 deletions src/routes/realEstateRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Request, Response, Router } from 'express';
import RealEstateService from '../service/realEstateService';
import RealEstateInterface from '../interfaces/RealEstateInterface';
import verifyJWT from '../utils/verifyAuth';
import RealEstateQueryInterface from '../interfaces/RealEstateQueryInterface';

const routes = Router();

Expand All @@ -15,22 +16,10 @@ routes.post('/real-estate', verifyJWT, (req: Request, res: Response) => {
})

routes.get('/real-estate', (req: Request, res: Response) => {
const cidade = req.query.cidade as string;
const menorValor = req.query.menorValor as unknown as number;
const maiorValor = req.query.maiorValor as unknown as number;
if (cidade !== undefined && cidade !== null){
RealEstateService.findByCity(cidade)
.then(result => res.json(result))
.catch(err => res.status(500).json(err));
} else if (menorValor !== undefined && maiorValor !== undefined){
RealEstateService.orderByPrice(menorValor, maiorValor)
.then(result => res.json(result))
.catch(err => res.status(500).json(err));
} else {
RealEstateService.list()
.then(result => res.json(result))
.catch(err => res.status(500).json(err));
}
const queryParams = req.query as unknown as RealEstateQueryInterface;
RealEstateService.list(queryParams)
.then(result => res.json(result))
.catch(err => res.status(500).json(err));
})

routes.get('/real-estate/user', verifyJWT, (req: Request, res: Response) => {
Expand Down
56 changes: 36 additions & 20 deletions src/service/realEstateService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Op } from 'sequelize';
import RealEstateInterface from '../interfaces/RealEstateInterface';
import RealEstateQueryInterface from '../interfaces/RealEstateQueryInterface';
import RealEstate from '../model/RealEstate';
import User from '../model/User';

Expand All @@ -10,8 +11,21 @@ class RealEstateService{
return await newRealEstate.save();
}

static async list() {
return await RealEstate.findAll({include: [User], where: {isActive: true}});
static async list(params: RealEstateQueryInterface) {
const { cidade, menorValor, maiorValor } = params;
let result = await RealEstate.findAll({include: [User], where: {isActive: true}});

if (cidade){
result = result.filter(realEstate => realEstate.cidade == cidade);
}

if (menorValor && maiorValor){
result = result
.filter(realEstate => realEstate.preco >= menorValor && realEstate.preco <= maiorValor)
.sort((firstRealEstate, secondRealEstate) => secondRealEstate.preco - firstRealEstate.preco) // Ordena pela diferença dos preços deixando o maior na frente
}

return result
}

static async getById(id: number){
Expand All @@ -26,28 +40,30 @@ class RealEstateService{
return await RealEstate.update({ isActive: false }, {where: {id}});
}

static async findByCity(cidade: string){
return await RealEstate.findAll({include: [User], where: {cidade, isActive: true}})
}

static async orderByPrice(minValue: number, maxValue: number){
return await RealEstate.findAll({
include: [User],
where: {
preco: {
[Op.gte]: minValue,
[Op.lte]: maxValue
},
isActive: true
},
order: ['preco']
})
}

static async findByUser(userId: number){
return await RealEstate.findAll({include: [User], where: {userId, isActive: true}})
}

// As funções abaixo não são necessárias desde que eram utilizadas para lidar com query parameters

// static async findByCity(cidade: string){
// return await RealEstate.findAll({include: [User], where: {cidade, isActive: true}})
// }

// static async orderByPrice(minValue: number, maxValue: number){
// return await RealEstate.findAll({
// include: [User],
// where: {
// preco: {
// [Op.gte]: minValue,
// [Op.lte]: maxValue
// },
// isActive: true
// },
// order: ['preco']
// })
// }

}

export default RealEstateService;

0 comments on commit 2c3053b

Please sign in to comment.