2025-07-08 14:01:10 +08:00

136 lines
4.0 KiB
C#

using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Uow;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YunDa.SOMS.Application.Core;
using YunDa.SOMS.Application.Core.Session;
using YunDa.SOMS.Application.Core.SwaggerHelper;
using YunDa.SOMS.DataTransferObject;
using YunDa.SOMS.Entities.MongoDB;
using YunDa.SOMS.MongoDB.Repositories;
namespace YunDa.SOMS.Test
{
public class TestMongoDB : SOMSAppServiceBase, ITestMongoDB
{
private readonly IMongoDbRepository<TestEntity, Guid> _testEntityRepository;
private readonly ICurrentUnitOfWorkProvider _currentUnitOfWorkProvider;
public TestMongoDB(
IMongoDbRepository<TestEntity, Guid> testEntityRepository
, ICurrentUnitOfWorkProvider currentUnitOfWorkProvider
, ISessionAppService sessionAppService) :
base(sessionAppService)
{
_testEntityRepository = testEntityRepository;
_currentUnitOfWorkProvider = currentUnitOfWorkProvider;
}
//[UnitOfWork]
[AbpAllowAnonymous]
[HttpPost]
[ShowApi]
public async Task<RequestEasyResult> TestMongoDBAdd()
{
RequestEasyResult rst = new RequestEasyResult
{
Flag = true
};
try
{
TestEntityInput t = new TestEntityInput
{
Name = "测试MongoDB2233",
DTime = Convert.ToDateTime("2020-03-11 11:19:17"),
nBar = 0b101010101010101010101
};
var data = ObjectMapper.Map<TestEntity>(t);
//await _testEntityRepository.InsertOneAsync(data);
}
catch
{
rst.Flag = false;
}
return rst;
}
//[UnitOfWork]
[AbpAllowAnonymous]
[HttpPost]
public RequestPageResult<TestEntityOutput> TestMongoDBSearch()
{
RequestPageResult<TestEntityOutput> rst = new RequestPageResult<TestEntityOutput>
{
Flag = true
};
try
{
DateTime dt = Convert.ToDateTime("2020-03-11 11:19:17");
var output = _testEntityRepository.GetAll(e => e.DTime.Equals(dt)).ToList();
rst.ResultData = ObjectMapper.Map<List<TestEntityOutput>>(output);
}
catch
{
rst.Flag = false;
}
return rst;
}
[AbpAllowAnonymous]
[HttpPost]
[ShowApi]
public RequestResult<Dictionary<Guid, TestEntityOutput>> TestMongoDBSearchDic()
{
RequestResult<Dictionary<Guid, TestEntityOutput>> rst = new RequestResult<Dictionary<Guid, TestEntityOutput>>
{
Flag = true
};
try
{
DateTime dt = Convert.ToDateTime("2020-03-11 11:19:17");
var output = _testEntityRepository.GetAll(e => e.DTime.Equals(dt)).ToDictionary(key => key.Id, value => ObjectMapper.Map<TestEntityOutput>(value));
rst.ResultData = output;
}
catch
{
rst.Flag = false;
}
return rst;
}
}
[AutoMapTo(typeof(TestEntity))]
public class TestEntityInput
{
public string Name { get; set; }
private DateTime _dTime;
public int nBar { get; set; }
public DateTime DTime
{
get { return _dTime; }
set
{
_dTime = value;
}
}
}
[AutoMapFrom(typeof(TestEntity))]
public class TestEntityOutput
{
public Guid Id { get; set; }
public string Name { get; set; }
public DateTime DTime { get; set; }
public int nBar { get; set; }
}
}