diff --git a/pom.xml b/pom.xml index ca97d96..e8de942 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,10 @@ org.springframework.ai spring-ai-ollama-spring-boot-starter + + org.springframework.boot + spring-boot-starter-data-jpa + diff --git a/public/Videos/Concept of Valency _ Atoms and Molecules _ Don t Memorise.mp4 b/public/Videos/Concept of Valency _ Atoms and Molecules _ Don t Memorise.mp4 new file mode 100644 index 0000000..a52c4ce Binary files /dev/null and b/public/Videos/Concept of Valency _ Atoms and Molecules _ Don t Memorise.mp4 differ diff --git a/public/Videos/Isotopes and Isobars _ Atoms and Molecules _ Don t Memorise.mp4 b/public/Videos/Isotopes and Isobars _ Atoms and Molecules _ Don t Memorise.mp4 new file mode 100644 index 0000000..7d53f22 Binary files /dev/null and b/public/Videos/Isotopes and Isobars _ Atoms and Molecules _ Don t Memorise.mp4 differ diff --git a/src/main/java/com/example/treaders/Application.java b/src/main/java/com/example/treaders/Application.java index bb97431..674a38e 100644 --- a/src/main/java/com/example/treaders/Application.java +++ b/src/main/java/com/example/treaders/Application.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication +@EnableJpaRepositories public class Application { public static void main(String[] args) { diff --git a/src/main/java/com/example/treaders/DataBaseConnection/JdbcClientRepository.java b/src/main/java/com/example/treaders/DataBaseConnection/JdbcClientRepository.java deleted file mode 100644 index 7ba31d4..0000000 --- a/src/main/java/com/example/treaders/DataBaseConnection/JdbcClientRepository.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.example.treaders.DataBaseConnection; - -import com.example.treaders.videoFormat.VideoFormat; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.stereotype.Repository; - -import java.util.List; - -@Repository -public class JdbcClientRepository { - private static final Logger log = LoggerFactory.getLogger(JdbcClientRepository.class); - private final JdbcTemplate jdbcTemplate; - - public JdbcClientRepository(JdbcTemplate jdbcTemplate) { - this.jdbcTemplate = jdbcTemplate; - } - - public Boolean authenticate(String username, String password) { - String sql = "SELECT COUNT(*) FROM user WHERE username = ?"; - int count = jdbcTemplate.queryForObject(sql, Integer.class, username); - if(count == 0){ - return false; - } - sql = "SELECT password FROM user WHERE username = ?"; - String storedPassword = jdbcTemplate.queryForObject(sql, String.class, username); - return storedPassword.equals(password); - } - - public void addUser(String username, String password) { - String sql = "INSERT INTO user (username, password) VALUES (?, ?)"; - jdbcTemplate.update(sql, username, password); - } - - public List getAllVideoPath() { - String sql = "SELECT v.title, v.description, v.file_path, u.username " + - "FROM videos v " + - "INNER JOIN user u ON v.uploaded_by = u.id"; - return jdbcTemplate.query(sql,(vs,rowNum) -> new VideoFormat( - vs.getString("title"), - vs.getString("description"), - vs.getString("file_path"), - vs.getString("username") - )); - } - - public void saveVideoIntoDatabase(String title, String description, String filePath, String username) { - // First, query the user table to find the id of the user - String getUserIdSql = "SELECT id FROM user WHERE username = ?"; - Integer userId = jdbcTemplate.queryForObject(getUserIdSql, Integer.class, username); - - if (userId != null) { - // If userId is not null (user found), proceed to insert the video details - String sql = "INSERT INTO videos (title, description, file_path, uploaded_by) VALUES (?, ?, ?, ?)"; - jdbcTemplate.update(sql, title, description, filePath, userId); - } else { - // Handle the case where the user is not found - throw new RuntimeException("User not found for username: " + username); - } - } - -} diff --git a/src/main/java/com/example/treaders/controller/AllController.java b/src/main/java/com/example/treaders/controller/AllController.java index a4cad31..16c96fe 100644 --- a/src/main/java/com/example/treaders/controller/AllController.java +++ b/src/main/java/com/example/treaders/controller/AllController.java @@ -1,10 +1,11 @@ package com.example.treaders.controller; -import com.example.treaders.DataBaseConnection.JdbcClientRepository; +import com.example.treaders.models.VideoFormat; +import com.example.treaders.models.UserFormat; +import com.example.treaders.services.VideoRepository; import com.example.treaders.user.InputForm; import com.example.treaders.LLM.LlamaService; -import com.example.treaders.videoFormat.VideoFormat; -import com.example.treaders.videoFormat.VideoService; +import com.example.treaders.services.VideoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -12,6 +13,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; +import com.example.treaders.services.UserRepository; import java.io.IOException; import java.util.List; @@ -19,17 +21,23 @@ @Controller public class AllController { - private final JdbcClientRepository jdbcClientRepository; + + @Autowired + private UserRepository UserRepo; + + @Autowired + private VideoRepository VideoRepo; + @Autowired private LlamaService llamaService; + private boolean UserLoggedIn = false; + private String UserName; + @Autowired private VideoService videoService; - public AllController(JdbcClientRepository jdbcClientRepository) { - this.jdbcClientRepository = jdbcClientRepository; - } @GetMapping("/") public String Login(){ @@ -39,7 +47,7 @@ public String Login(){ @GetMapping("/home") public String home(Model model){ if(UserLoggedIn){ - List videos = jdbcClientRepository.getAllVideoPath(); + List videos = VideoRepo.findAll(); model.addAttribute("videos", videos); model.addAttribute("username", UserName); return "welcome"; @@ -49,8 +57,9 @@ public String home(Model model){ @PostMapping("/home") public String authenticate(@RequestParam String username, @RequestParam String password, Model model){ - if(jdbcClientRepository.authenticate(username, password)){ - List videos = jdbcClientRepository.getAllVideoPath(); + UserFormat user = UserRepo.findByUsername(username); + if(user!=null && user.getPassword().equals(password)){ + List videos = VideoRepo.findAll(); model.addAttribute("videos", videos); model.addAttribute("username", username); UserName=username; @@ -68,9 +77,14 @@ public String Signup(){ @PostMapping("/newuser") public String addNewUser(@RequestParam String username, @RequestParam String password){ - jdbcClientRepository.addUser(username, password); + UserFormat user=new UserFormat(); + user.setUsername(username); + user.setPassword(password); + UserRepo.save(user); return "redirect:/"; } + + @PostMapping("/logout") public String logout(){ UserLoggedIn = false; diff --git a/src/main/java/com/example/treaders/models/UserFormat.java b/src/main/java/com/example/treaders/models/UserFormat.java new file mode 100644 index 0000000..bf73a66 --- /dev/null +++ b/src/main/java/com/example/treaders/models/UserFormat.java @@ -0,0 +1,42 @@ +package com.example.treaders.models; + +import jakarta.persistence.*; + +@Entity +@Table(name = "user") +public class UserFormat { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private int id; + + @Column(unique = true, nullable = false, length = 250) + private String username; + + @Column(nullable = false, length = 250) + private String password; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/src/main/java/com/example/treaders/models/VideoFormat.java b/src/main/java/com/example/treaders/models/VideoFormat.java new file mode 100644 index 0000000..7372aa2 --- /dev/null +++ b/src/main/java/com/example/treaders/models/VideoFormat.java @@ -0,0 +1,78 @@ +package com.example.treaders.models; + +import jakarta.persistence.*; + +import java.time.LocalDateTime; + +@Entity +@Table(name = "videos") +public class VideoFormat { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private int id; + + @Column(length = 255) + private String title; + + @Column(columnDefinition = "TEXT") + private String description; + + @Column(length = 255) + private String filepath; + + @ManyToOne + @JoinColumn(name = "uploaded_by", nullable = false) + private UserFormat uploadedBy; + + @Column(name = "uploaded_at", columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") + private LocalDateTime uploadedAt=LocalDateTime.now(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getFilepath() { + return filepath; + } + + public void setFilepath(String filepath) { + this.filepath = filepath; + } + + public UserFormat getUploadedBy() { + return uploadedBy; + } + + public void setUploadedBy(UserFormat uploadedBy) { + this.uploadedBy = uploadedBy; + } + + public LocalDateTime getUploadedAt() { + return uploadedAt; + } + + public void setUploadedAt(LocalDateTime uploadedAt) { + this.uploadedAt = uploadedAt; + } +} diff --git a/src/main/java/com/example/treaders/services/UserRepository.java b/src/main/java/com/example/treaders/services/UserRepository.java new file mode 100644 index 0000000..1333fac --- /dev/null +++ b/src/main/java/com/example/treaders/services/UserRepository.java @@ -0,0 +1,10 @@ +package com.example.treaders.services; + +import com.example.treaders.models.UserFormat; +import org.springframework.data.jpa.repository.JpaRepository; + + + +public interface UserRepository extends JpaRepository { + UserFormat findByUsername(String username); +} diff --git a/src/main/java/com/example/treaders/services/VideoRepository.java b/src/main/java/com/example/treaders/services/VideoRepository.java new file mode 100644 index 0000000..0547cce --- /dev/null +++ b/src/main/java/com/example/treaders/services/VideoRepository.java @@ -0,0 +1,7 @@ +package com.example.treaders.services; + +import com.example.treaders.models.VideoFormat; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface VideoRepository extends JpaRepository { +} diff --git a/src/main/java/com/example/treaders/videoFormat/VideoService.java b/src/main/java/com/example/treaders/services/VideoService.java similarity index 52% rename from src/main/java/com/example/treaders/videoFormat/VideoService.java rename to src/main/java/com/example/treaders/services/VideoService.java index 7f99157..75344de 100644 --- a/src/main/java/com/example/treaders/videoFormat/VideoService.java +++ b/src/main/java/com/example/treaders/services/VideoService.java @@ -1,6 +1,7 @@ -package com.example.treaders.videoFormat; +package com.example.treaders.services; -import com.example.treaders.DataBaseConnection.JdbcClientRepository; +import com.example.treaders.models.VideoFormat; +import com.example.treaders.models.UserFormat; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -9,23 +10,42 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.time.LocalDateTime; @Service public class VideoService { - - private final JdbcClientRepository jdbcClientRepository; + @Autowired + private VideoRepository videoRepository; @Autowired - public VideoService(JdbcClientRepository jdbcClientRepository) { - this.jdbcClientRepository = jdbcClientRepository; - } + private UserRepository UserRepo; + + private UserFormat userFormat; +// private final JdbcClientRepository jdbcClientRepository; +// +// @Autowired +// public VideoService(JdbcClientRepository jdbcClientRepository) { +// this.jdbcClientRepository = jdbcClientRepository; +// } - public void saveVideo(MultipartFile file, String title, String description, String uploadedBy) throws IOException { + public void saveVideo(MultipartFile file, String title, String description, String UserName) throws IOException { // Save the uploaded video file to a directory String filePath = saveVideoFile(file); + UserFormat user = UserRepo.findByUsername(UserName); + + if(user==null){ + throw new IllegalArgumentException("User not found "+UserName); + } + // Save video details to the database - jdbcClientRepository.saveVideoIntoDatabase(title, description, filePath, uploadedBy); + VideoFormat videoFormat = new VideoFormat(); + videoFormat.setTitle(title); + videoFormat.setDescription(description); + videoFormat.setFilepath(filePath); + videoFormat.setUploadedBy(user); + videoFormat.setUploadedAt(LocalDateTime.now()); + videoRepository.save(videoFormat); } private String saveVideoFile(MultipartFile file) throws IOException { diff --git a/src/main/java/com/example/treaders/videoFormat/VideoFormat.java b/src/main/java/com/example/treaders/videoFormat/VideoFormat.java deleted file mode 100644 index 5df6f02..0000000 --- a/src/main/java/com/example/treaders/videoFormat/VideoFormat.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.example.treaders.videoFormat; - - -import org.springframework.lang.NonNull; - -public record VideoFormat( - @NonNull - String title, - @NonNull - String decription, - @NonNull - String filename, - @NonNull - String username -) { -} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index d9ba40a..9ec4a41 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -20,4 +20,8 @@ spring.servlet.multipart.max-request-size=30MB spring.ai.ollama.base-url=http://localhost:11434 spring.ai.ollama.chat.options.model=llama3 spring.ai.ollama.chat.options.temperature=0.4 -spring.ai.ollama.chat.options.num-gpu=1 \ No newline at end of file +spring.ai.ollama.chat.options.num-gpu=1 + + +spring.jpa.show-sql=true +spring.jpa.hibernate.ddl-auto=update \ No newline at end of file diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql deleted file mode 100644 index 7c4b69b..0000000 --- a/src/main/resources/schema.sql +++ /dev/null @@ -1,14 +0,0 @@ -CREATE TABLE IF NOT EXISTS user ( - id INT AUTO_INCREMENT PRIMARY KEY, - username VARCHAR(250) NOT NULL, - password VARCHAR(250) NOT NULL -); -CREATE TABLE IF NOT EXISTS videos ( - id INT AUTO_INCREMENT PRIMARY KEY, - title VARCHAR(255), - description TEXT, - file_path VARCHAR(255), - uploaded_by INT, - uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (uploaded_by) REFERENCES user(id) - ); diff --git a/src/main/resources/static/Query.sql b/src/main/resources/static/Query.sql index 67eb8c7..5dc8f10 100644 --- a/src/main/resources/static/Query.sql +++ b/src/main/resources/static/Query.sql @@ -24,3 +24,13 @@ UPDATE videos SET uploaded_by = 2 WHERE id = 9; DELETE FROM videos WHERE id = 23; DELETE FROM videos ORDER BY id ASC LIMIT 3; + +delete from user where id = 3; + + +ALTER TABLE videos +DROP COLUMN filepath; + + +ALTER TABLE videos +CHANGE COLUMN file_path filepath VARCHAR(255); diff --git a/src/main/resources/static/css/welcome.css b/src/main/resources/static/css/welcome.css index ea91ab7..cc9b6a0 100644 --- a/src/main/resources/static/css/welcome.css +++ b/src/main/resources/static/css/welcome.css @@ -61,7 +61,7 @@ body { background-color: #45a049; /* Darker Green */ } -.upload-button{ +.upload-button { display: block; width: 100%; padding: 10px; @@ -72,21 +72,10 @@ body { cursor: pointer; } -.upload-button:hover{ +.upload-button:hover { background-color: #45a049; } -.view-video-button{ - display: block; - width: 95%; - padding: 5px; - text-align: center; - background-color: #ff63; - color: white; - border: none; - cursor: pointer; -} - /* Style for welcome message */ h1 { font-size: 24px; @@ -108,21 +97,42 @@ h1 { border-radius: 5px; padding: 15px; background-color: #1a1a1a; /* Slightly lighter black for video item background */ + height: 500px; /* Fixed height */ + display: flex; + flex-direction: column; } -/* Style for video titles */ .video-item h3 { font-size: 24px; margin-bottom: 10px; - color: #fff; /* White text */ } -/* Style for video descriptions */ +.video-item h3 a { + color: #fff; /* White text for links */ + text-decoration: none; /* Optional: Remove underline from links */ +} + +.video-item h3 a:hover { + color: #ccc; /* Optional: Change color on hover */ +} + +.video-item video { + max-width: 100%; + margin-bottom: 10px; +} + .video-item p { margin-top: 10px; color: #ccc; /* Light grey text */ } +/* Style for video descriptions */ +.video-item .description { + flex-grow: 1; + overflow: auto; + margin-top: 10px; +} + /* Style for uploaded by text */ .video-item p span { color: #fff; /* White text for uploader name */ diff --git a/src/main/resources/templates/welcome.html b/src/main/resources/templates/welcome.html index c6d9478..007847f 100644 --- a/src/main/resources/templates/welcome.html +++ b/src/main/resources/templates/welcome.html @@ -30,15 +30,15 @@

TechEdu

-

+

-

-

Uploaded by:

- View Video +

+

Uploaded by:

+

Uploaded On: