7
7
8
8
import com .iluwatar .caching .UserAccount ;
9
9
import com .iluwatar .caching .constants .CachingConstants ;
10
+ import com .iluwatar .caching .database .exceptions .DatabaseConnectionException ;
10
11
import com .mongodb .MongoClient ;
11
12
import com .mongodb .client .MongoDatabase ;
12
13
import com .mongodb .client .model .UpdateOptions ;
14
+ import lombok .extern .slf4j .Slf4j ;
13
15
import org .bson .Document ;
14
16
15
17
/**
16
18
* Implementation of DatabaseManager.
17
19
* implements base methods to work with MongoDb.
18
20
*/
21
+ @ Slf4j
19
22
public class MongoDb implements DbManager {
20
- /**
21
- * Mongo db.
22
- */
23
- private MongoDatabase db ;
23
+ private static final String DATABASE_NAME ="test" ;
24
24
25
25
/**
26
- * Connect to Db.
26
+ * Connect to Db. Check th connection
27
27
*/
28
28
@ Override
29
- public void connect () {
30
- MongoClient mongoClient =new MongoClient ();
31
- db =mongoClient .getDatabase ("test" );
29
+ public void connect ()throws DatabaseConnectionException {
30
+ try (MongoClient mongoClient =new MongoClient ()) {
31
+ mongoClient .getDatabase ("test" );
32
+ }catch (NoClassDefFoundError e ) {
33
+ throw new DatabaseConnectionException ("Could not connect to DB." );
34
+ }
32
35
}
33
36
34
37
/**
@@ -39,19 +42,23 @@ public void connect() {
39
42
*/
40
43
@ Override
41
44
public UserAccount readFromDb (final String userId ) {
42
- if (db ==null ) {
43
- connect ();
44
- }
45
- var iterable =db
46
- .getCollection (CachingConstants .USER_ACCOUNT )
47
- .find (new Document (USER_ID ,userId ));
48
- if (iterable .first () ==null ) {
49
- return null ;
45
+ try (MongoClient mongoClient =new MongoClient ()) {
46
+ MongoDatabase db =mongoClient .getDatabase (DATABASE_NAME );
47
+ var iterable =db
48
+ .getCollection (CachingConstants .USER_ACCOUNT )
49
+ .find (new Document (USER_ID ,userId ));
50
+ if (iterable .first () ==null ) {
51
+ return null ;
52
+ }
53
+ Document doc =iterable .first ();
54
+ if (doc !=null ) {
55
+ String userName =doc .getString (USER_NAME );
56
+ String appInfo =doc .getString (ADD_INFO );
57
+ return new UserAccount (userId ,userName ,appInfo );
58
+ }else {
59
+ return null ;
60
+ }
50
61
}
51
- Document doc =iterable .first ();
52
- String userName =doc .getString (USER_NAME );
53
- String appInfo =doc .getString (ADD_INFO );
54
- return new UserAccount (userId ,userName ,appInfo );
55
62
}
56
63
57
64
/**
@@ -62,15 +69,15 @@ public UserAccount readFromDb(final String userId) {
62
69
*/
63
70
@ Override
64
71
public UserAccount writeToDb (final UserAccount userAccount ) {
65
- if (db ==null ) {
66
- connect ();
72
+ try (MongoClient mongoClient =new MongoClient ()) {
73
+ MongoDatabase db =mongoClient .getDatabase (DATABASE_NAME );
74
+ db .getCollection (USER_ACCOUNT ).insertOne (
75
+ new Document (USER_ID ,userAccount .getUserId ())
76
+ .append (USER_NAME ,userAccount .getUserName ())
77
+ .append (ADD_INFO ,userAccount .getAdditionalInfo ())
78
+ );
79
+ return userAccount ;
67
80
}
68
- db .getCollection (USER_ACCOUNT ).insertOne (
69
- new Document (USER_ID ,userAccount .getUserId ())
70
- .append (USER_NAME ,userAccount .getUserName ())
71
- .append (ADD_INFO ,userAccount .getAdditionalInfo ())
72
- );
73
- return userAccount ;
74
81
}
75
82
76
83
/**
@@ -81,15 +88,15 @@ public UserAccount writeToDb(final UserAccount userAccount) {
81
88
*/
82
89
@ Override
83
90
public UserAccount updateDb (final UserAccount userAccount ) {
84
- if (db ==null ) {
85
- connect ();
91
+ try (MongoClient mongoClient =new MongoClient ()) {
92
+ MongoDatabase db =mongoClient .getDatabase (DATABASE_NAME );
93
+ Document id =new Document (USER_ID ,userAccount .getUserId ());
94
+ Document dataSet =new Document (USER_NAME ,userAccount .getUserName ())
95
+ .append (ADD_INFO ,userAccount .getAdditionalInfo ());
96
+ db .getCollection (CachingConstants .USER_ACCOUNT )
97
+ .updateOne (id ,new Document ("$set" ,dataSet ));
98
+ return userAccount ;
86
99
}
87
- Document id =new Document (USER_ID ,userAccount .getUserId ());
88
- Document dataSet =new Document (USER_NAME ,userAccount .getUserName ())
89
- .append (ADD_INFO ,userAccount .getAdditionalInfo ());
90
- db .getCollection (CachingConstants .USER_ACCOUNT )
91
- .updateOne (id ,new Document ("$set" ,dataSet ));
92
- return userAccount ;
93
100
}
94
101
95
102
/**
@@ -100,21 +107,21 @@ public UserAccount updateDb(final UserAccount userAccount) {
100
107
*/
101
108
@ Override
102
109
public UserAccount upsertDb (final UserAccount userAccount ) {
103
- if (db ==null ) {
104
- connect ();
110
+ try (MongoClient mongoClient =new MongoClient ()) {
111
+ MongoDatabase db =mongoClient .getDatabase (DATABASE_NAME );
112
+ String userId =userAccount .getUserId ();
113
+ String userName =userAccount .getUserName ();
114
+ String additionalInfo =userAccount .getAdditionalInfo ();
115
+ db .getCollection (CachingConstants .USER_ACCOUNT ).updateOne (
116
+ new Document (USER_ID ,userId ),
117
+ new Document ("$set" ,
118
+ new Document (USER_ID ,userId )
119
+ .append (USER_NAME ,userName )
120
+ .append (ADD_INFO ,additionalInfo )
121
+ ),
122
+ new UpdateOptions ().upsert (true )
123
+ );
124
+ return userAccount ;
105
125
}
106
- String userId =userAccount .getUserId ();
107
- String userName =userAccount .getUserName ();
108
- String additionalInfo =userAccount .getAdditionalInfo ();
109
- db .getCollection (CachingConstants .USER_ACCOUNT ).updateOne (
110
- new Document (USER_ID ,userId ),
111
- new Document ("$set" ,
112
- new Document (USER_ID ,userId )
113
- .append (USER_NAME ,userName )
114
- .append (ADD_INFO ,additionalInfo )
115
- ),
116
- new UpdateOptions ().upsert (true )
117
- );
118
- return userAccount ;
119
126
}
120
127
}