package sotechat.domainService;

import java.util.Date;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sotechat.domain.Message;
import sotechat.domain.Conversation;
import sotechat.repo.ConversationRepo;
import sotechat.repo.MessageRepo;
import sotechat.repo.PersonRepo;

@Service
public class MessageService  {

    private MessageRepo messageRepo;

    private ConversationRepo conversationRepo;

    private PersonRepo personRepo;

    @Autowired
    public MessageService(MessageRepo pMessageRepo,
                          ConversationRepo pConversationRepo) {
        this.messageRepo = pMessageRepo;
        this.conversationRepo = pConversationRepo;
    }

    @Transactional
    public void addMessage(Message message) {
        messageRepo.save(message);
    }

    @Transactional
    public void removeMessage(Long messageId) {
        Message message = messageRepo.findOne(messageId);
        String conversationId = message.getConversation().getChannelId();
        conversationRepo.findOne(conversationId).getMessagesOfConversation()
                .remove(message);
        messageRepo.delete(messageId);
    }

    public List<Message> messagesOfConversation(String channelId){
        return messageRepo.findByConversation(channelId);
    }

    @Transactional
    public void removeConversation(String channelId){
        List<Message> messages= messageRepo.findByConversation(channelId);
        messageRepo.deleteInBatch(messages);
    }

    public void setMessageRepo(MessageRepo pMessageRepo) {

        this.messageRepo = pMessageRepo;
    }

    public MessageRepo getMessageRepo() {
        return this.messageRepo;
    }

    public void setConversationRepo(final ConversationRepo pConversationRepo) {
        this.conversationRepo = pConversationRepo;
    }

    public ConversationRepo getConversationRepo() {

        return this.conversationRepo;
    }
}


@@ -682,32 +682,75 @@
 nversationRepo) 
+%0A                          throws Exception
 %7B%0A        this.m
@@ -896,16 +896,33 @@
 message)
+ throws Exception
  %7B%0A     
@@ -1022,16 +1022,33 @@
 ssageId)
+ throws Exception
  %7B%0A     
@@ -1388,32 +1388,50 @@
 tring channelId)
+ throws Exception 
 %7B%0A        return
@@ -1552,16 +1552,34 @@
 annelId)
+ throws Exception 
 %7B%0A      
@@ -1755,16 +1755,33 @@
 ageRepo)
+ throws Exception
  %7B%0A%0A    
@@ -1863,16 +1863,33 @@
 geRepo()
+ throws Exception
  %7B%0A     
@@ -2000,16 +2000,33 @@
 ionRepo)
+ throws Exception
  %7B%0A     
@@ -2127,16 +2127,33 @@
 onRepo()
+ throws Exception
  %7B%0A%0A    

Update MessageService.java