ohctechv3/.svn/pristine/9f/9f4c9c552c18043e2e067fd2f35a1ee631deb3ca.svn-base
2024-10-28 15:03:36 +05:30

68 lines
2.6 KiB
Plaintext

package com.healthcare.ohctech.controller;
import com.healthcare.ohctech.dto.CityDto;
import com.healthcare.ohctech.entity.City;
import com.healthcare.ohctech.service.impl.AuthServiceImpl;
import com.healthcare.ohctech.service.impl.CityServiceImpl;
import com.healthcare.ohctech.util.PaginationUtil;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/cities")
public class CityController {
@Autowired
private CityServiceImpl cityServiceImpl;
@Autowired
private AuthServiceImpl authServiceImpl;
@GetMapping("/{cityId}")
public ResponseEntity<?> getCityById(@PathVariable Long cityId) {
City city = cityServiceImpl.getCityById(cityId);
if (city != null) {
return new ResponseEntity<>(city, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@GetMapping
public ResponseEntity<?> getAllCities(@RequestParam(required = false) Integer page,
@RequestParam(required = false) Integer size,
@RequestParam(required = false) String sortBy,
@RequestParam(required = false) String sortOrder) {
Pageable pageable = PaginationUtil.getPageableWithDefaults(page, size, sortBy, sortOrder);
Page<City> cityPage = cityServiceImpl.getAllCities(pageable);
Map<String, Object> response = PaginationUtil.getPageResponse(cityPage);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@PostMapping
public ResponseEntity<?> addCity(@Valid @RequestBody CityDto cityDto) {
Long userId = authServiceImpl.getCurrentUserId();
cityServiceImpl.addCity(cityDto,userId);
return new ResponseEntity<>("Saved Successfully", HttpStatus.CREATED);
}
@PutMapping("/{cityId}")
public ResponseEntity<?> updateCity(@Valid @RequestBody CityDto cityDto) {
Long userId = authServiceImpl.getCurrentUserId();
cityServiceImpl.updateCity(cityDto,userId);
return new ResponseEntity<>("Updated Successfully", HttpStatus.OK);
}
@DeleteMapping("/{cityId}")
public ResponseEntity<?> deleteCity(@PathVariable Long cityId) {
cityServiceImpl.deleteCity(cityId);
return new ResponseEntity<>(HttpStatus.OK);
}
}