Add message history
This commit is contained in:
@@ -1,12 +1,19 @@
|
||||
package be.naaturel.boardmateapi.services;
|
||||
|
||||
import be.naaturel.boardmateapi.common.exceptions.ServiceException;
|
||||
import be.naaturel.boardmateapi.common.helpers.Logger;
|
||||
import be.naaturel.boardmateapi.common.models.Message;
|
||||
import be.naaturel.boardmateapi.repository.MessageRepo;
|
||||
import be.naaturel.boardmateapi.repository.dtos.MessageDto;
|
||||
import be.naaturel.boardmateapi.repository.mappings.MessageMapper;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class MessageService {
|
||||
|
||||
@@ -23,9 +30,21 @@ public class MessageService {
|
||||
MessageDto result = repo.save(dto);
|
||||
return result.getId();
|
||||
} catch (Exception e){
|
||||
Logger.displayError(Arrays.toString(e.getStackTrace()));
|
||||
throw new ServiceException("Failed to save message : " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Message> getHistory(String clientId, int limit) throws ServiceException {
|
||||
try {
|
||||
Pageable pageable = PageRequest.of(0, limit, Sort.by(Sort.Direction.DESC, "timeStamp"));
|
||||
List<MessageDto> messages = repo.findByClientId(clientId, pageable);
|
||||
|
||||
return MessageMapper.toModels(messages);
|
||||
} catch (Exception e){
|
||||
Logger.displayError(Arrays.toString(e.getStackTrace()));
|
||||
throw new ServiceException("Failed to retrieve messages history : " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user