Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
wsq003 committed Apr 12, 2022
1 parent 4faf8cb commit 6205862
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/BazarServer.Application/Commands/MdtChannelHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public async Task<MdtResp> Handle(MdtRequest<ChannelCmd> req, CancellationToken
{
return new MdtResp(false, "lack data", new UserCommandRespDto(CommandErrorCode.NoUser, model.userID));
}
var old = await _conn.FirstOrDefaultAsync(x=>x.channelID == model.channelID);
if (old != null && old.userID != model.userID)
{
return new MdtResp(false, "you can only edit your own list");
}
var count = await _conn.CountAsync(x => x.userID == model.userID);
if (count > maxChannelCount)
{
Expand Down
10 changes: 9 additions & 1 deletion src/BazarServer.Application/Commands/MdtChannelMemberHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ public class MdtChannelMemberHandler : IRequestHandler<MdtRequest<ChannelMemberC
ISender _mediator;

IGenericMongoCollection<ChannelMember> _conn;
IGenericMongoCollection<Channel> _connChannel;
IUserRepository userRepository;

const int maxChannelMemberCount = 200;

public MdtChannelMemberHandler(ISender mediator, ILogger<MdtChannelMemberHandler> logger, IGenericMongoCollection<ChannelMember> conn, IUserRepository userRepository)
public MdtChannelMemberHandler(ISender mediator, ILogger<MdtChannelMemberHandler> logger, IGenericMongoCollection<ChannelMember> conn, IUserRepository userRepository, IGenericMongoCollection<Channel> connChannel)
{
_mediator = mediator;
_logger = logger;
_conn = conn;
this.userRepository = userRepository;
_connChannel = connChannel;
}

public async Task<MdtResp> Handle(MdtRequest<ChannelMemberCmd> req, CancellationToken cancellationToken)
Expand Down Expand Up @@ -51,6 +53,12 @@ public async Task<MdtResp> Handle(MdtRequest<ChannelMemberCmd> req, Cancellation
var model = new ChannelMember();
FastCopy.Copy(cmd, model);

var cc = await _connChannel.FirstOrDefaultAsync(x=>x.channelID == cmd.channelID);
if (cc != null && cc.userID != cmd.userID)
{
return (false, "you can only edit you own list member");
}

var old = await _conn.FirstOrDefaultAsync(x => x.channelID == model.channelID && x.memberID == model.memberID);
if (old != null)
{
Expand Down
6 changes: 5 additions & 1 deletion src/BazarServer.Test/Controllers/UserQueryControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BazarServer.Application.Query;
using BazarServer.Entity.Storage;
using BazarServer.Infrastructure.Repository;
using Common.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -46,14 +47,17 @@ public async Task GetHomeLineTestAsync()
IUserRepository userRepository = provider.GetService<IUserRepository>();
IPostRepository postRepository = provider.GetService<IPostRepository>();

var userID = "Kce3xXlO5D8bT4dIeuaq6p6wgAuDr9";
var userID = "k7RmZwQlkX4cbaOl3RGaMoD3LpE2Db";
var page = 0;
var pageSize = 20;

var begin = DateHelper.GetMilliSeconds();
var users = await userRepository.GetUserFollowees(userID, 0, 1000);
var ay = users.Select(x => x.targetID).ToList();
var posts = await PostQueryFacade.GetLatestPostsByUsers(postRepository, ay, page, pageSize);
var ret = await PostQueryFacade.GetPostDto(postRepository, userID, posts);
var used = DateHelper.GetMilliSeconds() - begin;
var ss = used.ToString("0.00");

Assert.IsTrue(true);
}
Expand Down
22 changes: 22 additions & 0 deletions src/BazarServer.Test/Repository/PostRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.DependencyInjection;
using Common.Utils;

namespace BazarServer.Infrastructure.Repository.Tests
{
[TestClass()]
public class PostRepositoryTests : BaseTest
{
[TestMethod()]
public async Task GetPostsByUserAsyncTest()
{
IPostRepository postRepository = provider.GetService<IPostRepository>();

var userID = "CZYZBUpXOtxtxmsHv7FexLf34bGdRX";
var begin = DateHelper.GetMilliSeconds();
var ret = await postRepository.GetPostsByUserAsync(userID, false, 0, 100);
var used = DateHelper.GetMilliSeconds() - begin;
var ss = used.ToString("0.00");
}
}
}

0 comments on commit 6205862

Please sign in to comment.