Skip to content

Commit

Permalink
MessageManager.Domain 重新调整
Browse files Browse the repository at this point in the history
  • Loading branch information
yuezhongxin committed Jul 8, 2014
1 parent 76fb46b commit b3fe705
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 103 deletions.
136 changes: 68 additions & 68 deletions MessageManager.Application/Implementation/MessageServiceImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
* address:https://www.github.com/yuezhongxin/MessageManager
**/

using AutoMapper;
using MessageManager.Application.DTO;
using MessageManager.Domain.DomainService;
using MessageManager.Domain.Entity;
using MessageManager.Domain.Repositories;
using MessageManager.Infrastructure;

Expand Down Expand Up @@ -50,28 +47,29 @@ public MessageServiceImpl(IRepositoryContext context,
/// <returns></returns>
public OperationResponse SendMessage(string title, string content, string senderLoginName, string receiverDisplayName)
{
User sendUser = userRepository.GetUserByLoginName(senderLoginName);
if (sendUser == null)
{
return OperationResponse.Error("未获取到发件人信息");
}
User receiveUser = userRepository.GetUserByDisplayName(receiverDisplayName);
if (receiveUser == null)
{
return OperationResponse.Error("未获取到收件人信息");
}
Message message = new Message(title, content, sendUser, receiveUser);
OperationResponse<Message> serviceResult = VerifyMessageService.VerifyMessage(message);
if (serviceResult.IsSuccess)
{
return serviceResult.GetOperationResponse();
//messageRepository.Add(message);
//return messageRepository.Context.Commit();
}
else
{
return serviceResult.GetOperationResponse();
}
//User sendUser = userRepository.GetUserByLoginName(senderLoginName);
//if (sendUser == null)
//{
// return OperationResponse.Error("未获取到发件人信息");
//}
//User receiveUser = userRepository.GetUserByDisplayName(receiverDisplayName);
//if (receiveUser == null)
//{
// return OperationResponse.Error("未获取到收件人信息");
//}
//Message message = new Message(title, content, sendUser, receiveUser);
//OperationResponse<Message> serviceResult = VerifyMessageService.VerifyMessage(message);
//if (serviceResult.IsSuccess)
//{
// return serviceResult.GetOperationResponse();
// //messageRepository.Add(message);
// //return messageRepository.Context.Commit();
//}
//else
//{
// return serviceResult.GetOperationResponse();
//}
return null;
}

/// <summary>
Expand All @@ -82,27 +80,28 @@ public OperationResponse SendMessage(string title, string content, string sender
/// <returns></returns>
public OperationResponse<MessageDTO> ReadMessage(string messageId, string readerLoginName)
{
User readUser = userRepository.GetUserByLoginName(readerLoginName);
if (readUser == null)
{
return new OperationResponse<MessageDTO>(false, "未获取到阅读人信息");
}
Message message = messageRepository.GetByKey(messageId);
if (message == null)
{
return new OperationResponse<MessageDTO>(false, "未获取到消息");
}
OperationResponse<Message> serviceResult = ReadMessageService.ReadMessage(message, readUser);
if (serviceResult.IsSuccess)
{
//messageRepository.Update(message);
//messageRepository.Context.Commit();
return new OperationResponse<MessageDTO>(serviceResult.IsSuccess, serviceResult.Message, Mapper.Map<Message, MessageDTO>(message));
}
else
{
return new OperationResponse<MessageDTO>(serviceResult.IsSuccess, serviceResult.Message);
}
//User readUser = userRepository.GetUserByLoginName(readerLoginName);
//if (readUser == null)
//{
// return new OperationResponse<MessageDTO>(false, "未获取到阅读人信息");
//}
//Message message = messageRepository.GetByKey(messageId);
//if (message == null)
//{
// return new OperationResponse<MessageDTO>(false, "未获取到消息");
//}
//OperationResponse<Message> serviceResult = ReadMessageService.ReadMessage(message, readUser);
//if (serviceResult.IsSuccess)
//{
// //messageRepository.Update(message);
// //messageRepository.Context.Commit();
// return new OperationResponse<MessageDTO>(serviceResult.IsSuccess, serviceResult.Message, Mapper.Map<Message, MessageDTO>(message));
//}
//else
//{
// return new OperationResponse<MessageDTO>(serviceResult.IsSuccess, serviceResult.Message);
//}
return null;
}

/// <summary>
Expand All @@ -115,28 +114,29 @@ public OperationResponse<MessageDTO> ReadMessage(string messageId, string reader
/// <returns></returns>
public OperationResponse ReplyMessage(string messageId, string title, string content, string replierLoginName)
{
Message message = messageRepository.GetByKey(messageId);
if (message == null)
{
return new OperationResponse(false, "未获取到消息");
}
User replyUser = userRepository.GetUserByLoginName(replierLoginName);
if (replyUser == null)
{
return OperationResponse.Error("未获取到回复人信息");
}
Message replyMessage = new Message(title, content, replyUser, message.SendUser);
OperationResponse<Message> serviceResult = VerifyMessageService.VerifyMessage(message);
if (serviceResult.IsSuccess)
{
return serviceResult.GetOperationResponse();
//messageRepository.Add(message);
//return messageRepository.Context.Commit();
}
else
{
return serviceResult.GetOperationResponse();
}
//Message message = messageRepository.GetByKey(messageId);
//if (message == null)
//{
// return new OperationResponse(false, "未获取到消息");
//}
//User replyUser = userRepository.GetUserByLoginName(replierLoginName);
//if (replyUser == null)
//{
// return OperationResponse.Error("未获取到回复人信息");
//}
//Message replyMessage = new Message(title, content, replyUser, message.SendUser);
//OperationResponse<Message> serviceResult = VerifyMessageService.VerifyMessage(message);
//if (serviceResult.IsSuccess)
//{
// return serviceResult.GetOperationResponse();
// //messageRepository.Add(message);
// //return messageRepository.Context.Commit();
//}
//else
//{
// return serviceResult.GetOperationResponse();
//}
return null;
}
#endregion
}
Expand Down
65 changes: 55 additions & 10 deletions MessageManager.Domain.Tests/MessageDomainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,84 @@

using MessageManager.Domain.DomainService;
using MessageManager.Domain.Entity;
using MessageManager.Infrastructure;
using System.Collections.Generic;
using Xunit;

namespace MessageManager.Domain.Tests
{
public class MessageDomainTest
{
/// <summary>
/// 发送消息
/// </summary>
[Fact]
public void DomainTest_SendMessage()
{
User sendUser = new User("xiaocai", "小菜");
User receiveUser = new User("dashen", "大神");
Message message = new Message("test", "test", sendUser, receiveUser);
OperationResponse<Message> serviceResult = VerifyMessageService.VerifyMessage(message);
Assert.True(VerifyMessageService.VerifyMessage(message).IsSuccess);
Assert.True(SendMessageService.SendMessage(message).IsSuccess);
}

/// <summary>
/// 阅读单条消息
/// </summary>
[Fact]
public void DomainTest_ReadMessage()
public void DomainTest_ReadSingleMessage()
{
User readUser = new User("xiaocai", "小菜");
Message message = new Message("test", "test", new User("xiaocai", "小菜"), new User("dashen", "大神"));
OperationResponse<Message> serviceResult = ReadMessageService.ReadMessage(message, readUser);
Assert.True(ReadMessageService.ReadMessage(message, readUser).IsSuccess);
Message message = new Message("test", "test", readUser, new User("dashen", "大神"));
Assert.True(ReadMessageService.ReadSingleMessage(message, readUser).IsSuccess);
}

/// <summary>
/// 回复消息
/// </summary>
[Fact]
public void DomainTest_ReplyMessage()
{
User replyUser = new User("dashen", "大神");
Message message = new Message("test", "test", new User("xiaocai", "小菜"), new User("dashen", "大神"));
OperationResponse<Message> serviceResult = VerifyMessageService.VerifyMessage(message);
Assert.True(VerifyMessageService.VerifyMessage(message).IsSuccess);
Message message = new Message("test", "test", new User("xiaocai", "小菜"), replyUser);
Assert.True(ReplyMessageService.ReplyMessage(message).IsSuccess);
}

/// <summary>
/// 删除消息
/// </summary>
[Fact]
public void DomainTest_DeleteMessage()
{
User operateUser = new User("xiaocai", "小菜");
Message message = new Message("test", "test", operateUser, new User("dashen", "大神"));
Assert.True(DeleteMessageService.DeleteMessage(message, operateUser).IsSuccess);
}

/// <summary>
/// 阅读发件箱
/// </summary>
[Fact]
public void DomainTest_ReadOutbox()
{
User readUser = new User("xiaocai", "小菜");
ICollection<Message> messages = new List<Message> {
new Message("test1", "test", readUser, new User("dashen", "大神")) ,
new Message("test2", "test", readUser, new User("dashen", "大神")) ,
new Message("test3", "test", readUser, new User("dashen", "大神")) };
Assert.True(ReadMessageService.ReadOutbox(messages, readUser).IsSuccess);
}

/// <summary>
/// 阅读收件箱
/// </summary>
[Fact]
public void DomainTest_ReadInbox()
{
User readUser = new User("xiaocai", "小菜");
ICollection<Message> messages = new List<Message> {
new Message("test1", "test", readUser, new User("dashen", "大神")) ,
new Message("test2", "test", readUser, new User("dashen", "大神")) ,
new Message("test3", "test", readUser, new User("dashen", "大神")) };
Assert.True(ReadMessageService.ReadInbox(messages, readUser).IsSuccess);
}
}
}
32 changes: 32 additions & 0 deletions MessageManager.Domain/DomainService/DeleteMessageService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* author:xishuai
* address:https://www.github.com/yuezhongxin/MessageManager
**/

using MessageManager.Domain.Entity;
using MessageManager.Infrastructure;
namespace MessageManager.Domain.DomainService
{
/// <summary>
/// DeleteMessage领域服务实现
/// </summary>
public class DeleteMessageService
{
public static OperationResponse DeleteMessage(Message message, User operateUser)
{
if (!(message.SendUser == operateUser || message.ReceiveUser == operateUser))
{
return OperationResponse.Error("您并不是收发件人,没有权限删除本条短消息");
}
//if (message.SendUser == operateUser)
//{
// operateUser.SendMessages.Remove(message);
//}
//else
//{
// operateUser.ReceiveMessages.Remove(message);
//}
return OperationResponse.Success("删除短消息成功");
}
}
}
22 changes: 21 additions & 1 deletion MessageManager.Domain/DomainService/ReadMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
using MessageManager.Domain.Entity;
using MessageManager.Domain.ValueObject;
using MessageManager.Infrastructure;
using System.Collections.Generic;
using System.Linq;
namespace MessageManager.Domain.DomainService
{
/// <summary>
/// ReadMessage领域服务实现
/// </summary>
public class ReadMessageService
{
public static OperationResponse<Message> ReadMessage(Message message, User readUser)
public static OperationResponse<Message> ReadSingleMessage(Message message, User readUser)
{
if (!(message.SendUser == readUser || message.ReceiveUser == readUser))
{
Expand All @@ -25,5 +27,23 @@ public static OperationResponse<Message> ReadMessage(Message message, User readU
}
return new OperationResponse<Message>(true, "", message);
}

public static OperationResponse<ICollection<Message>> ReadOutbox(ICollection<Message> messages, User readUser)
{
if (messages.Where(m => m.SendUser == readUser).Count() > 0)
{
return new OperationResponse<ICollection<Message>>(false, "您并不是发件人,没有权限阅读发件箱");
}
return new OperationResponse<ICollection<Message>>(true, "", messages);
}

public static OperationResponse<ICollection<Message>> ReadInbox(ICollection<Message> messages, User readUser)
{
if (messages.Where(m => m.ReceiveUser == readUser).Count() > 0)
{
return new OperationResponse<ICollection<Message>>(false, "您并不是收件人,没有权限阅读收件箱");
}
return new OperationResponse<ICollection<Message>>(true, "", messages);
}
}
}
32 changes: 32 additions & 0 deletions MessageManager.Domain/DomainService/ReplyMessageService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* author:xishuai
* address:https://www.github.com/yuezhongxin/MessageManager
**/

using MessageManager.Domain.Entity;
using MessageManager.Infrastructure;
using System;
using System.Linq;
namespace MessageManager.Domain.DomainService
{
/// <summary>
/// ReplyMessage领域服务实现
/// </summary>
public class ReplyMessageService
{
public static OperationResponse<Message> ReplyMessage(Message message)
{
//示例业务规则,对象导航关联访问需要探讨
if (message.SendUser.SendMessages.Where(m => m.SendTime.Date == DateTime.Now.Date).Count() > 200)
{
return new OperationResponse<Message>(false, "发件人一天之内只能回复200个短消息");
}
if (message.SendUser.SendMessages.Where(m => m.SendTime.Date == DateTime.Now.Date && m.ReceiveUser == message.ReceiveUser).Count() > 50)
{
return new OperationResponse<Message>(false, "发件人一天之内只能和同一人回复50个短消息");
}
//to do...
return new OperationResponse<Message>(true, "回复消息成功", message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace MessageManager.Domain.DomainService
/// <summary>
/// SendMessage领域服务实现
/// </summary>
public class VerifyMessageService
public class SendMessageService
{
public static OperationResponse<Message> VerifyMessage(Message message)
public static OperationResponse<Message> SendMessage(Message message)
{
//示例业务规则,对象导航关联访问需要探讨
if (message.SendUser.SendMessages.Where(m => m.SendTime.Date == DateTime.Now.Date).Count() > 200)
Expand Down
Loading

0 comments on commit b3fe705

Please sign in to comment.