77
88import com .iluwatar .caching .UserAccount ;
99import com .iluwatar .caching .constants .CachingConstants ;
10+ import com .iluwatar .caching .database .exceptions .DatabaseConnectionException ;
1011import com .mongodb .MongoClient ;
1112import com .mongodb .client .MongoDatabase ;
1213import com .mongodb .client .model .UpdateOptions ;
14+ import lombok .extern .slf4j .Slf4j ;
1315import org .bson .Document ;
1416
1517/**
1618 * Implementation of DatabaseManager.
1719 * implements base methods to work with MongoDb.
1820 */
21+ @ Slf4j
1922public class MongoDb implements DbManager {
20- /**
21- * Mongo db.
22- */
23- private MongoDatabase db ;
23+ private static final String DATABASE_NAME = "test" ;
2424
2525 /**
26- * Connect to Db.
26+ * Connect to Db. Check th connection
2727 */
2828 @ 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+ }
3235 }
3336
3437 /**
@@ -39,19 +42,23 @@ public void connect() {
3942 */
4043 @ Override
4144 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+ }
5061 }
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 );
5562 }
5663
5764 /**
@@ -62,15 +69,15 @@ public UserAccount readFromDb(final String userId) {
6269 */
6370 @ Override
6471 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 ;
6780 }
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 ;
7481 }
7582
7683 /**
@@ -81,15 +88,15 @@ public UserAccount writeToDb(final UserAccount userAccount) {
8188 */
8289 @ Override
8390 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 ;
8699 }
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 ;
93100 }
94101
95102 /**
@@ -100,21 +107,21 @@ public UserAccount updateDb(final UserAccount userAccount) {
100107 */
101108 @ Override
102109 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 ;
105125 }
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 ;
119126 }
120127}
0 commit comments