-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
111 lines (90 loc) · 2.99 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// eslint-disable-next-line
//@ts-nocheck
import { createServer, Model, Response } from "miragejs";
import { generateHotels } from "./lib/utils";
const hotels = generateHotels(200);
export function makeServer({ environment = "development" } = {}) {
const server = createServer({
environment,
models: {
hotel: Model,
reservation: Model,
},
seeds(server) {
for (const hotel of hotels) {
server.create("hotel", hotel);
}
},
routes() {
this.namespace = "api";
this.get("/hotels", (schema, request) => {
let hotels = schema.hotels.all().models;
const destination = request.queryParams.destination;
const rooms = request.queryParams.rooms;
const amenities = request.queryParams.amenities;
const orderPrice = request.queryParams.orderPrice;
const orderRating = request.queryParams.orderRating;
if (destination) {
hotels = hotels.filter(
(hotel) => hotel.city.toLowerCase() === destination.toLowerCase()
);
}
if (rooms) {
hotels = hotels.filter(
(hotel) => hotel.availableRooms >= parseInt(rooms)
);
}
if (orderPrice) {
hotels = hotels.sort((a, b) => {
if (orderPrice === "asc") {
return Number(a.price) - Number(b.price);
}
return Number(b.price) - Number(a.price);
});
}
if (orderRating) {
hotels = hotels.sort((a, b) => {
if (orderRating === "asc") {
return Number(a.rating) - Number(b.rating);
}
return Number(b.rating) - Number(a.rating);
});
}
//MIRAGE aceita apenas o ultimo valor quando a query param é passada como array (amenities=wifi&amenities=piscina...)
const amenitiesAsArray = amenities.split(",");
if (amenitiesAsArray.length > 0) {
hotels = hotels.filter((hotel) =>
amenitiesAsArray.every((amenity) =>
hotel.amenities.some((hotelAmenity) =>
hotelAmenity.toLowerCase().includes(amenity.toLowerCase())
)
)
);
}
return hotels;
});
this.get("/hotels/:id", (schema, request) => {
const id = request.params.id;
const hotel = schema.findBy("hotel", { id });
if (hotel) {
return hotel;
} else {
return new Response(404, {}, { error: "Hotel não encontrado" });
}
});
//cria uma reserva
this.post("/reservation", (schema, request) => {
const data = JSON.parse(request.requestBody);
return schema.reservations.create({
...data,
id: Date.now().toString(),
status: "pending",
});
});
this.passthrough("http://localhost:5173/**");
this.passthrough("https://picsum.photos/**");
this.passthrough("https://brasilapi.com.br/**");
},
});
return server;
}