Ejemplo n.º 1
0
        public bool CreatePatron(PatronRegisterModel model)
        {
            if (model.Id == 0)
            {
                LibraryCard libraryCard = new LibraryCard
                {
                    Created = DateTime.Now,
                    Fees    = model.Fees
                };
                _context.Add(libraryCard);
                _context.SaveChanges();

                model.LibraryCardId = libraryCard.Id;

                Patron newPatron = new Patron
                {
                    LastName            = model.LastName,
                    FirstName           = model.FirstName,
                    Address             = model.Address,
                    TelephoneNumber     = model.TelephoneNumber,
                    HomeLibraryBranchId = model.HomeLibraryBranchId,
                    LibraryCardId       = model.LibraryCardId
                };

                _context.Add(newPatron);
            }

            if (model.Id != 0)
            {
                var patron = _context.Patrons.Find(model.Id);


                patron.LastName            = model.LastName;
                patron.FirstName           = model.FirstName;
                patron.Address             = model.Address;
                patron.TelephoneNumber     = model.TelephoneNumber;
                patron.HomeLibraryBranchId = model.HomeLibraryBranchId;
                patron.DateOfBirth         = model.DateOfBirth;

                _context.Patrons.Update(patron);

                var libraryfees = _context.LibraryCards.Find(patron.LibraryCardId);

                libraryfees.Fees = model.Fees;

                _context.LibraryCards.Update(libraryfees);
            }

            var result = _context.SaveChanges();

            // if the user doesn't change anything the result becomes 0 which makes confliction.
            if (result != 0)
            {
                return(true);
            }
            return(false);
        }
        public IActionResult RegisterPatron(PatronRegisterModel model)
        {
            var newPatron = patron.CreatePatron(model);

            if (newPatron && model.Id != 0)
            {
                ViewBag.msg = "Success";
                return(RedirectToAction("Index"));
            }

            if (newPatron && model.Id == 0)
            {
                ViewBag.msg = "Success";
                return(RedirectToAction("Register", "Account"));
            }
            ViewBag.msg = "Error";
            return(RedirectToAction("Register", "Account"));
        }
        public IActionResult RegisterPatron(int id)
        {
            List <LibraryBranch> libraryList = new List <LibraryBranch>();

            libraryList = libraryBranch.GetAll().ToList();


            ViewBag.listOfBranches = libraryList;

            var model    = patron.Get(id);
            var newModel = new PatronRegisterModel
            {
                Id                  = model.Id,
                LastName            = model.LastName,
                FirstName           = model.FirstName,
                Address             = model.Address,
                TelephoneNumber     = model.TelephoneNumber,
                HomeLibraryBranchId = model.HomeLibraryBranchId,
                Fees                = model.LibraryCard.Fees,
                DateOfBirth         = model.DateOfBirth
            };

            return(PartialView("_CreatePatron", newModel));
        }