-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.patch
323 lines (323 loc) · 9.93 KB
/
mongo.patch
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
Index: src/main/java/com/handson/tinyurl/model/User.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/tinyurl/model/User.java b/src/main/java/com/handson/tinyurl/model/User.java
new file mode 100644
--- /dev/null (date 1644132200382)
+++ b/src/main/java/com/handson/tinyurl/model/User.java (date 1644132200382)
@@ -0,0 +1,116 @@
+package com.handson.tinyurl.model;
+
+
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.index.Indexed;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+import java.util.Map;
+import java.util.Objects;
+
+@Document(collection = "users")
+public class User {
+
+ @Id
+ private String id;
+
+ @Indexed(unique = true)
+ private String name;
+
+ private int allUrlClicks;
+ private Map<String, ShortUrl> shorts;
+
+ public Map<String, ShortUrl> getShorts() {
+ return shorts;
+ }
+
+ @Override
+ public String toString() {
+ return "User{" +
+ "id='" + id + '\'' +
+ ", name='" + name + '\'' +
+ ", allUrlClicks=" + allUrlClicks +
+ '}';
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ User user = (User) o;
+ return allUrlClicks == user.allUrlClicks && Objects.equals(id, user.id) && Objects.equals(name, user.name);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(id, name, allUrlClicks);
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public void setAllUrlClicks(int allUrlClicks) {
+ this.allUrlClicks = allUrlClicks;
+ }
+
+
+ public String getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getAllUrlClicks() {
+ return allUrlClicks;
+ }
+
+ public static final class UserBuilder {
+ private String id;
+ private String name;
+ private int allUrlClicks;
+ private Map<String, ShortUrl> shorts;
+
+ private UserBuilder() {
+ }
+
+ public static UserBuilder anUser() {
+ return new UserBuilder();
+ }
+
+ public UserBuilder withId(String id) {
+ this.id = id;
+ return this;
+ }
+
+ public UserBuilder withName(String name) {
+ this.name = name;
+ return this;
+ }
+
+ public UserBuilder withAllUrlClicks(int allUrlClicks) {
+ this.allUrlClicks = allUrlClicks;
+ return this;
+ }
+
+ public UserBuilder withShorts(Map<String, ShortUrl> shorts) {
+ this.shorts = shorts;
+ return this;
+ }
+
+ public User build() {
+ User user = new User();
+ user.setId(id);
+ user.setName(name);
+ user.setAllUrlClicks(allUrlClicks);
+ user.shorts = this.shorts;
+ return user;
+ }
+ }
+}
Index: src/main/java/com/handson/tinyurl/config/MongoConfig.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/tinyurl/config/MongoConfig.java b/src/main/java/com/handson/tinyurl/config/MongoConfig.java
new file mode 100644
--- /dev/null (date 1644132200380)
+++ b/src/main/java/com/handson/tinyurl/config/MongoConfig.java (date 1644132200380)
@@ -0,0 +1,19 @@
+package com.handson.tinyurl.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
+
+@Configuration
+public class MongoConfig extends AbstractMongoClientConfiguration {
+
+ @Override
+ protected String getDatabaseName() {
+ return "tinydb";
+ }
+
+ @Override
+ public boolean autoIndexCreation() {
+ return true;
+ }
+
+}
Index: src/main/java/com/handson/tinyurl/model/ShortUrl.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/tinyurl/model/ShortUrl.java b/src/main/java/com/handson/tinyurl/model/ShortUrl.java
new file mode 100644
--- /dev/null (date 1644132200381)
+++ b/src/main/java/com/handson/tinyurl/model/ShortUrl.java (date 1644132200381)
@@ -0,0 +1,38 @@
+package com.handson.tinyurl.model;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+public class ShortUrl {
+ private Map<String, Integer> clicks = new HashMap<>();
+
+ public Map<String, Integer> getClicks() {
+ return clicks;
+ }
+
+ public void setClicks(Map<String, Integer> clicks) {
+ this.clicks = clicks;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ ShortUrl shortUrl = (ShortUrl) o;
+ return Objects.equals(clicks, shortUrl.clicks);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(clicks);
+ }
+
+ @Override
+ public String toString() {
+ return "ShortUrl{" +
+ "clicks=" + clicks +
+ '}';
+ }
+
+}
Index: src/main/java/com/handson/tinyurl/util/Dates.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/tinyurl/util/Dates.java b/src/main/java/com/handson/tinyurl/util/Dates.java
new file mode 100644
--- /dev/null (date 1644132384693)
+++ b/src/main/java/com/handson/tinyurl/util/Dates.java (date 1644132384693)
@@ -0,0 +1,88 @@
+package com.handson.tinyurl.util;
+
+import org.joda.time.*;
+import org.springframework.lang.Nullable;
+
+import java.text.SimpleDateFormat;
+import java.time.OffsetDateTime;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Objects;
+import java.util.TimeZone;
+
+public class Dates {
+ public static SimpleDateFormat shortDate = new SimpleDateFormat("YYYY-MM-dd");
+ public static TimeZone TIME_ZONE = TimeZone.getTimeZone("Asia/Jerusalem");
+ public static SimpleDateFormat monthFormatter = new SimpleDateFormat("yyyy/MM");
+
+ public Dates() {
+ }
+
+ public static String getCurMonth() {
+ Date date = new Date();
+
+ return monthFormatter.format(date);
+ }
+
+ public static String dateToStr(@Nullable LocalDate date) {
+ return date == null ? null : shortDate.format(date);
+ }
+
+ public static Date atUtc(LocalDateTime date) {
+ return atUtc(date, TIME_ZONE);
+ }
+
+ public static Date atUtc(LocalDateTime date, TimeZone zone) {
+ if (date == null) return null;
+ Calendar calendar = Calendar.getInstance();
+ calendar.setFirstDayOfWeek(Calendar.SUNDAY);
+ calendar.setTimeZone(zone);
+ calendar.set(date.getYear(), date.getMonthOfYear()-1, date.getDayOfMonth());//convert from locatDateTime to Calender time
+ calendar.set(Calendar.HOUR_OF_DAY, date.getHourOfDay());
+ calendar.set(Calendar.MINUTE, date.getMinuteOfHour());
+ calendar.set(Calendar.SECOND, date.getSecondOfMinute());
+ calendar.set(Calendar.MILLISECOND, 0);
+ return calendar.getTime();
+ }
+
+ public static Date atUtc(@Nullable LocalDate date) {
+ return atUtc(date, TIME_ZONE);
+ }
+
+ public static Date atUtc(@Nullable LocalDate date, TimeZone zone) {
+ return date == null ? null : atUtc(date.toLocalDateTime(LocalTime.MIDNIGHT), zone);
+ }
+
+ public static LocalDateTime atLocalTime(Date date) {
+ return atLocalTime(date, TIME_ZONE);
+ }
+
+ public static LocalDateTime atLocalTime(Date date, TimeZone zone) {
+ if (date == null) return null;
+ var localDate = OffsetDateTime.ofInstant(date.toInstant(), zone.toZoneId()).toLocalDateTime();
+ Calendar c = Calendar.getInstance();
+ c.set(localDate.getYear(), localDate.getMonthValue() - 1, localDate.getDayOfMonth());
+ c.set(Calendar.HOUR_OF_DAY, localDate.getHour());
+ c.set(Calendar.MINUTE, localDate.getMinute());
+ c.set(Calendar.SECOND, localDate.getSecond());
+ c.set(Calendar.MILLISECOND, 0);
+ LocalDateTime res = LocalDateTime.fromCalendarFields(c);
+ return res;
+ }
+
+ public static Date nowUTC() {
+ return DateTime.now().withZone(DateTimeZone.UTC).toDate();
+ }
+
+ public static String getFullDateTime() {
+ return DateTime.now().withZone(DateTimeZone.UTC).toDateTimeISO().toString();
+ }
+
+ public static boolean equals(@Nullable Date date1, @Nullable Date date2) {
+ if (date1 != null && date2 != null) {
+ return date1.getTime() == date2.getTime();
+ } else {
+ return Objects.equals(date1, date2);
+ }
+ }
+}
Index: src/main/java/com/handson/tinyurl/repository/UserRepository.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/tinyurl/repository/UserRepository.java b/src/main/java/com/handson/tinyurl/repository/UserRepository.java
new file mode 100644
--- /dev/null (date 1644132200382)
+++ b/src/main/java/com/handson/tinyurl/repository/UserRepository.java (date 1644132200382)
@@ -0,0 +1,12 @@
+package com.handson.tinyurl.repository;
+
+import com.handson.tinyurl.model.User;
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+import java.util.List;
+
+public interface UserRepository extends MongoRepository<User, String> {
+ User findFirstByName(String name);
+
+}
+