Skip to content
Snippets Groups Projects
Commit 4186a87e authored by Burhan Akbar's avatar Burhan Akbar
Browse files

Bug fix search function

Add and delete hospital

See merge request !78
parent 9038e2b8
No related branches found
No related tags found
No related merge requests found
......@@ -3,18 +3,24 @@ package com.cardiff.client_project.controller.nursinghome;
import com.cardiff.client_project.pojo.dto.HospitalDTO;
import com.cardiff.client_project.service.NursingHomeService;
import com.cardiff.client_project.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/hospitals")
@CrossOrigin(origins = "*")
public class NursingHomeController {
private final NursingHomeService nursingHomeService;
@Autowired
private NursingHomeService nursingHomeService;
public NursingHomeController(NursingHomeService nursingHomeService) {
this.nursingHomeService = nursingHomeService;
}
// READ - Get available beds
@GetMapping("/available")
......@@ -58,4 +64,18 @@ public class NursingHomeController {
return Result.error("Error deleting hospital: " + e.getMessage());
}
}
@GetMapping("/search")
public Result<List<HospitalDTO>> searchHospitals(@RequestParam(required = false) String name) {
try {
log.info("Searching hospitals with name: {}", name);
if (name == null || name.trim().isEmpty()) {
return nursingHomeService.getAvailableBeds();
}
return nursingHomeService.searchHospitals(name.trim());
} catch (Exception e) {
log.error("Error searching hospitals: ", e);
return Result.error("Error searching hospitals: " + e.getMessage());
}
}
}
......@@ -107,4 +107,36 @@ public class NursingHomeMapper {
""";
return jdbcTemplate.update(sql, currentPatients, hospitalId, currentPatients);
}
public List<HospitalDTO> searchHospitals(String name) {
String sql = """
SELECT
id,
name,
address,
phone,
maxAmount,
amountPatient
FROM hospital
WHERE status = 1
AND name LIKE CONCAT('%', ?, '%')
ORDER BY name ASC
""";
return jdbcTemplate.query(sql,
ps -> ps.setString(1, name),
(rs, rowNum) -> {
HospitalDTO dto = new HospitalDTO();
dto.setId(rs.getInt("id"));
dto.setName(rs.getString("name"));
dto.setLocation(rs.getString("address"));
dto.setPhone(rs.getString("phone"));
int maxAmount = rs.getInt("maxAmount");
int amountPatient = rs.getInt("amountPatient");
dto.setTotalBeds(maxAmount);
dto.setAvailableBeds(maxAmount - amountPatient);
dto.setOccupancyRate(calculateOccupancy(maxAmount, amountPatient));
return dto;
});
}
}
\ No newline at end of file
......@@ -11,4 +11,5 @@ public interface NursingHomeService {
Result<String> deletePatientById(List<Integer> ids);
Result<List<HospitalDTO>> getAllPatients();
Result<String> updateBedCount(int hospitalId, int currentPatients);
Result<List<HospitalDTO>> searchHospitals(String name);
}
\ No newline at end of file
......@@ -7,14 +7,22 @@ import com.cardiff.client_project.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.dao.DataAccessException;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
@Service
@Transactional
@Slf4j
public class NursingHomeServiceImp implements NursingHomeService {
private final NursingHomeMapper nursingHomeMapper;
@Autowired
private NursingHomeMapper nursingHomeMapper;
public NursingHomeServiceImp(NursingHomeMapper nursingHomeMapper) {
this.nursingHomeMapper = nursingHomeMapper;
}
@Override
public Result<List<HospitalDTO>> getAvailableBeds() {
......@@ -96,6 +104,29 @@ public class NursingHomeServiceImp implements NursingHomeService {
}
}
@Override
@Transactional(readOnly = true)
public Result<List<HospitalDTO>> searchHospitals(String name) {
try {
log.info("Searching hospitals with name: {}", name);
if (name == null || name.trim().isEmpty()) {
return Result.error("Search term cannot be empty");
}
List<HospitalDTO> hospitals = nursingHomeMapper.searchHospitals(name.trim());
if (hospitals.isEmpty()) {
log.info("No hospitals found matching: {}", name);
return Result.error("No hospitals found");
}
log.info("Found {} hospitals matching search term", hospitals.size());
return Result.success(hospitals);
} catch (Exception e) {
log.error("Error searching hospitals: ", e);
return Result.error("Failed to search hospitals");
}
}
private boolean validateHospitalData(HospitalDTO hospitalDTO) {
if (hospitalDTO == null) return false;
return hospitalDTO.getName() != null &&
......
......@@ -78,9 +78,6 @@ $(document).ready(function () {
}
function initializeEventHandlers() {
// Search Handler
$("#searchButton").click(handleSearch);
// Add Hospital Handler
$("#addHospitalBtn").click(function() {
$("#hospitalModal").fadeIn();
......@@ -159,17 +156,23 @@ $(document).ready(function () {
});
}
});
// Update event handler binding
$("#searchButton").click(handleSearch);
}
function handleSearch(event) {
event.preventDefault();
const searchTerm = $("#searchName").val().trim();
if (!searchTerm) {
loadHospitals();
return;
}
$.ajax({
url: `${API_BASE_URL}/search`,
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({ name: searchTerm }),
url: `${API_BASE_URL}/search?name=${encodeURIComponent(searchTerm)}`,
method: 'GET',
success: function(response) {
if(response && response.code === 1) {
updateTable(response.data || []);
......@@ -177,7 +180,10 @@ $(document).ready(function () {
showError('No hospitals found');
}
},
error: handleAjaxError
error: function(xhr) {
console.error('Search Error:', xhr);
showError('Search failed');
}
});
}
......@@ -326,9 +332,6 @@ $(document).ready(function () {
$('<style>').text(styles).appendTo('head');
// Initialize tooltips and other UI enhancements
$('[data-toggle="tooltip"]').tooltip();
// Add fade effects for smoother transitions
$('.table-container').hide().fadeIn();
});
......
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