package com.technokratos.agona.entity; import java.util.UUID; public record User (…
- User
package com.technokratos.agona.entity;
import java.util.UUID;
public record User ( String id, String username, String password_hash, short age, boolean is_online, float mood ){ public static User createUser(String username, String password_hash, short age, boolean is_online, float mood) { return new User(UUID.randomUUID().toString(), username, password_hash, age, is_online,mood); } } package com.technokratos.agona.repository.Impl;
import com.technokratos.agona.entity.Chat; import com.technokratos.agona.entity.User; import com.technokratos.agona.db.DatabaseConnection; import com.technokratos.agona.repository.UserRepository;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Optional;
public class UserRepositoryImpl implements UserRepository {
private final String findByIdQuery = "SELECT * FROM user_data WHERE id = ?"; private final String deleteUserByIdQuery = "DELETE FROM user_data WHERE id = ?"; private final String userInsertQuery = "INSERT INTO user_data (id, username, password_hash, age, is_online, mood) VALUES (?, ?, ?, ?, ?, ?)"; private final String findAllQuery = "SELECT * FROM user_data"; private final String findAllChatThisUserQuery = "SELECT chat_id FROM user_chat WHERE user_id=?"; private final String findChatsByIdQuery = "SELECT * FROM chat_data WHERE id=?"; private final String chatDeleteUserComparisonChatQuery = "DELETE FROM user_chat WHERE user_id=?"; private final String deleteChatOwnerQuery = "DELETE FROM chat_data WHERE ownerid = ?"; DatabaseConnection databaseConnection; public UserRepositoryImpl(DatabaseConnection databaseConnection) { this.databaseConnection = databaseConnection; } @Override public List<User> getAll() { List<User> userList = new ArrayList<>(); try (Connection connection = databaseConnection.getConnection(); PreparedStatement userStatement = connection.prepareStatement(findAllQuery)) { ResultSet userResultSet = userStatement.executeQuery(); while (userResultSet.next()) { User user = new User( userResultSet.getString("id"), userResultSet.getString("username"), userResultSet.getString("password_hash"), userResultSet.getShort("age"), userResultSet.getBoolean("is_online"), userResultSet.getFloat("mood") ); userList.add(user); } } catch (SQLException e) { e.printStackTrace(); } return userList; } @Override public Optional<User> findById(String id) { try (Connection connection = databaseConnection.getConnection(); PreparedStatement userStatement = connection.prepareStatement(findByIdQuery)) { userStatement.setString(1, id); ResultSet userResultSet = userStatement.executeQuery(); if (userResultSet.next()) { return Optional.of(new User( id, userResultSet.getString("username"), userResultSet.getString("password_hash"), userResultSet.getShort("age"), userResultSet.getBoolean("is_online"), userResultSet.getFloat("mood") )); } } catch (SQLException e) { e.printStackTrace(); } return Optional.empty(); } private void deleteUserComparisonChat(String idUser) { try (Connection connection = databaseConnection.getConnection(); PreparedStatement messageStatement = connection.prepareStatement(chatDeleteUserComparisonChatQuery)) { messageStatement.setString(1, idUser); messageStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } private void deleteChatOwner(String idUser) { try (Connection connection = databaseConnection.getConnection(); PreparedStatement messageStatement = connection.prepareStatement(deleteChatOwnerQuery)) { messageStatement.setString(1, idUser); messageStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } @Override public boolean deleteById(String id) { try (Connection connection = databaseConnection.getConnection(); PreparedStatement userStatement = connection.prepareStatement(deleteUserByIdQuery)) { deleteUserComparisonChat(id); deleteChatOwner(id); userStatement.setString(1, id); int rowsAffected = userStatement.executeUpdate(); return rowsAffected > 0; } catch (SQLException e) { e.printStackTrace(); } return false; } @Override public void save(User user) { try (Connection connection = databaseConnection.getConnection(); PreparedStatement userStatement = connection.prepareStatement(userInsertQuery)) { userStatement.setString(1, user.id()); userStatement.setString(2, user.username()); userStatement.setString(3, user.password_hash()); userStatement.setShort(4, user.age()); userStatement.setBoolean(5, user.is_online()); userStatement.setFloat(6, user.mood()); userStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } @Override public List<User> getAllChatThisUser(String idUser) { List<Chat> chatList = new ArrayList<>(); try (Connection connection = databaseConnection.getConnection(); PreparedStatement chatStatement = connection.prepareStatement(findAllChatThisUserQuery); PreparedStatement userStatement = connection.prepareStatement(findChatsByIdQuery)) { chatStatement.setString(1, idUser); ResultSet chatResultSet = chatStatement.executeQuery(); while (chatResultSet.next()) { String chatId = chatResultSet.getString("chat_id"); userStatement.setString(1, chatId); ResultSet userResultSet = userStatement.executeQuery(); if (userResultSet.next()) { Chat chat = new Chat( userResultSet.getString("id"), userResultSet.getString("name"), userResultSet.getTimestamp("date_create"), userResultSet.getString("ownerid") ); chatList.add(chat); } } } catch (SQLException e) { e.printStackTrace(); } return List.of(); } - Assistant
- User
CREATE DATABASE DB_HOMEWORK;
CREATE TABLE IF NOT EXISTS user_data ( id VARCHAR(36) PRIMARY KEY, username VARCHAR(255) NOT NULL UNIQUE, password_hash VARCHAR(255) NOT NULL, age SMALLINT NOT NULL, is_online boolean NOT NULL, mood REAL ); CREATE TABLE IF NOT EXISTS chat_data ( id VARCHAR(36) PRIMARY KEY, name VARCHAR(255) NOT NULL UNIQUE, date_create timestamp, ownerId VARCHAR(36), FOREIGN KEY (ownerId) REFERENCES user_data(id) );
CREATE TABLE IF NOT EXISTS user_chat( user_id VARCHAR(36), chat_id VARCHAR(36), FOREIGN KEY (user_id) REFERENCES user_data(id), FOREIGN KEY (chat_id) REFERENCES chat_data(id) ); Добавь сюда в нужные места каскадное удаление
- Assistant
To add cascading deletes to your database schema, you will need to modify the foreign key constraints in your tables to include
ON DELETE CASCADE. This will ensure that when a user is deleted, any associated entries in thechat_dataanduser_chattables will also be automatically removed.Here's how you can update your SQL schema to include cascading deletes:
CREATE DATABASE DB_HOMEWORK; CREATE TABLE IF NOT EXISTS user_data ( id VARCHAR(36) PRIMARY KEY, username VARCHAR(255) NOT NULL UNIQUE, password_hash VARCHAR(255) NOT NULL, age SMALLINT NOT NULL, is_online BOOLEAN NOT NULL, mood REAL ); CREATE TABLE IF NOT EXISTS chat_data ( id VARCHAR(36) PRIMARY KEY, name VARCHAR(255) NOT NULL UNIQUE, date_create TIMESTAMP, ownerId VARCHAR(36), FOREIGN KEY (ownerId) REFERENCES user_data(id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS user_chat( user_id VARCHAR(36), chat_id VARCHAR(36), FOREIGN KEY (user_id) REFERENCES user_data(id) ON DELETE CASCADE, FOREIGN KEY (chat_id) REFERENCES chat_data(id) ON DELETE CASCADE );