Exemple #1
0
        public ActionResult Create([Bind(Include = "GenreId,GenreName")] Genre genre)
        {
            if (ModelState.IsValid)
            {
                db.Genres.Add(genre);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(genre));
        }
Exemple #2
0
        public ActionResult Create([Bind(Include = "AuthorId,FirstName,LastName")] Author author)
        {
            if (ModelState.IsValid)
            {
                db.Authors.Add(author);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(author));
        }
Exemple #3
0
        public ActionResult Create([Bind(Include = "BookId,Title,ReleaseDate,Description,GenreId")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "GenreName", book.GenreId);
            return(View(book));
        }
Exemple #4
0
        protected override void Seed(BookLibraryDbContext context)
        {
            this.userManager = new UserManager <User>(new UserStore <User>(context));
            context.Roles.AddOrUpdate(x => x.Name, new IdentityRole("Admin"));
            context.Roles.AddOrUpdate(x => x.Name, new IdentityRole("User"));
            context.SaveChanges();

            var defaultUser = new User {
                Email = "*****@*****.**", UserName = "******"
            };

            this.userManager.Create(defaultUser, "diMANA123");
            this.userManager.AddToRole(defaultUser.Id, "User");

            var adminUser = new User
            {
                Email    = "*****@*****.**",
                UserName = "******"
            };

            this.userManager.Create(adminUser, "diMANA123");
            this.userManager.AddToRole(adminUser.Id, "Admin");

            Genre genreAction = new Genre {
                GenreName = "Action"
            };
            Genre genreDrama = new Genre {
                GenreName = "Drama"
            };
            Genre genreAdventure = new Genre {
                GenreName = "Adventure"
            };

            context.Genres.AddOrUpdate(genreAction, genreDrama, genreAdventure);

            Author teryPratched = new Author {
                FirstName = "Author", LastName = "Demo"
            };
            Author ivanPetrov = new Author {
                FirstName = "Ivan", LastName = "Petrov"
            };

            context.Books.AddOrUpdate(
                new Book {
                Title = "Demo Book One", ReleaseDate = DateTime.Now, Autors = { teryPratched, ivanPetrov }, Description = "some description for test", Genre = genreAction
            },
                new Book {
                Title = "Test Boom Two", ReleaseDate = DateTime.Now, Autors = { teryPratched }, Description = "some description for test for", Genre = genreAdventure
            }
                );

            context.SaveChanges();
        }
        public IActionResult Delete(Book book)
        {
            using (var db = new BookLibraryDbContext())
            {
                db.Books.Remove(book);
                db.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
        public IActionResult Edit(Book book)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }
            using (var db = new BookLibraryDbContext())
            {
                db.Books.Update(book);
                db.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
        public async Task <JwtAuthenticationDto> Authentication(User user)
        {
            var userClaims = await userManager.GetClaimsAsync(user);

            userClaims.Add(new Claim(ClaimTypes.NameIdentifier, user.UserName));
            userClaims.Add(new Claim(ClaimTypes.Name, user.UserName));
            var jwtToken     = GenerateJwtToken(userClaims);
            var refreshToken = generateRefreshToken();

            if (!refreshToken.IsActive)
            {
                return(null);
            }
            // save refresh token
            user.RefreshTokens.Add(refreshToken);
            ctx.Update(user);
            ctx.SaveChanges();
            return(new()
            {
                JwtToken = jwtToken,
                RefreshToken = refreshToken.Token
            });
        }
Exemple #8
0
        private Book AddBook()
        {
            var author = db.Authors.FirstOrDefault(a => a.Name == this.AuthorName);

            if (author == null)
            {
                author = new Author {
                    Name = this.AuthorName
                };
                db.Authors.Add(author);
                db.SaveChanges();
            }

            var category = db.Categories.FirstOrDefault(c => c.Name == this.CategoryName);

            if (category == null)
            {
                category = new Category {
                    Name = this.CategoryName
                };
                db.Categories.Add(category);
                db.SaveChanges();
            }

            var book = new Book
            {
                Title         = this.Title,
                AuthorId      = author.Id,
                CategoryId    = category.Id,
                CoverImageUrl = this.CoverImageUrl,
                Description   = this.Description
            };

            db.Books.Add(book);
            db.SaveChanges();
            return(book);
        }
Exemple #9
0
        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(this.Page());
            }

            int bookId = int.Parse(this.BookId);

            Loan loan = new Loan()
            {
                BookId     = bookId,
                BorrowerId = int.Parse(this.BorrowerId),
                DateOfLoan = this.DateOfLoan,
                DueDate    = this.DueDate
            };

            db.Loans.Add(loan);
            db.Books.Find(bookId).StatusId = 2;
            db.SaveChanges();

            return(RedirectToPage("/Index"));
        }
 public void Create(Book obj)
 {
     _dbContext.Books.Add(obj);
     _dbContext.SaveChanges();
 }
Exemple #11
0
 public void Create(Author obj)
 {
     _dbContext.Authors.Add(obj);
     _dbContext.SaveChanges();
 }