|
6 | 6 | import org.springframework.stereotype.Service;
|
7 | 7 |
|
8 | 8 | @Service
|
9 |
| -public class ConnectionManagerService { |
10 |
| - private final DatabaseConnectionRepository repository; |
11 |
| - private final Snowflake snowflake; |
| 9 | +public class ConnectionManagerService extends AbstractService<DatabaseConnection> { |
12 | 10 |
|
13 | 11 | public ConnectionManagerService(DatabaseConnectionRepository repository, Snowflake snowflake) {
|
14 |
| - this.repository = repository; |
15 |
| - this.snowflake = snowflake; |
| 12 | + super(repository, snowflake); |
16 | 13 | }
|
17 | 14 |
|
18 |
| - public DatabaseConnection create(DatabaseConnection databaseConnection) { |
19 |
| - if (databaseConnection.getDatabase() == null || databaseConnection.getDatabase().isBlank()) { |
20 |
| - throw new IllegalArgumentException("Invalid database name"); |
21 |
| - } else if (databaseConnection.getHost() == null || databaseConnection.getHost().isBlank()) { |
22 |
| - throw new IllegalArgumentException("Invalid host name"); |
23 |
| - } else if (databaseConnection.getPassword() == null || databaseConnection.getPassword().isBlank()) { |
24 |
| - throw new IllegalArgumentException("Invalid password"); |
25 |
| - } else if (databaseConnection.getPort() < 0 || databaseConnection.getPort() > 65_534) { |
26 |
| - throw new IllegalArgumentException("Port must be valid"); |
27 |
| - } else if (databaseConnection.getDriver() == null) { |
28 |
| - throw new IllegalArgumentException("Invalid driver"); |
29 |
| - } |
30 |
| - |
31 |
| - databaseConnection.setId(snowflake.next()); |
32 |
| - return repository.save(databaseConnection); |
33 |
| - } |
34 |
| - |
35 |
| - public DatabaseConnection update(DatabaseConnection request) { |
36 |
| - if (repository.findById(request.getId()).isPresent()) { |
37 |
| - return repository.save(request); |
38 |
| - } |
39 |
| - |
40 |
| - throw new IllegalArgumentException("Database Connection not found"); |
41 |
| - } |
42 |
| - |
43 |
| - public DatabaseConnection delete(long id) { |
44 |
| - DatabaseConnection conn = repository.findById(id) |
45 |
| - .orElseThrow(() -> new IllegalArgumentException("Database Connection not found")); |
46 |
| - |
47 |
| - repository.delete(conn); |
48 |
| - |
49 |
| - return conn; |
50 |
| - } |
51 | 15 | }
|
0 commit comments