Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
327066e
Update pom.xml
Siyuan106 Feb 9, 2025
ebc8637
Update DatabaseController.java
Siyuan106 Feb 9, 2025
4b23807
Update Login.vue
Liuwf4319 Feb 11, 2025
2d7b756
Update MainContent.vue
Liuwf4319 Feb 11, 2025
fb6f194
Update SpringController.java
Liuwf4319 Feb 11, 2025
6e5ff36
Update DatabaseController.java
Liuwf4319 Feb 11, 2025
3156d33
Update SpringController.java
Liuwf4319 Feb 11, 2025
22ddcbc
Update User.java
Liuwf4319 Feb 11, 2025
5f73074
Update MainContent.vue
Liuwf4319 Feb 11, 2025
e0a552a
Update MainContent.vue
Liuwf4319 Feb 12, 2025
b414a90
feature: update pom.xml
Siyuan106 Feb 17, 2025
03c65e4
Create ChatEntity.java
Siyuan106 Feb 17, 2025
cc3ae54
Update ChatEntity.java
Siyuan106 Feb 17, 2025
f6bbf85
Add files via upload
Siyuan106 Feb 17, 2025
b8fe668
Create UserRepository.java
Siyuan106 Feb 17, 2025
f2a39df
Create ChatRepository.java
Siyuan106 Feb 17, 2025
f62f40a
feature: update AiLearningToolApplication.java
Siyuan106 Feb 17, 2025
f93bee0
feature: update application.properties
Siyuan106 Feb 17, 2025
a9f6bdb
feature: update SpringController.java
Siyuan106 Feb 17, 2025
5286ce3
feature: update DatabaseController.java
Siyuan106 Feb 17, 2025
2bb11ee
Update DatabaseController.java
Siyuan106 Feb 22, 2025
7a7f3db
Update SpringController.java
Siyuan106 Feb 22, 2025
b317c97
Update application.properties
Siyuan106 Feb 22, 2025
f11d14c
Update UserEntity.java
Liuwf4319 Feb 22, 2025
6d658b0
Update UserEntity.java
Liuwf4319 Feb 22, 2025
202599f
Update UserTest.java
Liuwf4319 Feb 22, 2025
8d6cde8
Update ChatTest.java
Liuwf4319 Feb 22, 2025
9104f67
Update SpringControllerTest.java
Liuwf4319 Feb 22, 2025
3cd5471
Update DatabaseControllerTest.java
Liuwf4319 Feb 22, 2025
722e959
chore: Update the architecture diagram.
vladislav-k1 Feb 24, 2025
c660fef
Update Login.vue
Liuwf4319 Feb 24, 2025
7c8cb12
Update App.vue
Liuwf4319 Feb 24, 2025
0c7ea2e
Update App.vue
Liuwf4319 Feb 24, 2025
70edb07
Update MainContent.vue
Liuwf4319 Feb 24, 2025
1bf9b8f
Update DatabaseControllerTest.java
Liuwf4319 Feb 24, 2025
5952501
Update SpringControllerTest.java
Liuwf4319 Feb 24, 2025
bb28896
Update SpringControllerTest.java
Liuwf4319 Feb 24, 2025
c08b49f
Update ChatEntity.java
Liuwf4319 Feb 24, 2025
5ce3f20
Update UserEntity.java
Liuwf4319 Feb 24, 2025
6a82fae
Update ChatEntity.java
Liuwf4319 Feb 24, 2025
832292c
Update SpringController.java
Liuwf4319 Feb 24, 2025
ddfa48c
Update UserRepository.java
Liuwf4319 Feb 24, 2025
57b4348
Update ChatRepository.java
Liuwf4319 Feb 24, 2025
b115261
Update SpringController.java
Siyuan106 Feb 27, 2025
1249afd
Update MainContent.vue
Siyuan106 Feb 27, 2025
4d57285
Update MainView.vue
Siyuan106 Mar 2, 2025
cd2af63
Update Login.vue
Siyuan106 Mar 2, 2025
c5c8cf9
Update MainContent.vue
Siyuan106 Mar 2, 2025
8167aba
Update SpringController.java
Siyuan106 Mar 2, 2025
3082ed8
Create language.js
Liuwf4319 Mar 3, 2025
790fefa
Update UserEntity.java
Liuwf4319 Mar 3, 2025
6a4d84e
Update HistorySidebar.vue
Siyuan106 Mar 3, 2025
b9e967c
Update User.java
Liuwf4319 Mar 3, 2025
b4bd47f
Update Login.vue
Siyuan106 Mar 3, 2025
4467a18
Update DatabaseController.java
Liuwf4319 Mar 3, 2025
a4e2437
Update main.js
Liuwf4319 Mar 3, 2025
ac4fe7d
Update App.vue
Liuwf4319 Mar 3, 2025
d2c243b
Update HistorySidebar.vue
Siyuan106 Mar 3, 2025
ebd12f6
Update HistorySidebar.vue
Siyuan106 Mar 3, 2025
71639a7
Update DatabaseController.java
Siyuan106 Mar 3, 2025
621d63b
Update ChatEntity.java
Liuwf4319 Mar 3, 2025
df534d1
Update ChatEntity.java
Siyuan106 Mar 3, 2025
4763392
Merge branch 'dev' into feature/issue-184/create-sqlite-database-to-s…
Siyuan106 Mar 3, 2025
55c2b8d
Update SpringControllerTest.java
Liuwf4319 Mar 3, 2025
fa84395
Update DatabaseControllerTest.java
Liuwf4319 Mar 3, 2025
8018fa3
Update ChatTest.java
Liuwf4319 Mar 3, 2025
3d186a2
Update Chat.java
Liuwf4319 Mar 3, 2025
7f762cf
Update SpringControllerTest.java
Liuwf4319 Mar 3, 2025
c8a9b3c
Update DatabaseControllerTest.java
Liuwf4319 Mar 3, 2025
ceb336d
Update ChatTest.java
Liuwf4319 Mar 3, 2025
4faf030
Update MainView.vue
RainBOY-ZZX Mar 5, 2025
0c3fb66
Update ChatEntity.java
Liuwf4319 Mar 11, 2025
4c3adaa
Update UserEntity.java
Liuwf4319 Mar 11, 2025
d4afb9d
Update ChatRepository.java
Liuwf4319 Mar 11, 2025
e3e3c63
Update UserRepository.java
Liuwf4319 Mar 11, 2025
3bdb047
Update DatabaseController.java
Liuwf4319 Mar 11, 2025
f1681f5
Update SpringController.java
Liuwf4319 Mar 11, 2025
75519a8
Update DatabaseControllerTest.java
Liuwf4319 Mar 11, 2025
9e39296
Update MainView.vue
Liuwf4319 Mar 11, 2025
e7704fe
Update MainContent.vue
Liuwf4319 Mar 11, 2025
7f4afa7
Update Login.vue
Liuwf4319 Mar 11, 2025
fd4e87a
Merge branch 'dev' into feature/issue-184/create-sqlite-database-to-s…
Liuwf4319 Mar 11, 2025
a1cbde3
Update StringTools.java
Liuwf4319 Mar 11, 2025
56c5043
Update HistorySidebar.vue
Liuwf4319 Mar 11, 2025
f796225
Update SpringController.java
Liuwf4319 Mar 12, 2025
a8e2e92
Update MainContent.vue
Liuwf4319 Mar 12, 2025
e4c2608
Update HistorySidebar.vue
Liuwf4319 Mar 12, 2025
2c4b66a
Update Login.vue
Liuwf4319 Mar 14, 2025
317dc6f
Update SpringController.java
Liuwf4319 Mar 14, 2025
8ab7a35
Update SpringController.java
Liuwf4319 Mar 17, 2025
3aad4d6
Update Chat.java
Liuwf4319 Mar 17, 2025
6340d00
Update DatabaseController.java
Liuwf4319 Mar 17, 2025
f3f5866
Update MainContent.vue
Liuwf4319 Mar 17, 2025
e2885a6
Merge branch 'dev' into feature/issue-184/create-sqlite-database-to-s…
Liuwf4319 Mar 17, 2025
dfbd580
Merge branch 'dev' into feature/issue-184/create-sqlite-database-to-s…
Siyuan106 Mar 19, 2025
b154648
fix: Change chat structure to JSONArray in the ChatEntity class.
vladislav-k1 Mar 19, 2025
7568821
Merge branch 'feature/issue-184/create-sqlite-database-to-store-user-…
vladislav-k1 Mar 19, 2025
29f13f3
fix: Refactor /createChat and /sendMessage backend API methods.
vladislav-k1 Mar 19, 2025
095e3de
Refactor SpringController for DB compatibility.
vladislav-k1 Mar 21, 2025
d707396
fix: Delete remnants of axios that prevented frontend from building.
vladislav-k1 Mar 23, 2025
8b9fcc5
fix: Migrate from 'User' and 'Chat' classes to '*Entity' classes.
vladislav-k1 Mar 24, 2025
2728331
fix: Add database support, migrate from 'username' to 'sessionID' aut…
vladislav-k1 Apr 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,34 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.42.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-community-dialects</artifactId>
<version>6.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.13.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,79 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import java.io.IOException;
import java.nio.file.*;
import java.util.Arrays;
import java.util.List;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, SecurityAutoConfiguration.class })
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
@EnableJpaRepositories("com.UoB.AILearningTool.repository")
public class AiLearningToolApplication implements CommandLineRunner {

public static void main(String[] args) {
// disable SSL if launched without keystore.p12
if (!Files.exists(Paths.get("src/main/resources/keystore.p12"))) {
// Force disable SSL
System.setProperty("server.ssl.enabled", "false");
}
SpringApplication.run(AiLearningToolApplication.class, args);
}
public static void main(String[] args) {
// disable SSL if launched without keystore.p12
if (!Files.exists(Paths.get("src/main/resources/keystore.p12"))) {
// Force disable SSL
System.setProperty("server.ssl.enabled", "false");
}
SpringApplication.run(AiLearningToolApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
Path keystorePath = Paths.get("src", "main", "resources", "keystore.p12");
@Override
public void run(String... args) throws Exception {
Path keystorePath = Paths.get("src", "main", "resources", "keystore.p12");

// If there's no keystore in the working directory, skip the SSL setup.
if (!Files.exists(keystorePath)) {
System.out.println("No keystore.p12 found in working directory; skipping SSL copy/overwrite");
return;
}
// If there's no keystore in the working directory, skip the SSL setup.
if (!Files.exists(keystorePath)) {
System.out.println("No keystore.p12 found in working directory; skipping SSL copy/overwrite");
return;
}

Path applicationProps = Paths.get("src", "main", "resources", "application.properties");
overwriteApplicationProperties(applicationProps);
}

// Otherwise, proceed with the copy and overwrite
Path resourcesDir = Paths.get("src", "main", "resources");
Path targetKeystore = resourcesDir.resolve("keystore.p12");
copyFile(keystorePath, targetKeystore);
System.out.println("keystore.p12 copied to src/main/resources successfully");

void copyFile(Path source, Path target) throws IOException {
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
}
Path applicationProps = Paths.get("src", "main", "resources", "application.properties");
overwriteApplicationProperties(applicationProps);
System.out.println("application.properties has been updated successfully");
}

private void overwriteApplicationProperties(Path propsFile) throws IOException {
List<String> lines = Arrays.asList(
"spring.application.name=AILearningTool",
"server.port=8080",
"spring.servlet.multipart.max-file-size=50MB",
"spring.servlet.multipart.max-request-size=50MB",
"spring.web.resources.static-locations=classpath:/static/",
"",
"spring.http.encoding.charset=UTF-8",
"spring.http.encoding.enabled=true",
"spring.http.encoding.force=true",
"",
"server.ssl.key-store=classpath:keystore.p12",
"server.ssl.key-store-password=ailearntool",
"server.ssl.key-store-type=PKCS12",
"server.ssl.key-alias=myalias"
);
System.out.println("application.properties has been updated successfully");
Files.write(propsFile, lines, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE );
}

}
private void copyFile(Path source, Path target) throws IOException {
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
}

private void overwriteApplicationProperties(Path propsFile) throws IOException {
List<String> lines = Arrays.asList(
"spring.application.name=AILearningTool",
"server.port=8080",
"spring.servlet.multipart.max-file-size=50MB",
"spring.servlet.multipart.max-request-size=50MB",
"spring.web.resources.static-locations=classpath:/static/",
"",
"spring.http.encoding.charset=UTF-8",
"spring.http.encoding.enabled=true",
"spring.http.encoding.force=true",
"",
"server.ssl.key-store=classpath:keystore.p12",
"server.ssl.key-store-password=ailearntool",
"server.ssl.key-store-type=PKCS12",
"server.ssl.key-alias=myalias",

"spring.datasource.url=jdbc:sqlite:database.db",
"spring.datasource.driver-class-name=org.sqlite.JDBC",
"spring.datasource.username=",
"spring.datasource.password=",
"spring.jpa.database-platform=org.hibernate.dialect.SQLiteDialect",
"spring.jpa.hibernate.ddl-auto=update"
);
Files.write(propsFile, lines, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
}
}
49 changes: 0 additions & 49 deletions backend/src/main/java/com/UoB/AILearningTool/Chat.java

This file was deleted.

114 changes: 72 additions & 42 deletions backend/src/main/java/com/UoB/AILearningTool/DatabaseController.java
Original file line number Diff line number Diff line change
@@ -1,72 +1,102 @@
package com.UoB.AILearningTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.UoB.AILearningTool.model.ChatEntity;
import com.UoB.AILearningTool.model.UserEntity;
import com.UoB.AILearningTool.repository.ChatRepository;
import com.UoB.AILearningTool.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

// Communication with SQL database.
@Service
public class DatabaseController {
private Map<String, User> users = new HashMap<>();
private Map<String, Chat> chats = new HashMap<>();
private final UserRepository userRepository;
private final ChatRepository chatRepository;

public User getUser(String userID) {
return users.get(userID);
@Autowired
public DatabaseController(UserRepository userRepository, ChatRepository chatRepository) {
this.userRepository = userRepository;
this.chatRepository = chatRepository;
}

public DatabaseController() {
// TODO: Connect to a MariaDB database.
public UserEntity getUser(String username) {
return userRepository.findById(username).orElse(null);
}

// Create a new user and return their ID for cookie assignment
public String addUser(boolean optionalConsent) {
User user = new User(optionalConsent);
String id = user.getID();
// TODO: Add a user profile record to the MariaDB database.
users.put(id, user);
return id;
// Create a new account
public boolean addUser(String username, String password) {
if (userRepository.existsById(username)) {
return false;
}

UserEntity user = new UserEntity(username, password);
userRepository.save(user);
return true;
}

// Remove all data stored about the user (profile, chat, etc.)
public boolean removeUser(String id) {
// TODO: Remove a user profile record from the MariaDB database.
if (users.containsKey(id)) {
users.remove(id);
// Delete existing user account
public boolean removeUser(String sessionID) {
Optional<UserEntity> user = userRepository.findBySessionID(sessionID);

// TODO: Delete chats as well

if (user.isPresent()) {
userRepository.delete(user.get());
return true;
} else {return false;}
} else {
return false;
}
}

// Creates a new chat
public String createChat(User user, String initialMessage, String threadID) {
public String createChat(UserEntity user, String initialMessage, String threadID) {
String id = StringTools.RandomString(20);
chats.put(id, new Chat(user, initialMessage, threadID));
return id;
ChatEntity chat = new ChatEntity(user, initialMessage, threadID);
chatRepository.save(chat);
return chat.getChatID();
}


// Deletes an existing chat
public Boolean deleteChat(User user, String chatID) {
Boolean success;
Chat chat = chats.get(chatID);
if (chat != null) {
if (chat.checkOwner(user)) {
chats.remove(chatID);
return true;
}
public boolean deleteChat(UserEntity user, String chatID) {
Optional<ChatEntity> chatOpt = chatRepository.findById(chatID);
if (chatOpt.isPresent() && chatOpt.get().getOwner().getUsername().equals(user.getUsername())) {
chatRepository.deleteById(chatID);
return true;
}
return false;
}

public Chat getChat(User user, String chatID) {
Chat chat = chats.get(chatID);
if (chat != null) {
if (chat.checkOwner(user)) {
return chat;
} else {return null;}
} else {
public ChatEntity createChat(String sessionID, String initialMessage) {
Optional<UserEntity> userOpt = userRepository.findBySessionID(sessionID);
if (userOpt.isEmpty()) {
return null;
}

UserEntity user = userOpt.get();
ChatEntity chat = new ChatEntity(user, initialMessage, sessionID);
chatRepository.save(chat);

return chat;
}

public ChatEntity getChat(String username, String chatID) {
Optional<UserEntity> userOpt = userRepository.findById(username);
if (userOpt.isEmpty()) {
return null;
}

Optional<ChatEntity> chatOpt = chatRepository.findById(chatID);
return chatOpt.filter(chat -> chat.getOwner().getUsername().equals(username)).orElse(null);
}

}
public boolean deleteChat(String username, String chatID) {
Optional<ChatEntity> chatOpt = chatRepository.findById(chatID);
if (chatOpt.isPresent() && chatOpt.get().getOwner().getUsername().equals(username)) {
chatRepository.deleteById(chatID);
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.UoB.AILearningTool;

import com.UoB.AILearningTool.model.ChatEntity;
import org.json.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -83,7 +84,7 @@ public int runThread(String threadID) {
}

// Check if a run is in progress for a thread
public boolean isLocked(Chat chat) {
public boolean isLocked(ChatEntity chat) {
HttpRequest listMessagesRequest = HttpRequest
.newBuilder(URI.create("https://api.openai.com/v1/threads/" + chat.getThreadID() + "/runs"))
.headers("Content-Type", "application/json",
Expand Down Expand Up @@ -138,7 +139,7 @@ public WatsonxResponse getLastThreadMessage(String threadID) {
}

// Add a message to an OpenAI thread.
public Integer sendUserMessage(Chat chat, String message) {
public Integer sendUserMessage(ChatEntity chat, String message) {
// Creating a request body
JSONObject newMessage = new JSONObject();
newMessage.put("role", "user");
Expand Down
Loading