public async Task DeleteAsync(Material entity)
        {
            if (_context.Entry(entity).State == EntityState.Detached)
            {
                _context.Materials.Attach(entity);
            }

            await Task.FromResult(_context.Materials.Remove(entity));
        }
Beispiel #2
0
        private static void UpdateNewsItems()
        {
            Console.WriteLine("Start UpdateNewsItems");
            using (FileStream fs = new FileStream(Path + "news.txt", FileMode.Open))
            {
                byte[] data = new byte[fs.Length];
                fs.Read(data, 0, Convert.ToInt32(fs.Length));

                char[] chars = Encoding.UTF8.GetString(data).ToCharArray();
                var limit = chars.Length;
                if (UseLimit && MaxChars < chars.Length)
                {
                    limit = MaxChars * 10;
                }

                var categories = MaterialCategoryRepository.GetAsync().Result.Where(x => x.MaterialType == MaterialType.News).ToList();

                for (int i = 0; i < limit; i++)
                {
                    Material newsItem = new Material()
                    {
                        Type = MaterialType.News
                    };
                    // id
                    string id = null;
                    while (chars[i] != '|')
                    {
                        id += chars[i];
                        i++;
                    }
                    newsItem.OldId = int.Parse(id);
                    i++;
                    // Category id
                    string categoryId = "";
                    while (chars[i] != '|')
                    {
                        categoryId += chars[i];
                        i++;
                    }
                    //  newsItem.CategoryId = int.Parse(categoryId);


                    i++;
                    // year
                    string year = null;
                    while (chars[i] != '|')
                    {
                        year += chars[i];
                        i++;
                    }
                    // newsItem.Year = int.Parse(year);
                    i++;
                    // month
                    string month = null;
                    while (chars[i] != '|')
                    {
                        month += chars[i];
                        i++;
                    }
                    // newsItem.Month = int.Parse(month);
                    i++;
                    // day
                    string day = null;
                    while (chars[i] != '|')
                    {
                        day += chars[i];
                        i++;
                    }
                    //newsItem.Day = int.Parse(day);
                    i++;
                    // pending
                    while (chars[i] != '|')
                    {
                        if (chars[i] == '1')
                            newsItem.Pending = true;
                        i++;
                    }
                    i++;
                    // onTop
                    while (chars[i] != '|')
                    {
                        if (chars[i] == '1')
                            newsItem.OnTop = true;
                        i++;
                    }
                    i++;
                    //commentary_may
                    while (chars[i] != '|')
                    {
                        if (chars[i] == '1')
                            newsItem.CanCommentary = true;
                        i++;
                    }
                    i++;
                    //add time
                    string addTime = null;
                    while (chars[i] != '|')
                    {
                        addTime += chars[i];
                        i++;
                    }
                    i++;
                    newsItem.AdditionTime = DateTimeHelpers.ConvertUtcToLocalTime(long.Parse(addTime));
                    // commentary Number
                    string numberCommentary = null;
                    while (chars[i] != '|')
                    {
                        numberCommentary += chars[i];
                        i++;
                    }
                    i++;
                    //      newsItem.NumberCommentaries = int.Parse(numberCommentary);
                    //author
                    string userName = null;
                    while (chars[i] != '|')
                    {
                        userName += chars[i];
                        i++;
                    }
                    i++;

                    newsItem.Author = UserRepository.FindByNameAsync(userName).Result;
                    if (newsItem.Author == null)
                    {
                        newsItem.AuthorId = _deleted.Id;
                    }
                    //title
                    string title = null;
                    while (chars[i] != '|')
                    {
                        title += chars[i];
                        i++;
                    }
                    i++;

                    newsItem.Title = title;
                    //brief
                    string brief = null;
                    while (chars[i] != '|')
                    {
                        brief += chars[i];
                        i++;
                    }
                    i++;
                    if (brief != null)
                    {
                        brief = Regex.Replace(brief, "<!--IMG1-->", "");
                        var matches = Regex.Matches(brief, "src\\s*=\\s*['\"](?<src>[^'\"]*)['\"]", RegexOptions.IgnoreCase);
                        if (matches.Count > 0)
                        {
                            var path1 = Regex.Replace(matches[0].Value, "src=", "");
                            var path2 = Regex.Replace(path1, "\"", "");
                            newsItem.PhotoPath = path2;
                        }
                        brief = Regex.Replace(brief, "<img([\\w\\W]+?)/>", "");
                    }
                    newsItem.Brief = brief;
                    // message
                    string message = null;
                    while (chars[i] != '|')
                    {
                        message += chars[i];
                        i++;
                    }
                    i++;

                    newsItem.Message = message;

                    //while (true)
                    //{
                    //    if ( chars[i] == '|' && char.IsDigit(chars[i + 1]))
                    //    {
                    //        break;
                    //    }
                    //    i++;
                    //}
                    //i += 3;
                    // attach
                    while (chars[i] != '|')
                    {
                        //id += chars[i];
                        i++;
                    }
                    i++;

                    // files
                    while (chars[i] != '|')
                    {
                        i++;
                    }
                    i += 1;
                    i++;
                    if (chars[i] == '|')
                        i++;

                    // reads
                    string reads = null;


                    while (chars[i] != '|')
                    {
                        reads += chars[i];
                        i++;
                    }
                    i++;

                    newsItem.Reads = int.Parse(reads);
                    // rating
                    string rating = null;
                    while (chars[i] != '|')
                    {
                        rating += chars[i];
                        i++;
                    }
                    i++;

                    newsItem.Rating = float.Parse(rating);
                    // rate_num
                    string ratingNumbers = null;
                    while (chars[i] != '|')
                    {
                        ratingNumbers += chars[i];
                        i++;
                    }
                    i++;

                    newsItem.RatingNumbers = int.Parse(ratingNumbers);
                    // rate sum
                    string ratingSumm = null;
                    while (chars[i] != '|')
                    {
                        ratingSumm += chars[i];
                        i++;
                    }
                    i++;

                    newsItem.RatingSumm = int.Parse(ratingSumm);
                    //rate ip
                    while (chars[i] != '|')
                    {
                        reads += chars[i];
                        i++;
                    }
                    i++;

                    // other 1
                    while (chars[i] != '|')
                    {
                        reads += chars[i];
                        i++;
                    }
                    i++;

                    // other 2 SOURCE
                    string source = null;
                    while (chars[i] != '|')
                    {
                        source += chars[i];
                        i++;
                    }
                    newsItem.Source = source;
                    i++;

                    // other 3
                    while (chars[i] != '|')
                    {
                        reads += chars[i];
                        i++;
                    }
                    i++;

                    // other 4
                    while (chars[i] != '|')
                    {
                        reads += chars[i];
                        i++;
                    }
                    i++;

                    // other 5
                    while (chars[i] != '|')
                    {
                        reads += chars[i];
                        i++;
                    }
                    i++;
                    // other6
                    while (chars[i] != '|')
                    {
                        // reads += chars[i];
                        i++;
                    }
                    i++;

                    // uid
                    while (chars[i] != '|')
                    {
                        // reads += chars[i];
                        i++;
                    }
                    i++;

                    // subscr
                    while (chars[i] != '|')
                    {
                        // reads += chars[i];
                        i++;
                    }
                    i++;

                    // last modified
                    string lastDate = null;
                    while (char.IsDigit(chars[i]))// != '|' || chars[i] != '\\'))
                    {
                        lastDate += chars[i];
                        i++;
                    }
                    //i++;

                    newsItem.LastModified = DateTimeHelpers.ConvertUtcToLocalTime(long.Parse(lastDate));

                    //if (useLimit && newsItem.Author == null)
                    //{
                    //}
                    //else
                    //{
                    var category = categories.FirstOrDefault(x => x.OldId == int.Parse(categoryId)) ??
                                   categories.First(x => x.OldId == 5);
                    newsItem.CategoryId = category.Id;

                    //  category.Materials.Add(newsItem);
                    MaterialRepository.AddAsync(newsItem);
                    // }
                    // while (chars[i] != 10)
                    //  {
                    //       i++;
                    //  }
                }
            }
        }
Beispiel #3
0
        private static void UpdateBlogItems()
        {
            Console.WriteLine("Start UpdateBlogItems");
            using (FileStream fs = new FileStream(Path + "blog.txt", FileMode.Open))
            {
                byte[] data = new byte[fs.Length];
                fs.Read(data, 0, Convert.ToInt32(fs.Length));

                char[] chars = Encoding.UTF8.GetString(data).ToCharArray();
                var limit = chars.Length;
                if (UseLimit && MaxChars < chars.Length)
                {
                    limit = MaxChars;
                }

                var categories = MaterialCategoryRepository.GetAsync().Result.Where(x => x.MaterialType == MaterialType.Blog).ToList();

                for (int i = 0; i < limit; i++)
                {
                    Material blogItem = new Material()
                    {
                        Type = MaterialType.Blog
                    };
                    // id
                    string id = null;
                    while (chars[i] != '|')
                    {
                        id += chars[i];
                        i++;
                    }
                    blogItem.OldId = int.Parse(id);
                    i++;
                    // Category id
                    string categoryId = null;
                    while (chars[i] != '|')
                    {
                        categoryId += chars[i];
                        i++;
                    }
                    // blogItem.CategoryId = int.Parse(categoryId);

                    i++;
                    // year
                    string year = null;
                    while (chars[i] != '|')
                    {
                        year += chars[i];
                        i++;
                    }
                    //blogItem.Year = int.Parse(year);
                    i++;
                    // month
                    string month = null;
                    while (chars[i] != '|')
                    {
                        month += chars[i];
                        i++;
                    }
                    // blogItem.Month = int.Parse(month);
                    i++;
                    // day
                    string day = null;
                    while (chars[i] != '|')
                    {
                        day += chars[i];
                        i++;
                    }
                    // blogItem.Day = int.Parse(day);
                    i++;
                    // pending
                    while (chars[i] != '|')
                    {
                        if (chars[i] == '1')
                            blogItem.Pending = true;
                        i++;
                    }
                    i++;
                    // onTop
                    while (chars[i] != '|')
                    {
                        if (chars[i] == '1')
                            blogItem.OnTop = true;
                        i++;
                    }
                    i++;
                    //commentary_may
                    while (chars[i] != '|')
                    {
                        if (chars[i] == '1')
                            blogItem.CanCommentary = true;
                        i++;
                    }
                    i++;
                    //add time
                    string addTime = null;
                    while (chars[i] != '|')
                    {
                        addTime += chars[i];
                        i++;
                    }
                    i++;
                    blogItem.AdditionTime = DateTimeHelpers.ConvertUtcToLocalTime(long.Parse(addTime));
                    // commentary Number
                    string numberCommentary = null;
                    while (chars[i] != '|')
                    {
                        numberCommentary += chars[i];
                        i++;
                    }
                    i++;
                    //   blogItem.NumberCommentaries = int.Parse(numberCommentary);

                    //author
                    string userName = null;
                    while (chars[i] != '|')
                    {
                        userName += chars[i];
                        i++;
                    }
                    i++;

                    blogItem.Author = UserRepository.FindByNameAsync(userName).Result;
                    if (blogItem.Author == null)
                    {
                        blogItem.AuthorId = _deleted.Id;
                    }
                    //title
                    string title = null;
                    while (chars[i] != '|')
                    {
                        title += chars[i];
                        i++;
                    }
                    i++;

                    blogItem.Title = title;
                    //brief
                    string brief = null;
                    while (chars[i] != '|')
                    {
                        brief += chars[i];
                        i++;
                    }
                    i++;

                    blogItem.Brief = brief;
                    // message
                    string message = null;
                    while (chars[i] != '|')
                    {
                        message += chars[i];
                        i++;
                    }
                    i++;

                    blogItem.Message = message;
                    // attach
                    while (chars[i] != '|')
                    {
                        id += chars[i];
                        i++;
                    }
                    i++;

                    // files
                    while (true)
                    {
                        if (chars[i] == '\\' && chars[i + 1] == '|' && chars[i + 2] == '|')
                        {
                            break;
                        }
                        i++;
                    }
                    i += 3;
                    //i++;

                    // reads
                    string reads = null;


                    while (chars[i] != '|')
                    {
                        reads += chars[i];
                        i++;
                    }
                    i++;

                    blogItem.Reads = int.Parse(reads);
                    // rating
                    string rating = null;
                    while (chars[i] != '|')
                    {
                        rating += chars[i];
                        i++;
                    }
                    i++;

                    blogItem.Rating = float.Parse(rating);
                    // rate_num
                    string ratingNumbers = null;
                    while (chars[i] != '|')
                    {
                        ratingNumbers += chars[i];
                        i++;
                    }
                    i++;

                    blogItem.RatingNumbers = int.Parse(ratingNumbers);
                    // rate sum
                    string ratingSumm = null;
                    while (chars[i] != '|')
                    {
                        ratingSumm += chars[i];
                        i++;
                    }
                    i++;

                    blogItem.RatingSumm = int.Parse(ratingSumm);
                    //rate ip
                    while (chars[i] != '|')
                    {
                        reads += chars[i];
                        i++;
                    }
                    i++;

                    // other 1
                    while (chars[i] != '|')
                    {
                        reads += chars[i];
                        i++;
                    }
                    i++;

                    // other 2 SOURCE
                    string source = null;
                    while (chars[i] != '|')
                    {
                        source += chars[i];
                        i++;
                    }
                    blogItem.Source = source;
                    i++;

                    // other 3
                    while (chars[i] != '|')
                    {
                        reads += chars[i];
                        i++;
                    }
                    i++;

                    // other 4
                    while (chars[i] != '|')
                    {
                        reads += chars[i];
                        i++;
                    }
                    i++;

                    // other 5
                    while (chars[i] != '|')
                    {
                        reads += chars[i];
                        i++;
                    }
                    i++;
                    // other6
                    while (chars[i] != '|')
                    {
                        // reads += chars[i];
                        i++;
                    }
                    i++;

                    // uid
                    while (chars[i] != '|')
                    {
                        // reads += chars[i];
                        i++;
                    }
                    i++;

                    // subscr
                    while (chars[i] != '|')
                    {
                        // reads += chars[i];
                        i++;
                    }
                    i++;

                    // last modified
                    string lastDate = null;
                    while (char.IsDigit(chars[i]))// != '|' || chars[i] != '\\'))
                    {
                        lastDate += chars[i];
                        i++;
                    }
                    //i++;

                    blogItem.LastModified = DateTimeHelpers.ConvertUtcToLocalTime(long.Parse(lastDate));

                    //if (useLimit && blogItem.Author == null)
                    //{
                    //}
                    //else
                    //{
                    var category = categories.First(x => x.Id == int.Parse(categoryId));
                    blogItem.CategoryId = category.Id;
                    //   category.BlogItems.Add(blogItem);
                    MaterialRepository.AddAsync(blogItem);
                    //  }
                    // while (chars[i] != 10)
                    //  {
                    //       i++;
                    //  }
                }

            }
        }
 public void Update(Material entity)
 {
     _context.Materials.Attach(entity);
     _context.Entry(entity).State = EntityState.Modified;
 }
 public async Task<Material> AddAsync(Material entity)
 {
     var addedEntity = await _context.Materials.AddAsync(entity);
     return addedEntity.Entity;
 }