Package com.sivalabs.ssdemo.services

Source Code of com.sivalabs.ssdemo.services.MessageService

package com.sivalabs.ssdemo.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sivalabs.ssdemo.entities.Message;
import com.sivalabs.ssdemo.repositories.MessageRepository;
import com.sivalabs.ssdemo.repositories.UserRepository;

/**
* @author Siva
*
*/
@Service
@Transactional
public class MessageService
{
  @Autowired
  private UserRepository userRepository;

  @Autowired
  private MessageRepository messageRepository;

  public List<Message> getAllMessages()
  {
    return messageRepository.findAll(new Sort(Sort.Direction.DESC, "createdOn"));
  }

  public List<Message> getAllMessages(int size)
  {
    return getAllMessages(1, size);
  }

  public List<Message> getAllMessages(int page, int size)
  {
    Pageable pageable = new PageRequest(page, size, new Sort(Sort.Direction.DESC, "createdOn"));
    return messageRepository.findAll(pageable).getContent();
  }

  public void createMessage(Message msg)
  {
    messageRepository.save(msg);
  }

  public List<Message> getMessagesByUser(int userId)
  {
    return messageRepository.findByPostedById(userId);
  }

}
TOP

Related Classes of com.sivalabs.ssdemo.services.MessageService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.