Skip to content
Snippets Groups Projects
Commit 646be814 authored by Marnuri Nitish -'s avatar Marnuri Nitish -
Browse files

Create api methods for delete and update

parent 13ff2e3d
No related branches found
No related tags found
No related merge requests found
package polish_community_group_11.polish_community.admin.controllers;
import org.springframework.web.bind.annotation.*;
import polish_community_group_11.polish_community.admin.models.AdminBoard;
import polish_community_group_11.polish_community.admin.models.ManageUser;
import polish_community_group_11.polish_community.admin.services.AdminService;
@RestController
public class AdminApiController {
private final AdminService adminService;
public AdminApiController(AdminService adminService) {
this.adminService = adminService;
}
@PutMapping("admin/edit/role/{role_name}")
public void changeUserRole(@PathVariable("role_name") String role_name,
@ModelAttribute ManageUser user){
adminService.updateUserRole(user,role_name);
}
@DeleteMapping("admin/delete/{user_id}")
public void removeUser(@PathVariable("user_id") int user_id){
adminService.deleteUser(user_id);
}
}
......@@ -9,4 +9,6 @@ import java.util.List;
public interface AdminRepository {
List<ManageUser> getUserManagementInfo() throws SQLException;
AdminBoard getBoardManagementInfo() throws SQLException;
int updateUserRole(ManageUser user);
void deleteUser(int userId);
}
......@@ -31,6 +31,7 @@ public class AdminRepositoryImpl implements AdminRepository {
rs.getString("fullname"),
rs.getString("email"),
rs.getBoolean("enabled"),
rs.getInt("role_id"),
rs.getString("role_name")
);
};
......@@ -93,4 +94,14 @@ public class AdminRepositoryImpl implements AdminRepository {
}
return dashboard;
}
public int updateUserRole(ManageUser user){
String sql="UPDATE users SET role_id=? WHERE id =?";
return jdbc.update(sql,user.getRole_id(),user.getId());
}
public void deleteUser(int userId){
String sql="DELETE FROM users WHERE id =?";
jdbc.update(sql,userId);
}
}
......@@ -12,5 +12,6 @@ public class ManageUser {
private String fullName;
private String email;
private Boolean enabled;
private int role_id;
private String role;
}
......@@ -9,4 +9,6 @@ import java.util.List;
public interface AdminService {
List<ManageUser> getUserManagementInfo() throws SQLException;
AdminBoard getBoardManagementInfo() throws SQLException;
void updateUserRole(ManageUser user,String roleName);
void deleteUser(int userId);
}
......@@ -21,4 +21,18 @@ public class AdminServiceImpl implements AdminService {
public AdminBoard getBoardManagementInfo() throws SQLException{
return adminRepository.getBoardManagementInfo();
}
public void updateUserRole(ManageUser user, String roleName){
boolean needsUpdate=roleName.toLowerCase().equals(user.getRole().toLowerCase())?
false:true;
if(needsUpdate){
int updatedRoleId=roleName.toLowerCase().equals("admin")?1:2;
user.setRole_id(updatedRoleId);
adminRepository.updateUserRole(user);
}
}
public void deleteUser(int userId){
adminRepository.deleteUser(userId);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment