server.ts
2.38 KB
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
import { createServer, Model } from "miragejs";
import { ModelDefinition } from "miragejs/-types";
interface Item {
id: string;
is_folder: boolean;
name: string;
mime_type: string | null;
path: string | null;
parent: string | null;
user_id: string;
size: number;
is_deleted: boolean;
created_timestamp: string;
updated_timestamp: string;
status: boolean;
}
interface SharedItem {
id: string;
file_id: string;
until: string;
password: string;
}
interface User {
id: string;
user_id: string;
name: string;
password: string;
total_size: number;
current_size: number;
}
const ItemModel: ModelDefinition<Item> = Model;
const SharedItemModel: ModelDefinition<SharedItem> = Model;
const UserModel: ModelDefinition<User> = Model;
createServer({
models: {
item: ItemModel,
shared_item: SharedItemModel,
user: UserModel,
},
factories: {},
routes() {
this.namespace = "api";
this.get("/items/:item_id/children", (schema, request) => {
const directory = schema.find("item", request.params.item_id);
if (!directory || !directory.is_folder) {
return {
status: 404,
message: "Not Found",
};
}
const list = schema.where("item", { parent: directory.id }).models;
return {
status: 200,
data: {
...directory.attrs,
count: list.length,
list,
},
};
});
},
seeds(server) {
const user = server.create("user", {
id: "1",
user_id: "test",
name: "테스트",
password: "password",
total_size: 1024 * 1024 * 1024 * 5, // 5GB
current_size: 1024 * 1024 * 1024 * 1, // 1GB
});
const rootDir = server.create("item", {
id: "1",
is_folder: true,
name: "root",
mime_type: null,
path: null,
parent: null,
user_id: user.id,
size: 0,
is_deleted: false,
created_timestamp: "2020-05-14T06:20:44Z",
updated_timestamp: "2020-05-14T06:20:44Z",
status: true,
});
server.create("item", {
id: "2",
is_folder: false,
name: "image.jpg",
mime_type: "image/jpeg",
path: "",
parent: rootDir.id,
user_id: user.id,
size: 1024 * 1024 * 5,
is_deleted: false,
created_timestamp: "2020-05-14T06:20:44Z",
updated_timestamp: "2020-05-14T06:20:44Z",
status: true,
});
},
});