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.ISAS.Application.Core; using YunDa.ISAS.Application.Core.Session; using YunDa.ISAS.Application.Core.SwaggerHelper; using YunDa.ISAS.DataTransferObject; using YunDa.ISAS.Entities.MongoDB; using YunDa.ISAS.MongoDB.Repositories; namespace YunDa.ISAS.Test { public class TestMongoDB : ISASAppServiceBase, ITestMongoDB { private readonly IMongoDbRepository _testEntityRepository; private readonly ICurrentUnitOfWorkProvider _currentUnitOfWorkProvider; public TestMongoDB( IMongoDbRepository testEntityRepository , ICurrentUnitOfWorkProvider currentUnitOfWorkProvider , ISessionAppService sessionAppService) : base(sessionAppService) { _testEntityRepository = testEntityRepository; _currentUnitOfWorkProvider = currentUnitOfWorkProvider; } //[UnitOfWork] [AbpAllowAnonymous] [HttpPost] [ShowApi] public async Task 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(t); //await _testEntityRepository.InsertOneAsync(data); } catch { rst.Flag = false; } return rst; } //[UnitOfWork] [AbpAllowAnonymous] [HttpPost] public RequestPageResult TestMongoDBSearch() { RequestPageResult rst = new RequestPageResult { 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>(output); } catch { rst.Flag = false; } return rst; } [AbpAllowAnonymous] [HttpPost] [ShowApi] public RequestResult> TestMongoDBSearchDic() { RequestResult> rst = new RequestResult> { 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(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; } } }