/// <summary>
        /// get IEnumberable of all characters sorted by Id
        /// </summary>
        /// <param name="dalErrorCode">DAL error code</param>
        /// <param name="message">error message</param>
        /// <returns></returns>
        public IEnumerable <Character> GetAllCharacters(out DalErrorCode dalErrorCode, out string message)
        {
            List <Character> characters = null;

            message = "";

            using (_characterRepository)
            {
                characters = _characterRepository.GetAll(out dalErrorCode) as List <Character>;

                if (dalErrorCode == DalErrorCode.GOOD)
                {
                    if (characters != null)
                    {
                        characters.OrderBy(c => c.Id);
                    }
                }
                else
                {
                    message = "An error occurred connecting to the database.";
                }
            }

            return(characters);
        }
        /// <summary>
        /// get character by id
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="dalErrorCode">DAL error code</param>
        /// <param name="message">message</param>
        /// <returns></returns>
        public Character GetCharacterById(int id, out DalErrorCode dalErrorCode, out string message)
        {
            message = "";
            Character character;

            using (_characterRepository)
            {
                character = _characterRepository.GetById(id, out dalErrorCode);

                if (dalErrorCode == DalErrorCode.GOOD)
                {
                    if (character == null)
                    {
                        message      = $"No character has id {id}.";
                        dalErrorCode = DalErrorCode.ERROR;
                    }
                }
                else
                {
                    message = "An error occurred connecting to the database.";
                }
            }

            return(character);
        }
        /// <summary>
        /// get IEnumberable of all characters sorted by Id
        /// </summary>
        /// <param name="dalErrorCode">DAL error code</param>
        /// <param name="message">error message</param>
        /// <returns></returns>
        public IEnumerable <Person> GetAllPersons(out DalErrorCode dalErrorCode, out string message)
        {
            List <Person> persons = null;

            message = "";

            using (_personRepository)
            {
                persons = _personRepository.GetAll(out dalErrorCode) as List <Person>;

                if (dalErrorCode == DalErrorCode.GOOD)
                {
                    if (persons != null)
                    {
                        persons.OrderBy(c => c.Id);
                    }
                }
                else
                {
                    message = "An error occurred connecting to the database.";
                }
            }

            return(persons);
        }
        /// <summary>
        /// get character by id
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="dalErrorCode">DAL error code</param>
        /// <param name="message">message</param>
        /// <returns></returns>
        public Person GetPersonById(int id, out DalErrorCode dalErrorCode, out string message)
        {
            message = "";
            Person person;

            using (_personRepository)
            {
                person = _personRepository.GetById(id, out dalErrorCode);

                if (dalErrorCode == DalErrorCode.GOOD)
                {
                    if (person == null)
                    {
                        message      = $"No person has id {id}.";
                        dalErrorCode = DalErrorCode.ERROR;
                    }
                }
                else
                {
                    message = "An error occurred connecting to the database.";
                }
            }

            return(person);
        }
        /// <summary>
        /// add a character to the data file
        /// </summary>
        /// <param name="character">character</param>
        /// <param name="dalErrorCode">DAL error code</param>
        /// <param name="message">message</param>
        public void AddPerson(Person person, out DalErrorCode dalErrorCode, out string message)
        {
            message = "";

            _personRepository.Insert(person, out dalErrorCode);

            if (dalErrorCode == DalErrorCode.ERROR)
            {
                message = "There was an error connecting to the data file.";
            }
        }
        /// <summary>
        /// add a character to the data file
        /// </summary>
        /// <param name="character">character</param>
        /// <param name="dalErrorCode">DAL error code</param>
        /// <param name="message">message</param>
        public void AddCharacter(Character character, out DalErrorCode dalErrorCode, out string message)
        {
            message = "";

            _characterRepository.Insert(character, out dalErrorCode);

            if (dalErrorCode == DalErrorCode.ERROR)
            {
                message = "There was an error connecting to the data file.";
            }
        }
Example #7
0
        public void Insert(Character character, out DalErrorCode dalErrorCode)
        {
            try
            {
                var characterCollection = GetCharacterColletion();

                characterCollection.InsertOne(character);

                dalErrorCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                dalErrorCode = DalErrorCode.ERROR;
            }
        }
        public void Delete(int id, out DalErrorCode dalErrorCode)
        {
            try
            {
                var personCollection = GetPersonCollection();

                var result = personCollection.DeleteOne(Builders <Person> .Filter.Eq("Id", id));

                dalErrorCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                dalErrorCode = DalErrorCode.ERROR;
            }
        }
 private bool CharacterDocumentExists(int id, out DalErrorCode dalErrorCode)
 {
     using (_characterRepository)
     {
         if (_characterRepository.GetById(id, out dalErrorCode) != null)
         {
             return(true);
         }
         else
         {
             dalErrorCode = DalErrorCode.ERROR;
             return(false);
         }
     }
 }
        public void Insert(Person person, out DalErrorCode dalErrorCode)
        {
            try
            {
                var personCollection = GetPersonCollection();

                personCollection.InsertOne(person);

                dalErrorCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                dalErrorCode = DalErrorCode.ERROR;
            }
        }
        public void Update(Person person, out DalErrorCode dalErrorCode)
        {
            try
            {
                var personCollection = GetPersonCollection();

                //characterList.Find(Builders<Character>.Filter.Eq("id", character.Id)). UpdateOne;
                personCollection.FindOneAndReplace(Builders <Person> .Filter.Eq("Id", person.Id), person);

                dalErrorCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                dalErrorCode = DalErrorCode.ERROR;
            }
        }
Example #12
0
        public void Update(Character character, out DalErrorCode dalErrorCode)
        {
            try
            {
                var characterCollection = GetCharacterColletion();

                //characterList.Find(Builders<Character>.Filter.Eq("id", character.Id)). UpdateOne;
                characterCollection.FindOneAndReplace(Builders <Character> .Filter.Eq("Id", character.Id), character);

                dalErrorCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                dalErrorCode = DalErrorCode.ERROR;
            }
        }
Example #13
0
        public IEnumerable <Character> GetAll(out DalErrorCode dalErrorCode)
        {
            try
            {
                var characterCollection = GetCharacterColletion();

                _characters = characterCollection.FindSync(Builders <Character> .Filter.Empty).ToList();

                dalErrorCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                dalErrorCode = DalErrorCode.ERROR;
            }

            return(_characters);
        }
        public IEnumerable <Person> GetAll(out DalErrorCode dalErrorCode)
        {
            try
            {
                var personCollection = GetPersonCollection();

                _persons = personCollection.FindSync(Builders <Person> .Filter.Empty).ToList();

                dalErrorCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                dalErrorCode = DalErrorCode.ERROR;
            }

            return(_persons);
        }
        public Person GetById(int id, out DalErrorCode dalErrorCode)
        {
            Person person = null;

            try
            {
                var personCollection = GetPersonCollection();

                person = personCollection.Find(Builders <Person> .Filter.Eq("Id", id)).SingleOrDefault() as Person;

                dalErrorCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                dalErrorCode = DalErrorCode.ERROR;
            }

            return(person);
        }
Example #16
0
        public Character GetById(int id, out DalErrorCode dalErrorCode)
        {
            Character character = null;

            try
            {
                var characterCollection = GetCharacterColletion();

                character = characterCollection.Find(Builders <Character> .Filter.Eq("Id", id)).SingleOrDefault() as Character;

                dalErrorCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                dalErrorCode = DalErrorCode.ERROR;
            }

            return(character);;
        }
        /// <summary>
        /// read the mongoDb collection and load a list of character objects
        /// </summary>
        /// <returns>list of characters</returns>
        public IEnumerable <Character> ReadAll(out DalErrorCode statusCode)
        {
            List <Character> characters = new List <Character>();

            try
            {
                var            client   = new MongoClient(_connectionString);
                IMongoDatabase database = client.GetDatabase("cit255");
                IMongoCollection <Character> characterList = database.GetCollection <Character>("flintstone_characters");

                characters = characterList.Find(Builders <Character> .Filter.Empty).ToList();

                statusCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                statusCode = DalErrorCode.ERROR;
            }

            return(characters);
        }
        /// <summary>
        /// read the mongoDb collection and load a list of character objects
        /// </summary>
        /// <returns>list of characters</returns>
        public IEnumerable <Person> ReadAll(out DalErrorCode statusCode)
        {
            List <Person> persons = new List <Person>();

            try
            {
                var                       client     = new MongoClient(_connectionString);
                IMongoDatabase            database   = client.GetDatabase("cit255");
                IMongoCollection <Person> personList = database.GetCollection <Person>("person");

                persons = personList.Find(Builders <Person> .Filter.Empty).ToList();

                statusCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                statusCode = DalErrorCode.ERROR;
            }

            return(persons);
        }
        /// <summary>
        /// write the current list of characters to the mongoDb collection
        /// </summary>
        /// <param name="characters">list of characters</param>
        public void WriteAll(IEnumerable <Character> characters, out DalErrorCode statusCode)
        {
            try
            {
                var            client   = new MongoClient(_connectionString);
                IMongoDatabase database = client.GetDatabase("cit255");
                IMongoCollection <Character> characterList = database.GetCollection <Character>("flintstone_characters");

                //
                // delete all documents in the collection to reset the collection
                //
                characterList.DeleteMany(Builders <Character> .Filter.Empty);

                characterList.InsertMany(characters);

                statusCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                statusCode = DalErrorCode.ERROR;
            }
        }
        /// <summary>
        /// update a character in the data file
        /// </summary>
        /// <param name="character">character</param>
        /// <param name="dalErrorCode">status code</param>
        /// <param name="message">message</param>
        public void UpdateCharacter(Character character, out DalErrorCode dalErrorCode, out string message)
        {
            message = "";

            using (_characterRepository)
            {
                if (CharacterDocumentExists(character.Id, out dalErrorCode))
                {
                    _characterRepository.Update(character, out dalErrorCode);

                    if (dalErrorCode == DalErrorCode.ERROR)
                    {
                        message = "There was an error connecting to the data file.";
                    }
                }
                else
                {
                    message      = $"Character with id {character.Id} does not exist.";
                    dalErrorCode = DalErrorCode.ERROR;
                }
            }
        }
        /// <summary>
        /// update a character in the data file
        /// </summary>
        /// <param name="character">character</param>
        /// <param name="dalErrorCode">status code</param>
        /// <param name="message">message</param>
        public void UpdatePerson(Person person, out DalErrorCode dalErrorCode, out string message)
        {
            message = "";

            using (_personRepository)
            {
                if (PersonDocumentExists(person.Id, out dalErrorCode))
                {
                    _personRepository.Update(person, out dalErrorCode);

                    if (dalErrorCode == DalErrorCode.ERROR)
                    {
                        message = "There was an error connecting to the data file.";
                    }
                }
                else
                {
                    message      = $"Person with id {person.Id} does not exist.";
                    dalErrorCode = DalErrorCode.ERROR;
                }
            }
        }
        /// <summary>
        /// write the current list of characters to the mongoDb collection
        /// </summary>
        /// <param name="characters">list of characters</param>
        public void WriteAll(IEnumerable <Person> persons, out DalErrorCode statusCode)
        {
            try
            {
                var                       client     = new MongoClient(_connectionString);
                IMongoDatabase            database   = client.GetDatabase("cit255");
                IMongoCollection <Person> personList = database.GetCollection <Person>("person");

                //
                // delete all documents in the collection to reset the collection
                //
                personList.DeleteMany(Builders <Person> .Filter.Empty);

                personList.InsertMany(persons);

                statusCode = DalErrorCode.GOOD;
            }
            catch (Exception)
            {
                statusCode = DalErrorCode.ERROR;
            }
        }
Example #23
0
 public DalResult(DalErrorCode errorCode = DalErrorCode.NoError, Exception?exception = null)
 {
     ErrorCode = errorCode;
     Exception = exception;
 }