@@ -15,15 +15,26 @@ public class Main {
15
15
put ("Content-Type" , "application/x-www-form-urlencoded" );
16
16
}};
17
17
18
+ private static final HashMap <String , Post > postMap = new HashMap <>();
19
+ private static int sId = 0 ;
20
+
21
+ private static HTMLDivElement posts = null ;
22
+ private static HTMLTextareaElement newPost = null ;
23
+ private static HTMLInputElement name = null ;
24
+ private static HTMLPElement textCount = null ;
25
+ private static HTMLButtonElement postButton = null ;
26
+
27
+ static {
28
+ posts = $ .document .<HTMLDivElement >getElementByIdG ("posts" );
29
+ newPost = $ .document .<HTMLTextareaElement >getElementByIdG ("newPost" );
30
+ name = $ .document .<HTMLInputElement >getElementByIdG ("name" );
31
+ textCount = $ .document .<HTMLPElement >getElementByIdG ("textCount" );
32
+ postButton = $ .document .<HTMLButtonElement >getElementByIdG ("postButton" );
33
+ }
34
+
18
35
public static void main (String [] args ) {
19
36
System .out .println ("Hello from Main.java!" );
20
37
21
- var posts = $ .document .<HTMLDivElement >getElementByIdG ("posts" );
22
- var newPost = $ .document .<HTMLTextareaElement >getElementByIdG ("newPost" );
23
- var name = $ .document .<HTMLInputElement >getElementByIdG ("name" );
24
- var textCount = $ .document .<HTMLPElement >getElementByIdG ("textCount" );
25
- var postButton = $ .document .<HTMLButtonElement >getElementByIdG ("postButton" );
26
-
27
38
newPost .addEventListener ("input" , new EventListener () {
28
39
@ Override
29
40
public void handleEvent (Event evt ) {
@@ -51,17 +62,27 @@ public void handleEvent(Event evt) {
51
62
52
63
System .out .println ("投稿中..." );
53
64
54
- var post = $ .fetch ("http://localhost:8080/api/v1/post" , RequestInit .builder ()
55
- .method ("PUT" )
56
- .body ("name=" + nameValue + "&text=" + textValue )
57
- .headers (HEADERS )
58
- .build ()
59
- ).json (Post .class );
60
-
65
+ // var post = $.fetch("http://localhost:8080/api/v1/post", RequestInit.builder()
66
+ // .method("PUT")
67
+ // .body("name=" + nameValue + "&text=" + textValue)
68
+ // .headers(HEADERS)
69
+ // .build()
70
+ // ).json(Post.class);
71
+
72
+ var post = Post .builder ()
73
+ .text (textValue )
74
+ .name (nameValue )
75
+ .date (String .valueOf (System .currentTimeMillis ()))
76
+ .sId (String .valueOf (sId ))
77
+ .build ();
78
+
79
+ postMap .put (String .valueOf (sId ), post );
61
80
var postElement = createPostElement (post );
62
81
posts .appendChild (postElement );
63
82
64
83
System .out .println ("投稿完了:" + post .getSId ());
84
+
85
+ sId ++;
65
86
}
66
87
});
67
88
@@ -71,11 +92,9 @@ public void handleEvent(Event evt) {
71
92
private static void updatePosts () {
72
93
System .out .println ("投稿を取得中..." );
73
94
74
- var posts = $ .document .<HTMLDivElement >getElementByIdG ("posts" );
75
- var postsList = $ .fetch ("http://localhost:8080/api/v1/posts" ).json (Post [].class );
76
- var i = 0 ;
77
-
78
- for (var post : postsList ) {
95
+ int i = 0 ;
96
+ for (var entry : postMap .entrySet ()) {
97
+ var post = entry .getValue ();
79
98
if (post .getText ().equals ("testdate" )) {
80
99
i ++;
81
100
continue ;
@@ -84,7 +103,7 @@ private static void updatePosts() {
84
103
posts .appendChild (postElement );
85
104
}
86
105
87
- System .out .println ((postsList . length - i ) + "件を取得しました!" );
106
+ System .out .println ((postMap . size () - i ) + "件を取得しました!" );
88
107
}
89
108
90
109
private static HTMLElement createPostElement (Post post ) {
@@ -96,7 +115,7 @@ private static HTMLElement createPostElement(Post post) {
96
115
nameElement .setClassName ("name" );
97
116
nameElement .setTextContent (post .getName ());
98
117
99
- var divElement = $ .document .<HTMLDivElement >createElementG ("div" );
118
+ var divElement = $ .document .<HTMLDivElement >createElementG ("div" );
100
119
divElement .appendChild (imgElement );
101
120
divElement .appendChild (nameElement );
102
121
0 commit comments