Ejemplo n.º 1
0
 private static void Create_SettingsFile(ComicSettingsModel settingsModel)
 {
     File.WriteAllText(
         settingsPath,
         JsonConvert.SerializeObject(settingsModel)
         );
 }
Ejemplo n.º 2
0
        public static ResultModel Read_SettingsFile(out ComicSettingsModel settingsModel)
        {
            ResultModel modelToReturn = new ResultModel()
            {
                Result     = Enums.ResultTypes.Error,
                Data       = Constants.Error,
                Occurrence = DateTime.Now
            };

            var result = SettingsFile_Exists();

            settingsModel = null;

            try
            {
                if (result)
                {
                    using (StreamReader r = new StreamReader(settingsPath))
                    {
                        settingsModel = JsonConvert.DeserializeObject <ComicSettingsModel>(r.ReadToEnd()) ?? null;
                    }

                    modelToReturn.Result = Enums.ResultTypes.Success;
                }
            }
            catch (Exception ex)
            {
                modelToReturn.Data = $"{ex.Message}\n{ex.StackTrace}";
            }

            return(modelToReturn);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds a new Comic to the Dropdown list of available Comic sites to scrape from.
        /// </summary>
        /// <param name="model">The Comic to add.</param>
        /// <param name="updatedListOfComics">Updated list of comics.</param>
        /// <returns>Result Model.</returns>
        public static ResultModel Add_ComicSite(ComicModel model)
        {
            ResultModel modelToReturn = new ResultModel()
            {
                Result     = Enums.ResultTypes.Error,
                Data       = Constants.Error,
                Occurrence = DateTime.Now
            };

            ComicSettingsModel settingsModel = null;

            if (SettingsFile_Exists() && Read_SettingsFile(out settingsModel).Result == Enums.ResultTypes.Success)
            {
                int highest = 0;
                foreach (var comic in settingsModel.Comics)
                {
                    if (comic.Id > highest)
                    {
                        highest = comic.Id;
                    }
                }

                model.Id = highest + 1;
                settingsModel.Comics.Add(model);

                modelToReturn.Result = Enums.ResultTypes.Success;
                modelToReturn.Data   = Constants.AddedComicSite;
            }
            else
            {
                model.Id = 1;

                //create a new settings model and add the new site
                settingsModel = new ComicSettingsModel()
                {
                    LastUpdated = DateTime.Now,
                    Comics      = new List <ComicModel>()
                    {
                        model
                    }
                };

                modelToReturn.Result = Enums.ResultTypes.SettingsNotFound;
                modelToReturn.Data   = Constants.NewSettingsFile;
            }

            //this will create or overwrite the settings file
            Create_SettingsFile(settingsModel);

            return(modelToReturn);
        }