feat: implement database entities, repository, controllers, and data seeder for Notes & PYQs module
This commit is contained in:
113
backend/src/main/java/com/rit/portal/config/DataInitializer.java
Normal file
113
backend/src/main/java/com/rit/portal/config/DataInitializer.java
Normal file
@@ -0,0 +1,113 @@
|
||||
package com.rit.portal.config;
|
||||
|
||||
import com.rit.portal.entity.NotePyq;
|
||||
import com.rit.portal.repository.NotePyqRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
|
||||
@Component
|
||||
public class DataInitializer implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private NotePyqRepository noteRepository;
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
if (noteRepository.count() == 0) {
|
||||
noteRepository.saveAll(Arrays.asList(
|
||||
NotePyq.builder()
|
||||
.title("Engineering Mathematics – Unit 1-5")
|
||||
.subject("Mathematics")
|
||||
.department("Computer Science & Engineering")
|
||||
.semester(1)
|
||||
.fileType("notes")
|
||||
.downloadUrl("#")
|
||||
.fileSize("4.2 MB")
|
||||
.downloadsCount(348)
|
||||
.uploadedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
NotePyq.builder()
|
||||
.title("Physics PYQ 2020-2024")
|
||||
.subject("Physics")
|
||||
.department("Computer Science & Engineering")
|
||||
.semester(1)
|
||||
.fileType("pyq")
|
||||
.downloadUrl("#")
|
||||
.fileSize("8.1 MB")
|
||||
.downloadsCount(512)
|
||||
.uploadedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
NotePyq.builder()
|
||||
.title("C Programming Complete Notes")
|
||||
.subject("Programming")
|
||||
.department("Computer Science & Engineering")
|
||||
.semester(1)
|
||||
.fileType("notes")
|
||||
.downloadUrl("#")
|
||||
.fileSize("6.3 MB")
|
||||
.downloadsCount(734)
|
||||
.uploadedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
NotePyq.builder()
|
||||
.title("Data Structures PYQ 2019-2024")
|
||||
.subject("Data Structures")
|
||||
.department("Computer Science & Engineering")
|
||||
.semester(3)
|
||||
.fileType("pyq")
|
||||
.downloadUrl("#")
|
||||
.fileSize("10.2 MB")
|
||||
.downloadsCount(621)
|
||||
.uploadedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
NotePyq.builder()
|
||||
.title("Circuit Theory Full Notes")
|
||||
.subject("Circuit Theory")
|
||||
.department("Electronics & Communication")
|
||||
.semester(2)
|
||||
.fileType("notes")
|
||||
.downloadUrl("#")
|
||||
.fileSize("5.7 MB")
|
||||
.downloadsCount(289)
|
||||
.uploadedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
NotePyq.builder()
|
||||
.title("Anna University Syllabus 2021")
|
||||
.subject("Syllabus")
|
||||
.department("All Departments")
|
||||
.semester(1)
|
||||
.fileType("syllabus")
|
||||
.downloadUrl("#")
|
||||
.fileSize("2.1 MB")
|
||||
.downloadsCount(890)
|
||||
.uploadedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
NotePyq.builder()
|
||||
.title("Thermodynamics Notes")
|
||||
.subject("Thermodynamics")
|
||||
.department("Mechanical Engineering")
|
||||
.semester(3)
|
||||
.fileType("notes")
|
||||
.downloadUrl("#")
|
||||
.fileSize("3.9 MB")
|
||||
.downloadsCount(201)
|
||||
.uploadedAt(LocalDateTime.now())
|
||||
.build(),
|
||||
NotePyq.builder()
|
||||
.title("Digital Electronics PYQ")
|
||||
.subject("Digital Electronics")
|
||||
.department("Electronics & Communication")
|
||||
.semester(4)
|
||||
.fileType("pyq")
|
||||
.downloadUrl("#")
|
||||
.fileSize("7.3 MB")
|
||||
.downloadsCount(445)
|
||||
.uploadedAt(LocalDateTime.now())
|
||||
.build()
|
||||
));
|
||||
System.out.println("🌱 Database successfully seeded with Notes & PYQs test data!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.rit.portal.controller;
|
||||
|
||||
import com.rit.portal.entity.NotePyq;
|
||||
import com.rit.portal.repository.NotePyqRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/notes")
|
||||
@CrossOrigin(origins = "*") // CrossOrigin configured globally, but added here for safety
|
||||
public class NotePyqController {
|
||||
|
||||
@Autowired
|
||||
private NotePyqRepository noteRepository;
|
||||
|
||||
// Get all notes
|
||||
@GetMapping
|
||||
public List<NotePyq> getAllNotes() {
|
||||
return noteRepository.findAll();
|
||||
}
|
||||
|
||||
// Get notes by semester
|
||||
@GetMapping("/semester/{semester}")
|
||||
public List<NotePyq> getNotesBySemester(@PathVariable Integer semester) {
|
||||
return noteRepository.findBySemester(semester);
|
||||
}
|
||||
|
||||
// Get notes by department
|
||||
@GetMapping("/department")
|
||||
public List<NotePyq> getNotesByDepartment(@RequestParam String dept) {
|
||||
return noteRepository.findByDepartment(dept);
|
||||
}
|
||||
|
||||
// Add a new note metadata
|
||||
@PostMapping
|
||||
public NotePyq createNote(@RequestBody NotePyq note) {
|
||||
return noteRepository.save(note);
|
||||
}
|
||||
|
||||
// Increment downloads count
|
||||
@PostMapping("/{id}/download")
|
||||
public ResponseEntity<Void> incrementDownloads(@PathVariable Long id) {
|
||||
return noteRepository.findById(id).map(note -> {
|
||||
note.setDownloadsCount(note.getDownloadsCount() + 1);
|
||||
note.setFileType(note.getFileType()); // Keep dirty check
|
||||
noteRepository.save(note);
|
||||
return ResponseEntity.ok().<Void>build();
|
||||
}).orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
}
|
||||
46
backend/src/main/java/com/rit/portal/entity/NotePyq.java
Normal file
46
backend/src/main/java/com/rit/portal/entity/NotePyq.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.rit.portal.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Table(name = "notes_pyqs")
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class NotePyq {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String title;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String subject;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String department;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Integer semester;
|
||||
|
||||
@Column(name = "file_type", nullable = false)
|
||||
private String fileType; // 'notes', 'pyq', 'syllabus', 'assignment'
|
||||
|
||||
@Column(name = "download_url", nullable = false)
|
||||
private String downloadUrl;
|
||||
|
||||
@Column(name = "file_size", nullable = false)
|
||||
private String fileSize;
|
||||
|
||||
@Column(name = "downloads_count")
|
||||
private Integer downloadsCount = 0;
|
||||
|
||||
@Column(name = "uploaded_at")
|
||||
private LocalDateTime uploadedAt = LocalDateTime.now();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.rit.portal.repository;
|
||||
|
||||
import com.rit.portal.entity.NotePyq;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface NotePyqRepository extends JpaRepository<NotePyq, Long> {
|
||||
List<NotePyq> findBySemester(Integer semester);
|
||||
List<NotePyq> findByDepartment(String department);
|
||||
List<NotePyq> findByFileType(String fileType);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
# ─── DATABASE CONNECTION CONFIGURATION ───
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/rit_freshers_hub?sslmode=disable
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=your_local_postgres_password_here
|
||||
spring.datasource.password=Amudiesh22@.
|
||||
|
||||
# ─── JPA / HIBERNATE SETTINGS ───
|
||||
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
|
||||
|
||||
Reference in New Issue
Block a user