Пример #1
0
        static AppSettings()
        {
            _solutionSettingsFile = Path.Combine(Path.GetTempPath(), "SolutionTemplate.json");

            if (_solutionSettingsFile.FileExists())
            {
                try
                {
                    SolutionTemplateSettings = JsonFileHelper.ReadJsonFile <ProjectTemplate>(_solutionSettingsFile);
                }
                catch (Exception e)
                {
                    Trace.WriteLine(e);
                }
            }

            if (SolutionTemplateSettings == null)
            {
                SolutionTemplateSettings = new ProjectTemplate
                {
                    TemplateName    = "My Multi-Template Solution",
                    LanguageTag     = "C#",
                    PlatformTags    = "Windows",
                    ProjectTypeTags = "Web"
                };
            }
        }
Пример #2
0
        /// <summary>
        /// Insert a CustomerEntity to Json File
        /// </summary>
        /// <param name="customerEntity"></param>
        /// <returns>True, if the entity is successfully inserted;
        ///         False, if the entity has the identical user name with other customer
        /// </returns>
        public bool Insert(CustomerEntity customerEntity)
        {
            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("[CustomersRepository::Insert] Starting insert costomerEntity. CustomerEntity: {}", customerEntity);
            }

            try
            {
                if (customerEntity == null)
                {
                    throw new ArgumentNullException(
                              nameof(customerEntity), "[CustomersRepository::Insert] CustomerEntity can not be null.");
                }

                var jObjectFromFile = JsonFileHelper.ReadJsonFile(_customersFilePath, _customersFileName);

                var customerEntityJObject = jObjectFromFile["Customers"]
                                            .FirstOrDefault(c => customerEntity.UserName.Equals((string)c["UserName"]));

                // Can't insert when there is a same userName
                if (customerEntityJObject != null)
                {
                    _logger.Warn(
                        "[CustomersRepository::Insert] Can't insert when there is a same userName. CustomerEntity: {}",
                        customerEntity);
                    return(false);
                }

                var jObjectFromEntity = CustomerEntityToJToken(customerEntity);
                jObjectFromFile.Merge(jObjectFromEntity,
                                      new JsonMergeSettings {
                    MergeArrayHandling = MergeArrayHandling.Union
                });
                JsonFileHelper.WriteJsonFile(_customersFilePath, _customersFileName, jObjectFromFile);

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug(
                        "[CustomersRepository::Insert] Insert customerEntity Successfully. InsertedEntity: {}",
                        customerEntity);
                }

                return(true);
            }
            catch (FileNotFoundException e)
            {
                CreateCustomersFile();
                _logger.Error(e, "[CustomersRepository::Insert] Insert customerEntity failed. Can note find file. Entity: {}",
                              customerEntity);
                throw new DataAccessException("[CustomersRepository::Insert] Insert customerEntity failed.", e);
            }
            catch (Exception e)
            {
                _logger.Error(e, "[CustomersRepository::Insert] Insert customerEntity failed. Can note find file. Entity: {}",
                              customerEntity);
                throw new DataAccessException("[CustomersRepository::Insert] Insert customerEntity failed.", e);
            }
        }
Пример #3
0
        /// <summary>
        /// Update a CustomerEntity to Json File
        /// </summary>
        /// <param name="customerEntity"></param>
        /// <returns>True, if the entity is successfully updated;
        ///         False, if there is no customer has the identical user name
        /// </returns>
        public bool Update(CustomerEntity customerEntity)
        {
            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("[CustomersRepository::Update] Starting update customerEntity. CustomerEntity: {}", customerEntity);
            }

            try
            {
                if (customerEntity == null)
                {
                    throw new ArgumentNullException(
                              nameof(customerEntity), "[CustomersRepository::Update] CustomerEntity can not be null.");
                }

                var jObjectFromFile = JsonFileHelper.ReadJsonFile(_customersFilePath, _customersFileName);

                var customerEntityJObject = jObjectFromFile["Customers"].FirstOrDefault(
                    c => string.Equals(customerEntity.UserName, (string)c["UserName"]) &&
                    customerEntity.Id == (int)c["Id"]);

                // Can't update if id and userName are not matched
                if (customerEntityJObject == null)
                {
                    _logger.Warn(
                        "[CustomersRepository::Update] Can't update if id and userName are not matched. CustomerEntity: {}",
                        customerEntity);
                    return(false);
                }

                UpdateJTokenByEntity(customerEntityJObject, customerEntity);
                JsonFileHelper.WriteJsonFile(_customersFilePath, _customersFileName, jObjectFromFile);

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug(
                        "[CustomersRepository::Update] Update customerEntity Successfully. UpdatedEntity: {0}",
                        customerEntity);
                }

                return(true);
            }
            catch (FileNotFoundException e)
            {
                CreateCustomersFile();
                _logger.Error(e, "[CustomersRepository::Update] Update customerEntity failed. Can note find file. Entity: {0}",
                              customerEntity);
                throw new DataAccessException("[CustomersRepository::Update] Update customerEntity failed. Can note find file.", e);
            }
            catch (Exception e)
            {
                _logger.Error(e, "[CustomersRepository::Update] Update customerEntity failed. Entity: {0}",
                              customerEntity);
                throw new DataAccessException("[CustomersRepository::Update] Update customerEntity failed. ", e);
            }
        }
Пример #4
0
        public IList <CustomerEntity> SelectByFirstOrLastName(string name)
        {
            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("[CustomersRepository::SelectByFirstOrLastName] Starting select customerEntity by Name. Name : {0}", name);
            }

            try
            {
                if (name == null)
                {
                    throw new ArgumentNullException(
                              nameof(name), "[CustomersRepository::SelectByFirstOrLastName] name can not be null.");
                }

                var jObject = JsonFileHelper.ReadJsonFile(_customersFilePath, _customersFileName);

                var customerEntities = jObject["Customers"]
                                       .Where(c => ((string)c["FirstName"]).Contains(name) || ((string)c["LastName"]).Contains(name))
                                       .Select(JTokenToCustomerEntity).ToList();

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug(
                        "[CustomersRepository::SelectByFirstOrLastName] Select customerEntity by Name Successfully. Entities: {0}",
                        customerEntities);
                }

                return(customerEntities);
            }
            catch (FileNotFoundException e)
            {
                CreateCustomersFile();
                _logger.Error(e,
                              "[CustomersRepository::SelectByFirstOrLastName] Select customerEntity by Name failed. Can note find file. Name : {0}",
                              name);
                throw new DataAccessException(
                          "[CustomersRepository::SelectByFirstOrLastName] Select customerEntity by Name failed. Can note find file.", e);
            }
            catch (Exception e)
            {
                _logger.Error(e,
                              "[CustomersRepository::SelectByFirstOrLastName] Select customerEntity by Name failed. Name : {0}",
                              name);
                throw new DataAccessException(
                          "[CustomersRepository::SelectByFirstOrLastName] Select customerEntity by Name failed.", e);
            }
        }
Пример #5
0
        public CustomerEntity SelectByUserName(string userName)
        {
            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("[CustomersRepository::SelectByUserName] Starting select customerEntity by userName. UserName : {0}", userName);
            }

            try
            {
                if (string.IsNullOrEmpty(userName))
                {
                    throw new ArgumentNullException(
                              nameof(userName), "[CustomersRepository::SelectByUserName] UserName can not be null or empty.");
                }

                var jObject = JsonFileHelper.ReadJsonFile(_customersFilePath, _customersFileName);

                var customerEntityJObject =
                    jObject["Customers"].FirstOrDefault(c => userName.Equals((string)c["UserName"]));
                var customerEntity = JTokenToCustomerEntity(customerEntityJObject);

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug(
                        "[CustomersRepository::SelectByUserName] Select customerEntity by userName Successfully. Entity: {0}",
                        customerEntity);
                }

                return(customerEntity);
            }
            catch (FileNotFoundException e)
            {
                CreateCustomersFile();
                _logger.Error(e,
                              "[CustomersRepository::SelectByUserName] Select customerEntity by userName failed. Can note find file. UserName : {0}",
                              userName);
                throw new DataAccessException(
                          "[CustomersRepository::SelectByUserName] Select customerEntity by userName failed. Can note find file.", e);
            }
            catch (Exception e)
            {
                _logger.Error(e,
                              "[CustomersRepository::SelectByUserName] Select customerEntity by userName failed. UserName : {0}",
                              userName);
                throw new DataAccessException(
                          "[CustomersRepository::SelectByUserName] Select customerEntity by userName failed.", e);
            }
        }
Пример #6
0
        public void ReadJsonFile_GivenExistPathAndName_ReadCorrectly()
        {
            // Write to the json file before read
            using (StreamWriter file = File.CreateText(Path.Combine(_filePath, _fileName)))
            {
                using (JsonTextWriter writer = new JsonTextWriter(file))
                {
                    writer.Formatting = Formatting.Indented;
                    _jObject.WriteTo(writer);
                }
            }

            var jObjectRead = JsonFileHelper.ReadJsonFile(_filePath, _fileName);

            Assert.IsTrue(JToken.DeepEquals(_jObject, jObjectRead));
        }
Пример #7
0
        public bool DeleteById(int id)
        {
            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("[CustomersRepository::DeleteById] Starting delete customerEntity. Id: {}", id);
            }

            try
            {
                var jObject = JsonFileHelper.ReadJsonFile(_customersFilePath, _customersFileName);
                var jArray  = (JArray)jObject["Customers"];
                var customerEntityJObject = jArray.FirstOrDefault(c => (int)c["Id"] == id);

                // Can't delete when there is no such id
                if (customerEntityJObject == null)
                {
                    _logger.Debug("[CustomersRepository::DeleteById] Can't delete when there is no such id. Id: {}",
                                  id);
                    return(false);
                }

                jArray.Remove(customerEntityJObject);
                JsonFileHelper.WriteJsonFile(_customersFilePath, _customersFileName, jObject);

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug(
                        "[CustomersRepository::DeleteById] Delete customerEntity Successfully. DeletedEntity: {}",
                        JTokenToCustomerEntity(customerEntityJObject));
                }

                return(true);
            }
            catch (FileNotFoundException e)
            {
                CreateCustomersFile();
                _logger.Error(e, "[CustomersRepository::DeleteById] Delete customerEntity failed. Can note find file. Id: {0}", id);
                throw new DataAccessException("[CustomersRepository::DeleteById] Update customerEntity failed. Can note find file.", e);
            }
            catch (Exception e)
            {
                _logger.Error(e, "[CustomersRepository::DeleteById] Delete customerEntity failed. Id: {0}", id);
                throw new DataAccessException("[CustomersRepository::DeleteById] Update customerEntity failed.", e);
            }
        }
Пример #8
0
        public IList <CustomerEntity> SelectAll()
        {
            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("[CustomersRepository::SelectAll] Starting select all customerEntity.");
            }

            try
            {
                if (!File.Exists(Path.Combine(_customersFilePath, _customersFileName)))
                {
                    CreateCustomersFile();
                }

                var jObject = JsonFileHelper.ReadJsonFile(_customersFilePath, _customersFileName);

                var customerEntities = jObject["Customers"].Select(JTokenToCustomerEntity).ToList();

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug(
                        "[CustomersRepository::SelectAll] Select all customerEntity Successfully. Entities: {0}",
                        customerEntities);
                }

                return(customerEntities);
            }
            catch (FileNotFoundException e)
            {
                CreateCustomersFile();
                _logger.Error(e, "[CustomersRepository::SelectAll] Select all customerEntity from file failed. Can note find file.");
                throw new DataAccessException(
                          "[CustomersRepository::SelectAll] Select all customerEntity from file failed. Can note find file.", e);
            }
            catch (Exception e)
            {
                _logger.Error(e, "[CustomersRepository::SelectAll] Select all customerEntity from file failed.");
                throw new DataAccessException(
                          "[CustomersRepository::SelectAll] Select all customerEntity from file failed.", e);
            }
        }
Пример #9
0
        public CustomerEntity SelectById(int id)
        {
            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("[CustomersRepository::SelectById] Starting select customerEntity by Id. Id : {0}", id);
            }

            try
            {
                var jObject = JsonFileHelper.ReadJsonFile(_customersFilePath, _customersFileName);

                var customerEntityJObject = jObject["Customers"].FirstOrDefault(c => (int)c["Id"] == id);
                var customerEntity        = JTokenToCustomerEntity(customerEntityJObject);

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug(
                        "[CustomersRepository::SelectById] Select customerEntity by Id Successfully. Entity: {0}",
                        customerEntity);
                }

                return(customerEntity);
            }
            catch (FileNotFoundException e)
            {
                CreateCustomersFile();
                _logger.Error(e, "[CustomersRepository::SelectById] Select customerEntity by Id failed. Can note find file. Id : {0}", id);
                throw new DataAccessException("[CustomersRepository::SelectById] Select customerEntity by Id failed. Can note find file.",
                                              e);
            }
            catch (Exception e)
            {
                _logger.Error(e, "[CustomersRepository::SelectById] Select customerEntity by Id failed. Id : {0}", id);
                throw new DataAccessException("[CustomersRepository::SelectById] Select customerEntity by Id failed.",
                                              e);
            }
        }
Пример #10
0
        private static void BuildSiteMap()
        {
            var sitemap        = new Sitemap();
            var JsonFileHelper = new JsonFileHelper();
            var allMovies      = JsonFileHelper.ReadJsonFile("Data.json");
            var movieList      = JsonConvert.DeserializeObject <List <AinunuMovieDTO> >(allMovies);
            var totalpages     = movieList.Count / 48;

            if (movieList.Count % 48 != 0)
            {
                totalpages++;
            }

            for (int i = 1; i <= totalpages; i++)
            {
                sitemap.Add(new Url
                {
                    Location        = baseUrl + i.ToString(),
                    ChangeFrequency = ChangeFrequency.Daily,
                    Priority        = 0.5,
                    TimeStamp       = DateTime.Now
                });
            }

            foreach (var movie in movieList)
            {
                sitemap.Add(new Url
                {
                    Location        = baseUrl + "content/" + movie.id,
                    ChangeFrequency = ChangeFrequency.Daily,
                    Priority        = 0.3,
                    TimeStamp       = DateTime.Now
                });
            }
            JsonFileHelper.WriteJsonFile("sitemap.xml", sitemap.ToXml());
        }
Пример #11
0
 public void ReadJsonFile_NullPathAndName_ThrowDataAccessException()
 {
     JsonFileHelper.ReadJsonFile(null, null);
 }
Пример #12
0
 public void ReadJsonFile_NullName_ThrowDataAccessException()
 {
     JsonFileHelper.ReadJsonFile(_filePath, null);
 }
Пример #13
0
 public void ReadJsonFile_NullPath_ThrowDataAccessException()
 {
     JsonFileHelper.ReadJsonFile(null, _fileName);
 }