-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
120 lines (117 loc) · 3.08 KB
/
server.js
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
112
113
114
115
116
117
118
119
120
import random from "lodash/random";
import { Server, Model, Factory, RestSerializer, belongsTo, hasMany, association } from "miragejs"
import faker from "faker";
export const mockServer = ({ environment = "development" }) => new Server({
environment,
serializers: {
order: RestSerializer.extend({
embed: true,
include: ['client']
}),
application: RestSerializer,
},
models: {
order: Model.extend({
client: belongsTo("client"),
}),
client: Model.extend({
orders: hasMany("order"),
}),
},
factories: {
order: Factory.extend({
endDate() {
return faker.date.past(0);
},
startDate() {
return faker.date.past(0, this.endDate);
},
description() {
return faker.lorem.sentence();
},
car() {
const cars = [
{
"Alfa Romeo": [
"145", "147", "Giulia", "Stelvio",
],
},
{
"Audi": [
"A1", "A2", "A3", "A4", "A5", "A6", "A7", "TT", "Q5",
],
},
{
"Daewoo": [
"Evanda", "Matiz", "Lanos", "Kalos", "Tico",
],
},
{
"Honda": [
"Accord", "Civic", "Legend", "Prelude", "Stream",
],
},
{
"Jaguar": [
"F-Type", "S-Type", "XF", "XJ", "XK",
]
},
{
"Mercedes-Benz": [
"CLA", "GLC", "Klasa C", "Sprinter", "W123", "W201",
],
},
];
const index = Math.floor(Math.random() * cars.length);
const manufacturer = Object.keys(cars[index])[0];
const model = cars[index][manufacturer][random(cars[index][manufacturer].length - 1)];
const minYear = 2002;
const maxYear = 2019;
const year = Math.floor(Math.random() * (maxYear - minYear + 1)) + minYear;
return {
manufacturer,
model,
year,
}
},
completed: () => !!Math.floor(Math.random() * 2),
}),
client: Factory.extend({
name: () => faker.name.findName(),
phone: () => faker.phone.phoneNumber(),
email: () => faker.internet.email(),
regular: () => !!Math.floor(Math.random() * 2),
}),
},
seeds(server) {
server.createList('client', 15).forEach((client) => {
server.createList('order', 1, { client });
});
},
routes() {
this.passthrough()
this.namespace = "/api"
this.get('/orders');
this.get('/clients');
this.get('/cars', { cars: {
"Alfa Romeo": [
"145", "147", "Giulia", "Stelvio",
],
"Audi": [
"A1", "A2", "A3", "A4", "A5", "A6", "A7", "TT", "Q5",
],
"Daewoo": [
"Evanda", "Matiz", "Lanos", "Kalos", "Tico",
],
"Honda": [
"Accord", "Civic", "Legend", "Prelude", "Stream",
],
"Jaguar": [
"F-Type", "S-Type", "XF", "XJ", "XK",
],
"Mercedes-Benz": [
"CLA", "GLC", "Klasa C", "Sprinter", "W123", "W201",
],
}});
},
});