protected async Task DeleteAsync(TEntity persistedEntity, string etag, CancellationToken cancellationToken)
        {
            using (new SessionScope(SessionFactory))
            {
                if (persistedEntity == null)
                {
                    throw new NotFoundException("Resource to delete was not found.");
                }

                // only check last modified data
                if (!string.IsNullOrEmpty(etag))
                {
                    var lastModifiedDate = _eTagProvider.GetDateTime(etag);

                    if (!persistedEntity.LastModifiedDate.Equals(lastModifiedDate))
                    {
                        throw new ConcurrencyException("Resource was modified by another consumer.");
                    }
                }

                using (var trans = Session.BeginTransaction())
                {
                    try
                    {
                        var classMetadata = (AbstractEntityPersister)Session.SessionFactory.GetClassMetadata(typeof(TEntity));

                        string entityName = classMetadata.IsInherited
                            ? classMetadata.MappedSuperclass
                            : classMetadata.Name;

                        await Session.CreateQuery($"delete from {entityName} where Id = :id")
                        .SetParameter("id", persistedEntity.Id)
                        .ExecuteUpdateAsync(cancellationToken);

                        await trans.CommitAsync(cancellationToken);
                    }
                    catch (Exception)
                    {
                        await trans.RollbackAsync(cancellationToken);

                        throw;
                    }
                }
            }
        }
Esempio n. 2
0
        public void When_GetDateTime_input_invalid__throw_()
        {
            const string etag = "-0080000000000058000"; //invalid DateTime test

            Assert.Throws <BadRequestException>(() => _eTagProvider.GetDateTime(etag));
        }