0% found this document useful (0 votes)
2 views11 pages

Week - 10 - Assignment

The document outlines the design and implementation of an Online Course Enrollment System where students can view and enroll in courses, while admins can manage course listings and view enrolled students. The system is built using Spring Boot for the backend, MySQL for the database, and Thymeleaf for the frontend. It includes model classes for students and courses, repositories for data access, service classes for business logic, and controllers for handling web requests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Week - 10 - Assignment

The document outlines the design and implementation of an Online Course Enrollment System where students can view and enroll in courses, while admins can manage course listings and view enrolled students. The system is built using Spring Boot for the backend, MySQL for the database, and Thymeleaf for the frontend. It includes model classes for students and courses, repositories for data access, service classes for business logic, and controllers for handling web requests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Assignment: Online Course Enrollment System

**Problem Statement:
Build an Online Course Enrollment System** where:
 Students can:
o View available courses
o Enroll in courses
o View their enrolled courses
 Admin can:
o Add/update/delete courses
o View all enrolled students per course

Tech Stack
 Backend: Spring Boot
 Database: MySQL
 ORM: Spring Data JPA
 View: Thymeleaf
Source code :

// Project: Online Course Enrollment System

// Tech Stack: Spring Boot, Spring Data JPA, MySQL, Thymeleaf


// --- MODEL CLASSES ---

// model/Student.java

package model;

import jakarta.persistence.*;

import java.util.List;

@Entity

public class Student {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;

private String name;

@ManyToMany(mappedBy = "students")

private List<Course> courses;

// Getters and Setters

// model/Course.java

package model;

import jakarta.persistence.*;

import java.util.List;
@Entity

public class Course {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;

private String title;

private String description;

@ManyToMany

@JoinTable(

name = "enrollment",

joinColumns = @JoinColumn(name = "course_id"),

inverseJoinColumns = @JoinColumn(name = "student_id")

private List<Student> students;

// Getters and Setters

// --- REPOSITORIES ---

// repository/CourseRepository.java

package repository;

import model.Course;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CourseRepository extends JpaRepository<Course, Long> {

// repository/StudentRepository.java

package repository;

import model.Student;

import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepository extends JpaRepository<Student, Long> {

// --- SERVICE CLASSES ---

// service/CourseService.java

package service;

import model.Course;

import repository.CourseRepository;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;
@Service

public class CourseService {

@Autowired

private CourseRepository courseRepository;

public List<Course> getAllCourses() {

return courseRepository.findAll();

public Course getCourseById(Long id) {

return courseRepository.findById(id).orElse(null);

public Course saveCourse(Course course) {

return courseRepository.save(course);

public void deleteCourse(Long id) {

courseRepository.deleteById(id);

// service/StudentService.java

package service;

import model.Course;
import model.Student;

import repository.StudentRepository;

import repository.CourseRepository;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class StudentService {

@Autowired

private StudentRepository studentRepository;

@Autowired

private CourseRepository courseRepository;

public Student saveStudent(Student student) {

return studentRepository.save(student);

public void enrollInCourse(Long studentId, Long courseId) {

Student student = studentRepository.findById(studentId).orElse(null);

Course course = courseRepository.findById(courseId).orElse(null);

if (student != null && course != null) {

student.getCourses().add(course);

studentRepository.save(student);
}

public List<Course> getEnrolledCourses(Long studentId) {

Student student = studentRepository.findById(studentId).orElse(null);

return student != null ? student.getCourses() : null;

// --- CONTROLLER CLASSES ---

// controller/CourseController.java

package controller;

import model.Course;

import service.CourseService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;

@Controller

@RequestMapping("/courses")

public class CourseController {

@Autowired

private CourseService courseService;


@GetMapping

public String viewCourses(Model model) {

model.addAttribute("courses", courseService.getAllCourses());

return "course_list";

@GetMapping("/admin")

public String manageCourses(Model model) {

model.addAttribute("courses", courseService.getAllCourses());

return "admin_dashboard";

@PostMapping("/admin/save")

public String saveCourse(@ModelAttribute Course course) {

courseService.saveCourse(course);

return "redirect:/courses/admin";

@GetMapping("/admin/delete/{id}")

public String deleteCourse(@PathVariable Long id) {

courseService.deleteCourse(id);

return "redirect:/courses/admin";

}
// controller/StudentController.java

package controller;

import model.Student;

import service.StudentService;

import service.CourseService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;

@Controller

@RequestMapping("/students")

public class StudentController {

@Autowired

private StudentService studentService;

@Autowired

private CourseService courseService;

@GetMapping("/enroll")

public String enrollPage(Model model) {

model.addAttribute("courses", courseService.getAllCourses());

return "enroll_form";

}
@PostMapping("/enroll")

public String enroll(@RequestParam Long studentId, @RequestParam Long courseId) {

studentService.enrollInCourse(studentId, courseId);

return "redirect:/students/enrolled?studentId=" + studentId;

@GetMapping("/enrolled")

public String viewEnrolled(@RequestParam Long studentId, Model model) {

model.addAttribute("courses", studentService.getEnrolledCourses(studentId));

return "enrolled_courses";

You might also like