SYBD-labs/src/main/java/com/example/sybd/services/GroupServiceService.java
2023-05-16 13:53:19 +04:00

55 lines
1.6 KiB
Java

package com.example.sybd.services;
import com.example.sybd.models.ServiceGroup;
import com.example.sybd.repository.ServiceGroupRepository;
import com.example.sybd.utils.mappingUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class GroupServiceService implements IService<ServiceGroup, Long> {
private final ServiceGroupRepository serviceGroupRepository;
public GroupServiceService(ServiceGroupRepository serviceGroupRepository) {
this.serviceGroupRepository = serviceGroupRepository;
}
public ServiceGroup create(@NotNull String name) {
ServiceGroup serviceGroup = new ServiceGroup();
serviceGroup.setName(name);
return serviceGroupRepository.save(serviceGroup);
}
@Override
public @NotNull List<ServiceGroup> all() {
return mappingUtils.iterableToList(serviceGroupRepository.findAll());
}
@Override
public @Nullable ServiceGroup get(Long id) {
return serviceGroupRepository.findById(id).orElse(null);
}
@Override
public @Nullable ServiceGroup delete(Long id) {
ServiceGroup serviceGroup = serviceGroupRepository.findById(id).orElse(null);
if (serviceGroup == null)
return null;
serviceGroupRepository.deleteById(id);
return serviceGroup;
}
@Override
public @Nullable ServiceGroup update(ServiceGroup other) {
ServiceGroup serviceGroup = serviceGroupRepository.findById(other.getServicesGroup_id()).orElse(null);
if (serviceGroup == null)
return null;
serviceGroup.setName(other.getName());
return serviceGroupRepository.save(serviceGroup);
}
}