예제 #1
0
 private static void SetCreatedBy(SquidexEvent @event, MongoEntity entity)
 {
     if (entity is IEntityWithCreatedBy withCreatedBy)
     {
         withCreatedBy.CreatedBy = @event.Actor;
     }
 }
예제 #2
0
 private static void SetLastModifiedBy(SquidexEvent @event, MongoEntity entity)
 {
     if (entity is IEntityWithLastModifiedBy withModifiedBy)
     {
         withModifiedBy.LastModifiedBy = @event.Actor;
     }
 }
예제 #3
0
        public ComRet Login(string account, string password, ClientType client, string client_sn)
        {
            var passport = MongoEntity.Get <Passport>(p => p.Account == account && p.Password == password);

            if (passport == null)
            {
                return(Result(false, "账户名不正确"));
            }

            Profile profile = MongoEntity.Get <Profile>(p => p.Account == account);

            if (profile == null)
            {
                return(Result(false, "账户不合法"));
            }

            profile.Client   = client;
            profile.ClientSN = client_sn;
            profile.Save();

            passport.Ticket = BusinessAuthentication.GenTicket(profile);
            passport.Save();

            return(Result(true, passport.Ticket));
        }
예제 #4
0
 private static void SetVersion(EnvelopeHeaders headers, MongoEntity entity)
 {
     if (entity is IEntityWithVersion withVersion)
     {
         withVersion.Version = headers.EventStreamNumber();
     }
 }
        public void CreateFileTestCase()
        {
            //加载本地文件,并实例一个IMongoFile
            IMongoFile file = MongoEntity.CreateFile <MyFile>(@"c:\pic1.jpg", "pic2.jpg", "jpg");

            //下载文件,等同于文件另存为
            file.Download(@"c:\beforesave.jpg");

            //文件保存至数据库
            file.Save();

            //从数据中加载刚才保存的文件
            IMongoFile fs = MongoEntity.LoadFile <MyFile>(file.Id);

            //将从数据中加载的文件下载
            fs.Download(@"c:\aftersave.jpg");

            //根据数据库中的文件名检索文件
            var files = MongoEntity.LoadAllFiles <MyFile>("pic2.jpg");

            //根据文件id,将数据库中的文件下载到本地
            MongoEntity.DownloadFile <MyFile>(file.Id, @"c:\copy.jpg");

            Assert.AreEqual(file.Id, fs.Id);
            Assert.AreEqual(1, files.Count);
            Assert.AreEqual(file.Id, files[0].Id);
            Assert.IsNull(file.MD5);
            Assert.IsNotNull(fs.MD5);
            Assert.AreEqual(file.Size, fs.Size);
            Assert.AreEqual(file.Data, fs.Data);
        }
예제 #6
0
        public void TestRemove()
        {
            long count = MongoEntity.RemoveAll <Student>(e => e.Name == "hyf");
            var  ret   = MongoEntity.Select <Student>(s => s.Name == "hyf").Count();

            Assert.Greater(count, ret);
        }
예제 #7
0
        public void TestEntityList()
        {
            School school = new School()
            {
                Name = "WinStudio"
            };

            List <Department> depts = new List <Department>()
            {
                new Department()
                {
                    Name = "Master"
                },
                new Department()
                {
                    Name = "Admin"
                }
            };

            foreach (Department dept in depts)
            {
                school.Departments.Add(dept);
            }

            foreach (Student student in students)
            {
                school.Students.Add(student);
            }
            school.Save();

            School sch = MongoEntity.Get <School>(school.Id);

            Console.WriteLine(sch.Name);
        }
예제 #8
0
        public void Setup()
        {
            MongoDBRepository.RegisterMongoDBContext(new TestDBContext());
            MongoDBRepository.RegisterMongoIndex();

            students = new List <Student>()
            {
                new Student {
                    Name = "hyf", Age = 33
                },
                new Student {
                    Name = "zhc", Age = 30
                }
            };
            teachers = new List <Teacher>()
            {
                new Teacher {
                    Name = "Lee", Age = 53
                },
                new Teacher {
                    Name = "Chen", Age = 50
                }
            };
            Grade grade = new Grade();

            teachers.ForEach(t => grade.Teachers.Add(t.ToDBRef()));
            grades = new List <Grade>()
            {
                grade
            };

            MongoEntity.Save(students);
            MongoEntity.Save(teachers);
            MongoEntity.Save(grades);
        }
예제 #9
0
        public IUserSnap GetUserSnap(string ticket, LoginType type)
        {
            var passport = MongoEntity.Get <Passport>(p => p.SecurityKey == ticket);

            if (passport == null)
            {
                return(null);
            }
            var profile = MongoEntity.Get <Profile>(p => p.SecurityKey == ticket);

            if (profile == null)
            {
                return(null);
            }
            IUserSnap snap = new UserSnap()
            {
                Id             = profile.Id,
                SecurityKey    = profile.SecurityKey,
                Name           = profile.Name,
                Account        = profile.Account,
                LoginCode      = passport.GetLoginCode(type),
                LoginType      = type,
                Email          = profile.Email,
                RegisteredTime = profile.BsonObjectId.CreationTime
            };

            Log(snap.Name + " do GetUserSnap");
            return(snap);
            //return new UserInfo(profile.Id, profile.Account, profile.Name, profile.Email, (profile.Native == null ? "" : string.Format("{0}{1}{2}", profile.Region.Name, profile.Nationality.Name, profile.Native.Name)), DateTime.Now, profile.SecurityKey);
        }
예제 #10
0
 private static void SetAppId(SquidexEvent @event, MongoEntity entity)
 {
     if (entity is IAppRefEntity appEntity && @event is AppEvent appEvent)
     {
         appEntity.AppId = appEvent.AppId.Id;
     }
 }
예제 #11
0
 public void InitModel(string id)
 {
     if (IsModelReady)
     {
         return;
     }
     _dto_T = MongoEntity.Get <T>(id);
 }
예제 #12
0
            /// <summary>
            /// Insert a generic MongoEntity which is in New state
            /// </summary>
            public void GenericInsert(MongoContext ctx, MongoEntity ent)
            {
                if (ctx.Debug)
                {
                    Debug.WriteLine($"Insert ent type={ent.GetType()} id={ent.Id}");
                }

                Collection.InsertOne((T)ent);
            }
예제 #13
0
            /// <summary>
            /// enclose the given entity taking a backup
            /// </summary>
            public AttachedMongoEntity(MongoEntity entity, Type type = null)
            {
                type = type ?? entity.GetType();

                NominalType = type;

                Entity = entity;

                ResetOrigEntity();
            }
예제 #14
0
        public void Clear()
        {
            MongoEntity.RemoveAll <Student>();
            MongoEntity.RemoveAll <Teacher>();
            MongoEntity.RemoveAll <Grade>();
            MongoEntity.RemoveAll <School>();
            MongoEntity.RemoveAllFiles <MyFile>();

            MongoDBRepository.UnregisterDBContext <TestDBContext>();
        }
        public void ValidateStringIdAndParseToObjId()
        {
            MongoEntity sut = new MongoEntity(STR_ID);

            var act    = sut.InternalId;
            var expect = ObjectId.Parse(STR_ID);

            Assert.Equal(STR_ID, act.ToString());
            Assert.Equal(STR_ID, sut.Id);
            Assert.Equal(expect, act);
        }
예제 #16
0
        public ComRet GetProfile(string id)
        {
            var profile = MongoEntity.Get <Profile>(id);

            if (profile == null)
            {
                return(Result("不存在的用户"));
            }
            Log("GetProfile");
            return(Result(profile));
        }
예제 #17
0
            /// <summary>
            /// Remove a generic MongoEntity which is in Deleted state
            /// </summary>
            /// <param name="ent"></param>
            public void GenericDelete(MongoContext ctx, MongoEntity ent)
            {
                if (ctx.Debug)
                {
                    Debug.WriteLine($"Delete ent type={ent.GetType()} id={ent.Id}");
                }

                var filter = Builders <T> .Filter.Eq((x) => x.Id, ent.Id);

                Collection.DeleteOne(filter);
            }
        public void TestUpdate()
        {
            Student student2 = grade.Pick <Student>(students[2].Id);

            student2.Age = 100;
            grade.Update();

            var g = MongoEntity.Get <Grade>(grade.Id);

            Assert.AreEqual(student2.Age, g.Pick <Student>(student2.Id).Age);
        }
예제 #19
0
        public ComRet Register(string account, string password, LoginType origin)
        {
            if (MongoEntity.Exists <Passport>(p => p.Account == account))
            {
                return(Result("账户名已存在"));
            }
            var passport = new Passport(account, password, origin);

            passport.SecurityKey = string.Format("{0}{1}{2}{3}{4}", passport.Id, passport.Account, passport.BsonObjectId.CreationTime.ToString("yyyyMMddHHmmss"), passport.BsonObjectId.Pid, passport.BsonObjectId.Timestamp).ToMD5(); //SecurityManager.Instance.GenSecurityKey(account, passport.Id);
            passport.Save();
            return(Result(true, passport.Id));
        }
예제 #20
0
        public void TestSave()
        {
            Student student = new Student();

            student.Name = "hyf";
            student.Age  = 30;
            student.Save();
            MongoEntity.Save <Student>(students);
            var stud = MongoEntity.Get <Student>(student.Id);

            MongoEntity.Get <Student>(s => s.Name == "hyf" && s.Age > 33);
            Assert.AreEqual(student.Name, stud.Name);
            Assert.AreEqual(student.Age, stud.Age);
        }
예제 #21
0
        public ComRet UpdateInfo(string ticket, string telphone)
        {
            var passport = MongoEntity.Get <Passport>(p => p.Ticket == ticket);

            if (passport == null)
            {
                return(Result(false, "账户不存在"));
            }
            var profile = MongoEntity.Get <Profile>(p => p.Account == passport.Account);

            profile.Telphone = telphone;
            profile.Save();
            return(Result(true));
        }
예제 #22
0
        public ComRet Register(string passportid, string account, string name, string email)
        {
            if (MongoEntity.Exists <Profile>(p => p.Account == account))
            {
                return(Result("账户名称已存在"));
            }
            var profile = new Profile()
            {
                Id = passportid, Account = account, Name = name, Email = email
            };

            profile.SecurityKey = string.Format("{0}{1}{2}{3}{4}", profile.Id, profile.Account, profile.BsonObjectId.CreationTime.ToString("yyyyMMddHHmmss"), profile.BsonObjectId.Pid, profile.BsonObjectId.Timestamp).ToMD5(); //SecurityManager.Instance.GenSecurityKey(account, passport.Id);
            profile.Save();
            return(Result(true, profile.Id));
        }
        public void ThrowErrorAndNotConvertIfObjIdIsNotValid()
        {
            var act = Assert.Throws <BadRequestException>(() => MongoEntity.ValidateAndParseObjId("AnyId"));

            var expect = new {
                statusCode = HttpStatusCode.BadRequest,
                message    = "BadRequest id: AnyId is not valid"
            };

            var obj1Str = JsonConvert.SerializeObject(expect);
            var obj2Str = JsonConvert.SerializeObject(act.HttpErrorResponse);

            Assert.Equal("BadRequest id: AnyId is not valid", act.Message);
            Assert.Equal(obj1Str, obj2Str);
        }
예제 #24
0
        public ComRet UpdateInfo(string ticket, string name, Gender gender)
        {
            var passport = MongoEntity.Get <Passport>(p => p.Ticket == ticket);

            if (passport == null)
            {
                return(Result(false, "账户不存在"));
            }
            var profile = MongoEntity.Get <Profile>(p => p.Account == passport.Account);

            profile.Name   = name;
            profile.Gender = gender;
            profile.Save();
            return(Result(true));
        }
예제 #25
0
        public void TestTypeTest()
        {
            TypeTest tt = new TypeTest();

            tt.Name  = "TypeTester";
            tt.Bytes = Encoding.UTF8.GetBytes(tt.Name);
            tt.Save();

            var dbtt = MongoEntity.Get <TypeTest>(tt.Id);

            string b64 = Convert.ToBase64String(tt.Bytes);

            Assert.AreEqual(tt.Name, dbtt.Name);
            Assert.AreEqual(tt.Bytes.Length, dbtt.Bytes.Length);
            Assert.AreEqual(tt.Bytes, dbtt.Bytes);
        }
        public void TestAdd()
        {
            grade.Update();

            students[0].Name = "NameChanged";
            students[0].Save();

            var g = MongoEntity.Get <Grade>(grade.Id);

            Assert.AreSame(students[0].Name, grade.Pick <Student>(students[0].Id).Name);
            Assert.AreNotSame(grade.Pick <Student>(students[0].Id).Name, g.Pick <Student>(students[0].Id).Name);
            Assert.AreEqual(grade.Count <Student>(), g.Count <Student>());
            Assert.AreEqual(grade.Count <Teacher>(), g.Count <Teacher>());

            Assert.AreEqual(g.Count <Student>(), MongoEntity.Select <Student>(s => s.Age > 0).Count());
        }
        public void ShouldThrowBadRequestExceptionIfStringIdIsNotAnValidObjId()
        {
            MongoEntity sut = new MongoEntity();

            var act = Assert.Throws <BadRequestException>(() => sut.Id = "AnyId");

            var expect = new {
                statusCode = HttpStatusCode.BadRequest,
                message    = "BadRequest id: AnyId is not valid"
            };

            var obj1Str = JsonConvert.SerializeObject(expect);
            var obj2Str = JsonConvert.SerializeObject(act.HttpErrorResponse);

            Assert.Equal("BadRequest id: AnyId is not valid", act.Message);
            Assert.Equal(obj1Str, obj2Str);
        }
예제 #28
0
        public ComRet Register(string account, string password)
        {
            if (MongoEntity.Exists <Passport>(p => p.Account == account))
            {
                return(Result(false, "账户已存在"));
            }
            Passport passport = new Passport(account, password);

            passport.Save();

            Profile profile = new Profile()
            {
                Account = passport.Account, Name = account
            };

            profile.Save();
            return(Result(true));
        }
예제 #29
0
        static void Main(string[] args)
        {
            MongoDBContext context = new MongoDBContext("mongodb://localhost:27017", "mongoeav");

            MongoEntity ent1 = new MongoEntity();

            ent1.EntityName = "test entity";
            ent1.EntityFields.Add(new MongoEntityField {
                name = "f1", type = MongoDB.Bson.BsonType.String
            });
            ent1.EntityFields.Add(new MongoEntityField {
                name = "f2", type = MongoDB.Bson.BsonType.Int32
            });

            context.CreateEntityDef(ent1);

            Console.ReadKey(); // wait until exit...
        }
예제 #30
0
        public void TestSelectPaged()
        {
            int pageCount, allCount;
            //MongoEntity.Save<Student>(students);
            var querable = MongoEntity.Select <Student>(s => s.Age >= 19 && s.Age <= 40, s => s.Age, 0, 2, out pageCount, out allCount).ToList();

            Assert.AreEqual(2, querable.Count);
            Assert.AreEqual(2, pageCount);
            Assert.AreEqual(3, allCount);
            MongoEntity.Save(new List <Student>()
            {
                new Student {
                    Name = "hyf", Age = 33
                },
                new Student {
                    Name = "zhc", Age = 30
                }
            });
        }