Skip to content

Commit

Permalink
Registering user with School object
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoleNG18 committed Apr 10, 2024
1 parent d373409 commit ef33642
Show file tree
Hide file tree
Showing 25 changed files with 318 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public SchoolEntity setStudents(List<StudentEntity> students) {
return this;
}

public void addTeacher(UserEntity teacher) {
this.teachers.add(teacher);
}

public List<UserEntity> getTeachers() {
return teachers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class UserEntity extends BaseEntity {
private String lastName;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
private String school;
@ManyToOne
private SchoolEntity school;
@Column(nullable = false)
private String password;

Expand Down Expand Up @@ -106,11 +106,11 @@ public UserEntity setEmail(String email) {
return this;
}

public String getSchool() {
public SchoolEntity getSchool() {
return school;
}

public UserEntity setSchool(String school) {
public UserEntity setSchool(SchoolEntity school) {
this.school = school;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public interface SchoolRepository extends JpaRepository<SchoolEntity, UUID> {

Optional<SchoolEntity> findBySchoolName(String name);
SchoolEntity findSchoolEntityBySchoolName(String name);

List<SchoolEntity> findAll();

Expand Down
17 changes: 15 additions & 2 deletions src/main/java/pmgkn/pescores/pescores/service/DbInitService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.annotation.PostConstruct;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import pmgkn.pescores.pescores.domain.entity.SchoolEntity;
import pmgkn.pescores.pescores.domain.entity.UserEntity;
import pmgkn.pescores.pescores.domain.entity.UserRoleEntity;
import pmgkn.pescores.pescores.domain.entity.norms.*;
Expand All @@ -29,14 +30,17 @@ public class DbInitService {

private final PasswordEncoder passwordEncoder;

private final SchoolRepository schoolRepository;

public DbInitService(UserRoleRepository userRoleRepository,
DenseBallRepository denseBallRepository,
JumpRepository jumpRepository,
TTestRepository tTestRepository,
ThirtyMetersRepository thirtyMetersRepository,
TwoHundredMetersRepository twoHundredMetersRepository,
UserRepository userRepository,
PasswordEncoder passwordEncoder) {
PasswordEncoder passwordEncoder,
SchoolRepository schoolRepository) {
this.userRoleRepository = userRoleRepository;
this.denseBallRepository = denseBallRepository;
this.jumpRepository = jumpRepository;
Expand All @@ -45,10 +49,12 @@ public DbInitService(UserRoleRepository userRoleRepository,
this.twoHundredMetersRepository = twoHundredMetersRepository;
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
this.schoolRepository = schoolRepository;
}

@PostConstruct
public void init() {
initSchool();
initRoles();
initSuperAdmin();
initDenseBall();
Expand All @@ -58,14 +64,21 @@ public void init() {
initThirtyMeters();
}

private void initSchool() {
if(this.schoolRepository.count()==0){
SchoolEntity school=new SchoolEntity().setCity("Kyustendil").setName("PMG Prof. Emanuil Ivanov");
this.schoolRepository.saveAndFlush(school);
}
}

public void initSuperAdmin() {
if (this.userRepository.count() == 0) {
UserEntity user = new UserEntity()
.setFirstName("Nikol")
.setLastName("Georgieva")
.setEmail("[email protected]")
.setPassword(passwordEncoder.encode("nikol@superadmin"))
.setSchool("PMG prof emanuil ivanov")
.setSchool(this.schoolRepository.findSchoolEntityBySchoolName("PMG Prof. Emanuil Ivanov"))
.setRoles(userRoleRepository.findAll());
this.userRepository.saveAndFlush(user);
}
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/pmgkn/pescores/pescores/service/SchoolService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import pmgkn.pescores.pescores.domain.dto.binding.ClassAddBindingDto;
import org.springframework.transaction.annotation.Transactional;
import pmgkn.pescores.pescores.domain.dto.binding.SchoolAddBindingDto;
import pmgkn.pescores.pescores.domain.dto.view.SchoolViewDto;
import pmgkn.pescores.pescores.domain.entity.ClassEntity;
import pmgkn.pescores.pescores.domain.entity.SchoolEntity;
import pmgkn.pescores.pescores.domain.entity.UserEntity;
import pmgkn.pescores.pescores.repositories.SchoolRepository;

import java.util.List;
Expand Down Expand Up @@ -40,4 +41,20 @@ private SchoolEntity mapToSchoolEntity(SchoolAddBindingDto schoolAddBindingDto)
public List<SchoolViewDto> getAllSchools() {
return this.schoolRepository.findAll().stream().map(s -> this.modelMapper.map(s, SchoolViewDto.class)).collect(Collectors.toList());
}

public String addTeacher(UserEntity userToSave,
String school) {

SchoolEntity schoolEntity=this.getSchoolByName(school);
schoolEntity.addTeacher(userToSave);

this.schoolRepository.saveAndFlush(schoolEntity);

return schoolEntity.getName();
}

public SchoolEntity getSchoolByName(String school) {
return this.schoolRepository.findBySchoolName(school).orElse(null);
}

}
22 changes: 17 additions & 5 deletions src/main/java/pmgkn/pescores/pescores/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import pmgkn.pescores.pescores.domain.dto.binding.UserRegistrationBindingDto;
import pmgkn.pescores.pescores.domain.entity.ClassEntity;
import pmgkn.pescores.pescores.domain.entity.TaskEntity;
import pmgkn.pescores.pescores.domain.entity.UserEntity;
import pmgkn.pescores.pescores.domain.entity.UserRoleEntity;
import pmgkn.pescores.pescores.domain.entity.*;
import pmgkn.pescores.pescores.domain.enums.TaskStatusEnum;
import pmgkn.pescores.pescores.domain.enums.UserRoleEnum;
import pmgkn.pescores.pescores.repositories.UserRepository;
Expand All @@ -26,15 +23,19 @@ public class UserService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;

private final SchoolService schoolService;

@Autowired
public UserService(UserRoleService userRoleService,
ModelMapper modelMapper,
PasswordEncoder passwordEncoder,
UserRepository userRepository) {
UserRepository userRepository,
SchoolService schoolService) {
this.userRoleService = userRoleService;
this.modelMapper = modelMapper;
this.passwordEncoder = passwordEncoder;
this.userRepository = userRepository;
this.schoolService = schoolService;
}

public UserEntity getUserById(UUID id) {
Expand All @@ -51,6 +52,17 @@ public void registerUser(UserRegistrationBindingDto userRegistrationDTO) {

this.userRepository.saveAndFlush(userToSave);

setSchoolAndTeacher(userRegistrationDTO, userToSave);

this.userRepository.saveAndFlush(userToSave);

}

private void setSchoolAndTeacher(UserRegistrationBindingDto userRegistrationDTO,
UserEntity userToSave) {
String schoolName = this.schoolService.addTeacher(userToSave, userRegistrationDTO.getSchool());

userToSave.setSchool(this.schoolService.getSchoolByName(schoolName));
}

public List<TaskEntity> getAllInProgressTasks(String email) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import pmgkn.pescores.pescores.domain.dto.binding.UserRegistrationBindingDto;
import pmgkn.pescores.pescores.service.SchoolService;
import pmgkn.pescores.pescores.service.UserService;

@Controller
@RequestMapping("/users")
public class UserRegisterController {

private final UserService userService;
private final SchoolService schoolService;

public UserRegisterController(UserService userService) {
public UserRegisterController(UserService userService,
SchoolService schoolService) {
this.userService = userService;
this.schoolService = schoolService;
}

@ModelAttribute("registerDto")
Expand All @@ -24,7 +29,10 @@ public UserRegistrationBindingDto initRegisterDto() {
}

@GetMapping("/register")
public String getRegister() {
public String getRegister(Model model) {

model.addAttribute("schools",this.schoolService.getAllSchools());

return "register";
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/static/css/register.css
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@
font-weight: 300;
}

option {
background-color: #d8d7fe;
font-size: 13px;
font-weight: 500;
color: #5d54c3;
}

#school {
background-color: #d8d7fe;
color: #5d54c3;
font-size: 13px;
font-weight: 500;
margin-top: 0.5em;
text-align: center;
border-radius: 5px;
}

::placeholder {
color: #362bb7;
}
Expand Down
22 changes: 12 additions & 10 deletions src/main/resources/templates/add-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@
</ul>

<ul class="navbar-buttons" sec:authorize="isAuthenticated()">
<a sec:authorize="hasAnyAuthority('ROLE_USER')" th:href="@{/denseBall}">Dense ball</a>
<a sec:authorize="hasAnyAuthority('ROLE_USER')" th:href="@{/jump}">Jump</a>
<a sec:authorize="hasAnyAuthority('ROLE_USER')" th:href="@{/tTest}">T-test</a>
<a sec:authorize="hasAnyAuthority('ROLE_USER')" th:href="@{/thirty}">30 meters</a>
<a sec:authorize="hasAnyAuthority('ROLE_USER')" th:href="@{/twoHundred}">200 meters</a>
<a sec:authorize="hasAnyAuthority('ROLE_ADMIN')" th:href="@{/classes}">Classes</a>
<!-- TODO: SMTH LIKE OWN CLASSES OR WHAT-->
<!-- <a sec:authorize="hasAnyAuthority('ROLE_USER')" th:href="@{/classes}">Classes</a>-->
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/denseBall}">Dense ball</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/jump}">Jump</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/tTest}">T-test</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/thirty}">30 meters</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/twoHundred}">200 meters</a>
<a sec:authorize="hasAnyAuthority('ROLE_SUPERADMIN')" th:href="@{/schools/add}">Add school</a>
<a sec:authorize="hasAnyAuthority('ROLE_SUPERADMIN')" th:href="@{/schools/all}">Schools</a>
<a sec:authorize="!hasAnyAuthority('ROLE_SUPERADMIN','ROLE_USER')" th:href="@{/classes}">Classes</a>
<!-- TODO: SMTH LIKE OWN CLASSES OR WHAT-->
<!-- <a sec:authorize="hasAnyAuthority('ROLE_USER')" th:href="@{/classes}">Classes</a>-->
<a th:href="@{/tasks}">Tasks</a>
<a sec:authorize="hasAnyAuthority('ROLE_ADMIN')" th:href="@{/classes/add}">Add class</a>
<a sec:authorize="hasAnyAuthority('ROLE_ADMIN')" th:href="@{/students/add}">Add student</a>
<a sec:authorize="!hasAnyAuthority('ROLE_SUPERADMIN','ROLE_USER')" th:href="@{/classes/add}">Add class</a>
<a sec:authorize="!hasAnyAuthority('ROLE_SUPERADMIN','ROLE_USER')" th:href="@{/students/add}">Add student</a>
<a>
<form class="logout" th:method="post" th:action="@{/users/logout}">
<input class="dropdown-item btn-link nav-link" type="submit" value="Logout">
Expand Down
20 changes: 12 additions & 8 deletions src/main/resources/templates/add-school.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@
</ul>

<ul class="navbar-buttons" sec:authorize="isAuthenticated()">
<a th:href="@{/denseBall}">Dense ball</a>
<a th:href="@{/jump}">Jump</a>
<a th:href="@{/tTest}">T-test</a>
<a th:href="@{/thirty}">30 meters</a>
<a th:href="@{/twoHundred}">200 meters</a>
<a th:href="@{/classes}">Classes</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/denseBall}">Dense ball</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/jump}">Jump</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/tTest}">T-test</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/thirty}">30 meters</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/twoHundred}">200 meters</a>
<a sec:authorize="hasAnyAuthority('ROLE_SUPERADMIN')" th:href="@{/schools/add}">Add school</a>
<a sec:authorize="hasAnyAuthority('ROLE_SUPERADMIN')" th:href="@{/schools/all}">Schools</a>
<a sec:authorize="!hasAnyAuthority('ROLE_SUPERADMIN','ROLE_USER')" th:href="@{/classes}">Classes</a>
<!-- TODO: SMTH LIKE OWN CLASSES OR WHAT-->
<!-- <a sec:authorize="hasAnyAuthority('ROLE_USER')" th:href="@{/classes}">Classes</a>-->
<a th:href="@{/tasks}">Tasks</a>
<a th:href="@{/classes/add}">Add class</a>
<a th:href="@{/students/add}">Add student</a>
<a sec:authorize="!hasAnyAuthority('ROLE_SUPERADMIN','ROLE_USER')" th:href="@{/classes/add}">Add class</a>
<a sec:authorize="!hasAnyAuthority('ROLE_SUPERADMIN','ROLE_USER')" th:href="@{/students/add}">Add student</a>
<a>
<form class="logout" th:method="post" th:action="@{/users/logout}">
<input class="dropdown-item btn-link nav-link" type="submit" value="Logout">
Expand Down
20 changes: 12 additions & 8 deletions src/main/resources/templates/all-schools.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@
</ul>

<ul class="navbar-buttons" sec:authorize="isAuthenticated()">
<a th:href="@{/denseBall}">Dense ball</a>
<a th:href="@{/jump}">Jump</a>
<a th:href="@{/tTest}">T-test</a>
<a th:href="@{/thirty}">30 meters</a>
<a th:href="@{/twoHundred}">200 meters</a>
<a th:href="@{/classes}">Classes</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/denseBall}">Dense ball</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/jump}">Jump</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/tTest}">T-test</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/thirty}">30 meters</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/twoHundred}">200 meters</a>
<a sec:authorize="hasAnyAuthority('ROLE_SUPERADMIN')" th:href="@{/schools/add}">Add school</a>
<a sec:authorize="hasAnyAuthority('ROLE_SUPERADMIN')" th:href="@{/schools/all}">Schools</a>
<a sec:authorize="!hasAnyAuthority('ROLE_SUPERADMIN','ROLE_USER')" th:href="@{/classes}">Classes</a>
<!-- TODO: SMTH LIKE OWN CLASSES OR WHAT-->
<!-- <a sec:authorize="hasAnyAuthority('ROLE_USER')" th:href="@{/classes}">Classes</a>-->
<a th:href="@{/tasks}">Tasks</a>
<a th:href="@{/classes/add}">Add class</a>
<a th:href="@{/students/add}">Add student</a>
<a sec:authorize="!hasAnyAuthority('ROLE_SUPERADMIN','ROLE_USER')" th:href="@{/classes/add}">Add class</a>
<a sec:authorize="!hasAnyAuthority('ROLE_SUPERADMIN','ROLE_USER')" th:href="@{/students/add}">Add student</a>
<a>
<form class="logout" th:method="post" th:action="@{/users/logout}">
<input class="dropdown-item btn-link nav-link" type="submit" value="Logout">
Expand Down
20 changes: 12 additions & 8 deletions src/main/resources/templates/classes-edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@
</ul>

<ul class="navbar-buttons" sec:authorize="isAuthenticated()">
<a th:href="@{/denseBall}">Dense ball</a>
<a th:href="@{/jump}">Jump</a>
<a th:href="@{/tTest}">T-test</a>
<a th:href="@{/thirty}">30 meters</a>
<a th:href="@{/twoHundred}">200 meters</a>
<a th:href="@{/classes}">Classes</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/denseBall}">Dense ball</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/jump}">Jump</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/tTest}">T-test</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/thirty}">30 meters</a>
<a sec:authorize="!hasAnyAuthority('ROLE_ADMIN','ROLE_SUPERADMIN')" th:href="@{/twoHundred}">200 meters</a>
<a sec:authorize="hasAnyAuthority('ROLE_SUPERADMIN')" th:href="@{/schools/add}">Add school</a>
<a sec:authorize="hasAnyAuthority('ROLE_SUPERADMIN')" th:href="@{/schools/all}">Schools</a>
<a sec:authorize="!hasAnyAuthority('ROLE_SUPERADMIN','ROLE_USER')" th:href="@{/classes}">Classes</a>
<!-- TODO: SMTH LIKE OWN CLASSES OR WHAT-->
<!-- <a sec:authorize="hasAnyAuthority('ROLE_USER')" th:href="@{/classes}">Classes</a>-->
<a th:href="@{/tasks}">Tasks</a>
<a th:href="@{/classes/add}">Add class</a>
<a th:href="@{/students/add}">Add student</a>
<a sec:authorize="!hasAnyAuthority('ROLE_SUPERADMIN','ROLE_USER')" th:href="@{/classes/add}">Add class</a>
<a sec:authorize="!hasAnyAuthority('ROLE_SUPERADMIN','ROLE_USER')" th:href="@{/students/add}">Add student</a>
<a>
<form class="logout" th:method="post" th:action="@{/users/logout}">
<input class="dropdown-item btn-link nav-link" type="submit" value="Logout">
Expand Down
Loading

0 comments on commit ef33642

Please sign in to comment.