Commit 1577d277 authored by Muhammad Suleman's avatar Muhammad Suleman

Added Student Registration

parent fc4efb2b
package com.school.project.controller;
import com.school.project.dto.StudentDTO;
import com.school.project.service.student.StudentServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.NoSuchElementException;
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired(required = true)
public StudentServiceImp studentServiceImp;
@PostMapping("/save")
public ResponseEntity<StudentDTO> saveStudent(@RequestBody StudentDTO studentDTO) {
StudentDTO student = studentServiceImp.saveStudent(studentDTO);
if (student == null) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).build();
} else {
return ResponseEntity.status(HttpStatus.OK).body(student);
}
}
@GetMapping("/findAll")
public ResponseEntity<List<StudentDTO>> readAllStudents() {
List<StudentDTO> studentsDTO = studentServiceImp.readAllStudents();
if (studentsDTO.size() <= 0) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
} else {
return ResponseEntity.status(HttpStatus.OK).body(studentsDTO);
}
}
@GetMapping("/find/{id}")
public ResponseEntity<StudentDTO> readStudentById(@PathVariable("id") Long id) {
try {
StudentDTO dto = studentServiceImp.readStudentById(id);
return ResponseEntity.status(HttpStatus.OK).body(dto);
} catch (NoSuchElementException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}
@PutMapping("/update/{id}")
public ResponseEntity<StudentDTO> updateStudentById(@RequestBody StudentDTO studentDTO, @PathVariable("id") Long id) {
try {
StudentDTO updateStudent = studentServiceImp.updateStudentById(studentDTO, id);
return ResponseEntity.status(HttpStatus.OK).body(updateStudent);
} catch (NoSuchElementException e) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).build();
}
}
@DeleteMapping("/delete/{id}")
public ResponseEntity deleteStudentById(@PathVariable("id") Long id) {
try {
studentServiceImp.deleteStudentById(id);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} catch (EmptyResultDataAccessException e) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).build();
}
}
}
\ No newline at end of file
package com.school.project.controller;
package com.school.project.controller.other;
import com.school.project.dto.CourseDTO;
import com.school.project.dto.modelDTO.CourseDTO;
import com.school.project.service.course.CourseServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
......
package com.school.project.controller;
package com.school.project.controller.other;
import com.school.project.dto.DepartmentDTO;
import com.school.project.dto.modelDTO.DepartmentDTO;
import com.school.project.service.department.DepartmentServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
......
package com.school.project.controller;
package com.school.project.controller.other;
import com.school.project.dto.TeacherDTO;
import com.school.project.dto.modelDTO.TeacherDTO;
import com.school.project.service.teacher.TeacherServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
......
package com.school.project.controller.registration;
import com.school.project.dto.StudentRegistrationDTO;
import com.school.project.dto.UserDTO;
import com.school.project.dto.registrationDTO.StudentRegistrationDTO;
import com.school.project.dto.registrationDTO.UserDTO;
import com.school.project.service.userRegistration.StudentRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import javax.management.relation.RelationServiceNotRegisteredException;
@RestController
@RequestMapping("/register")
public class StudentRegistrationController {
public class RegistrationController {
@Autowired
private StudentRegistrationService studentRegistrationService;
@PostMapping("/student")
public ResponseEntity<UserDTO> registerStudent(@RequestBody StudentRegistrationDTO student) {
try {
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Data is not correct", e);
}
UserDTO userDTO = studentRegistrationService.registerStudent(student);
return ResponseEntity.ok(userDTO);
......
package com.school.project.dto;
import com.school.project.model.school.Course;
import lombok.Data;
import java.util.List;
@Data
public class StudentDTO {
private Long id;
private String name;
private Long age;
private List<Course> courseLists;
}
package com.school.project.dto;
package com.school.project.dto.modelDTO;
import com.school.project.model.registration.StudentReg;
import com.school.project.model.registration.Student;
import com.school.project.model.registration.Teacher;
import lombok.Data;
......@@ -13,7 +13,7 @@ public class CourseDTO {
private String title;
List<StudentReg> studentList;
List<Student> studentList;
private List<Teacher> teacherList;
......
package com.school.project.dto;
package com.school.project.dto.modelDTO;
import lombok.Data;
......
package com.school.project.dto;
package com.school.project.dto.modelDTO;
import com.school.project.model.school.Course;
import com.school.project.model.school.Department;
......
package com.school.project.dto;
package com.school.project.dto.registrationDTO;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
......
package com.school.project.dto;
package com.school.project.dto.registrationDTO;
import lombok.AllArgsConstructor;
......
package com.school.project.mapper;
import com.school.project.dto.CourseDTO;
import com.school.project.dto.modelDTO.CourseDTO;
import com.school.project.model.school.Course;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
......
package com.school.project.mapper;
import com.school.project.dto.DepartmentDTO;
import com.school.project.dto.modelDTO.DepartmentDTO;
import com.school.project.model.school.Department;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
......
package com.school.project.mapper;
import com.school.project.dto.StudentDTO;
import com.school.project.model.registration.Student;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper(componentModel = "spring")
public interface StudentMapper {
StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class);
StudentDTO studentModelToDTO(Student student);
Student studentDTOToModel(StudentDTO studentDTO);
List<StudentDTO> studentModelToDTO(List<Student> all);
}
package com.school.project.mapper;
import com.school.project.dto.TeacherDTO;
import com.school.project.dto.modelDTO.TeacherDTO;
import com.school.project.model.registration.Teacher;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
......
package com.school.project.model.registration;
import com.sun.istack.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import javax.persistence.*;
@Data
@Entity
@Table(name = "Role")
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Table(name = "role")
public class Role {
public static enum ROLES {
......
package com.school.project.model.registration;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.school.project.model.school.Course;
import com.sun.istack.NotNull;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.List;
@Data
@Entity()
@NoArgsConstructor
@Table(name = "Student")
public class Student {
@Id
@Column(name = "id", nullable = false)
@Column(name = "student_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private Long studentId;
@NotNull
@Column(name = "roll_no",unique = true)
private String rollNumber;
@NotNull
@Column(name = "name")
private String name;
@NotNull
@Column(name = "age")
private Long age;
@JsonIgnore
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE})
@JoinTable(
name = "studentsCourses",
joinColumns = {@JoinColumn(name = "student_id")},
inverseJoinColumns = {@JoinColumn(name = "course_id")}
)
@JsonIgnore
private List<Course> courseLists;
@OneToOne
@MapsId
@JoinColumn(name = "student_id")
private User studentAppUser;
}
package com.school.project.model.registration;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.school.project.model.school.Course;
import com.sun.istack.NotNull;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.List;
@Data
@Entity()
@NoArgsConstructor
@Table(name = "StudentReg")
public class StudentReg {
@Id
@Column(name = "student_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long studentId;
@Column(name = "roll_no",unique = true)
private String rollNumber;
@NotNull
@Column(name = "name")
private String name;
@NotNull
@Column(name = "age")
private Long age;
@JsonIgnore
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE})
@JoinTable(
name = "studentsCourses",
joinColumns = {@JoinColumn(name = "student_id")},
inverseJoinColumns = {@JoinColumn(name = "course_id")}
)
private List<Course> courseLists;
@OneToOne
@MapsId
@JoinColumn(name = "student_id")
private User studentAppUser;
}
......@@ -14,7 +14,7 @@ import javax.persistence.*;
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "user_id")
private Long userId;
......@@ -33,7 +33,7 @@ public class User {
@OneToOne(mappedBy = "studentAppUser", cascade = {CascadeType.MERGE})
@PrimaryKeyJoinColumn
private StudentReg studentUser;
private Student studentUser;
// @ElementCollection(fetch = FetchType.EAGER)
// @CollectionTable(
......
package com.school.project.model.school;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.school.project.model.registration.StudentReg;
import com.school.project.model.registration.Student;
import com.school.project.model.registration.Teacher;
import lombok.AllArgsConstructor;
import lombok.Data;
......@@ -24,7 +24,7 @@ public class Course {
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "courseLists")
@JsonIgnore
private List<StudentReg> studentList;
private List<Student> studentList;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "courseList")
@JsonIgnore
......
package com.school.project.repository;
import com.school.project.model.registration.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
}
package com.school.project.repository;
package com.school.project.repository.modelRepositery;
import com.school.project.model.school.Course;
import org.springframework.data.jpa.repository.JpaRepository;
......
package com.school.project.repository;
package com.school.project.repository.modelRepositery;
import com.school.project.model.school.Department;
import org.springframework.data.jpa.repository.JpaRepository;
......
package com.school.project.repository;
package com.school.project.repository.modelRepositery;
import com.school.project.model.registration.Teacher;
import org.springframework.data.jpa.repository.JpaRepository;
......
package com.school.project.repository;
package com.school.project.repository.registrationRepositery;
import com.school.project.model.registration.Role;
import org.springframework.data.jpa.repository.JpaRepository;
......
package com.school.project.repository;
package com.school.project.repository.registrationRepositery;
import com.school.project.model.registration.StudentReg;
import com.school.project.model.registration.Student;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRegRepository extends JpaRepository<StudentReg,Long> {
public interface StudentRegRepository extends JpaRepository<Student,Long> {
}
package com.school.project.repository;
package com.school.project.repository.registrationRepositery;
import com.school.project.model.registration.User;
import org.springframework.data.jpa.repository.JpaRepository;
......
package com.school.project.service.course;
import com.school.project.dto.CourseDTO;
import com.school.project.dto.modelDTO.CourseDTO;
import java.util.List;
......
package com.school.project.service.course;
import com.school.project.dto.CourseDTO;
import com.school.project.dto.modelDTO.CourseDTO;
import com.school.project.mapper.CourseMapper;
import com.school.project.repository.CourseRepository;
import com.school.project.repository.modelRepositery.CourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......
package com.school.project.service.department;
import com.school.project.dto.DepartmentDTO;
import com.school.project.dto.modelDTO.DepartmentDTO;
import java.util.List;
......
package com.school.project.service.department;
import com.school.project.dto.DepartmentDTO;
import com.school.project.dto.modelDTO.DepartmentDTO;
import com.school.project.mapper.DepartmentMapper;
import com.school.project.repository.DepartmentRepository;
import com.school.project.repository.modelRepositery.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......
package com.school.project.service.student;
import com.school.project.dto.StudentDTO;
import java.util.List;
public interface StudentService {
public StudentDTO saveStudent(StudentDTO studentDTO);
public List<StudentDTO> readAllStudents();
public StudentDTO readStudentById(Long id);
public StudentDTO updateStudentById(StudentDTO studentDTO, Long id);
public void deleteStudentById(Long id);
}
package com.school.project.service.student;
import com.school.project.dto.StudentDTO;
import com.school.project.mapper.StudentMapper;
import com.school.project.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImp implements StudentService {
@Autowired(required = true)
public StudentRepository studentRepository;
@Autowired
public StudentMapper studentMapper;
@Override
public StudentDTO saveStudent(StudentDTO studentDTO) {
return studentMapper.studentModelToDTO(
studentRepository.save(
studentMapper.studentDTOToModel(studentDTO)));
}
@Override
public List<StudentDTO> readAllStudents() {
return studentMapper.studentModelToDTO(studentRepository.findAll());
}
@Override
public StudentDTO readStudentById(Long id) {
return (studentMapper.studentModelToDTO(studentRepository.findById(id).get()));
}
@Override
public StudentDTO updateStudentById(StudentDTO studentDTO, Long id) {
StudentDTO dto = studentMapper.studentModelToDTO(studentRepository.findById(id).get());
if (studentDTO.getName() != null) {
dto.setName(studentDTO.getName());
}
if (studentDTO.getAge() != null) {
dto.setAge(studentDTO.getAge());
}
if (studentDTO.getCourseLists() != null) {
dto.setCourseLists(studentDTO.getCourseLists());
}
return studentMapper.studentModelToDTO(
studentRepository.save(
studentMapper.studentDTOToModel(dto)));
}
@Override
public void deleteStudentById(Long id) {
studentRepository.deleteById(id);
}
}
package com.school.project.service.teacher;
import com.school.project.dto.TeacherDTO;
import com.school.project.dto.modelDTO.TeacherDTO;
import org.springframework.stereotype.Service;
import java.util.List;
......
package com.school.project.service.teacher;
import com.school.project.dto.TeacherDTO;
import com.school.project.dto.modelDTO.TeacherDTO;
import com.school.project.mapper.TeacherMapper;
import com.school.project.repository.TeacherRepository;
import com.school.project.repository.modelRepositery.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......
package com.school.project.service.userRegistration;
import com.school.project.dto.StudentRegistrationDTO;
import com.school.project.dto.UserDTO;
import com.school.project.dto.registrationDTO.StudentRegistrationDTO;
import com.school.project.dto.registrationDTO.UserDTO;
public interface StudentRegistrationService {
......
package com.school.project.service.userRegistration;
import com.school.project.dto.StudentRegistrationDTO;
import com.school.project.dto.UserDTO;
import com.school.project.dto.registrationDTO.StudentRegistrationDTO;
import com.school.project.dto.registrationDTO.UserDTO;
import com.school.project.model.registration.Role;
import com.school.project.model.registration.StudentReg;
import com.school.project.model.registration.Student;
import com.school.project.model.registration.User;
import com.school.project.repository.RoleRepository;
import com.school.project.repository.StudentRegRepository;
import com.school.project.repository.UserRepository;
import com.school.project.repository.registrationRepositery.RoleRepository;
import com.school.project.repository.registrationRepositery.StudentRegRepository;
import com.school.project.repository.registrationRepositery.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
......@@ -16,7 +16,7 @@ import javax.transaction.Transactional;
import java.util.UUID;
@Service
public class StudentRegistrationServiceImpl implements StudentRegistrationService{
public class StudentRegistrationServiceImpl implements StudentRegistrationService {
@Autowired
private StudentRegRepository studentRegRepository;
......@@ -34,21 +34,24 @@ public class StudentRegistrationServiceImpl implements StudentRegistrationServic
@Transactional
public UserDTO registerStudent(StudentRegistrationDTO studentRegistrationDTO) {
//Creating New User
User newUser = new User();
newUser.setEmail(studentRegistrationDTO.getEmail());
newUser.setRole(roleRepository.findByRole(Role.ROLES.ADMIN.name()));
newUser.setRole(roleRepository.findByRole(Role.ROLES.STUDENT.name()));
newUser.setPassword(passwordEncoder.encode(studentRegistrationDTO.getPassword()));
newUser = userRepository.save(newUser);
StudentReg newStudentReg = new StudentReg();
//Creating New Student
Student newStudent = new Student();
newStudentReg.setStudentAppUser(newUser);
newStudentReg.setRollNumber(UUID.randomUUID().toString());
newStudent.setName(studentRegistrationDTO.getName());
newStudent.setAge(studentRegistrationDTO.getAge());
newStudent.setStudentAppUser(newUser);
newStudent.setRollNumber(UUID.randomUUID().toString());
studentRegRepository.save(newStudentReg);
studentRegRepository.save(newStudent);
return new UserDTO(newUser.getUserId(), newUser.getEmail());
......
package com.school.project.service.userRegistration;
import com.school.project.model.registration.User;
import com.school.project.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
@Service
public class UserServiceImpl implements UserService, UserDetailsService {
......
......@@ -2,7 +2,7 @@
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/school-portal
spring.datasource.username=root
spring.datasource.password=charlie123
spring.datasource.password=root
#spring.jpa.hibernate.ddl-auto=update
#spring.datasource.url=jdbc:mysql://localhost:3306/schooldataBase
......
INSERT INTO role VALUES (1, 'ADMIN');
INSERT INTO role VALUES (2, 'STUDENT');
INSERT INTO role VALUES (3, 'TEACHER');
\ No newline at end of file
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