using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MongoDbGenericRepository.DataAccess.Base;
using MongoDbGenericRepository.Models;
using MongoDbGenericRepository.Utils;
namespace MongoDbGenericRepository.DataAccess.Create
{
///
/// A class to insert MongoDb document.
///
public class MongoDbCreator : DataAccessBase, IMongoDbCreator
{
///
/// The construct of the MongoDbCreator class.
///
/// A instance.
public MongoDbCreator(IMongoDbContext mongoDbContext) : base(mongoDbContext)
{
}
///
public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument
where TKey : IEquatable
{
FormatDocument(document);
await HandlePartitioned(document)
.InsertOneAsync(document, null, cancellationToken);
}
///
public virtual void AddOne(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument
where TKey : IEquatable
{
FormatDocument(document);
HandlePartitioned(document)
.InsertOne(document, null, cancellationToken);
}
///
public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default)
where TDocument : IDocument
where TKey : IEquatable
{
var documentsList = documents.ToList();
if (!documentsList.Any())
{
return;
}
foreach (var document in documentsList)
{
FormatDocument(document);
}
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
if (documentsList.Any(e => e is IPartitionedDocument))
{
foreach (var group in documentsList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey))
{
await HandlePartitioned(group.FirstOrDefault()).InsertManyAsync(group.ToList(), null, cancellationToken);
}
}
else
{
await GetCollection().InsertManyAsync(documentsList, null, cancellationToken);
}
}
///
public virtual void AddMany(IEnumerable documents, CancellationToken cancellationToken = default)
where TDocument : IDocument
where TKey : IEquatable
{
var documentList = documents.ToList();
if (!documentList.Any())
{
return;
}
foreach (var document in documentList)
{
FormatDocument(document);
}
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
if (documentList.Any(e => e is IPartitionedDocument))
{
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey))
{
HandlePartitioned(group.FirstOrDefault()).InsertMany(group.ToList(), cancellationToken: cancellationToken);
}
}
else
{
GetCollection().InsertMany(documentList, cancellationToken: cancellationToken);
}
}
///
/// Sets the value of the document Id if it is not set already.
///
/// The document type.
/// The type of the primary key.
/// The document.
protected void FormatDocument(TDocument document)
where TDocument : IDocument
where TKey : IEquatable
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
var defaultTKey = default(TKey);
if (document.Id == null
|| (defaultTKey != null
&& defaultTKey.Equals(document.Id)))
{
document.Id = IdGenerator.GetId();
}
}
}
}