Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/model creation #3

Merged
merged 7 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing
@SpringBootApplication
public class UniversityConnectApplication {

Expand Down
78 changes: 40 additions & 38 deletions src/main/java/edu/university_connect/config/Laufe.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package edu.university_connect.config;

import edu.university_connect.model.entity.SystemAction;
import edu.university_connect.model.entity.SystemRole;
import edu.university_connect.model.entity.SystemUser;
import edu.university_connect.repository.SystemActionRepository;
import edu.university_connect.repository.SystemRoleRepository;
import edu.university_connect.repository.SystemUserRepository;
import edu.university_connect.domain.entity.Action;
import edu.university_connect.domain.entity.Resource;
import edu.university_connect.domain.entity.Role;
import edu.university_connect.domain.entity.User;
import edu.university_connect.repository.ActionRepository;
import edu.university_connect.repository.RoleRepository;
import edu.university_connect.repository.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
Expand All @@ -18,51 +19,52 @@

//@Component
public class Laufe implements CommandLineRunner {
SystemActionRepository systemActionRepository;
SystemRoleRepository systemRoleRepository;
SystemUserRepository systemUserRepository;
ActionRepository actionRepository;
RoleRepository roleRepository;
UserRepository userRepository;

public Laufe(SystemActionRepository systemActionRepository, SystemRoleRepository systemRoleRepository, SystemUserRepository systemUserRepository) {
this.systemActionRepository = systemActionRepository;
this.systemRoleRepository = systemRoleRepository;
this.systemUserRepository = systemUserRepository;
public Laufe(ActionRepository actionRepository, RoleRepository roleRepository, UserRepository userRepository) {
this.actionRepository = actionRepository;
this.roleRepository = roleRepository;
this.userRepository = userRepository;
}

@Override
public void run(String... args) throws Exception {
SystemAction action1=new SystemAction("Create System User","create_user","Create System User");
SystemAction action2=new SystemAction("Read System User","view_user","Read System User");
SystemAction action3=new SystemAction("Update System User","modify_user","Update System User");
SystemAction action4=new SystemAction("Delete System User","delete_user","Delete System User");
SystemAction action5=new SystemAction("Read System User List","view_user_list","Read System User List");
Action action1=new Action("Create User","create_user","Create User");
Action action2=new Action("Read User","view_user","Read User");
Action action3=new Action("Update User","modify_user","Update User");
Action action4=new Action("Delete User","delete_user","Delete User");
Action action5=new Action("Read User List","view_user_list","Read User List");

SystemAction action11 = new SystemAction("Create System Role", "create_role", "Create new system roles");
SystemAction action12 = new SystemAction("Read System Role", "view_role", "View existing system roles");
SystemAction action13 = new SystemAction("Update System Role", "modify_role", "Modify system role details");
SystemAction action14 = new SystemAction("Delete System Role", "delete_role", "Delete system roles");
SystemAction action15=new SystemAction("Read System Action List","view_role_list","Read System role List");
Action action11 = new Action("Create Role", "create_role", "Create new roles");
Action action12 = new Action("Read Role", "view_role", "View existing roles");
Action action13 = new Action("Update Role", "modify_role", "Modify role details");
Action action14 = new Action("Delete Role", "delete_role", "Delete roles");
Action action15=new Action("Read Action List","view_role_list","Read role List");

SystemAction action6 = new SystemAction("Create System Action", "create_action", "Create new system actions");
SystemAction action7 = new SystemAction("Read System Action", "view_action", "View existing system actions");
SystemAction action8 = new SystemAction("Update System Action", "modify_action", "Modify system action details");
SystemAction action9 = new SystemAction("Delete System Action", "delete_action", "Delete system actions");
SystemAction action10=new SystemAction("Read System Action List","view_action_list","Read System action List");
Action action6 = new Action("Create Action", "create_action", "Create new actions");
Action action7 = new Action("Read Action", "view_action", "View existing actions");
Action action8 = new Action("Update Action", "modify_action", "Modify action details");
Action action9 = new Action("Delete Action", "delete_action", "Delete actions");
Action action10=new Action("Read Action List","view_action_list","Read action List");


systemActionRepository.saveAll(List.of(action1,action2,action3,action4,action5 ,action6,action7,action8,action9,action10 ,action11,action12,action13,action14,action15));
Set<SystemAction> systemActionSet=new HashSet<SystemAction>(systemActionRepository.findAll());
SystemRole role=new SystemRole("SuperUser","superuser","SuperUser",systemActionSet.stream().map(SystemAction::getCode).collect(Collectors.toSet()));
systemRoleRepository.save(role);
Optional<SystemRole> roleOpt=systemRoleRepository.findByCodeIgnoreCase("superuser");
actionRepository.saveAll(List.of(action1,action2,action3,action4,action5 ,action6,action7,action8,action9,action10 ,action11,action12,action13,action14,action15));
Set<Action> actionSet =new HashSet<Action>(actionRepository.findAll());
Role role=new Role("SuperUser","superuser","SuperUser", actionSet.stream().map(Action::getCode).collect(Collectors.toSet()));
roleRepository.save(role);
Optional<Role> roleOpt= roleRepository.findByCodeIgnoreCase("superuser");
if(roleOpt.isPresent()){
Set<SystemRole> roles=new HashSet<>();
Set<Role> roles=new HashSet<>();
roles.add(roleOpt.get());
// Create an encoder with strength 16
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(16);
// Create an encoder with strength 10
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(10);
String password = encoder.encode("kush");
SystemUser user=new SystemUser("kush",password,"kushraj1204@gmail.com");
User user=new User("kush",password,"kushraj1204@gmail.com");
user.setEnabled(true);
user.setRoles(roles);
systemUserRepository.save(user);
userRepository.save(user);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http,

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(16);
return new BCryptPasswordEncoder();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.university_connect.config.jwt;

import edu.university_connect.model.SecurityUser;
import edu.university_connect.model.TokenType;
import edu.university_connect.model.enums.TokenType;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package edu.university_connect.controller;

import edu.university_connect.model.enums.AppStatusCode;
import edu.university_connect.model.contract.dto.ActionDto;
import edu.university_connect.model.contract.request.action.ActionCreateRequest;
import edu.university_connect.model.contract.request.action.ActionUpdateRequest;
import edu.university_connect.model.contract.response.ApiResponse;
import edu.university_connect.service.MessagingService;
import edu.university_connect.service.action.ActionService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/action")
@CrossOrigin
@Slf4j
public class ActionController {
private final ActionService service;

private final MessagingService messagingService;

public ActionController(ActionService service, MessagingService messagingService) {
this.service = service;
this.messagingService = messagingService;
}
@GetMapping("/all")
public ResponseEntity<ApiResponse<List<ActionDto>>> getAll() {
List<ActionDto> response= service.getAll();
ApiResponse<List<ActionDto>> apiResponse = new ApiResponse<List<ActionDto>>();
apiResponse.setResponseData(response);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20001,new String[]{"action"}));
return ResponseEntity.ok(apiResponse);
}

@GetMapping("")
public ResponseEntity<ApiResponse<Page<ActionDto>>> getPage(Pageable pageableReq) {
Pageable pageable = PageRequest.of(pageableReq.getPageNumber()>0? pageableReq.getPageNumber()-1 : 0,
pageableReq.getPageSize() ,
pageableReq.getSort());
Page<ActionDto> response= service.getPage(pageable);
ApiResponse<Page<ActionDto>> apiResponse = new ApiResponse<Page<ActionDto>>();
apiResponse.setResponseData(response);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20001,new String[]{"action"}));
return ResponseEntity.ok(apiResponse);

}
@PostMapping("")
public ResponseEntity<ApiResponse<ActionDto>> create(@Valid @RequestBody ActionCreateRequest createRequest) {
ActionDto response= service.create(createRequest);
ApiResponse<ActionDto> apiResponse = new ApiResponse<ActionDto>();
apiResponse.setResponseData(response);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20002,new String[]{"action"}));
return ResponseEntity.ok(apiResponse);

}

@GetMapping("/{id}")
public ResponseEntity<ApiResponse<ActionDto>> get(@PathVariable Long id) {
ActionDto response= service.getById(id);
ApiResponse<ActionDto> apiResponse = new ApiResponse<ActionDto>();
apiResponse.setResponseData(response);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20003,new String[]{"action"}));
return ResponseEntity.ok(apiResponse);

}

@PutMapping("/{id}")
public ResponseEntity<ApiResponse<ActionDto>> update(@Valid @RequestBody ActionUpdateRequest updateRequest,
@PathVariable Long id) {
ActionDto response= service.update(id,updateRequest);
ApiResponse<ActionDto> apiResponse = new ApiResponse<ActionDto>();
apiResponse.setResponseData(response);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20004,new String[]{"action"}));
return ResponseEntity.ok(apiResponse);
}

@DeleteMapping("/{id}")
public ResponseEntity<ApiResponse<Boolean>> delete(@PathVariable Long id) {
boolean response= service.delete(id);
ApiResponse<Boolean> apiResponse = new ApiResponse<Boolean>();
apiResponse.setResponseData(response);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20005,new String[]{"action"}));
return ResponseEntity.ok(apiResponse);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package edu.university_connect.controller;

import edu.university_connect.model.AppStatusCode;
import edu.university_connect.model.enums.AppStatusCode;
import edu.university_connect.model.contract.request.auth.AuthenticationRequest;
import edu.university_connect.model.contract.request.auth.RefreshTokenRequest;
import edu.university_connect.model.contract.response.common.ApiResponse;
import edu.university_connect.model.contract.response.common.AuthenticationResponse;
import edu.university_connect.model.contract.response.ApiResponse;
import edu.university_connect.model.contract.response.AuthenticationResponse;
import edu.university_connect.service.MessagingService;
import edu.university_connect.service.impl.AuthServiceImpl;
import edu.university_connect.service.auth.AuthServiceImpl;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package edu.university_connect.controller;

import edu.university_connect.model.enums.AppStatusCode;
import edu.university_connect.model.contract.dto.RoleDto;
import edu.university_connect.model.contract.request.role.RoleCreateRequest;
import edu.university_connect.model.contract.request.role.RoleUpdateRequest;
import edu.university_connect.model.contract.response.ApiResponse;
import edu.university_connect.service.MessagingService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/role")
@CrossOrigin
@Slf4j
public class RoleController {
private final MessagingService.RoleService service;

private final MessagingService messagingService;

public RoleController(MessagingService.RoleService service, MessagingService messagingService) {
this.service = service;
this.messagingService = messagingService;
}
@GetMapping("/all")
public ResponseEntity<ApiResponse<List<RoleDto>>> getAll() {
List<RoleDto> response= service.getAll();
ApiResponse<List<RoleDto>> apiResponse = new ApiResponse<List<RoleDto>>();
apiResponse.setResponseData(response);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20001,new String[]{"role"}));
return ResponseEntity.ok(apiResponse);
}

@GetMapping("")
public ResponseEntity<ApiResponse<Page<RoleDto>>> getPage(Pageable pageableReq) {
Pageable pageable = PageRequest.of(pageableReq.getPageNumber()>0? pageableReq.getPageNumber()-1 : 0,
pageableReq.getPageSize() ,
pageableReq.getSort());
Page<RoleDto> response= service.getPage(pageable);
ApiResponse<Page<RoleDto>> apiResponse = new ApiResponse<Page<RoleDto>>();
apiResponse.setResponseData(response);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20001,new String[]{"role"}));
return ResponseEntity.ok(apiResponse);

}
@PostMapping("")
public ResponseEntity<ApiResponse<RoleDto>> create(@Valid @RequestBody RoleCreateRequest createRequest) {
RoleDto response= service.create(createRequest);
ApiResponse<RoleDto> apiResponse = new ApiResponse<RoleDto>();
apiResponse.setResponseData(response);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20002,new String[]{"role"}));
return ResponseEntity.ok(apiResponse);

}

@GetMapping("/{id}")
public ResponseEntity<ApiResponse<RoleDto>> get(@PathVariable Long id) {
RoleDto response= service.getById(id);
ApiResponse<RoleDto> apiResponse = new ApiResponse<RoleDto>();
apiResponse.setResponseData(response);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20003,new String[]{"role"}));
return ResponseEntity.ok(apiResponse);

}

@PutMapping("/{id}")
public ResponseEntity<ApiResponse<RoleDto>> update(@Valid @RequestBody RoleUpdateRequest updateRequest,
@PathVariable Long id) {
RoleDto response= service.update(id,updateRequest);
ApiResponse<RoleDto> apiResponse = new ApiResponse<RoleDto>();
apiResponse.setResponseData(response);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20004,new String[]{"role"}));
return ResponseEntity.ok(apiResponse);
}

@DeleteMapping("/{id}")
public ResponseEntity<ApiResponse<Boolean>> delete(@PathVariable Long id) {
boolean response= service.delete(id);
ApiResponse<Boolean> apiResponse = new ApiResponse<Boolean>();
apiResponse.setResponseData(response);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20005,new String[]{"role"}));
return ResponseEntity.ok(apiResponse);
}

}
Loading