Пример #1
0
        public T Update <T>(T entity) where T : ProductModel
        {
            if (entity is null || entity.Id.Equals(0))
            {
                return(null);
            }
            var oldEntity = _context.Products
                            .FirstOrDefault(p => p.Id == entity.Id);

            if (oldEntity is null)
            {
                return(null);
            }

            var updatedEntity = _mapper.Map <Product>(entity);

            updatedEntity.ModificationDate = DateTime.Now;;
            var changedFields = ObjectsComparer.GetChangedFields(oldEntity, updatedEntity);

            oldEntity.InjectFrom(new AvoidNullProps(new[] { "Id", "CreationDate" }), updatedEntity);
            _context.Entry(oldEntity).State = EntityState.Modified;
            _context.SaveChanges();

            return(entity);
        }
Пример #2
0
        public void CanWriteAndRead()
        {
            var apiEntity = new ApiSample
            {
                Id       = Guid.NewGuid(),
                SomeData = "test"
            };

            this.service.WriteAsync(apiEntity).Wait();
            var readedApiEntity = this.service.ReadAsync(apiEntity.Id).Result;

            Assert.IsTrue(ObjectsComparer.AreEqual(apiEntity, readedApiEntity));
        }
Пример #3
0
        public void CanWriteAndRead()
        {
            var apiEntity = new ApiSample
            {
                Id       = Guid.NewGuid(),
                SomeData = "test"
            };

            service.Write(apiEntity);
            var readedApiEntity = service.Read(apiEntity.Id);

            Assert.IsTrue(ObjectsComparer.AreEqual(apiEntity, readedApiEntity));
        }
        public void CanGetById()
        {
            var apiEntity = new ApiSample
            {
                Id = Guid.NewGuid()
            };

            this.service.WriteAsync(apiEntity).Wait();
            var sampleController = new SampleController(this.service);
            var resultApiEntity  = sampleController.GetAsync(apiEntity.Id).Result;

            Assert.IsTrue(ObjectsComparer.AreEqual(apiEntity, resultApiEntity));
        }
Пример #5
0
        public void CanGetById()
        {
            var apiEntity = new ApiSample
            {
                Id = Guid.NewGuid()
            };

            service.Write(apiEntity);
            var sampleController = new SampleController(service);
            var resultApiEntity  = sampleController.Get(apiEntity.Id);

            Assert.IsTrue(ObjectsComparer.AreEqual(apiEntity, resultApiEntity));
        }
        public async Task CanWriteAndReadAsync()
        {
            var apiEntity = new ApiSample
            {
                Id       = Guid.NewGuid(),
                SomeData = "test"
            };

            await service.WriteAsync(apiEntity);

            var readedApiEntity = await service.ReadAsync(apiEntity.Id);

            Assert.IsTrue(ObjectsComparer.AreEqual(apiEntity, readedApiEntity));
        }
        public void CanPostApiEntity()
        {
            var apiEntity = new ApiSample
            {
                Id = Guid.NewGuid()
            };

            var sampleController = new SampleController(this.service);

            sampleController.PostAsync(apiEntity).Wait();

            var readedEtity = this.service.ReadAsync(apiEntity.Id).Result;

            Assert.IsTrue(ObjectsComparer.AreEqual(apiEntity, readedEtity));
        }
Пример #8
0
        public void CanPostApiEntity()
        {
            var apiEntity = new ApiSample
            {
                Id = Guid.NewGuid()
            };

            var sampleController = new SampleController(service);

            sampleController.Post(apiEntity);

            var readedEtity = service.Read(apiEntity.Id);

            Assert.IsTrue(ObjectsComparer.AreEqual(apiEntity, readedEtity));
        }
Пример #9
0
        public void TryReadIsCorrect()
        {
            var apiEntity = new ApiSample
            {
                Id       = Guid.NewGuid(),
                SomeData = "test"
            };

            service.Write(apiEntity);
            ApiSample resultApiEntity;
            var       readResult = service.TryRead(apiEntity.Id, out resultApiEntity);

            Assert.IsTrue(readResult);
            Assert.IsTrue(ObjectsComparer.AreEqual(apiEntity, resultApiEntity));

            readResult = service.TryRead(Guid.NewGuid(), out resultApiEntity);
            Assert.IsFalse(readResult);
            Assert.AreEqual(default(ApiSample), resultApiEntity);
        }
        private static void InternalCanReplaceEntity(Action <GuidIdTestEntity> insert, Func <Guid, GuidIdTestEntity> getById, Action <GuidIdTestEntity> replace)
        {
            var testEntity = new GuidIdTestEntity
            {
                Id          = Guid.NewGuid(),
                SomeData    = 10,
                AnotherData = 5
            };

            insert(testEntity);
            var replaceWith = new GuidIdTestEntity
            {
                Id          = testEntity.Id,
                SomeData    = 5,
                AnotherData = 10
            };

            replace(replaceWith);
            var resultEntity = getById(testEntity.Id);

            Assert.IsTrue(ObjectsComparer.AreEqual(replaceWith, resultEntity));
        }
Пример #11
0
        public async Task TryReadIsCorrectAsync()
        {
            var apiEntity = new ApiSample
            {
                Id       = Guid.NewGuid(),
                SomeData = "test"
            };

            await service.WriteAsync(apiEntity);

            var readResult = await service.TryReadAsync(apiEntity.Id);

            var resultApiEntity = readResult.Item2;

            Assert.IsTrue(readResult.Item1);
            Assert.IsTrue(ObjectsComparer.AreEqual(apiEntity, resultApiEntity));

            readResult = await service.TryReadAsync(Guid.NewGuid());

            resultApiEntity = readResult.Item2;
            Assert.IsFalse(readResult.Item1);
            Assert.AreEqual(default(ApiSample), resultApiEntity);
        }
Пример #12
0
        public T Update <T>(T entity) where T : CustomerModel
        {
            if (entity is null || entity.Id.Equals(0))
            {
                return(null);
            }
            var oldEntity = _context.Customers
                            .FirstOrDefault(p => p.Id == entity.Id);

            if (oldEntity is null)
            {
                return(null);
            }

            var updatedEntity = _mapper.Map <Customer>(entity);

            var changedFields = ObjectsComparer.GetChangedFields(oldEntity, updatedEntity);

            oldEntity.InjectFrom <AvoidNullProps>(updatedEntity);
            _context.Entry(oldEntity).State = EntityState.Modified;
            _context.SaveChanges();

            return(entity);
        }