Skip to content

Commit

Permalink
fixing index definition builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Anggara Suwartana committed Oct 27, 2018
1 parent c474460 commit 7d9db6e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/IntoItIf.MongoDb/MongoDataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Base.Domain.Options;
using Base.Helpers;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;

Expand Down Expand Up @@ -99,6 +98,7 @@ public Option<IEnumerable<PropertyInfo>> GetAlternateKeyProperties<T>()
if (!(definition.Value is StartModelParameter startParam)) continue;
foreach (var indexParam in startParam.IndexModelParameters)
{
if (!indexParam.Options.Unique.GetValueOrDefault()) continue;
foreach (var dictProperty in indexParam.Rendered.ToDictionary())
{
var pi = type.GetProperty(dictProperty.Key);
Expand Down
76 changes: 38 additions & 38 deletions src/IntoItIf.MongoDb/MongoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -758,41 +758,37 @@ private Option<IClientSessionHandle> GetSessionOrNone()
.MapFlattenAsync(
async x =>
{
return (
await
(
await x.DataContext.BuildPrimaryKeyPredicate(x.InputEntity)
.MapAsync(
async y => (
Exist: (await InternalGetFirstOrDefaultAsync(y.Predicate, session, x.Ctok)).ReduceOrDefault(),
y.PropertyNames,
x.InputEntity
))
)
.IfMap(
y => y.Exist != null,
y => (MatchValidatedEntity: y.Exist, y.PropertyNames, y.InputEntity))
.ElseMapFlattenAsync(
async y =>
{
var existAk = await x.DataContext.BuildAlternateKeyPredicate(y.InputEntity)
.MapAsync(
async z => (
Exist: (await InternalGetFirstOrDefaultAsync(z.Predicate, session, x.Ctok))
.ReduceOrDefault(),
z.PropertyNames,
y.InputEntity
));
return existAk
.IfMap(
z => z.Exist != null,
z => (MatchValidatedEntity: z.Exist, z.PropertyNames, z.InputEntity))
.ElseMap(
z => (MatchValidatedEntity: (T)null, z.PropertyNames, z.InputEntity))
.Output;
})
)
.Output;
var existPk = await x.DataContext.BuildPrimaryKeyPredicate(x.InputEntity)
.MapAsync(
async y => (
Exist: (await InternalGetFirstOrDefaultAsync(y.Predicate, session, x.Ctok)).ReduceOrDefault(),
y.PropertyNames,
x.InputEntity
));
var _ = await existPk
.IfMap(
y => y.Exist != null,
y => (MatchValidatedEntity: y.Exist, y.PropertyNames, y.InputEntity))
.ElseMapFlattenAsync(
async y =>
{
var existAk = await x.DataContext.BuildAlternateKeyPredicate(y.InputEntity)
.MapAsync(
async z => (
Exist: (await InternalGetFirstOrDefaultAsync(z.Predicate, session, x.Ctok))
.ReduceOrDefault(),
z.PropertyNames,
y.InputEntity
));
return existAk
.IfMap(
z => z.Exist != null,
z => (MatchValidatedEntity: z.Exist, z.PropertyNames, z.InputEntity))
.ElseMap(
z => (MatchValidatedEntity: (T)null, z.PropertyNames, z.InputEntity))
.Output;
});
return _.Output;
});
}

Expand Down Expand Up @@ -931,7 +927,7 @@ await x.DataContext.BuildPrimaryKeyPredicate(x.InputEntity)
Option<IClientSessionHandle> session,
Option<CancellationToken> ctok)
{
return (
var result = (
await MongoDataContext.MapFlatten(x => x.Collection<T>())
.Combine(selector)
.Combine(predicate, true, _ => true)
Expand All @@ -945,9 +941,13 @@ await MongoDataContext.MapFlatten(x => x.Collection<T>())
Session: x.Item1.Item2,
Ctok: x.Item2
))
.IfMapAsync(x => x.Session == null, x => x.Set.Find(x.Predicate).Project(x.Selector).FirstOrDefaultAsync(x.Ctok))
.ElseMapAsync(x => x.Set.Find(x.Session, x.Predicate).Project(x.Selector).FirstOrDefaultAsync(x.Ctok))
.IfMapAsync(
x => x.Session == null,
x => x.Set.Find(x.Predicate).Project(x.Selector).FirstOrDefaultAsync(x.Ctok))
.ElseMapAsync(
x => x.Set.Find(x.Session, x.Predicate).Project(x.Selector).FirstOrDefaultAsync(x.Ctok))
).Output;
return result;
}

private Option<List<TResult>> InternalGetList<TResult>(
Expand Down
9 changes: 5 additions & 4 deletions test/IntoItIf.Tests/MongoDb/ClientTest.cs

Large diffs are not rendered by default.

49 changes: 48 additions & 1 deletion test/IntoItIf.Tests/Preparation/Mongo/OpsBossesDataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
{
using Base.Domain.Options;
using IntoItIf.MongoDb;
using MongoDB.Bson.Serialization.IdGenerators;
using MongoDB.Driver;
using PnrInfos;

public class OpsBossesDataContext : MongoDataContext
{
Expand All @@ -22,13 +24,58 @@ public class OpsBossesDataContext : MongoDataContext
protected override void OnModelCreating(MongoModelBuilder modelBuilder)
{
var consumer = modelBuilder.Entity<Consumer>();
consumer.SetIndexes(x => x.Ascending(y => y.Code).IsUnique());
consumer.CreateMap(
x =>
{
x.AutoMap();
x.MapCreator(y => new Consumer(y.Id, y.Code, y.Name, y.ClientId));
});
consumer.SetIndexes(x => x.Ascending(y => y.Code).IsUnique());

var pnr = modelBuilder.Entity<Pnr>();
pnr.SetIndexes(
x => x.Ascending(y => y.PnrId).IsUnique(),
x => x.Ascending(y => y.Airline),
x => x.Ascending(y => y.Agent),
x => x.Ascending(y => y.BookedVia),
x => x.Ascending(y => y.Company),
x => x.Ascending(y => y.Created),
x => x.Ascending(y => y.CustomerType),
x => x.Ascending(y => y.IntlAirline),
x => x.Ascending(y => y.IsInternational),
x => x.Ascending(y => y.IssuedBy),
x => x.Ascending(y => y.IssuedVia),
x => x.Ascending(y => y.IssuedWithLg),
x => x.Ascending(y => y.PassivePnrCode),
x => x.Ascending(y => y.PassivePcc),
x => x.Ascending(y => y.PassivePnrCode),
x => x.Ascending(y => y.PaymentType),
x => x.Ascending(y => y.Pcc),
x => x.Ascending(y => y.PnrCode),
x => x.Ascending(y => y.PnrNumericCode),
x => x.Ascending(y => y.PnrReff),
x => x.Ascending(y => y.PostStatus),
x => x.Ascending(y => y.Prefix),
x => x.Ascending(y => y.PrintStatus),
x => x.Ascending(y => y.RequestToIssuedWithLG),
x => x.Ascending(y => y.Reserved),
x => x.Ascending(y => y.RsvNotifSent),
x => x.Ascending(y => y.RoomTypeMapping),
x => x.Ascending(y => y.Source),
x => x.Ascending(y => y.SourcePnrCode),
x => x.Ascending(y => y.Status),
x => x.Ascending(y => y.StatusPassiveSegment),
x => x.Ascending(y => y.Ticketed),
x => x.Ascending(y => y.TimeLimit),
x => x.Ascending(y => y.TransactionStatus),
x => x.Ascending(y => y.UseInsurance),
x => x.Ascending(y => y.Username)
);
pnr.CreateMap(x =>
{
x.AutoMap();
x.MapIdMember(c => c.Id).SetIdGenerator(CombGuidGenerator.Instance);
});
}

#endregion
Expand Down
3 changes: 2 additions & 1 deletion test/IntoItIf.Tests/Preparation/Mongo/PnrInfos/Pnr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Pnr : BaseEntity<Pnr>
{
public Pnr()
{
Id = ObjectId.GenerateNewId();
Attachments = new List<object>();
ElectronicTickets = new List<ElectronicTicket>();
Histories = new List<History>();
Expand All @@ -28,7 +29,7 @@ public Pnr()
[BsonId]
public ObjectId Id { get; set; }

public string PnrId { get; set; }
public Guid PnrId { get; set; }
public int? Adult { get; set; }
public int? Child { get; set; }
public string Agent { get; set; }
Expand Down

0 comments on commit 7d9db6e

Please sign in to comment.