Пример #1
0
        protected void SetLastUpdated(IAuditableEntity entity, IEntityHeader user)
        {
            var date = DateTime.Now.ToJSONString();

            entity.LastUpdatedBy   = EntityHeader.Create(user.Id, user.Text);
            entity.LastUpdatedDate = date;
        }
Пример #2
0
        public async Task DeleteAsync <TEntity>(string id, IEntityHeader org) where TEntity : class, IIDEntity, INoSQLEntity, IKeyedEntity, IOwnedEntity
        {
            var documentLink = await GetCollectionDocumentsLinkAsync();

            var docClient = GetDocumentClient();

            var collectionName = _dbName + "_Collections";
            var docUri         = UriFactory.CreateDocumentUri(_dbName, collectionName, id);
            var response       = await docClient.ReadDocumentAsync(docUri);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var json = response.Resource.ToString();

                if (String.IsNullOrEmpty(json))
                {
                    _logger.AddCustomEvent(LogLevel.Error, "DocumentDBRepoBase_GetDocumentAsync", $"Empty Response Content", new KeyValuePair <string, string>("entityType", typeof(TEntity).Name), new KeyValuePair <string, string>("id", id));
                    throw new RecordNotFoundException(typeof(TEntity).Name, id);
                }

                var entity = JsonConvert.DeserializeObject <TEntity>(json);
                if (org.Id != entity.OwnerOrganization.Id)
                {
                    throw new NotAuthorizedException($"Attempt to delete record of type {typeof(TEntity).Name} owned by org {entity.OwnerOrganization.Text} by org {org.Text}");
                }

                await docClient.DeleteDocumentAsync(docUri);
            }
        }
Пример #3
0
        public async Task DeleteIfExistsAsync <TEntity>(string key, IEntityHeader org) where TEntity : class, IIDEntity, INoSQLEntity, IKeyedEntity, IOwnedEntity
        {
            var entity = await FindWithKeyAsync <TEntity>(key, org, false);

            if (entity != null)
            {
                await DeleteAsync <TEntity>(entity.Id, org);
            }
        }
Пример #4
0
 public static void SetLastUpdatedFields(this IAuditableEntity entity, IEntityHeader user)
 {
     entity.LastUpdatedDate = DateTime.Now.ToJSONString();
     entity.LastUpdatedBy   = new EntityHeader()
     {
         Id   = user.Id,
         Text = user.Text
     };
 }
Пример #5
0
        public async Task <TEntity> FindWithKeyAsync <TEntity>(string key, IEntityHeader org, bool throwOnNotFound = true) where TEntity : class, IIDEntity, INoSQLEntity, IKeyedEntity, IOwnedEntity
        {
            var documentLink = await GetCollectionDocumentsLinkAsync();

            var docClient = GetDocumentClient();

            var docQuery = docClient.CreateDocumentQuery <TEntity>(documentLink)
                           .Where(itm => itm.Key == key && itm.OwnerOrganization.Id == org.Id && itm.EntityType == typeof(TEntity).Name)
                           .AsDocumentQuery();

            var result = await docQuery.ExecuteNextAsync <TEntity>();

            if (result == null && throwOnNotFound)
            {
                throw new Exception("Null Response from Query");
            }

            return(result.FirstOrDefault());
        }
Пример #6
0
        public static void SetCreationUpdatedFields(this IAuditableEntity entity, IEntityHeader user)
        {
            if (user == null || user.IsEmpty())
            {
                throw new InvalidOperationException("Must provide a valid user instance to assign to auditable fields.");
            }

            entity.CreationDate = DateTime.Now.ToJSONString();
            entity.CreatedBy    = new EntityHeader()
            {
                Id   = user.Id,
                Text = user.Text
            };

            entity.LastUpdatedDate = entity.CreationDate;
            entity.LastUpdatedBy   = new EntityHeader()
            {
                Id   = user.Id,
                Text = user.Text
            };
        }