/// <summary> /// Creates a new <see cref="TEntity"/> in the collection. /// </summary> /// <param name="elm">The <see cref="TEntity"/> to create.</param> /// <returns>The created <see cref="TEntity"/>.</returns> public virtual TEntity Create(TEntity elm) { if (elm is IDbTrackedEntity) { IDbTrackedEntity elmTracked = elm as IDbTrackedEntity; if (elmTracked.Created == null) { (elm as IDbTrackedEntity).Created = DateTime.UtcNow; } if (elmTracked.Updated == DateTime.MinValue) { (elm as IDbTrackedEntity).Updated = (elm as IDbTrackedEntity).Created; } if (elmTracked.CreatedBy == null) { (elm as IDbTrackedEntity).CreatedBy = new UserReference() { Id = Guid.NewGuid(), Username = "******", }; } if (elmTracked.UpdatedBy == null) { (elm as IDbTrackedEntity).UpdatedBy = (elm as IDbTrackedEntity).CreatedBy; } } // Id field is automatically populated. this.Entities.InsertOne(elm); return(elm); }
/// <summary> /// Updates an <see cref="TEntity"/> from the collection, based on it's id. /// </summary> /// <param name="id">The id of the <see cref="TEntity"/> to update.</param> /// <param name="elmIn">The <see cref="TEntity"/> to update.</param> /// <returns>The operation result.</returns> public virtual ReplaceOneResult Update(Guid id, TEntity elmIn) { if (elmIn is IDbTrackedEntity) { IDbTrackedEntity elmTracked = elmIn as IDbTrackedEntity; (elmIn as IDbTrackedEntity).Updated = DateTime.UtcNow; (elmIn as IDbTrackedEntity).UpdatedBy = new UserReference() { Id = Guid.NewGuid(), Username = "******", // TODO. }; } return(this.Entities.ReplaceOne(book => book.Id == id, elmIn)); }