package com.example.kafka.service;

import com.example.kafka.entity.Department;
import com.example.kafka.repository.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public Mono<Department> createDepartment(Department department) {
        return departmentRepository.save(department);
    }

    @Override
    public Flux<Department> getAllDepartment() {
        return departmentRepository.findAll();
    }

    @Override
    public Mono<Department> findById(String departmentId) {
        return departmentRepository.findById(departmentId);
    }

    @Override
    public Mono<Department> updateDepartment(String departmentId, Department department) {
        return departmentRepository.findById(departmentId).flatMap(DBdepartment -> {
           DBdepartment.setName(department.getName());
           return departmentRepository.save(DBdepartment);
        });
    }
}
