コード例 #1
0
ファイル: WritersController.cs プロジェクト: HommyDeJ/Library
        public ActionResult Create(WriterView writerView)
        {
            bool   photoExist = true;
            string count      = string.Empty;
            int    i          = 0;

            if (ModelState.IsValid)
            {
                string picture = string.Empty;
                string folder  = "~/Content/Photos/Writers";

                if (writerView.PhotoFile != null)
                {
                    while (photoExist)
                    {
                        i++;
                        count = Convert.ToString(i);

                        photoExist = System.IO.File.Exists(Server.MapPath($"{folder}/{count}{writerView.PhotoFile.FileName}"));
                    }

                    if (count == "0")
                    {
                        count = string.Empty;
                    }

                    picture = FilesHelper.UploadPhoto(writerView.PhotoFile, folder, count);
                    picture = string.Format($"{folder}/{count}{picture}");
                }
                else
                {
                    picture = "Default.gif";
                    folder  = "~/Content/Photos/Writers";
                    picture = string.Format($"{folder}/{picture}");
                }

                Writer writer = ToWriter(writerView);
                writer.Photo = picture;
                db.Writers.Add(writer);

                try
                {
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException.InnerException.Message.Contains("_Index"))
                    {
                        ModelState.AddModelError(string.Empty, "El escritor/a no puede ser guardado/a porque existe un/a con el mismo nombre.");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }
                }
            }

            return(View(writerView));
        }
コード例 #2
0
        public ActionResult Create(BookType bookType)
        {
            if (ModelState.IsValid)
            {
                db.BookTypes.Add(bookType);

                try
                {
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException.InnerException.Message.Contains("_Index"))
                    {
                        ModelState.AddModelError(string.Empty, "El género no puede ser guardado porque existe uno con el mismo nombre.");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }
                }
            }

            return(View(bookType));
        }
コード例 #3
0
        public ActionResult Create(Supplier supplier)
        {
            if (ModelState.IsValid)
            {
                db.Suppliers.Add(supplier);

                try
                {
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException.InnerException.Message.Contains("_Description_Index"))
                    {
                        ModelState.AddModelError(string.Empty, "El suplidor no puede ser guardado porque existe uno con el mismo nombre.");
                    }

                    if (ex.InnerException.InnerException.Message.Contains("_EMail_Index"))
                    {
                        ModelState.AddModelError(string.Empty, "El suplidor no puede ser guardado porque ya está registrado ese correo.");
                    }
                }
            }

            return(View(supplier));
        }
コード例 #4
0
 public ActionResult Create(FormCollection collection)
 {
     try
     {
         if (collection["RoleName"].Length != 0)
         {
             db.Roles.Add(new IdentityRole()
             {
                 Name = collection["RoleName"]
             });
             db.SaveChanges();
             ViewBag.ResultMessage = "Role created successfully !";
             return(RedirectToAction("Index"));
         }
         else
         {
             ViewBag.Blank = "You didnt enter a role name!";
             return(View());
         }
     }
     catch
     {
         return(View());
     }
 }
コード例 #5
0
        public ActionResult Create(Editorial editorial)
        {
            if (ModelState.IsValid)
            {
                db.Editorials.Add(editorial);

                try
                {
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException.InnerException.Message.Contains("_Index"))
                    {
                        ModelState.AddModelError(string.Empty, "La editorial no puede ser guardada porque existe una con el mismo nombre.");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }
                }
            }

            return(View(editorial));
        }
コード例 #6
0
        public ActionResult Create([Bind(Include = "ID,Name,Surname,Street,HouseNumber,ApartmentNumber,PostalCode,City,PhoneNumber,Email")] Reader reader)
        {
            if (ModelState.IsValid)
            {
                db.Readers.Add(reader);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(reader));
        }
コード例 #7
0
        public ActionResult Create([Bind(Include = "Id,Author,Title,Publisher,PublishingDate,Isbn,BorrowDate,ReturnDate,ReaderId")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ReaderId = new SelectList(db.Readers, "ID", "Name", book.ReaderId);
            return(View(book));
        }
コード例 #8
0
        public ActionResult Create([Bind(Include = "BookId,ISBN,WriterId,BookTypeId,Title,Edition,Plot,DateOfRelease")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.BookTypeId = new SelectList(db.BookTypes, "BookTypeId", "Description", book.BookTypeId);
            ViewBag.WriterId   = new SelectList(db.Writers, "WriterId", "Name", book.WriterId);
            return(View(book));
        }
コード例 #9
0
        /// <summary>
        /// Commits any data changes to the database
        /// </summary>
        public void SaveChanges()
        {
            try
            {
                m_Context.SaveChanges();
            }
            catch (Exception ex) when(ex is DbEntityValidationException || ex is DbUpdateException)
            {
                Console.WriteLine(ex);

                throw;
            }
        }
コード例 #10
0
       /// <summary>
       /// Method which allows for a user to added to a Identity role.
       /// Updates the users role and updates the user through the UOW
       /// </summary>
       /// <param name="UserName">User to be added to role</param>
       /// <param name="RoleName">Role to which user is the be added</param>
        public void RoleAddToUser(string UserName, string RoleName)
        {
            ApplicationUser user = uow.UserRepository.Get(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase));
            var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new LibraryAppContext()));

            try
            {
                if (user != null)
            {
                var idResult = um.AddToRole(user.Id, RoleName);
                var authenticationManager = HttpContext.GetOwinContext().Authentication;

                authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);


                var identity = um.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity);
                
                user.Role = RoleName;

                uow.UserRepository.Update(user);

                db.SaveChanges();

                uow.SaveChanges();
            }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }


        }
コード例 #11
0
 public Users Add(Users user)
 {
     ctx.Attach(user).State = EntityState.Added;
     ctx.SaveChanges();
     return(user);
 }
コード例 #12
0
 public void Create(T entity)
 {
     _dbContext.Set <T>().Add(entity);
     _dbContext.SaveChanges();
 }
コード例 #13
0
 public Books Add(Books book)
 {
     ctx.Attach(book).State = EntityState.Added;
     ctx.SaveChanges();
     return(book);
 }