示例#1
0
        public ActionResult AddBook(HttpPostedFileBase file)
        {
            Book           b    = new Book();
            List <TOCLine> tocs = new List <TOCLine>();
            string         css;
            string         name;
            string         shortCode;
            string         isbn;
            string         author;
            string         publisher;
            string         description;
            DateTime       published;

            using (StreamReader reader = new StreamReader(file.InputStream))
            {
                shortCode   = reader.ReadLine().Trim().Split('/')[5];
                name        = reader.ReadLine().Trim();
                published   = DateTime.ParseExact(reader.ReadLine(), "MMMM yyyy", CultureInfo.InvariantCulture);
                isbn        = reader.ReadLine().Trim();
                author      = reader.ReadLine().Trim();
                publisher   = reader.ReadLine().Trim();
                description = reader.ReadLine().Trim();
                css         = reader.ReadLine().Trim();
                if (reader.ReadLine().Trim() == "")
                {
                    reader.ReadLine();
                }
                int section      = 0;
                int contentOrder = 1;
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine().Trim();
                    if (line == "-----")
                    {
                        section     += 1;
                        contentOrder = 1;
                        continue;
                    }
                    if (section == 0)
                    {
                        var    parts = line.Split(new string[] { "<KHOA>" }, StringSplitOptions.None);
                        string code  = parts[0].Split('.')[0];
                        if (code.Contains("ch02"))
                        {
                            code = code;
                        }
                        string html     = parts.Last().Replace("<PHONGDAOTAO>", Environment.NewLine);
                        var    document = new HtmlDocument();
                        document.LoadHtml(html);
                        var ps = document.DocumentNode.Descendants("p");
                        int id = 1;
                        foreach (var p in ps)
                        {
                            p.Attributes.Add("id", code + "|" + id);
                            id++;
                        }
                        string test = document.DocumentNode.OuterHtml;
                        b.Contents.Add(new Content
                        {
                            Code  = code,
                            HTML  = CompressHelper.CompressString(document.DocumentNode.OuterHtml),
                            Order = contentOrder
                        });
                    }
                    else
                    {
                        var    parts  = line.Split('|');
                        string title  = parts[0];
                        string code   = null;
                        string anchor = null;
                        int    level  = int.Parse(parts[2]);

                        if (parts[1].Contains("#"))
                        {
                            parts  = parts[1].Split('#');
                            code   = parts[0].Replace(".html", "").Replace(".htm", "");
                            anchor = parts[1];
                        }
                        else
                        {
                            code = parts[1].Replace(".html", "").Replace(".htm", "");
                        }

                        b.TOCLines.Add(new TOCLine
                        {
                            Anchor  = anchor,
                            Level   = level,
                            Title   = title,
                            Order   = contentOrder,
                            Content = b.Contents.FirstOrDefault(x => x.Code == code)
                        });
                    }
                    contentOrder += 1;
                }
            }
            b.ShortCode   = shortCode;
            b.Name        = name;
            b.Description = description;
            b.ISBN        = isbn;
            b.PublishDate = published;
            b.CustomCSS   = css;
            Publisher pub = DBHelper.GetPublisher(publisher);

            if (pub == null)
            {
                pub = new Publisher
                {
                    Name = publisher
                };
                // b.PublisherId = DBHelper.AddPublisher(pub);
            }

            var        authors = author.Split(',');
            List <int> autIds  = new List <int>();

            foreach (string au in authors)
            {
                Author aut = DBHelper.GetAuthor(au);
                if (aut == null)
                {
                    aut = new Author
                    {
                        Name = au
                    };
                    autIds.Add(DBHelper.AddAuthor(aut));
                }
                else
                {
                    autIds.Add(aut.Id);
                }
            }
            // b.Authors = autIds.Select(x => new Author { Id = x }).ToList();
            TempData["addingBook"] = b;
            return(RedirectToAction("AddBookDetail"));
        }