Skip to content

Commit c78fc51

Browse files
committed
Second solution
1 parent beea47f commit c78fc51

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
<version>2.0.0-rc2</version>
6666
</dependency>
6767

68+
<dependency>
69+
<groupId>org.projectlombok</groupId>
70+
<artifactId>lombok</artifactId>
71+
<optional>true</optional>
72+
</dependency>
73+
6874
</dependencies>
6975

7076
<build>

src/main/java/mate/academy/rickandmorty/controller/RickAndMortyController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import mate.academy.rickandmorty.dto.CharacterDto;
66
import mate.academy.rickandmorty.service.RickAndMortyClient;
77
import org.springframework.web.bind.annotation.GetMapping;
8-
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RequestParam;
99
import org.springframework.web.bind.annotation.RestController;
1010

1111
@RestController
@@ -24,7 +24,7 @@ public CharacterDto getRandomCharacter() {
2424

2525
@Operation(summary = "Get list of characters matching parameter")
2626
@GetMapping("/character")
27-
public List<CharacterDto> getCharactersList(@PathVariable String character) {
27+
public List<CharacterDto> getCharactersList(@RequestParam String character) {
2828
return rickAndMortyClient.getCharactersList(character);
2929
}
3030
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package mate.academy.rickandmorty.model;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
import jakarta.persistence.Table;
8+
import lombok.Data;
9+
10+
@Entity
11+
@Table(name = "characters")
12+
@Data
13+
public class Character {
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private Long id;
17+
private Long externalId;
18+
private String name;
19+
private String status;
20+
private String gender;
21+
}

src/main/java/mate/academy/rickandmorty/service/RickAndMortyClient.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import java.net.http.HttpResponse;
1010
import java.util.List;
1111
import mate.academy.rickandmorty.dto.CharacterDto;
12+
import org.hibernate.Session;
13+
import org.hibernate.SessionFactory;
14+
import org.hibernate.Transaction;
1215
import org.springframework.stereotype.Component;
1316

1417
@Component
@@ -18,34 +21,56 @@ public class RickAndMortyClient {
1821
private static final int MAX = 826;
1922
private static final int MIN = 1;
2023
private static final int RANGE = MAX - MIN + 1;
24+
private final SessionFactory sessionFactory;
25+
26+
public RickAndMortyClient(SessionFactory sessionFactory) {
27+
this.sessionFactory = sessionFactory;
28+
}
2129

2230
public CharacterDto getRandomCharacter() {
31+
Transaction transaction = null;
2332
int random = (int) (Math.random() * RANGE) + MIN;
2433
HttpClient httpClient = HttpClient.newHttpClient();
2534
String url = BASE_URL + CHARACTER + "/" + random;
2635
HttpRequest httpRequest = HttpRequest.newBuilder().GET().uri(URI.create(url)).build();
2736
try {
37+
Session session = sessionFactory.openSession();
38+
transaction = session.beginTransaction();
2839
HttpResponse<String> httpResponse = httpClient
2940
.send(httpRequest, HttpResponse.BodyHandlers.ofString());
3041
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;
3347
} catch (IOException | InterruptedException e) {
48+
if (transaction != null) {
49+
transaction.rollback();
50+
}
3451
throw new RuntimeException(e);
3552
}
3653
}
3754

3855
public List<CharacterDto> getCharactersList(String character) {
56+
Transaction transaction = null;
3957
HttpClient httpClient = HttpClient.newHttpClient();
40-
String url = BASE_URL + CHARACTER + "/" + character;
58+
String url = BASE_URL + CHARACTER + "?name=" + character;
4159
HttpRequest httpRequest = HttpRequest.newBuilder().GET().uri(URI.create(url)).build();
4260
try {
61+
Session session = sessionFactory.openSession();
62+
transaction = session.beginTransaction();
4363
HttpResponse<String> httpResponse = httpClient
4464
.send(httpRequest, HttpResponse.BodyHandlers.ofString());
4565
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;
4870
} catch (IOException | InterruptedException e) {
71+
if (transaction != null) {
72+
transaction.rollback();
73+
}
4974
throw new RuntimeException(e);
5075
}
5176
}

src/main/resources/application.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
33
spring.datasource.username=root
44
spring.datasource.password=root
55
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
6+
spring.jpa.hibernate.ddl-auto=create-drop
7+
spring.jpa.show-sql=true
8+
spring.jpa.open-in-view=false

0 commit comments

Comments
 (0)