Esempio n. 1
0
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(Entrying).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EntryingExists(Entrying.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 2
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Entrying = await _context.Entrying.FindAsync(id);

            if (Entrying != null)
            {
                _context.Entrying.Remove(Entrying);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 3
0
        // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToPage("./Create"));
            }
            if (!_context.Book.Any(info => info.Name == Entrying.ProjectName))
            {
                return(RedirectToPage("./Create", new { msg = "error" }));
            }
            if (Entrying.ProjectName.Contains('.'))
            {
                Entrying.ProjectName = Entrying.ProjectName.Split('.')[0];
            }
            _context.Entrying.Add(Entrying);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
Esempio n. 4
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            // get entry by using id sent from request index
            Entrying = await _context.Entrying.FirstOrDefaultAsync(m => m.ID == id);

            // book need to be found by name in entrying
            Book = await _context.Book.FirstOrDefaultAsync(m => m.Name.Contains(Entrying.ProjectName));

            if (Book != null)
            {
                byte[] contents = Book.DataFiles;

                string username = User.Identity.Name;

                var Stat = await _context.Stat.FirstOrDefaultAsync(stat => stat.userName == username);

                if (Stat == null)
                {
                    var stat = new Stat();
                    stat.userName        = User.Identity.Name;
                    stat.DownloadedBooks = Book.Name;
                    await _context.Stat.AddAsync(stat);
                }
                else
                {
                    if (!Stat.DownloadedBooks.Contains(Book.Name))
                    {
                        Stat.DownloadedBooks += "," + Book.Name;
                    }
                }
                Book.downloadCount += 1;
                await _context.SaveChangesAsync();

                return(File(contents, "application/pdf"));
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 5
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Book = await _context.Book.FindAsync(id);

            if (Book != null)
            {
                byte[] contents = Book.DataFiles;

                string username = User.Identity.Name;

                var Stat = await _context.Stat.FirstOrDefaultAsync(stat => stat.userName == username);

                if (Stat == null)
                {
                    var stat = new Stat();
                    stat.userName        = User.Identity.Name;
                    stat.DownloadedBooks = Book.Name;
                    await _context.Stat.AddAsync(stat);
                }
                else
                {
                    if (!Stat.DownloadedBooks.Contains(Book.Name))
                    {
                        Stat.DownloadedBooks += "," + Book.Name;
                    }
                }
                Book.downloadCount += 1;
                await _context.SaveChangesAsync();

                return(File(contents, "application/pdf"));
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 6
0
        public static async Task AddBookAsync(SeniorLibraryContext context)
        {
            System.Diagnostics.Debug.WriteLine("Adding Books");
            string currentDir = Directory.GetCurrentDirectory();
            string bookRoot   = currentDir + @"/book";
            string addedRoot  = bookRoot + @"/Added";

            string[] entries = Directory.GetFiles(bookRoot);
            Console.WriteLine(currentDir);
            foreach (string filePath in entries)
            {
                string fileName = Path.GetFileName(filePath);
                //Prepare tools needed for filereading
                //Console.WriteLine(Path.GetFileName(filePath));
                byte[]       fileContent  = null;
                FileStream   fs           = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader binaryReader = new System.IO.BinaryReader(fs);
                long         byteLength   = new FileInfo(filePath).Length;
                fileContent = binaryReader.ReadBytes((Int32)byteLength);
                fs.Close();
                fs.Dispose();
                binaryReader.Close();
                //Create a book object
                var book = new Book();
                book.Name      = fileName;
                book.DataFiles = fileContent;
                book.CreatedOn = DateTime.Now;
                //Insert to database
                File.Move(filePath, addedRoot + @"/" + fileName);
                await context.Book.AddAsync(book);

                await context.SaveChangesAsync();

                System.Diagnostics.Debug.WriteLine("Successfully Added" + fileName + "!! to the database");
            }
            System.Diagnostics.Debug.WriteLine("DONE !!!!!!!!!!!!!!");
        }