dgPadBootcamps / Java-Bootcamp-2024

1 stars 0 forks source link

Task 4: Spring boot with database #221

Open mohammad-fahs opened 3 months ago

mohammad-fahs commented 3 months ago

Task: Create an Advanced Student Management System Using Spring Boot, MySQL Database, and REST APIs

Objective:

Your task is to create an advanced Student Management System using Spring Boot, MySQL, and RESTful APIs. This system will manage not only students but also courses and enrollments. You will create a relational database structure that includes multiple entities and relationships between them. This exercise will help you understand how to build a more complex CRUD application with multiple entities and relationships using Spring Boot, JPA, and MySQL.

user this discussion for help:

https://github.com/dgPadBootcamps/Java-Bootcamp-2024/discussions/195

Make Sure to use the best practice architecture discussed in the latest sessions

Requirements:

  1. Project Setup:
    • Set up a Spring Boot project using Maven.
    • Include dependencies for Spring Web, Spring Data JPA, MySQL Connector, and any other libraries you may find necessary.
  2. Database Setup:
    • Install MySQL or use an existing MySQL server.
    • Create a new database named student_management.
    • In your application.properties (or application.yml) file, configure the connection to the MySQL database, including the database URL, username, and password.
  3. Entity Classes:

    • Create the following entities with appropriate JPA annotations:

    a. Student:

    • Fields:
      • id: Primary key, auto-generated.
      • firstName: String, not null.
      • lastName: String, not null.
      • email: String, not null, unique.
      • age: Integer.
      • enrollments: One-to-Many relationship with the Enrollment entity.

    b. Course:

    • Fields:
      • id: Primary key, auto-generated.
      • courseName: String, not null.
      • courseCode: String, not null, unique.
      • credits: Integer.
      • instructor: String, not null.
      • enrollments: One-to-Many relationship with the Enrollment entity.

    c. Enrollment:

    • Fields:
      • id: Primary key, auto-generated.
      • student: Many-to-One relationship with the Student entity.
      • course: Many-to-One relationship with the Course entity.
      • enrollmentDate: Date of enrollment.
      • grade: String, grade received in the course.
    • Ensure that each entity class is properly annotated to represent the relationships, such as @OneToMany, @ManyToOne, and @JoinColumn.
  4. Repository Layer:
    • Create repository interfaces for each entity:
      • StudentRepository, CourseRepository, and EnrollmentRepository should extend JpaRepository.
    • These repositories will provide CRUD operations and any custom queries you may need later.
  5. Service Layer:
    • Create service classes for each entity, such as StudentService, CourseService, and EnrollmentService.
    • Each service class should contain business logic to handle CRUD operations and manage relationships between entities.
    • Inject the corresponding repository into each service to interact with the database.
  6. Controller Layer:

    • Create REST controllers to expose the following endpoints:

    a. Student Endpoints:

    • Create a new student:
      • Endpoint: POST /api/students
      • Request Body: JSON object with student details.
      • Response: JSON object representing the created student.
    • Retrieve all students:
      • Endpoint: GET /api/students
      • Response: JSON array of student objects.
    • Retrieve a student by ID:
      • Endpoint: GET /api/students/{id}
      • Path Variable: id (the student's ID).
      • Response: JSON object representing the student.
    • Update a student:
      • Endpoint: PUT /api/students/{id}
      • Path Variable: id (the student's ID).
      • Request Body: JSON object with updated student details.
      • Response: Updated JSON object of the student.
    • Delete a student:
      • Endpoint: DELETE /api/students/{id}
      • Path Variable: id (the student's ID).
      • Response: Confirmation message or status code indicating the deletion.

    b. Course Endpoints:

    • Create a new course:
      • Endpoint: POST /api/courses
      • Request Body: JSON object with course details.
      • Response: JSON object representing the created course.
    • Retrieve all courses:
      • Endpoint: GET /api/courses
      • Response: JSON array of course objects.
    • Retrieve a course by ID:
      • Endpoint: GET /api/courses/{id}
      • Path Variable: id (the course's ID).
      • Response: JSON object representing the course.
    • Update a course:
      • Endpoint: PUT /api/courses/{id}
      • Path Variable: id (the course's ID).
      • Request Body: JSON object with updated course details.
      • Response: Updated JSON object of the course.
    • Delete a course:
      • Endpoint: DELETE /api/courses/{id}
      • Path Variable: id (the course's ID).
      • Response: Confirmation message or status code indicating the deletion.

    c. Enrollment Endpoints:

    • Enroll a student in a course:
      • Endpoint: POST /api/enrollments
      • Request Body: JSON object with studentId, courseId, enrollmentDate, and grade.
      • Response: JSON object representing the created enrollment.
    • Retrieve all enrollments:
      • Endpoint: GET /api/enrollments
      • Response: JSON array of enrollment objects.
    • Retrieve enrollments for a specific student:
      • Endpoint: GET /api/students/{studentId}/enrollments
      • Path Variable: studentId.
      • Response: JSON array of enrollment objects for the specified student.
    • Retrieve enrollments for a specific course:
      • Endpoint: GET /api/courses/{courseId}/enrollments
      • Path Variable: courseId.
      • Response: JSON array of enrollment objects for the specified course.
    • Update an enrollment:
      • Endpoint: PUT /api/enrollments/{id}
      • Path Variable: id (the enrollment's ID).
      • Request Body: JSON object with updated enrollment details.
      • Response: Updated JSON object of the enrollment.
    • Delete an enrollment:
      • Endpoint: DELETE /api/enrollments/{id}
      • Path Variable: id (the enrollment's ID).
      • Response: Confirmation message or status code indicating the deletion.
  7. Validation:
    • Add validation to ensure:
      • Required fields are not null.
      • The email field in the Student entity is unique and follows a valid email format.
      • The courseCode field in the Course entity is unique.
      • The grade field in the Enrollment entity follows a specific format (e.g., A, B, C, etc.).
  8. Error Handling:
    • Implement error handling for:
      • Entity not found (e.g., when trying to retrieve or delete a non-existent student, course, or enrollment).
      • Invalid input data (e.g., missing required fields, invalid data format, etc.).
    • Return appropriate HTTP status codes (e.g., 404 for not found, 400 for bad request).
  9. Relationships:
    • Ensure that the relationships between entities (Student, Course, and Enrollment) are correctly handled in both the database and the application.
    • Test that creating, updating, or deleting a student or course also correctly updates or deletes related enrollments.

The task is submitted using Postman collection and code snippets as usual in addition to screenshots from the database

janafaour99 commented 2 months ago

TASK4.zip

@mohammad-fahs