Commit 0c70dc0c authored by Muhammad Suleman's avatar Muhammad Suleman

Added Assign Course Service

parent 3e163747
package com.school.project.controller;
import com.school.project.dto.teacher.TeacherCourseAssignDTO;
import com.school.project.service.admin.TeacherFunctionsForAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Set;
@RestController
@RequestMapping("/admin")
public class AdminController {
@Autowired
TeacherFunctionsForAdminService teacher;
@PostMapping("/assignCourses")
public void assignCourseToTeacher(@RequestBody TeacherCourseAssignDTO assignCourses) {
teacher.assignCourseToTeachers(assignCourses);
}
}
......@@ -4,15 +4,12 @@ package com.school.project.controller;
import com.school.project.dto.student.StudentCourseRegistrationDTO;
import com.school.project.dto.student.StudentCoursesDTO;
import com.school.project.dto.student.StudentViewDataDTO;
import com.school.project.service.courseRegistration.CourseRegistrationService;
import com.school.project.service.student.CourseRegistrationService;
import com.school.project.service.student.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.CurrentSecurityContext;
import org.springframework.web.bind.annotation.*;
import java.security.Principal;
import java.util.List;
import java.util.Set;
@RestController
......@@ -26,9 +23,9 @@ public class StudentController {
private CourseRegistrationService courseRegistrationService;
@GetMapping("")
public StudentViewDataDTO getUserDetails(@CurrentSecurityContext(expression = "authentication") Authentication authentication) {
public StudentViewDataDTO getUserDetails(Principal principal) {
return studentService.viewData(authentication.getName());
return studentService.viewData(principal.getName());
}
......
package com.school.project.controller;
import com.school.project.dto.teacher.TeacherViewCoursesDTO;
import com.school.project.dto.teacher.TeacherViewDataDTO;
import com.school.project.service.teacher.TeacherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.security.Principal;
@RestController
@RequestMapping("/teacher")
public class TeacherController {
@Autowired
TeacherServiceImpl teacherService;
@GetMapping("")
public TeacherViewDataDTO getTeacherData(Principal principal) {
return teacherService.getTeacherData(principal.getName());
}
@GetMapping("/course")
public TeacherViewCoursesDTO getTeacherCourses(Principal principal) {
return teacherService.getTeacherCourses(principal.getName());
}
}
......@@ -15,21 +15,7 @@ import java.util.List;
public class UserController {
@GetMapping("/user")
public List<String> currentUserName(@CurrentSecurityContext(expression = "authentication")
Authentication authentication, Principal principal) {
List<String> data = new ArrayList<>();
data.add(authentication.getName());
data.add(String.valueOf(authentication.getAuthorities()));
data.add(String.valueOf(authentication.getCredentials()));
data.add(String.valueOf(authentication.getDetails()));
data.add(String.valueOf(authentication.getPrincipal()));
data.add("\nNow Here Is Principal Details\n");
data.add(principal.getName());
return data;
public String currentUserName(Principal principal) {
return principal.getName();
}
}
package com.school.project.dto.teacher;
import com.school.project.model.Course;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import java.util.Set;
@Getter
@Setter
@RequiredArgsConstructor
public class TeacherCourseAssignDTO {
private String teacherNumber;
private Set<String> courseList;
}
package com.school.project.dto.teacher;
import com.school.project.model.Course;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.stereotype.Component;
import java.util.Set;
@Getter @Setter
@RequiredArgsConstructor
public class TeacherViewCoursesDTO {
private String name;
private String teacherNumber;
private Set<Course> courseList;
}
package com.school.project.dto.teacher;
import com.school.project.model.Department;
import lombok.*;
@Getter @Setter
@RequiredArgsConstructor
public class TeacherViewDataDTO {
private String name;
private String teacherNumber;
private String degree;
private Department department;
}
package com.school.project.mapper;
import com.school.project.dto.teacher.TeacherViewCoursesDTO;
import com.school.project.model.Teacher;
import org.springframework.stereotype.Component;
@Component
public class TeacherViewCoursesMapper {
public TeacherViewCoursesDTO modelToDTO(Teacher teacher) {
TeacherViewCoursesDTO teacherViewCoursesDTO = new TeacherViewCoursesDTO();
teacherViewCoursesDTO.setName(teacher.getName());
teacherViewCoursesDTO.setTeacherNumber(teacher.getTeacherNumber());
teacherViewCoursesDTO.setCourseList(teacher.getCourseList());
return teacherViewCoursesDTO;
}
}
package com.school.project.mapper;
import com.school.project.dto.teacher.TeacherViewDataDTO;
import com.school.project.model.Teacher;
import org.springframework.stereotype.Component;
@Component
public class TeacherViewDataMapper {
public TeacherViewDataDTO modelToDto(Teacher teacher) {
TeacherViewDataDTO teacherViewDataDTO = new TeacherViewDataDTO();
teacherViewDataDTO.setName(teacher.getName());
teacherViewDataDTO.setTeacherNumber(teacher.getTeacherNumber());
teacherViewDataDTO.setDegree(teacher.getDegree());
teacherViewDataDTO.setDepartment(teacher.getDepartment());
return teacherViewDataDTO;
}
}
package com.school.project.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.school.project.model.Course;
import com.school.project.model.userRegistration.User;
import com.sun.istack.NotNull;
import lombok.Data;
......@@ -10,10 +9,8 @@ import lombok.NoArgsConstructor;
import lombok.ToString;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.context.annotation.Lazy;
import javax.persistence.*;
import java.util.List;
import java.util.Set;
@Data
......
......@@ -30,7 +30,7 @@ public class Teacher {
private String name;
@NotNull
@Column(name = "teacher_num")
@Column(name = "teacher_num",unique = true)
private String teacherNumber;
@NotNull
......
......@@ -2,10 +2,14 @@ package com.school.project.repository.modelRepositery;
import com.school.project.model.Teacher;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface TeacherRepository extends JpaRepository<Teacher,Long> {
Teacher findOneByTeacherId(Long id);
@Query(value = "SELECT * FROM teacher WHERE teacher_num= ?1",nativeQuery = true)
Teacher findByTeacherNumber(String number);
}
package com.school.project.service.admin;
import com.school.project.dto.teacher.TeacherCourseAssignDTO;
import java.util.Set;
public interface TeacherFunctionsForAdminService {
public Long getUserId(String username);
void assignCourseToTeachers(TeacherCourseAssignDTO assignCourses);
}
package com.school.project.service.admin;
import com.school.project.dto.teacher.TeacherCourseAssignDTO;
import com.school.project.model.Course;
import com.school.project.model.Teacher;
import com.school.project.model.userRegistration.User;
import com.school.project.repository.modelRepositery.CourseRepository;
import com.school.project.repository.modelRepositery.TeacherRepository;
import com.school.project.repository.userRegistration.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.Set;
@Service
public class TeacherFunctionsForAdminServiceImpl implements TeacherFunctionsForAdminService {
@Autowired
TeacherRepository teacherRepository;
@Autowired
UserRepository userRepository;
@Autowired
CourseRepository courseRepository;
@Override
public Long getUserId(String username) {
User user = userRepository.findUserByEmail(username).get();
return user.getUserId();
}
@Override
@Transactional
@Modifying
public void assignCourseToTeachers(TeacherCourseAssignDTO assignCourses) {
Teacher teacher = teacherRepository.findByTeacherNumber(assignCourses.getTeacherNumber());
Set<Course> teacherCourses = teacher.getCourseList();
Set<String> newCourses = assignCourses.getCourseList();
newCourses.forEach(course -> teacherCourses.add(courseRepository.findByCourseName(course)));
teacher.setCourseList(teacherCourses);
}
}
package com.school.project.service.courseRegistration;
package com.school.project.service.student;
import com.school.project.dto.student.StudentCourseRegistrationDTO;
import org.springframework.stereotype.Service;
......
package com.school.project.service.courseRegistration;
package com.school.project.service.student;
import com.school.project.dto.student.StudentCourseRegistrationDTO;
import com.school.project.model.Course;
......@@ -35,7 +35,6 @@ public class CourseRegistrationServiceImpl implements CourseRegistrationService
student.setCourseLists(studentCourses);
studentRepository.save(student);
}
}
package com.school.project.service.teacher;
import com.school.project.dto.teacher.TeacherViewCoursesDTO;
import com.school.project.dto.teacher.TeacherViewDataDTO;
public interface TeacherService {
TeacherViewDataDTO getTeacherData(String username);
TeacherViewCoursesDTO getTeacherCourses(String username);
}
package com.school.project.service.teacher;
import com.school.project.dto.teacher.TeacherViewCoursesDTO;
import com.school.project.dto.teacher.TeacherViewDataDTO;
import com.school.project.mapper.TeacherViewCoursesMapper;
import com.school.project.mapper.TeacherViewDataMapper;
import com.school.project.model.userRegistration.User;
import com.school.project.repository.modelRepositery.TeacherRepository;
import com.school.project.repository.userRegistration.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TeacherServiceImpl implements TeacherService {
@Autowired
TeacherRepository teacherRepository;
@Autowired
UserRepository userRepository;
@Autowired
TeacherViewDataMapper teacherViewDataMapper;
@Autowired
TeacherViewCoursesMapper teacherViewCoursesMapper;
@Override
public TeacherViewDataDTO getTeacherData(String username) {
User user = userRepository.findUserByEmail(username).get();
return teacherViewDataMapper.modelToDto(
teacherRepository.findOneByTeacherId(user.getUserId()));
}
@Override
public TeacherViewCoursesDTO getTeacherCourses(String username) {
User user = userRepository.findUserByEmail(username).get();
return teacherViewCoursesMapper.modelToDTO(
teacherRepository.findOneByTeacherId(user.getUserId()));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment