TrainServiceImpl.java
package train.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import train.entity.TrainType;
import train.repository.TrainTypeRepository;
import java.util.List;
@Service
public class TrainServiceImpl implements TrainService {
@Autowired
private TrainTypeRepository repository;
private static final Logger LOGGER = LoggerFactory.getLogger(TrainServiceImpl.class);
@Override
public boolean create(TrainType trainType, HttpHeaders headers) {
boolean result = false;
if(trainType.getName().isEmpty()){
TrainServiceImpl.LOGGER.error("[create][Create train error][Train Type name not specified]");
return result;
}
if (repository.findByName(trainType.getName()) == null) {
TrainType type = new TrainType(trainType.getName(), trainType.getEconomyClass(), trainType.getConfortClass());
type.setAverageSpeed(trainType.getAverageSpeed());
repository.save(type);
result = true;
}
else {
TrainServiceImpl.LOGGER.error("[create][Create train error][Train already exists][TrainTypeId: {}]",trainType.getId());
}
return result;
}
@Override
public TrainType retrieve(String id, HttpHeaders headers) {
if (!repository.findById(id).isPresent()) {
TrainServiceImpl.LOGGER.error("[retrieve][Retrieve train error][Train not found][TrainTypeId: {}]",id);
return null;
} else {
return repository.findById(id).get();
}
}
@Override
public TrainType retrieveByName(String name, HttpHeaders headers) {
TrainType tt = repository.findByName(name);
if (tt == null) {
TrainServiceImpl.LOGGER.error("[retrieveByName][RetrieveByName error][Train not found][TrainTypeName: {}]", name);
return null;
} else {
return tt;
}
}
@Override
public List<TrainType> retrieveByNames(List<String> names, HttpHeaders headers) {
List<TrainType> tt = repository.findByNames(names);
if (tt == null || tt.isEmpty()) {
TrainServiceImpl.LOGGER.error("[retrieveByNames][RetrieveByNames error][Train not found][TrainTypeNames: {}]", names);
return null;
} else {
return tt;
}
}
@Override
@Transactional
public boolean update(TrainType trainType, HttpHeaders headers) {
boolean result = false;
if (repository.findById(trainType.getId()).isPresent()) {
TrainType type = new TrainType(trainType.getName(), trainType.getEconomyClass(), trainType.getConfortClass(), trainType.getAverageSpeed());
type.setId(trainType.getId());
repository.save(type);
result = true;
}
else {
TrainServiceImpl.LOGGER.error("[update][Update train error][Train not found][TrainTypeId: {}]",trainType.getId());
}
return result;
}
@Override
public boolean delete(String id, HttpHeaders headers) {
boolean result = false;
if (repository.findById(id).isPresent()) {
repository.deleteById(id);
result = true;
}
else {
TrainServiceImpl.LOGGER.error("[delete][Delete train error][Train not found][TrainTypeId: {}]",id);
}
return result;
}
@Override
public List<TrainType> query(HttpHeaders headers) {
return repository.findAll();
}
}