Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 8 additions & 23 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
name: Spring Boot Student Management App CI/CD
name: Spring Boot CI/CD

on:
push:
branches: [ "main", "dev" ]
branches: [ "dev", "feature/*", "fix/*" ]
pull_request:
branches: [ "main" ]
branches: [ "main", "dev" ]

jobs:
build-and-test:
runs-on: ubuntu-latest

# Spin up Postgres for integration tests
services:
postgres:
image: postgres:latest
Expand All @@ -27,33 +26,19 @@ jobs:
--health-retries 5

steps:
- name: Checkout code
uses: actions/checkout@v4
- uses: actions/checkout@v4

- name: Set up JDK 21 (matches your pom.xml)
- name: Setup Java 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
cache: maven

- name: Make Maven Wrapper executable
run: chmod +x mvnw

- name: Run Tests with Maven Wrapper
run: ./mvnw clean test
- name: Test & Build
run: ./mvnw clean verify
env:
# Match your application.properties/docker-compose settings
SPRING_DATASOURCE_URL: jdbc:postgresql://localhost:5432/smsdb_test
SPRING_DATASOURCE_USERNAME: admin
SPRING_DATASOURCE_PASSWORD: pass123
SPRING_JPA_HIBERNATE_DDL_AUTO: update
SPRING_JPA_SHOW_SQL: false

- name: Build JAR with Maven Wrapper
run: ./mvnw clean package -DskipTests

- name: Verify JAR file
run: |
ls -la target/*.jar
echo "Build completed successfully!"
SPRING_JPA_HIBERNATE_DDL_AUTO: update
3 changes: 1 addition & 2 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
postgres:
image: 'postgres:latest'
container_name: sms_DB
container_name: sms_db
environment:
- 'POSTGRES_DB=smsdb'
- 'POSTGRES_PASSWORD=pass123'
Expand All @@ -10,7 +10,6 @@ services:
- '5432:5432'
volumes:
- postgres_data:/var/lib/postgresql
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
networks:
- student-network
healthcheck:
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<!-- Security -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public PasswordEncoder passwordEncoder() {
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/", "/login", "/register/teacher/**").permitAll()
.requestMatchers("/", "/login", "/register/teacher/**", "/css/**").permitAll()
.requestMatchers("/student/**").hasRole("STUDENT")
.requestMatchers("/teacher/**").hasRole("TEACHER")
.anyRequest().authenticated()
)
.formLogin(form -> form
.loginPage("/login")
.defaultSuccessUrl("/dashboard")
.defaultSuccessUrl("/dashboard", true)
.permitAll()
)
.logout(logout -> logout
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.shuvocse21.StudentManagementApp.controller;

import com.shuvocse21.StudentManagementApp.entity.Department;
import com.shuvocse21.StudentManagementApp.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.List;

@Controller
@RequiredArgsConstructor
public class AuthController {

private final UserService userService;
private final com.shuvocse21.StudentManagementApp.repository.DepartmentRepository departmentRepository;

@GetMapping("/")
public String home() {
Expand All @@ -30,33 +28,34 @@ public String login() {

@GetMapping("/dashboard")
public String dashboard() {
return "dashboard";
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth.isAuthenticated() && !"anonymousUser".equals(auth.getPrincipal())) {
String role = auth.getAuthorities().iterator().next().getAuthority();
if (role.equals("ROLE_STUDENT")) {
return "redirect:/student/dashboard";
} else if (role.equals("ROLE_TEACHER")) {
return "redirect:/teacher/dashboard";
}
}
return "redirect:/login";
}

// TEACHER REGISTRATION ENDPOINTS
@GetMapping("/register/teacher")
public String registerTeacher(Model model) {
List<Department> departments = departmentRepository.findAll();
model.addAttribute("departments", departments);
public String registerTeacher() {
return "register-teacher";
}

@PostMapping("/register/teacher")
public String registerTeacherUser(
@RequestParam String username,
@RequestParam String password,
@RequestParam String email,
@RequestParam String employeeId,
@RequestParam Long departmentId,
RedirectAttributes redirectAttributes) {

public String registerTeacher(@RequestParam String username, @RequestParam String password,
@RequestParam String email, @RequestParam String employeeId,
RedirectAttributes redirectAttributes) {
try {
userService.registerTeacher(username, password, email, employeeId, departmentId);
redirectAttributes.addFlashAttribute("success", "Teacher registration successful! Please login.");
return "redirect:/login";
userService.registerTeacher(username, password, email, employeeId);
redirectAttributes.addFlashAttribute("success", "Registration successful! Please login.");
} catch (RuntimeException e) {
redirectAttributes.addFlashAttribute("error", e.getMessage());
return "redirect:/register/teacher";
}
return "redirect:/login";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.shuvocse21.StudentManagementApp.controller;

import com.shuvocse21.StudentManagementApp.dto.StudentDTO;
import com.shuvocse21.StudentManagementApp.entity.Student;
import com.shuvocse21.StudentManagementApp.entity.User;
import com.shuvocse21.StudentManagementApp.service.UserService;
Expand All @@ -19,52 +20,33 @@ public class StudentController {
private final UserService userService;

private User getCurrentUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.isAuthenticated() &&
!"anonymousUser".equals(authentication.getPrincipal())) {
String username = authentication.getName();
return userService.getUserByUsername(username);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth.isAuthenticated() && !"anonymousUser".equals(auth.getPrincipal())) {
return userService.getUserByUsername(auth.getName());
}
return null;
}

@GetMapping("/profile")
public String profile(Model model) {
@GetMapping("/dashboard")
public String dashboard(Model model) {
User user = getCurrentUser();
if (user == null) {
return "redirect:/login";
}
if (user == null) return "redirect:/login";

Student student = userService.getStudentByUserId(user.getId());
model.addAttribute("student", student);
return "student/profile";
StudentDTO studentDTO = userService.getStudentDTOByUserId(user.getId());
model.addAttribute("student", studentDTO);
return "student/dashboard";
}

@PostMapping("/profile/update")
public String updateProfile(
@RequestParam String email,
@RequestParam String phone,
@RequestParam String address,
RedirectAttributes redirectAttributes) {

public String updateProfile(@RequestParam String email, @RequestParam String phone,
@RequestParam String address, RedirectAttributes redirectAttributes) {
User user = getCurrentUser();
if (user == null) {
return "redirect:/login";
}
if (user == null) return "redirect:/login";

// FIXED: Using getStudentByUserId method which now exists
Student student = userService.getStudentByUserId(user.getId());

// Update email in User entity
student.getUser().setEmail(email);

// Update phone and address in Student entity
student.setPhone(phone);
student.setAddress(address);

// Save the changes through service
userService.updateStudent(student.getId(), email, phone, address, student.getDepartment().getId());

userService.updateStudent(student.getId(), email, phone, address);
redirectAttributes.addFlashAttribute("success", "Profile updated successfully!");
return "redirect:/student/profile";
return "redirect:/student/dashboard";
}
}
Loading