뷰단
$.post('/addressbook/excelUpload', JSON.stringify(data), function(result){
if(result.length) {
alert('저장되었습니다.');
$('#btnSearchAddress').click();
$('#_excelPopup_DialogDiv').modal('hide');
}else {
alert('이미 저장되어 있는 수신자입니다.');
}
});
Controller
@RestController
@RequestMapping("/addressbook")
@RequiredArgsConstructor
@Slf4j
@ResponseBody
@RequestMapping(value = "/excelUpload", method = RequestMethod.POST)
public ResponseEntity uploadList(@RequestBody AddressDto addressDto) {
List<Address> newAddressList = this.addressService.createExcelList(addressDto);
return new ResponseEntity<>(newAddressList, HttpStatus.OK);
}
Service
@Service
@Slf4j
@Transactional
@RequiredArgsConstructor
public List<Address> createExcelList(AddressDto addressDto) {
List<Address> newAddressList = new ArrayList<>();
Group group = addressDto.getGroup();
addressDto.getAddressList().forEach((addressItem) -> {
addressItem.setGroup(group);
if(!addressRepository.existsByPhoneNumberAndReceiverAndGroup(addressItem.getPhoneNumber().replaceAll("-","")
, addressItem.getReceiver(), addressItem.getGroup())) {
addressItem.setPhoneNumber(addressItem.getPhoneNumber().replaceAll("-",""));
newAddressList.add(Address.of(addressItem));
}
});
return addressRepository.saveAll(newAddressList);
}
Repository
@Transactional(readOnly = true)
public interface AddressRepository extends JpaRepository<Address, Long> {
boolean existsByPhoneNumberAndReceiverAndGroup(String phoneNumber, String receiver, Group group)
}