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

96 lines
3.1 KiB
C#

using Microsoft.Extensions.Options;
using MongoDB.Driver;
using UGQDatabase.Models;
using UGQDataAccess.Settings;
using UGQDataAccess.Repository.Models;
using Amazon.SecurityToken.Model;
using Microsoft.AspNetCore.Mvc.RazorPages;
using UGQDataAccess.Repository.Query;
using System.Linq.Expressions;
using MetaAssets;
using ServerCommon.UGQ;
namespace UGQDataAccess.Repository;
public class CreatorPointHistoryRepository : BaseRepository<CreatorPointHistoryEntity>
{
private const string CollectionName = "CreatorPointHistory";
public CreatorPointHistoryRepository(IMongoClient mongoClient, IOptions<UGQDatabaseSettings> settings) :
base(mongoClient, settings.Value.DatabaseName, CollectionName)
{
}
public async Task<ServerErrorCode> insert(CreatorPointHistoryEntity entity)
{
await Collection.InsertOneAsync(entity);
return ServerErrorCode.Success;
}
public async Task<CreatorPointHistoryQueryResult> getList(string userGuid, int pageNumber, int pageSize, CreatorPointHistoryKind kind, DateTimeOffset startDate, DateTimeOffset endDate)
{
pageNumber = pageNumber < 1 ? 1 : pageNumber;
var filterBuilder = Builders<CreatorPointHistoryEntity>.Filter;
var filter = filterBuilder.Eq(x => x.UserGuid, userGuid);
if (kind != CreatorPointHistoryKind.None)
filter &= filterBuilder.Eq(x => x.Kind, kind);
if (startDate > endDate)
(startDate, endDate) = (endDate, startDate);
if (startDate != DateTimeOffset.MinValue)
filter &= filterBuilder.Gte(x => x.CreatedAt, startDate.UtcDateTime);
if (endDate != DateTimeOffset.MinValue)
filter &= filterBuilder.Lt(x => x.CreatedAt, endDate.UtcDateTime);
var sort = Builders<CreatorPointHistoryEntity>.Sort.Descending(x => x.CreatedAt);
var countFacet = AggregateFacet.Create("count",
new EmptyPipelineDefinition<CreatorPointHistoryEntity>()
.Count()
);
var pagingFacet = AggregateFacet.Create("paging",
new EmptyPipelineDefinition<CreatorPointHistoryEntity>()
.Sort(sort)
.Skip((pageNumber - 1) * pageSize)
.Limit(pageSize)
);
var pipeline = new EmptyPipelineDefinition<CreatorPointHistoryEntity>()
.Match(filter)
.Facet(countFacet, pagingFacet);
var aggregation = await (await Collection.AggregateAsync(pipeline)).ToListAsync();
var count = aggregation.First()
.Facets.First(x => x.Name == "count")
.Output<AggregateCountResult>()
?.FirstOrDefault()
?.Count;
var paging = aggregation.First()
.Facets.First(x => x.Name == "paging")
.Output<CreatorPointHistoryEntity>();
int totalPages = 0;
if (count != null)
totalPages = (int)Math.Ceiling((double)count / pageSize);
return new CreatorPointHistoryQueryResult
{
PageNumber = pageNumber,
PageSize = pageSize,
TotalPages = totalPages,
Items = paging.ToList(),
};
}
}