9
9
import java .net .http .HttpResponse ;
10
10
import java .util .List ;
11
11
import mate .academy .rickandmorty .dto .CharacterDto ;
12
+ import org .hibernate .Session ;
13
+ import org .hibernate .SessionFactory ;
14
+ import org .hibernate .Transaction ;
12
15
import org .springframework .stereotype .Component ;
13
16
14
17
@ Component
@@ -18,34 +21,56 @@ public class RickAndMortyClient {
18
21
private static final int MAX = 826 ;
19
22
private static final int MIN = 1 ;
20
23
private static final int RANGE = MAX - MIN + 1 ;
24
+ private final SessionFactory sessionFactory ;
25
+
26
+ public RickAndMortyClient (SessionFactory sessionFactory ) {
27
+ this .sessionFactory = sessionFactory ;
28
+ }
21
29
22
30
public CharacterDto getRandomCharacter () {
31
+ Transaction transaction = null ;
23
32
int random = (int ) (Math .random () * RANGE ) + MIN ;
24
33
HttpClient httpClient = HttpClient .newHttpClient ();
25
34
String url = BASE_URL + CHARACTER + "/" + random ;
26
35
HttpRequest httpRequest = HttpRequest .newBuilder ().GET ().uri (URI .create (url )).build ();
27
36
try {
37
+ Session session = sessionFactory .openSession ();
38
+ transaction = session .beginTransaction ();
28
39
HttpResponse <String > httpResponse = httpClient
29
40
.send (httpRequest , HttpResponse .BodyHandlers .ofString ());
30
41
ObjectMapper objectMapper = new ObjectMapper ();
31
- return objectMapper .readValue (httpResponse .body (), new TypeReference <>() {
32
- });
42
+ CharacterDto characterDto = objectMapper .readValue (httpResponse .body (),
43
+ new TypeReference <>() {});
44
+ session .persist (characterDto );
45
+ transaction .commit ();
46
+ return characterDto ;
33
47
} catch (IOException | InterruptedException e ) {
48
+ if (transaction != null ) {
49
+ transaction .rollback ();
50
+ }
34
51
throw new RuntimeException (e );
35
52
}
36
53
}
37
54
38
55
public List <CharacterDto > getCharactersList (String character ) {
56
+ Transaction transaction = null ;
39
57
HttpClient httpClient = HttpClient .newHttpClient ();
40
- String url = BASE_URL + CHARACTER + "/ " + character ;
58
+ String url = BASE_URL + CHARACTER + "?name= " + character ;
41
59
HttpRequest httpRequest = HttpRequest .newBuilder ().GET ().uri (URI .create (url )).build ();
42
60
try {
61
+ Session session = sessionFactory .openSession ();
62
+ transaction = session .beginTransaction ();
43
63
HttpResponse <String > httpResponse = httpClient
44
64
.send (httpRequest , HttpResponse .BodyHandlers .ofString ());
45
65
ObjectMapper objectMapper = new ObjectMapper ();
46
- return objectMapper .readValue (httpResponse .body (), new TypeReference <>() {
47
- });
66
+ List <CharacterDto > characters = objectMapper
67
+ .readValue (httpResponse .body (), new TypeReference <>() {});
68
+ transaction .commit ();
69
+ return characters ;
48
70
} catch (IOException | InterruptedException e ) {
71
+ if (transaction != null ) {
72
+ transaction .rollback ();
73
+ }
49
74
throw new RuntimeException (e );
50
75
}
51
76
}
0 commit comments