Esempio n. 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!(_context.Books.Any(a => a.Title == NewBook.Title)))
            {
                _context.Books.Add(NewBook);
                await _context.SaveChangesAsync();
            }

            string email = HttpContext.Session.GetString("email");
            var    usr   = _context.Users.Single(a => a.Email == email);
            var    booku = _context.Books.Single(a => a.Title == NewBook.Title);

            if (!_context.Connection.Any(a => (a.BookId == booku.Id) && (a.UserId == usr.Id)))
            {
                NewConnection.BookId = booku.Id;
                NewConnection.UserId = usr.Id;

                _context.Connection.Add(NewConnection);

                await _context.SaveChangesAsync();

                return(RedirectToPage("Add"));
            }
            else
            {
                Msg = "This book already exists in your library.";

                return(Page());
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            string email = HttpContext.Session.GetString("email");

            if (!SessionExists(email))
            {
                return(RedirectToPage("Index"));
            }

            if (id == null || !BookExists(id))
            {
                return(RedirectToPage("MainPage"));
            }

            var Book    = _context.Books.Single(m => m.Id == id);
            var usr     = _context.Users.Single(a => a.Email == email);
            var Connect = _context.Connection.Single(a => (a.UserId == usr.Id) && (a.BookId == Book.Id));

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

            return(RedirectToPage("MainPage"));
        }
Esempio n. 3
0
        public async Task <IActionResult> OnPostAsync()
        {
            string email = HttpContext.Session.GetString("email");

            var usr = _context.Users.Single(a => a.Email == email);

            usr.Fname    = Users.Fname;
            usr.Lname    = Users.Lname;
            usr.Bday     = Users.Bday;
            usr.Email    = Users.Email;
            usr.Password = Users.Password;
            usr.Photo    = Users.Photo;

            await _context.SaveChangesAsync();

            return(RedirectToPage("MainPage"));
        }
Esempio n. 4
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());
            }

            if (!_context.Users.Any(x => x.Email == this.User.Email))
            {
                _context.Users.Add(User);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            else
            {
                Msg = "This email already exists.";
                return(Page());
            }
        }
Esempio n. 5
0
        public async Task <IActionResult> OnPostAsync()
        {
            string email = HttpContext.Session.GetString("email");

            var usr   = _context.Users.Single(a => a.Email == email);
            var booku = _context.Books.Single(m => m.Id == Book.Id);

            booku.Title       = Book.Title;
            booku.Author      = Book.Author;
            booku.Categories  = Book.Categories;
            booku.Translator  = Book.Translator;
            booku.Publisher   = Book.Publisher;
            booku.Description = Book.Description;
            booku.Cover       = Book.Cover;

            var connectu = _context.Connection.Single(a => (a.UserId == usr.Id) && (a.BookId == Book.Id));

            connectu.Status = Connect.Status;
            connectu.Rating = Connect.Rating;

            await _context.SaveChangesAsync();

            return(RedirectToPage("MainPage"));
        }
Esempio n. 6
0
        private async Task ImportAsync()
        {
            string json, email = HttpContext.Session.GetString("email");
            var    user        = _context.Users.Single(a => a.Email == email);
            var    connectList = await _context.Connection.ToListAsync();

            string     filePath      = $"wwwroot/json/{user.Id}_{user.Fname}.json";
            Connection newConnection = new Connection();
            Books      newBook       = new Books();

            foreach (var item in connectList)
            {
                if (item.UserId == user.Id)
                {
                    _context.Connection.Remove(item);
                }
            }

            await _context.SaveChangesAsync();

            using (StreamReader r = new StreamReader(filePath))
            {
                json = r.ReadToEnd();
            }
            JArray ja = JArray.Parse(json);

            foreach (var item in ja)
            {
                if (!_context.Books.Any(a => a.Title == item.Value <string>("Title")))
                {
                    newBook             = new Books();
                    newBook.Title       = item.Value <string>("Title");
                    newBook.Author      = item.Value <string>("Author");
                    newBook.Translator  = item.Value <string>("Translator");
                    newBook.Publisher   = item.Value <string>("Publisher");
                    newBook.Description = item.Value <string>("Description");
                    newBook.Cover       = item.Value <string>("Cover");
                    newBook.Categories  = item.Value <string>("Categories");
                    _context.Books.Add(newBook);
                    await _context.SaveChangesAsync();
                }

                newConnection = new Connection();

                var book = _context.Books.Single(x => x.Title == item.Value <string>("Title"));
                newConnection.UserId = user.Id;
                newConnection.BookId = book.Id;
                var status = item.Value <string>("Status");

                if (status == "Completed" || status == "Reading" || status == "Not started")
                {
                    newConnection.Status = status;
                }
                else
                {
                    newConnection.Status = "Not started";
                }

                var rating = item.Value <int>("Rating");

                if (rating <= 10 && rating >= 0)
                {
                    newConnection.Rating = rating;
                }
                else
                {
                    newConnection.Rating = 0;
                }

                _context.Connection.Add(newConnection);

                await _context.SaveChangesAsync();
            }
        }