Exemple #1
0
        public virtual void ItFetchesEntityForUser()
        {
            // create entity
            var entity = converter.ToModel(CreateNew);

            // check entity fetched
            var entityFetched = service.GetForUser(user.Id);

            entityFetched.Should().NotBeNull();
            CheckAreEquivalent(entityFetched, entity);
        }
        public virtual void ItUpdatesEntity()
        {
            var entity = CreateNew;
            var model  = converter.ToModel(entity);

            ChangeEntity(model);
            model = service.Update(model);
            service.SaveChanges();

            // check entity changes saved
            var entitySaved = FetchItem(model);

            entitySaved.Should().NotBeNull();
            CheckAreEquivalent(entitySaved, model);
            OnEntityUpdated(entity);
        }
        public virtual TModel Update(TModel model)
        {
            // get from database
            var dbEntity = GetDbEntity(model);
            // get old model
            var oldModel = _converter.ToModel(dbEntity);

            // update entity by converter
            dbEntity = _converter.UpdateEntity(dbEntity, model);
            // update entity in database
            dbEntity = Update(dbEntity);
            // save changes
            SaveChanges();
            // call OnUpdated method
            OnUpdated(oldModel, model);

            // TODO: check if related entity foreign key was changed related entity will be updated Auto or not. For now implementation like "not"
            return(_converter.UpdateModel(GetDbDetailedEntity(dbEntity), model));
        }
        public virtual TModel GetForUser(int userId)
        {
            var settings = Scope.FirstOrDefault(e => e.UserId == userId);

            if (settings == null)
            {
                Entities.Add(new T()
                {
                    UserId = userId
                });
                _context.SaveChanges();

                settings = Scope.FirstOrDefault(e => e.UserId == userId);
            }

            return(_converter.ToModel(settings));
        }