Ejemplo n.º 1
0
        public List<TwitterApp.Models.User> FindAll(ActionState actionState)
        {
            List<TwitterApp.Models.User> userList;
            MongoServer server = null;
            MongoDatabase database = null;
            TwitterApp.Models.User entity;

            userList = new List<User>();
            entity = null;
            try
            {
                server = MongoServer.Create(ConfigurationManager.AppSettings[CommonConstants.ConnictionString]);
                database = server.GetDatabase(CommonConstants.DatabaseName);
                MongoCollection<BsonDocument> user = database.GetCollection<BsonDocument>(UserConstants.UserCollection);
                foreach (BsonDocument userEntity in user.FindAll())
                {
                    entity = new User();
                    entity.ID = userEntity[UserConstants.ID].ToString();
                    entity.Name = userEntity[UserConstants.Name].AsString;
                    entity.Password = userEntity[UserConstants.Password].AsString;
                    entity.CreatedDate = userEntity[UserConstants.CreatedDate].AsDateTime;
                    entity.Username = userEntity[UserConstants.Username].AsString;
                    userList.Add(entity);
                }
                actionState.SetSuccess();
            }
            catch (Exception ex)
            {
                actionState.SetFail(ActionStateEnum.Exception, ex.Message);
            }
            finally
            {
                server.Disconnect();
                server = null;
                database = null;
            }
            return userList;
        }
Ejemplo n.º 2
0
 public void Delete(TwitterApp.Models.User entity, ActionState actionState)
 {
     MongoServer server = null;
     MongoDatabase database = null;
     try
     {
         server = MongoServer.Create(ConfigurationManager.AppSettings[CommonConstants.ConnictionString]);
         database = server.GetDatabase(CommonConstants.DatabaseName);
         MongoCollection<BsonDocument> user = database.GetCollection<BsonDocument>(UserConstants.UserCollection);
         user.Remove(new QueryDocument(UserConstants.ID, ObjectId.Parse(entity.ID)));
         actionState.SetSuccess();
     }
     catch (Exception ex)
     {
         actionState.SetFail(ActionStateEnum.Exception, ex.Message);
     }
     finally
     {
         server.Disconnect();
         server = null;
         database = null;
     }
 }
Ejemplo n.º 3
0
        public void Insert(TwitterApp.Models.User entity, ActionState actionState)
        {
            MongoServer server = null;
            MongoDatabase database = null;
            try
            {
                server = MongoServer.Create(ConfigurationManager.AppSettings[CommonConstants.ConnictionString]);
                database = server.GetDatabase(CommonConstants.DatabaseName);
                var dataCollection = database.CollectionExists(UserConstants.UserCollection);
                if (Convert.ToBoolean(dataCollection) == false)
                {
                    database.CreateCollection(UserConstants.UserCollection);
                }
                else
                {
                    MongoCollection<BsonDocument> user = database.GetCollection<BsonDocument>(UserConstants.UserCollection);
                    BsonDocument userEntity = new BsonDocument {
                { UserConstants.Name, entity.Name },
                { UserConstants.Username, entity.Username },
                { UserConstants.Password, entity.Password },
                { UserConstants.CreatedDate, entity.CreatedDate }
                };
                    user.Insert(userEntity);
                    actionState.SetSuccess();

                }
            }
            catch (Exception ex)
            {
                actionState.SetFail(ActionStateEnum.Exception, ex.Message);
            }
            finally
            {
                server.Disconnect();
                server = null;
                database = null;

            }
        }
Ejemplo n.º 4
0
        public void Update(TwitterApp.Models.User entity, ActionState actionState)
        {
            MongoServer server = null;
            MongoDatabase database = null;
            try
            {
                server = MongoServer.Create(ConfigurationManager.AppSettings[CommonConstants.ConnictionString]);
                database = server.GetDatabase(CommonConstants.DatabaseName);
                MongoCollection<BsonDocument> user = database.GetCollection<BsonDocument>(UserConstants.UserCollection);
                var userEntity = user.FindOneById(ObjectId.Parse(entity.ID));
                userEntity[UserConstants.Name] = entity.Name;
                userEntity[UserConstants.Password] = entity.Password;
                user.Save(userEntity);
                actionState.SetSuccess();

            }
            catch (Exception ex)
            {
                actionState.SetFail(ActionStateEnum.Exception, ex.Message);
            }
            finally
            {
                server.Disconnect();
                server = null;
                database = null;
            }
        }