Files
caliverse_server/UGQDataAccess/Repository/Query/AccountQuery.cs
2025-05-01 07:20:41 +09:00

73 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using UGQDataAccess.Repository.Models;
using UGQDatabase.Models;
using static UGQDataAccess.Repository.Query.QuestContentQuery;
namespace UGQDataAccess.Repository.Query;
public static class AccountQuery
{
public class AllAccountQueryJoin : AccountEntity
{
public IEnumerable<QuestContentEntity> QuestContents { get; set; } = null!;
public int QuestCount { get; set; } = 0;
}
public static Expression<Func<AllAccountQueryJoin, AccountItemResult>> AccountItemResultProjection =
x => new AccountItemResult
{
UserGuid = x.UserGuid,
Nickname = x.Nickname,
AccountId = x.AccountId ?? "",
AdditionalSlotCount = x.AdditionalSlotCount,
GradeType = x.GradeType,
CreatorPoint = x.CreatorPoint,
CreatedAt = x.CreatedAt,
QuestCount = x.QuestCount,
};
public static PipelineDefinition<AccountEntity, T> allAccountPipeline<T>(
FilterDefinition<AccountEntity> filter,
Expression<Func<AllAccountQueryJoin, T>> projection)
{
var lookupStage1 = new BsonDocument("$lookup",
new BsonDocument
{
{ "from", "QuestContent" },
{ "localField", "UserGuid" },
{ "foreignField", "UserGuid" },
{ "pipeline",
new BsonArray
{
new BsonDocument("$match",
new BsonDocument("IsDeleted", false))
} },
{ "as", "QuestContents" }
});
var addFields1 = new BsonDocument("$addFields",
new BsonDocument("QuestCount",
new BsonDocument("$size", "$QuestContents")));
var pipeline = new EmptyPipelineDefinition<AccountEntity>()
.Match(filter)
.AppendStage<AccountEntity, AccountEntity, AllAccountQueryJoin>(lookupStage1)
.AppendStage<AccountEntity, AllAccountQueryJoin, AllAccountQueryJoin>(addFields1)
.Project(projection);
return pipeline;
}
}