-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprivate-rest.ts
156 lines (141 loc) · 4.04 KB
/
private-rest.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {
Body,
Delete,
Get,
Patch,
Path,
Post,
Route,
Tags,
UploadedFile,
} from "tsoa";
import { BadgeHubData } from "@domain/BadgeHubData";
import { PostgreSQLBadgeHubMetadata } from "@db/PostgreSQLBadgeHubMetadata";
import type { ProjectSlug } from "@domain/readModels/app/Project";
import type { DBInsertUser, DBUser } from "@db/models/app/DBUser";
import type { DBInsertProject } from "@db/models/app/DBProject";
import type { DBInsertAppMetadataJSON } from "@db/models/app/DBAppMetadataJSON";
import { NodeFSBadgeHubFiles } from "@fs/NodeFSBadgeHubFiles";
import { Readable } from "node:stream";
interface UserProps extends Omit<DBInsertUser, "id"> {}
interface ProjectProps extends Omit<DBInsertProject, "slug"> {}
interface ProjectPropsPartial extends Partial<ProjectProps> {}
interface DbInsertAppMetadataJSONPartial
extends Partial<DBInsertAppMetadataJSON> {}
// TODO verify user_name against logged in user
@Route("/api/v3")
@Tags("private")
export class PrivateRestController {
public constructor(
private badgeHubData: BadgeHubData = new BadgeHubData(
new PostgreSQLBadgeHubMetadata(),
new NodeFSBadgeHubFiles()
)
) {}
/**
* Create a new user
*/
@Post("/users/{userId}")
public async insertUser(
@Path() userId: DBUser["id"],
@Body() props: UserProps
): Promise<void> {
// TODO implement with proper password handling (salting, hashing, ...)
throw new Error("Not implemented");
}
/**
* Create a new app
*/
@Post("/apps/{slug}")
public async createApp(
@Path() slug: ProjectSlug,
@Body() props: ProjectProps
): Promise<void> {
await this.badgeHubData.insertProject({ ...props, slug });
}
/**
* Create a new app
*/
@Delete("/apps/{slug}")
public async deleteApp(@Path() slug: ProjectSlug): Promise<void> {
await this.badgeHubData.deleteProject(slug);
}
/**
* Create a new app
*/
@Patch("/apps/{slug}")
public async updateApp(
@Path() slug: ProjectSlug,
@Body() changes: ProjectPropsPartial
): Promise<void> {
await this.badgeHubData.updateProject(slug, changes);
}
/**
* Upload a file to the latest draft version of the project.
*/
@Post("/apps/{slug}/draft/files/{filePath}")
public async writeDraftFile(
@Path() slug: string,
@Path() filePath: string,
@UploadedFile() file: Express.Multer.File
): Promise<void> {
await this.badgeHubData.writeDraftFile(slug, filePath, {
mimetype: file.mimetype,
fileContent: file.buffer,
directory: file.destination,
fileName: file.filename,
size: file.size,
});
}
/**
* Change the metadata of the latest draft version of the project.
*/
@Patch("/apps/{slug}/draft/metadata")
public async changeDraftAppMetadata(
@Path() slug: string,
@Body() appMetadataChanges: DbInsertAppMetadataJSONPartial
): Promise<void> {
await this.badgeHubData.updateDraftMetadata(slug, appMetadataChanges);
}
/**
* get the latest draft version of the project.
*/
@Get("/apps/{slug}/draft/files/{filePath}")
public async getDraftFile(
@Path() slug: string,
@Path() filePath: string
): Promise<Readable> {
const fileContents = await this.badgeHubData.getFileContents(
slug,
"draft",
filePath
);
return Readable.from(fileContents);
}
/**
* get the latest draft version of the app in zip format
*/
@Get("/apps/{slug}/draft/zip")
public async getLatestPublishedZip(
@Path() slug: string
): Promise<Uint8Array> {
return await this.badgeHubData.getVersionZipContents(slug, "draft");
}
/**
* Upload a file to the latest draft version of the project.
*/
@Post("/apps/{slug}/draft/zip")
public async writeZip(
@Path() slug: string,
@Body() zipContent: Uint8Array
): Promise<void> {
await this.badgeHubData.writeDraftProjectZip(slug, zipContent);
}
/**
* Publish the latest draft version
*/
@Patch("/apps/{slug}/publish")
public async publishVersion(@Path() slug: string): Promise<void> {
await this.badgeHubData.publishVersion(slug);
}
}