develop #3
@ -6,11 +6,10 @@ import java.util.List;
|
|||||||
|
|
||||||
public interface OfficeService {
|
public interface OfficeService {
|
||||||
List<OfficeDTO> getAllOffice();
|
List<OfficeDTO> getAllOffice();
|
||||||
|
List<OfficeDTO> getAllSortedDistance(double userLatitude, double userLongitude);
|
||||||
OfficeDTO getById(long officeId);
|
OfficeDTO getById(long officeId);
|
||||||
|
|
||||||
OfficeDTO create(OfficeDTO officeDTO);
|
OfficeDTO create(OfficeDTO officeDTO);
|
||||||
OfficeDTO update(long officeId, OfficeDTO officeDTO);
|
OfficeDTO update(long officeId, OfficeDTO officeDTO);
|
||||||
void delete(long officeId);
|
void delete(long officeId);
|
||||||
|
|
||||||
void patchBlockVolunteer(long id, boolean blockStatus);
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
|
import com.example.nto.aspect.annotation.LogExample;
|
||||||
|
import com.example.nto.domain.entity.Office;
|
||||||
|
import com.example.nto.domain.exception.ResourceNotFoundException;
|
||||||
import com.example.nto.dto.entity.OfficeDTO;
|
import com.example.nto.dto.entity.OfficeDTO;
|
||||||
import com.example.nto.dto.mappers.OfficeMapper;
|
import com.example.nto.dto.mappers.OfficeMapper;
|
||||||
import com.example.nto.repository.OfficeRepository;
|
import com.example.nto.repository.OfficeRepository;
|
||||||
@ -7,6 +10,8 @@ import com.example.nto.service.OfficeService;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -16,32 +21,63 @@ public class OfficeServiceImpl implements OfficeService {
|
|||||||
private final OfficeRepository officeRepository;
|
private final OfficeRepository officeRepository;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@LogExample
|
||||||
public List<OfficeDTO> getAllOffice() {
|
public List<OfficeDTO> getAllOffice() {
|
||||||
return officeRepository.findAll().stream().map(OfficeMapper::convertToDTO).collect(Collectors.toList());
|
return officeRepository.findAll().stream().map(OfficeMapper::convertToDTO).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
public List<OfficeDTO> getAllSortedDistance(double userLatitude, double userLongitude) {
|
||||||
|
return officeRepository.findAll().stream().map(OfficeMapper::convertToDTO)
|
||||||
|
.sorted(createComparator(new Point2D.Double(userLatitude, userLongitude))).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@LogExample
|
||||||
public OfficeDTO getById(long officeId) {
|
public OfficeDTO getById(long officeId) {
|
||||||
return null;
|
return officeRepository.findById(officeId).map(OfficeMapper::convertToDTO)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException("Офис с id (" + officeId + ") не найден!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@LogExample
|
||||||
public OfficeDTO create(OfficeDTO officeDTO) {
|
public OfficeDTO create(OfficeDTO officeDTO) {
|
||||||
return null;
|
return OfficeMapper.convertToDTO(officeRepository.save(OfficeMapper.convertFromDTO(officeDTO)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@LogExample
|
||||||
public OfficeDTO update(long officeId, OfficeDTO officeDTO) {
|
public OfficeDTO update(long officeId, OfficeDTO officeDTO) {
|
||||||
return null;
|
Office office = officeRepository.findById(officeId)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException("Офис с id (" + officeId + ") не найден!"));
|
||||||
|
|
||||||
|
office.setName(officeDTO.getName());
|
||||||
|
office.setDescription(officeDTO.getDescription());
|
||||||
|
office.setAddress(officeDTO.getAddress());
|
||||||
|
office.setLatitude(officeDTO.getLatitude());
|
||||||
|
office.setLongitude(officeDTO.getLongitude());
|
||||||
|
|
||||||
|
// linkLogo меняется в отдельном запросе
|
||||||
|
|
||||||
|
office.setTelephone(officeDTO.getTelephone());
|
||||||
|
office.setEmail(officeDTO.getEmail());
|
||||||
|
|
||||||
|
return OfficeMapper.convertToDTO(officeRepository.save(office));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@LogExample
|
||||||
public void delete(long officeId) {
|
public void delete(long officeId) {
|
||||||
|
officeRepository.findById(officeId).orElseThrow(() -> new ResourceNotFoundException("Офис с id (" + officeId + ") не найден!"));
|
||||||
|
officeRepository.deleteById(officeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private static Comparator<OfficeDTO> createComparator(Point2D point) {
|
||||||
public void patchBlockVolunteer(long id, boolean blockStatus) {
|
return (office0, office1) -> {
|
||||||
|
Point2D point0 = new Point2D.Double(office0.getLatitude(), office0.getLongitude());
|
||||||
|
Point2D point1 = new Point2D.Double(office1.getLatitude(), office1.getLongitude());
|
||||||
|
|
||||||
|
return Double.compare(point0.distanceSq(point), point1.distanceSq(point));
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user