nestia 3.0.17 → 3.1.1-dev.20221015
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/lib/executable/internal/NestiaStarter.d.ts +3 -0
- package/lib/executable/internal/NestiaStarter.js +61 -0
- package/lib/executable/internal/NestiaStarter.js.map +1 -0
- package/lib/executable/nestia.js +5 -2
- package/lib/executable/nestia.js.map +1 -1
- package/package.json +1 -1
- package/template/.github/workflows/build.yml +18 -0
- package/template/.vscode/launch.json +19 -0
- package/template/.vscode/settings.json +10 -0
- package/template/LICENSE +21 -0
- package/template/README.md +131 -0
- package/template/nestia.config.ts +12 -0
- package/template/package.json +62 -0
- package/template/packages/api/LICENSE +21 -0
- package/template/packages/api/README.md +66 -0
- package/template/packages/api/package.json +21 -0
- package/template/src/Backend.ts +65 -0
- package/template/src/Configuration.ts +11 -0
- package/template/src/api/HttpError.ts +1 -0
- package/template/src/api/IConnection.ts +1 -0
- package/template/src/api/Primitive.ts +1 -0
- package/template/src/api/__internal/AesPkcs5.ts +1 -0
- package/template/src/api/__internal/Fetcher.ts +1 -0
- package/template/src/api/functional/bbs/articles/index.ts +166 -0
- package/template/src/api/functional/bbs/index.ts +7 -0
- package/template/src/api/functional/index.ts +7 -0
- package/template/src/api/index.ts +4 -0
- package/template/src/api/module.ts +5 -0
- package/template/src/api/structures/bbs/IBbsArticle.ts +142 -0
- package/template/src/api/structures/common/IAttachmentFile.ts +5 -0
- package/template/src/api/structures/common/IPage.ts +75 -0
- package/template/src/controllers/BbsArticlesController.ts +46 -0
- package/template/src/providers/bbs/BbsArticleProvider.ts +217 -0
- package/template/src/test/features/api/bbs/test_api_bbs_article_at.ts +52 -0
- package/template/src/test/features/api/bbs/test_api_bbs_article_index_search.ts +82 -0
- package/template/src/test/features/api/bbs/test_api_bbs_article_index_sort.ts +49 -0
- package/template/src/test/features/api/bbs/test_api_bbs_article_store.ts +44 -0
- package/template/src/test/features/api/bbs/test_api_bbs_article_update.ts +66 -0
- package/template/src/test/index.ts +45 -0
- package/template/src/test/internal/DynamicImportIterator.ts +119 -0
- package/template/src/test/internal/GaffComparator.ts +48 -0
- package/template/src/test/internal/IPointer.ts +3 -0
- package/template/src/test/internal/RandomGenerator.ts +72 -0
- package/template/src/test/internal/StopWatch.ts +16 -0
- package/template/src/test/internal/exception_must_be_thrown.ts +11 -0
- package/template/src/test/internal/validate_index.ts +29 -0
- package/template/src/test/internal/validate_index_sort.ts +39 -0
- package/template/src/utils/ArrayUtil.ts +86 -0
- package/template/src/utils/MapUtil.ts +14 -0
- package/template/tsconfig.api.json +15 -0
- package/template/tsconfig.json +118 -0
- package/tsconfig.json +82 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @module api.functional.bbs.articles
|
|
4
|
+
* @nestia Generated by Nestia - https://github.com/samchon/nestia
|
|
5
|
+
*/
|
|
6
|
+
//================================================================
|
|
7
|
+
import { Fetcher } from "nestia-fetcher";
|
|
8
|
+
import type { IConnection } from "nestia-fetcher";
|
|
9
|
+
|
|
10
|
+
import type { IBbsArticle } from "./../../../structures/bbs/IBbsArticle";
|
|
11
|
+
import type { IPage } from "./../../../structures/common/IPage";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @controller BbsArticlesController.index()
|
|
15
|
+
* @path PATCH /bbs/articles/:section
|
|
16
|
+
* @nestia Generated by Nestia - https://github.com/samchon/nestia
|
|
17
|
+
*/
|
|
18
|
+
export function index
|
|
19
|
+
(
|
|
20
|
+
connection: IConnection,
|
|
21
|
+
section: string,
|
|
22
|
+
input: IBbsArticle.IRequest
|
|
23
|
+
): Promise<index.Output>
|
|
24
|
+
{
|
|
25
|
+
return Fetcher.fetch
|
|
26
|
+
(
|
|
27
|
+
connection,
|
|
28
|
+
index.ENCRYPTED,
|
|
29
|
+
index.METHOD,
|
|
30
|
+
index.path(section),
|
|
31
|
+
input
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
export namespace index
|
|
35
|
+
{
|
|
36
|
+
export type Input = IBbsArticle.IRequest;
|
|
37
|
+
export type Output = IPage<IBbsArticle.ISummary>;
|
|
38
|
+
|
|
39
|
+
export const METHOD = "PATCH" as const;
|
|
40
|
+
export const PATH: string = "/bbs/articles/:section";
|
|
41
|
+
export const ENCRYPTED: Fetcher.IEncrypted = {
|
|
42
|
+
request: false,
|
|
43
|
+
response: true,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export function path(section: string): string
|
|
47
|
+
{
|
|
48
|
+
return `/bbs/articles/${encodeURIComponent(section)}`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @controller BbsArticlesController.at()
|
|
54
|
+
* @path GET /bbs/articles/:section/:id
|
|
55
|
+
* @nestia Generated by Nestia - https://github.com/samchon/nestia
|
|
56
|
+
*/
|
|
57
|
+
export function at
|
|
58
|
+
(
|
|
59
|
+
connection: IConnection,
|
|
60
|
+
section: string,
|
|
61
|
+
id: string
|
|
62
|
+
): Promise<at.Output>
|
|
63
|
+
{
|
|
64
|
+
return Fetcher.fetch
|
|
65
|
+
(
|
|
66
|
+
connection,
|
|
67
|
+
at.ENCRYPTED,
|
|
68
|
+
at.METHOD,
|
|
69
|
+
at.path(section, id)
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
export namespace at
|
|
73
|
+
{
|
|
74
|
+
export type Output = IBbsArticle;
|
|
75
|
+
|
|
76
|
+
export const METHOD = "GET" as const;
|
|
77
|
+
export const PATH: string = "/bbs/articles/:section/:id";
|
|
78
|
+
export const ENCRYPTED: Fetcher.IEncrypted = {
|
|
79
|
+
request: false,
|
|
80
|
+
response: false,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export function path(section: string, id: string): string
|
|
84
|
+
{
|
|
85
|
+
return `/bbs/articles/${encodeURIComponent(section)}/${encodeURIComponent(id)}`;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @controller BbsArticlesController.store()
|
|
91
|
+
* @path POST /bbs/articles/:section
|
|
92
|
+
* @nestia Generated by Nestia - https://github.com/samchon/nestia
|
|
93
|
+
*/
|
|
94
|
+
export function store
|
|
95
|
+
(
|
|
96
|
+
connection: IConnection,
|
|
97
|
+
section: string,
|
|
98
|
+
input: IBbsArticle.IStore
|
|
99
|
+
): Promise<store.Output>
|
|
100
|
+
{
|
|
101
|
+
return Fetcher.fetch
|
|
102
|
+
(
|
|
103
|
+
connection,
|
|
104
|
+
store.ENCRYPTED,
|
|
105
|
+
store.METHOD,
|
|
106
|
+
store.path(section),
|
|
107
|
+
input
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
export namespace store
|
|
111
|
+
{
|
|
112
|
+
export type Input = IBbsArticle.IStore;
|
|
113
|
+
export type Output = IBbsArticle;
|
|
114
|
+
|
|
115
|
+
export const METHOD = "POST" as const;
|
|
116
|
+
export const PATH: string = "/bbs/articles/:section";
|
|
117
|
+
export const ENCRYPTED: Fetcher.IEncrypted = {
|
|
118
|
+
request: false,
|
|
119
|
+
response: false,
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export function path(section: string): string
|
|
123
|
+
{
|
|
124
|
+
return `/bbs/articles/${encodeURIComponent(section)}`;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @controller BbsArticlesController.update()
|
|
130
|
+
* @path PUT /bbs/articles/:section/:id
|
|
131
|
+
* @nestia Generated by Nestia - https://github.com/samchon/nestia
|
|
132
|
+
*/
|
|
133
|
+
export function update
|
|
134
|
+
(
|
|
135
|
+
connection: IConnection,
|
|
136
|
+
section: string,
|
|
137
|
+
id: string,
|
|
138
|
+
input: IBbsArticle.IUpdate
|
|
139
|
+
): Promise<update.Output>
|
|
140
|
+
{
|
|
141
|
+
return Fetcher.fetch
|
|
142
|
+
(
|
|
143
|
+
connection,
|
|
144
|
+
update.ENCRYPTED,
|
|
145
|
+
update.METHOD,
|
|
146
|
+
update.path(section, id),
|
|
147
|
+
input
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
export namespace update
|
|
151
|
+
{
|
|
152
|
+
export type Input = IBbsArticle.IUpdate;
|
|
153
|
+
export type Output = IBbsArticle.IContent;
|
|
154
|
+
|
|
155
|
+
export const METHOD = "PUT" as const;
|
|
156
|
+
export const PATH: string = "/bbs/articles/:section/:id";
|
|
157
|
+
export const ENCRYPTED: Fetcher.IEncrypted = {
|
|
158
|
+
request: false,
|
|
159
|
+
response: false,
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
export function path(section: string, id: string): string
|
|
163
|
+
{
|
|
164
|
+
return `/bbs/articles/${encodeURIComponent(section)}/${encodeURIComponent(id)}`;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { IAttachmentFile } from "../common/IAttachmentFile";
|
|
2
|
+
import { IPage } from "../common/IPage";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* BBS article.
|
|
6
|
+
*/
|
|
7
|
+
export interface IBbsArticle {
|
|
8
|
+
/**
|
|
9
|
+
* Primary Key.
|
|
10
|
+
*
|
|
11
|
+
* @format uuid
|
|
12
|
+
*/
|
|
13
|
+
id: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Section code.
|
|
17
|
+
*/
|
|
18
|
+
section: string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Name of nickname of writer.
|
|
22
|
+
*/
|
|
23
|
+
writer: string;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* List of contents.
|
|
27
|
+
*
|
|
28
|
+
* Whenever updating an article, its contents would be accumulated.
|
|
29
|
+
*/
|
|
30
|
+
contents: IBbsArticle.IContent[];
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Creation time of the article.
|
|
34
|
+
*/
|
|
35
|
+
created_at: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export namespace IBbsArticle {
|
|
39
|
+
/**
|
|
40
|
+
* Page request info with some options.
|
|
41
|
+
*/
|
|
42
|
+
export interface IRequest extends IPage.IRequest {
|
|
43
|
+
/**
|
|
44
|
+
* Searching options.
|
|
45
|
+
*/
|
|
46
|
+
search?: IRequest.ISearch;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Sorting options.
|
|
50
|
+
*
|
|
51
|
+
* The plus sign means ASC and minus sign means DESC.
|
|
52
|
+
*/
|
|
53
|
+
sort?: IPage.IRequest.Sort<IRequest.SortableColumns>;
|
|
54
|
+
}
|
|
55
|
+
export namespace IRequest {
|
|
56
|
+
/**
|
|
57
|
+
* Searching options.
|
|
58
|
+
*/
|
|
59
|
+
export interface ISearch {
|
|
60
|
+
writer?: string;
|
|
61
|
+
title?: string;
|
|
62
|
+
body?: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* List of sortable columns.
|
|
67
|
+
*/
|
|
68
|
+
export type SortableColumns =
|
|
69
|
+
| "writer"
|
|
70
|
+
| "title"
|
|
71
|
+
| "created_at"
|
|
72
|
+
| "updated_at";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Summarized info.
|
|
77
|
+
*/
|
|
78
|
+
export interface ISummary {
|
|
79
|
+
id: string;
|
|
80
|
+
writer: string;
|
|
81
|
+
title: string;
|
|
82
|
+
created_at: string;
|
|
83
|
+
updated_at: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Content info.
|
|
88
|
+
*/
|
|
89
|
+
export interface IContent extends Omit<IUpdate, "password"> {
|
|
90
|
+
/**
|
|
91
|
+
* Primary key of individual content.
|
|
92
|
+
*
|
|
93
|
+
* @format uuid
|
|
94
|
+
*/
|
|
95
|
+
id: string;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Creation time of this content.
|
|
99
|
+
*/
|
|
100
|
+
created_at: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Store info.
|
|
105
|
+
*/
|
|
106
|
+
export interface IStore extends IUpdate {
|
|
107
|
+
/**
|
|
108
|
+
* Name or nickname of the writer.
|
|
109
|
+
*/
|
|
110
|
+
writer: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Update info.
|
|
115
|
+
*/
|
|
116
|
+
export interface IUpdate {
|
|
117
|
+
/**
|
|
118
|
+
* Title of the article.
|
|
119
|
+
*/
|
|
120
|
+
title: string;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Content body.
|
|
124
|
+
*/
|
|
125
|
+
body: string;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Format of the content body.
|
|
129
|
+
*/
|
|
130
|
+
format: "md" | "html" | "txt";
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* List of files (to be) attached.
|
|
134
|
+
*/
|
|
135
|
+
files: IAttachmentFile[];
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Password of the article.
|
|
139
|
+
*/
|
|
140
|
+
password: string;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @module api.structures.common
|
|
4
|
+
*/
|
|
5
|
+
//================================================================
|
|
6
|
+
/**
|
|
7
|
+
* Paged record set.
|
|
8
|
+
*
|
|
9
|
+
* @author Samchon
|
|
10
|
+
*/
|
|
11
|
+
export interface IPage<T extends object> {
|
|
12
|
+
/**
|
|
13
|
+
* Pagination info.
|
|
14
|
+
*/
|
|
15
|
+
pagination: IPage.IPagination;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* List of records
|
|
19
|
+
*/
|
|
20
|
+
data: T[];
|
|
21
|
+
}
|
|
22
|
+
export namespace IPage {
|
|
23
|
+
/**
|
|
24
|
+
* Pagination info.
|
|
25
|
+
*/
|
|
26
|
+
export interface IPagination {
|
|
27
|
+
/**
|
|
28
|
+
* Current page number.
|
|
29
|
+
*
|
|
30
|
+
* Starts from 1.
|
|
31
|
+
*/
|
|
32
|
+
page: number;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Limit records per a page.
|
|
36
|
+
*
|
|
37
|
+
* @default 100
|
|
38
|
+
*/
|
|
39
|
+
limit: number;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Number of total records.
|
|
43
|
+
*/
|
|
44
|
+
total_count: number;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Number of total pages.
|
|
48
|
+
*/
|
|
49
|
+
total_pages: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Request info of page.
|
|
54
|
+
*/
|
|
55
|
+
export interface IRequest {
|
|
56
|
+
/**
|
|
57
|
+
* Target page number.
|
|
58
|
+
*
|
|
59
|
+
* @default 1
|
|
60
|
+
*/
|
|
61
|
+
page?: number;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Limit per a page.
|
|
65
|
+
*
|
|
66
|
+
* @defualt 100
|
|
67
|
+
*/
|
|
68
|
+
limit?: number;
|
|
69
|
+
}
|
|
70
|
+
export namespace IRequest {
|
|
71
|
+
export type Sort<Literal extends string> = Array<
|
|
72
|
+
`-${Literal}` | `+${Literal}`
|
|
73
|
+
>;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import helper from "nestia-helper";
|
|
2
|
+
import { Controller } from "@nestjs/common";
|
|
3
|
+
import { IBbsArticle } from "../api/structures/bbs/IBbsArticle";
|
|
4
|
+
import { IPage } from "../api/structures/common/IPage";
|
|
5
|
+
import { BbsArticleProvider } from "../providers/bbs/BbsArticleProvider";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* This is a fake controller.
|
|
9
|
+
*
|
|
10
|
+
* Remove it or make it to be real one.
|
|
11
|
+
*/
|
|
12
|
+
@Controller("bbs/articles/:section")
|
|
13
|
+
export class BbsArticlesController {
|
|
14
|
+
@helper.EncryptedRoute.Patch()
|
|
15
|
+
public index(
|
|
16
|
+
@helper.TypedParam("section", "string") section: string,
|
|
17
|
+
@helper.TypedBody() input: IBbsArticle.IRequest,
|
|
18
|
+
): Promise<IPage<IBbsArticle.ISummary>> {
|
|
19
|
+
return BbsArticleProvider.index(section, input);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@helper.TypedRoute.Get(":id")
|
|
23
|
+
public at(
|
|
24
|
+
@helper.TypedParam("section", "string") section: string,
|
|
25
|
+
@helper.TypedParam("id", "uuid") id: string,
|
|
26
|
+
): Promise<IBbsArticle> {
|
|
27
|
+
return BbsArticleProvider.find(section, id);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@helper.TypedRoute.Post()
|
|
31
|
+
public store(
|
|
32
|
+
@helper.TypedParam("section", "string") section: string,
|
|
33
|
+
@helper.TypedBody() input: IBbsArticle.IStore,
|
|
34
|
+
): Promise<IBbsArticle> {
|
|
35
|
+
return BbsArticleProvider.store(section, input);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@helper.TypedRoute.Put(":id")
|
|
39
|
+
public update(
|
|
40
|
+
@helper.TypedParam("section", "string") section: string,
|
|
41
|
+
@helper.TypedParam("id", "uuid") id: string,
|
|
42
|
+
@helper.TypedBody() input: IBbsArticle.IUpdate,
|
|
43
|
+
): Promise<IBbsArticle.IContent> {
|
|
44
|
+
return BbsArticleProvider.update(section, id, input);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { ForbiddenException, NotFoundException } from "@nestjs/common";
|
|
2
|
+
import { v4 } from "uuid";
|
|
3
|
+
|
|
4
|
+
import { IBbsArticle } from "@ORGANIZATION/PROJECT-api/lib/structures/bbs/IBbsArticle";
|
|
5
|
+
import { IPage } from "@ORGANIZATION/PROJECT-api/lib/structures/common/IPage";
|
|
6
|
+
|
|
7
|
+
import { MapUtil } from "../../utils/MapUtil";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* This is a fake provider.
|
|
11
|
+
*
|
|
12
|
+
* Remove it or change it to be stored on the real DB.
|
|
13
|
+
*/
|
|
14
|
+
export namespace BbsArticleProvider {
|
|
15
|
+
export async function index(
|
|
16
|
+
section: string,
|
|
17
|
+
input: IBbsArticle.IRequest,
|
|
18
|
+
): Promise<IPage<IBbsArticle.ISummary>> {
|
|
19
|
+
// GET ENTIRE ARTICLES
|
|
20
|
+
const dict = storage.get(section);
|
|
21
|
+
if (dict === undefined)
|
|
22
|
+
throw new NotFoundException(
|
|
23
|
+
`Error on BbsArticleProvider.index(): unable to find the matched section "${section}".`,
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
/* disable-eslint */
|
|
27
|
+
let articles: IBbsArticle[] = Array.from(dict.values()).map(
|
|
28
|
+
(rec) => rec.article,
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// SEARCH
|
|
32
|
+
if (input.search !== undefined) {
|
|
33
|
+
if (input.search.writer)
|
|
34
|
+
articles = articles.filter(
|
|
35
|
+
(x) => x.writer.indexOf(input.search!.writer!) !== -1,
|
|
36
|
+
);
|
|
37
|
+
if (input.search.title)
|
|
38
|
+
articles = articles.filter(
|
|
39
|
+
(x) =>
|
|
40
|
+
x.contents
|
|
41
|
+
.at(-1)!
|
|
42
|
+
.title.indexOf(input.search!.title!) !== -1,
|
|
43
|
+
);
|
|
44
|
+
if (input.search.body)
|
|
45
|
+
articles = articles.filter(
|
|
46
|
+
(x) =>
|
|
47
|
+
x.contents.at(-1)!.body.indexOf(input.search!.body!) !==
|
|
48
|
+
-1,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// SORT
|
|
53
|
+
if (input.sort?.length) {
|
|
54
|
+
for (const comp of input.sort.reverse())
|
|
55
|
+
articles = articles.sort((x, y) => {
|
|
56
|
+
const sign = comp[0];
|
|
57
|
+
const column = comp.substring(1);
|
|
58
|
+
const closure = () => {
|
|
59
|
+
if (column === "created_at")
|
|
60
|
+
return (
|
|
61
|
+
new Date(x.created_at).getTime() -
|
|
62
|
+
new Date(y.created_at).getTime()
|
|
63
|
+
);
|
|
64
|
+
else if (column === "updated_at")
|
|
65
|
+
return (
|
|
66
|
+
new Date(
|
|
67
|
+
x.contents.at(-1)!.created_at,
|
|
68
|
+
).getTime() -
|
|
69
|
+
new Date(
|
|
70
|
+
y.contents.at(-1)!.created_at,
|
|
71
|
+
).getTime()
|
|
72
|
+
);
|
|
73
|
+
else if (column === "writer")
|
|
74
|
+
return x.writer.localeCompare(y.writer);
|
|
75
|
+
else
|
|
76
|
+
return x.contents
|
|
77
|
+
.at(-1)!
|
|
78
|
+
.title.localeCompare(y.contents.at(-1)!.title);
|
|
79
|
+
};
|
|
80
|
+
return sign === "+" ? closure() : -closure();
|
|
81
|
+
});
|
|
82
|
+
} else {
|
|
83
|
+
articles.sort(
|
|
84
|
+
(a, b) =>
|
|
85
|
+
new Date(b.created_at).getTime() -
|
|
86
|
+
new Date(a.created_at).getTime(),
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// PAGINATION
|
|
91
|
+
const limit: number = input.limit || 100;
|
|
92
|
+
const start: number = ((input.page || 1) - 1) * limit;
|
|
93
|
+
return {
|
|
94
|
+
pagination: {
|
|
95
|
+
page: input.page || 1,
|
|
96
|
+
limit: limit,
|
|
97
|
+
total_count: articles.length,
|
|
98
|
+
total_pages: Math.ceil(articles.length / limit),
|
|
99
|
+
},
|
|
100
|
+
data: articles.slice(start, start + limit).map((article) => ({
|
|
101
|
+
id: article.id,
|
|
102
|
+
section: article.section,
|
|
103
|
+
writer: article.writer,
|
|
104
|
+
title: article.contents.at(-1)!.title,
|
|
105
|
+
created_at: article.created_at,
|
|
106
|
+
updated_at: article.contents.at(-1)!.created_at,
|
|
107
|
+
})),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function find(
|
|
112
|
+
section: string,
|
|
113
|
+
id: string,
|
|
114
|
+
password?: string,
|
|
115
|
+
): Promise<IBbsArticle> {
|
|
116
|
+
const dict = storage.get(section);
|
|
117
|
+
if (dict === undefined)
|
|
118
|
+
throw new NotFoundException(
|
|
119
|
+
`Error on BbsArticleProvider.find(): unable to find the matched section "${section}".`,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const record = dict.get(id);
|
|
123
|
+
if (record === undefined)
|
|
124
|
+
throw new NotFoundException(
|
|
125
|
+
`Error on BbsArticleProvider.find(): unable to find the matched article "${id}".`,
|
|
126
|
+
);
|
|
127
|
+
else if (password !== undefined && password !== record.password)
|
|
128
|
+
throw new ForbiddenException(
|
|
129
|
+
`Error on BbsArticleProvider.find(): different password.`,
|
|
130
|
+
);
|
|
131
|
+
return record.article;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function store(
|
|
135
|
+
section: string,
|
|
136
|
+
input: IBbsArticle.IStore,
|
|
137
|
+
): Promise<IBbsArticle> {
|
|
138
|
+
const now: string = datetime_to_string(new Date());
|
|
139
|
+
const article: IBbsArticle = {
|
|
140
|
+
id: v4(),
|
|
141
|
+
section,
|
|
142
|
+
writer: input.writer,
|
|
143
|
+
contents: [
|
|
144
|
+
{
|
|
145
|
+
...{
|
|
146
|
+
...input,
|
|
147
|
+
password: undefined,
|
|
148
|
+
},
|
|
149
|
+
id: v4(),
|
|
150
|
+
created_at: now,
|
|
151
|
+
},
|
|
152
|
+
],
|
|
153
|
+
created_at: now,
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const dict: Map<string, IRecord> = MapUtil.take(
|
|
157
|
+
storage,
|
|
158
|
+
section,
|
|
159
|
+
() => new Map(),
|
|
160
|
+
);
|
|
161
|
+
dict.set(article.id, {
|
|
162
|
+
article,
|
|
163
|
+
password: input.password,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
return article;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export async function update(
|
|
170
|
+
section: string,
|
|
171
|
+
id: string,
|
|
172
|
+
input: IBbsArticle.IUpdate,
|
|
173
|
+
): Promise<IBbsArticle.IContent> {
|
|
174
|
+
const article: IBbsArticle = await find(section, id, input.password);
|
|
175
|
+
const content: IBbsArticle.IContent = {
|
|
176
|
+
...{
|
|
177
|
+
...input,
|
|
178
|
+
password: undefined,
|
|
179
|
+
},
|
|
180
|
+
id: v4(),
|
|
181
|
+
created_at: datetime_to_string(new Date()),
|
|
182
|
+
};
|
|
183
|
+
article.contents.push(content);
|
|
184
|
+
return content;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
interface IRecord {
|
|
189
|
+
article: IBbsArticle;
|
|
190
|
+
password: string;
|
|
191
|
+
}
|
|
192
|
+
const storage: Map<string, Map<string, IRecord>> = new Map();
|
|
193
|
+
|
|
194
|
+
function datetime_to_string(date: Date): string {
|
|
195
|
+
return (
|
|
196
|
+
cipher(4)(date.getFullYear()) +
|
|
197
|
+
"-" +
|
|
198
|
+
([
|
|
199
|
+
[date.getMonth() + 1, date.getDate()].map(cipher(2)).join("-"),
|
|
200
|
+
[date.getHours(), date.getMinutes(), date.getSeconds()]
|
|
201
|
+
.map(cipher(2))
|
|
202
|
+
.join(":"),
|
|
203
|
+
].join(" ") +
|
|
204
|
+
"." +
|
|
205
|
+
precision(date.getMilliseconds()))
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const cipher = (digit: number) => (value: number) => {
|
|
210
|
+
const str: string = String(value);
|
|
211
|
+
return "0".repeat(digit - str.length) + str;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const precision = (value: number) => {
|
|
215
|
+
const str: string = String(value);
|
|
216
|
+
return str + "0".repeat(3 - str.length);
|
|
217
|
+
};
|