package com.r11.tools.service;

import com.r11.tools.controller.RequestHistoryController;
import com.r11.tools.mappers.RequestHistoryMapper;
import com.r11.tools.model.RequestHistory;
import com.r11.tools.model.RequestHistoryDTO;
import com.r11.tools.repository.RequestHistoryRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * Spring Service for {@link RequestHistoryController}. Contains logic required for quering
 * the database for {@link RequestHistory} objects
 * @author Rafał Żukowicz
 * @author Mikołaj Widła
 */
@Service
@AllArgsConstructor
public class RequestHistoryServiceImpl implements RequestHistoryService {

    private final RequestHistoryRepository repository;
    private final RequestHistoryMapper requestMapper;

    @Override
    public List<RequestHistory> getHistoryRecordsByUUID(String uuid) {
        List<RequestHistory> history = repository.findAllByClientUUID(uuid);
        Collections.sort(history);
        return history;
    }

    @Override
    public void saveRequest(RequestHistoryDTO requestDTO) {
        repository.save(requestMapper.requestHistoryDTOToRequestHistory(requestDTO));
    }
}
