diff --git a/src/main/java/com/cardiff/client_project/controller/admin/SuperAdminController.java b/src/main/java/com/cardiff/client_project/controller/admin/SuperAdminController.java index a33e0be93552abfb37a66157ef00f73d996dd59f..1c01ed8b716310b0badffa0e2c83adbd53949a28 100644 --- a/src/main/java/com/cardiff/client_project/controller/admin/SuperAdminController.java +++ b/src/main/java/com/cardiff/client_project/controller/admin/SuperAdminController.java @@ -71,4 +71,15 @@ public class SuperAdminController { Result result= superAdminService.update(selectDTO); return result; } + + /** + * page selete + */ + @GetMapping("/pageSelect") + public Result pageSelect(@RequestParam String type,@RequestParam Integer pageSize,@RequestParam Integer pageNumber){ + Result result=superAdminService.selectPage(type,pageSize,pageNumber); + return result; + } } + + diff --git a/src/main/java/com/cardiff/client_project/service/SuperAdminService.java b/src/main/java/com/cardiff/client_project/service/SuperAdminService.java index 02968edbc4de931bdcbdec570f07a3b031b4fe6e..94d22a2d3dcb9d68d9cd789f8a3501bfb5c9bbfe 100644 --- a/src/main/java/com/cardiff/client_project/service/SuperAdminService.java +++ b/src/main/java/com/cardiff/client_project/service/SuperAdminService.java @@ -46,4 +46,6 @@ public interface SuperAdminService { * @return */ Result selectByItem(SelectDTO selectDTO); + + Result selectPage(String type, Integer pageSize, int i); } diff --git a/src/main/java/com/cardiff/client_project/service/imp/HospitalServiceImpl.java b/src/main/java/com/cardiff/client_project/service/imp/HospitalServiceImpl.java index 79fdb9d580c3f58e0e96ec2b4f0858a331c2bc74..bc6eedad25346d9f654db308b981a11cf84dc45a 100644 --- a/src/main/java/com/cardiff/client_project/service/imp/HospitalServiceImpl.java +++ b/src/main/java/com/cardiff/client_project/service/imp/HospitalServiceImpl.java @@ -1,4 +1,4 @@ -package com.cardiff.client_project.service.impl; +package com.cardiff.client_project.service.imp; import com.cardiff.client_project.mapper.HospitalMapper; import com.cardiff.client_project.pojo.dto.HospitalDTO; diff --git a/src/main/java/com/cardiff/client_project/service/imp/SuperAdminServiceImp.java b/src/main/java/com/cardiff/client_project/service/imp/SuperAdminServiceImp.java index 4589db4183759ad1b7be5687419df2ecf6cebc41..d238ec423a368aca939e788060b03ba5f86a8fda 100644 --- a/src/main/java/com/cardiff/client_project/service/imp/SuperAdminServiceImp.java +++ b/src/main/java/com/cardiff/client_project/service/imp/SuperAdminServiceImp.java @@ -200,5 +200,25 @@ public class SuperAdminServiceImp implements SuperAdminService { } } + + @Override + public Result selectPage(String type, Integer pageSize, int pageNumber) { + if(type.equals("commonAdmin")){ + List<CommonAdmin> commonAdmins = superAdminMapper.selectPageCommonAdmin(pageSize, pageNumber); + //get all and count items + List<AdminVO> adminVOS = superAdminMapper.selectAllAdmin(); + if(commonAdmins.size()>0){ + System.out.println(adminVOS.size()); + Integer count=adminVOS.size()%pageSize==0?adminVOS.size()/pageSize:adminVOS.size()/pageSize+1; + return Result.success(count.toString(),commonAdmins); + } + } + if(type.equals("hospital")){ + + } + return Result.error(ResponseCode.ERROR); + } + + } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index b0f4bde722ce1aad6861d9e9ce85187f71605cac..ae3309bbd3927093b2b55a5e17ae320797fbc6c0 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -3,4 +3,4 @@ healthcare: url: jdbc:mysql://localhost:3306/health_care port: 3306 username: root - password: 472500 \ No newline at end of file + password: root \ No newline at end of file diff --git a/src/main/resources/static/js/mainHospitalView.js b/src/main/resources/static/js/mainHospitalView.js index 1e6a9e183c6874cc0af7b47a9784c149e4a06117..22d8d4702ee4860fbbde1c75a974befcb15e194c 100644 --- a/src/main/resources/static/js/mainHospitalView.js +++ b/src/main/resources/static/js/mainHospitalView.js @@ -280,7 +280,7 @@ function updateTable(data) { <td class="tdOption"> <label id="edit" class="btn">Edit</label> <label id="delete" class="btn">Delete</label> - <label id="freeze" class="btn">Freeze</label> + <label id="freeze" class="btn">Change status</label> </td> </tr> `; diff --git a/src/main/resources/static/js/mainSupAdminView.js b/src/main/resources/static/js/mainSupAdminView.js index 8d4328f4f27bf1ae04881fd531bca973f0f1062f..2005f653be22fff349de1d1e1cf654b65587ec7a 100644 --- a/src/main/resources/static/js/mainSupAdminView.js +++ b/src/main/resources/static/js/mainSupAdminView.js @@ -247,23 +247,36 @@ function updateTable(data) { <td class="tdOption"> <label id="edit" class="btn">Edit</label> <label id="delete" class="btn">Delete</label> - <label id="freeze" class="btn">Freeze</label> + <label id="freeze" class="btn">Change status</label> </td> </tr> `; tbody.append(tr); }); } -$(document).on("change","#page-size",function (){ - let pageSize=document.getElementById("page-size").value + +function pageSelect(pageSize,paginationDiv,pageNumber) { + $.ajax({ contentType: "application/json", - url: `/superAdmin/pageSelect?type=commonAdmin&pageSize=${pageSize}`, + url: `/superAdmin/pageSelect?type=commonAdmin&pageSize=${pageSize}&pageNumber=${pageNumber}`, type: "GET", dataType: "Json", success: function (data) { - console.log(data) + console.log(data) if (data.code != 0) { + console.log(data.code) + console.log(data.data) + paginationDiv.innerHTML = ""; //clear old button + for (let i = 1; i <= data.msg; i++) { + const button = document.createElement("button"); + button.textContent = i; + button.style.margin = "0 5px"; + button.style.padding = "5px 10px"; + button.style.cursor = "pointer"; + button.onclick = () => pageSelect(pageSize,paginationDiv,button.textContent); + paginationDiv.appendChild(button); + } updateTable(data.data) //location.reload() } else { @@ -271,5 +284,10 @@ $(document).on("change","#page-size",function (){ } }, }) +} +$(document).on("change","#page-size",function (){ + let pageSize=document.getElementById("page-size").value + let paginationDiv = document.getElementById("pagination"); + pageSelect(pageSize,paginationDiv,"1"); }) \ No newline at end of file diff --git a/src/main/resources/templates/mainHospitalView.html b/src/main/resources/templates/mainHospitalView.html index d53b851731370c57dfc2d9123f68a6a57be608e0..8531c7773fb71da8cabe13a2c7af3edc4bf31b7f 100644 --- a/src/main/resources/templates/mainHospitalView.html +++ b/src/main/resources/templates/mainHospitalView.html @@ -60,7 +60,7 @@ <td class="tdOption"> <label id="edit" class="btn">Edit</label> <label id="delete" class="btn">Delete</label> - <label id="freeze" class="btn">Freeze</label> + <label id="freeze" class="btn">Change status</label> </td> </tr> </tbody> diff --git a/src/main/resources/templates/mainSupAdminView.html b/src/main/resources/templates/mainSupAdminView.html index c0d52d8c8936318bcfcdc2a215e498caa8fafdb3..d3d05a2464b6386f182cf8d9f090d41fdd9e2a8a 100644 --- a/src/main/resources/templates/mainSupAdminView.html +++ b/src/main/resources/templates/mainSupAdminView.html @@ -48,7 +48,7 @@ <td class="tdOption"> <label id="edit" class="btn">Edit</label> <label id="delete" class="btn">Delete</label> - <label id="freeze" class="btn">Freeze</label> + <label id="freeze" class="btn">Change status</label> </td> </tr> @@ -91,13 +91,22 @@ <button type="button" id="addAdmin" style="margin-right: 170px;margin-left: 20px">Submit</button> <button type="button" onclick="location.reload()">Cancel</button> </div> -<div> - <select id="page-size"> +<div style="display: flex; align-items: center; gap: 10px; justify-content: center;"> + <select id="page-size" style="background-color: #f9f9f9; + border: 1px solid #ccc; + border-radius: 4px; + padding: 8px 12px; + font-size: 14px; + color: #333; + cursor: pointer; + width: 150px; + transition: all 0.3s ease;"> <option value="">items per page</option> <option value="1">1</option> <option value="2">2</option> <option value="15">15</option> </select> + <div id="pagination" style="margin-top: 10px;"></div> </div> <script src="/js/mainSupAdminView.js"></script> </body> diff --git a/src/test/java/com/cardiff/client_project/SuperAdminControllerTest.java b/src/test/java/com/cardiff/client_project/SuperAdminControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c173f9d8e76571d6e34666518a1e1cc7089191a3 --- /dev/null +++ b/src/test/java/com/cardiff/client_project/SuperAdminControllerTest.java @@ -0,0 +1,106 @@ +package com.cardiff.client_project; + +import com.cardiff.client_project.pojo.dto.SelectDTO; +import com.cardiff.client_project.pojo.dto.SignUserDTO; +import com.cardiff.client_project.service.SuperAdminService; +import com.cardiff.client_project.utils.Result; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentMatchers; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.Arrays; +import java.util.List; + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +public class SuperAdminControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private SuperAdminService superAdminService; + + @Test + void testSignIn() throws Exception { + // Construct test data + SignUserDTO signDTO = new SignUserDTO(); + signDTO.setName("testUser"); + signDTO.setPassword("password123"); + + Result expectedResult = Result.success("User registered successfully"); + when(superAdminService.insertUserInform(any(SignUserDTO.class))).thenReturn(expectedResult); + + // post + mockMvc.perform(post("/superAdmin/sign") + .contentType(MediaType.APPLICATION_JSON) + .content(new ObjectMapper().writeValueAsString(signDTO))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.msg").value("User registered successfully")); + + // Verify that the service layer is invoked + verify(superAdminService, times(1)).insertUserInform(any(SignUserDTO.class)); + } + + @Test + void testDeleteHospitalById() throws Exception { + List<Integer> ids = Arrays.asList(1, 2, 3); + Result mockResult = Result.success("successfully"); + when(superAdminService.deleteById(eq(ids), eq("hospital"))).thenReturn(mockResult); + + mockMvc.perform(delete("/superAdmin/deleteHospitalById") + .contentType(MediaType.APPLICATION_JSON) + .content(new ObjectMapper().writeValueAsString(ids))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(1)) + .andExpect(jsonPath("$.msg").value("successfully")); + + verify(superAdminService, times(1)).deleteById(eq(ids), eq("hospital")); + } + + @Test + void testSelectByItem() throws Exception { + SelectDTO selectDTO = new SelectDTO(); + selectDTO.setName("testQuery"); + Result mockResult = Result.success("Query successful"); + when(superAdminService.selectByItem(any(SelectDTO.class))).thenReturn(mockResult); + + mockMvc.perform(post("/superAdmin/select") + .contentType(MediaType.APPLICATION_JSON) + .content(new ObjectMapper().writeValueAsString(selectDTO))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(1)) + .andExpect(jsonPath("$.msg").value("Query successful")); + + verify(superAdminService, times(1)).selectByItem(any(SelectDTO.class)); + } + + @Test + void testUpdateById() throws Exception { + SelectDTO selectDTO = new SelectDTO(); + selectDTO.setId(1); + selectDTO.setName("newData"); + Result mockResult = Result.success("Update successful"); + when(superAdminService.update(any(SelectDTO.class))).thenReturn(mockResult); + + mockMvc.perform(put("/superAdmin/update") + .contentType(MediaType.APPLICATION_JSON) + .content(new ObjectMapper().writeValueAsString(selectDTO))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(1)) + .andExpect(jsonPath("$.msg").value("Update successful")); + + verify(superAdminService, times(1)).update(any(SelectDTO.class)); + } +}