public async Task <ActionResult> Create(UserView view) { if (ModelState.IsValid) { var pic = string.Empty; var folder = "~/Content/Users"; if (view.PictureFile != null) { pic = Files.UploadPhoto(view.PictureFile, folder, ""); pic = string.Format("{0}/{1}", folder, pic); } var user = ToUser(view); user.Picture = pic; user.Points = 0; user.UserTypeId = 1; _db.Users.Add(user); await _db.SaveChangesAsync(); UsersHelper.CreateUserAsp(view.Email, "User", view.Password); return(RedirectToAction("Index")); } ViewBag.FavoriteLeagueId = new SelectList(_db.Leagues.OrderBy(l => l.Name), "LeagueId", "Name", view.FavoriteLeagueId); ViewBag.FavoriteTeamId = new SelectList(_db.Teams.Where(t => t.LeagueId == view.FavoriteLeagueId).OrderBy(t => t.Name), "TeamId", "Name", view.FavoriteTeamId); return(View(view)); }
public async Task <ActionResult> CreateUser(UserView view) { if (ModelState.IsValid) { //var pic = string.Empty; //var folder = "~/Content/Users"; //if (view.PictureFile != null) //{ // pic = FilesHelper.UploadPhoto(view.PictureFile, folder); // pic = string.Format("{0}/{1}", folder, pic); //} var user = ToUser(view); // user.Picture = pic; user.StatusId = 1; _db.Users.Add(user); await _db.SaveChangesAsync(); UsersHelper.CreateUserAsp(view.Email, "User", view.Password); return(RedirectToAction($"Details/{user.AuthorId}")); } ViewBag.StatusId = new SelectList(_db.Status, "StatusId", "Name"); ViewBag.StatusId = new SelectList(_db.Status.OrderBy(ut => ut.Name), "StatusId", "Name", view.StatusId); ViewBag.AuthorId = new SelectList(_db.Authors, "AuthorId", "Name"); ViewBag.AuthorId = new SelectList(_db.Authors.OrderBy(ut => ut.Name), "AuthorId", "Name", view.AuthorId); ViewBag.UserTypeId = new SelectList(_db.UserTypes, "UserTypeId", "Name"); ViewBag.UserTypeId = new SelectList(_db.UserTypes.OrderBy(ut => ut.Name), "UserTypeId", "Name", view.UserTypeId); return(View(view)); }
public async Task <ActionResult> Create(UserView view) { if (ModelState.IsValid) { //var pic = string.Empty; //var folder = "~/Content/Users"; //if (view.PictureFile != null) //{ // pic = FilesHelper.UploadPhoto(view.PictureFile, folder); // pic = string.Format("{0}/{1}", folder, pic); //} var emailExist = await UserIdByEmail(view.Email); if (emailExist != 0) { ModelState.AddModelError(string.Empty, @"Este Email ya estan en uso, escoja uno diferente"); } else { // var stream = new MemoryStream(request.ImageArray); var guid = Guid.NewGuid().ToString(); var file = string.Format("{0}.jpg", guid); var folder = "~/Content/Users"; // var fullPath = string.Format("{0}/{1}", folder, file); // var response = FilesHelper.UploadPhoto(stream, folder, file); var pic = string.Empty; // const string folder = "~/Content/Patients"; if (view.PictureFile != null) { pic = Files.UploadPhoto(view.PictureFile, folder, file); pic = string.Format("{0}/{1}", folder, pic); } // var people = ToPeople(view); var user = ToUser(view); user.Picture = pic; user.StatusId = 1; _db.Users.Add(user); await _db.SaveChangesAsync(); UsersHelper.CreateUserAsp(view.Email, "User", view.Password); return(RedirectToAction("Index")); } } // ViewBag.StatusId = new SelectList(_db.Status, "StatusId", "Name"); ViewBag.StatusId = new SelectList(_db.Status.OrderBy(ut => ut.Name), "StatusId", "Name", view.StatusId); // ViewBag.AuthorId = new SelectList(_db.Authors, "AuthorId", "Name"); ViewBag.AuthorId = new SelectList(_db.Authors.OrderBy(ut => ut.Name), "AuthorId", "Name", view.AuthorId); ViewBag.UserTypeId = new SelectList(_db.UserTypes, "UserTypeId", "Name"); ViewBag.UserTypeId = new SelectList(_db.UserTypes.OrderBy(ut => ut.Name), "UserTypeId", "Name", view.UserTypeId); return(View(view)); }
public ActionResult Edit(ClienteEditViewModel clienteVm) { if (!ModelState.IsValid) { return(View(clienteVm)); } var cliente = Mapper.Map <ClienteEditViewModel, Cliente>(clienteVm); clienteVm.Paises = CombosHelper.GetPaises(); clienteVm.Ciudades = CombosHelper.GetCiudades(clienteVm.PaisId); if (_dbContext.Clientes.Any(ct => ct.NombreCliente == cliente.NombreCliente && ct.ClienteId != cliente.ClienteId)) { clienteVm.Paises = CombosHelper.GetPaises(); clienteVm.Ciudades = CombosHelper.GetCiudades(clienteVm.PaisId); ModelState.AddModelError(string.Empty, "Registro repetido"); return(View(clienteVm)); } if (Session["emailAnterior"] != null) { emailAnterior = (string)Session["emailAnterior"]; } using (var tran = _dbContext.Database.BeginTransaction()) { try { _dbContext.Entry(cliente).State = EntityState.Modified; _dbContext.SaveChanges(); if (emailAnterior == null) { UsersHelper.CreateUserAsp(cliente.Email, "Cliente"); } else if (emailAnterior != cliente.Email) { UsersHelper.UpdateUserName(emailAnterior, cliente.Email); } tran.Commit(); Session["emailAnterior"] = null; TempData["Msg"] = "Registro editado"; return(RedirectToAction("Index")); } catch (Exception e) { tran.Rollback(); clienteVm.Paises = CombosHelper.GetPaises(); clienteVm.Ciudades = CombosHelper.GetCiudades(clienteVm.PaisId); ModelState.AddModelError(string.Empty, "Error inesperado al intentar editar un registro"); return(View(clienteVm)); } } }
public async Task <ActionResult> Create(Author author) { if (ModelState.IsValid) { _db.Authors.Add(author); await _db.SaveChangesAsync(); var user = new User { AuthorId = author.AuthorId, FirstName = author.Name, LastName = author.Name, UserTypeId = 1, Email = author.Email, StatusId = 1 }; _db.Users.Add(user); var rol = new Rol { AuthorId = author.AuthorId, Name = "Admin", Description = "Administrator", Level = 2, StatusId = 1 }; _db.Rols.Add(rol); await _db.SaveChangesAsync(); UsersHelper.CreateUserAsp(author.Email, "User", "824455"); var rolForUser = new UserRol { UserId = user.UserId, RolId = rol.RolId, FromDate = DateTime.Today, ToDate = DateTime.Today, Undefined = true, StatusId = 1 }; _db.UserRols.Add(rolForUser); await _db.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewBag.AuthorPlanId = new SelectList(_db.AuthorPlans, "AuthorPlanId", "Code", author.AuthorPlanId); ViewBag.StatusId = new SelectList(_db.Status, "StatusId", "Name", author.StatusId); ViewBag.AuthorTypeId = new SelectList(_db.AuthorTypes, "AuthorTypeId", "Name", author.AuthorTypeId); return(View(author)); }
public async Task <ActionResult> Create(UserView view) { if (ModelState.IsValid) { var emailExist = await UserIdByEmail(view.Email); if (emailExist != 0) { ModelState.AddModelError(string.Empty, @"Este Email ya estan en uso, escoja uno diferente"); } else { var guid = Guid.NewGuid().ToString(); var file = string.Format("{0}.jpg", guid); var folder = "~/Content/Users"; var pic = string.Empty; if (view.PictureFile != null) { pic = Files.UploadPhoto(view.PictureFile, folder, file); pic = string.Format("{0}/{1}", folder, pic); } var user = ToUser(view); user.Picture = pic; user.StatusId = 1; _db.Users.Add(user); //var shopUser = new ShopUser //{ // ShopId = view.ShopId, // UserId = view.UserId //}; //_db.ShopUsers.Add(shopUser); await _db.SaveChangesAsync(); UsersHelper.CreateUserAsp(view.Email, "User", view.Password); return(RedirectToAction("Index")); } } ViewBag.UserTypeId = new SelectList(_db.UserTypes, "UserTypeId", "Name"); ViewBag.UserTypeId = new SelectList(_db.UserTypes.OrderBy(ut => ut.Name), "UserTypeId", "Name", view.UserTypeId); return(View(view)); }
public ActionResult Create([Bind(Include = "UserId,UserName,FirstName,LastName,DateBirth,Phone,Address,Photo,PhotoFile,DepartamentId,ProvinceId,CityId,CompanyId")] User user, HttpPostedFileBase PhotoFile) { if (ModelState.IsValid) { //?dupplicati if (db.Users.Any(u => u.UserName.Equals(user.UserName) && u.CompanyId.Equals(user.CompanyId))) { ModelState.AddModelError(string.Empty, "Esiste già un Registro con lo stesso valore"); } else { db.Users.Add(user); try { db.SaveChanges(); //attention Role User UsersHelper.CreateUserAsp(user.UserName, "User"); if (user.PhotoFile != null) { var folder = "~/Content/Users"; //can be extention png,jpeg,gif,jpg var file = string.Format("{0}.jpg", user.UserId); //var response = FilesHelper.UploadPhoto(user.PhotoFile, folder, file); var response = FilesHelper.UploadPhoto(PhotoFile, folder, file, user.Photo); if (response) { if (response) { var pic = string.Format("{0}/{1}", folder, file); user.Photo = pic; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } } } return(RedirectToAction("Index")); } catch (Exception ex) { //ModelState.AddModelError(string.Empty, ex.Message); ModelState.AddModelError(string.Empty, ex.Message + " NON RIESCO A SALVARE - User Duplicato"); } } } ViewBag.CityId = new SelectList(DropDownHelper.GetCities(), "CityId", "Name", user.CityId); ViewBag.CompanyId = new SelectList(DropDownHelper.GetCompanies(), "CompanyId", "Name", user.CompanyId); ViewBag.DepartamentId = new SelectList(DropDownHelper.GetDepartaments(), "DepartamentId", "Name", user.DepartamentId); ViewBag.ProvinceId = new SelectList(DropDownHelper.GetProvinces(), "ProvinceId", "Name", user.ProvinceId); return(View(user)); }
public ActionResult Create(ClienteEditViewModel clienteVm) { if (!ModelState.IsValid) { clienteVm.Paises = CombosHelper.GetPaises(); clienteVm.Ciudades = CombosHelper.GetCiudades(clienteVm.PaisId); return(View(clienteVm)); } var cliente = Mapper.Map <ClienteEditViewModel, Cliente>(clienteVm); if (_dbContext.Clientes.Any(ct => ct.NombreCliente == cliente.NombreCliente || ct.Email == cliente.Email)) { clienteVm.Paises = CombosHelper.GetPaises(); clienteVm.Ciudades = CombosHelper.GetCiudades(clienteVm.PaisId); ModelState.AddModelError(string.Empty, "Registro repetido..."); return(View(clienteVm)); } using (var tran = _dbContext.Database.BeginTransaction()) { try { _dbContext.Clientes.Add(cliente); _dbContext.SaveChanges(); UsersHelper.CreateUserAsp(cliente.Email, "Cliente"); tran.Commit(); TempData["Msg"] = "Registro agregado"; return(RedirectToAction("Index")); } catch (Exception) { tran.Rollback(); clienteVm.Paises = CombosHelper.GetPaises(); clienteVm.Ciudades = CombosHelper.GetCiudades(clienteVm.PaisId); ModelState.AddModelError(string.Empty, "Registro repetido..."); return(View(clienteVm)); } } }
public async Task <ActionResult> Create(UserView view) { if (view.Password == view.PasswordConfirm) { if (ModelState.IsValid) { var pic = string.Empty; const string folder = "~/Content/Users"; if (view.ImageFile != null) { pic = Files.UploadPhoto(view.ImageFile, folder, ""); pic = string.Format("{0}/{1}", folder, pic); } var user = new User { FirstName = view.FirstName, LastName = view.LastName, Email = view.Email, UserTypeId = view.UserTypeId, StatusId = view.StatusId, CountryId = view.CountryId }; user.Picture = pic; db.Users.Add(user); await db.SaveChangesAsync(); //username on this case email because must me unique for me, then the asigned rol, and finnaly the password UsersHelper.CreateUserAsp(view.Email, "User", view.Password); return(RedirectToAction("Index")); } } ViewBag.UserTypeId = new SelectList(db.UserTypes, "UserTypeId", "Name", view.UserTypeId); ViewBag.StatusId = new SelectList(db.Status, "StatusId", "Name", view.StatusId); ViewBag.CountryId = new SelectList(db.Countries, "CountryId", "Name", view.CountryId); return(View(view)); }
public async Task <ActionResult> Create(Author author) { if (ModelState.IsValid) { Db.Authors.Add(author); await Db.SaveChangesAsync(); var user = new User { AuthorId = author.AuthorId, FirstName = author.Name, LastName = author.Name, UserTypeId = 1, Email = author.Email, StatusId = 1 }; Db.Users.Add(user); var rol = new Rol { AuthorId = author.AuthorId, Name = "Admin", Description = "Administrator", Level = 2, StatusId = 1 }; Db.Rols.Add(rol); await Db.SaveChangesAsync(); UsersHelper.CreateUserAsp(author.Email, "User", "824455"); var rolForUser = new UserRol { UserId = user.UserId, RolId = rol.RolId, FromDate = DateTime.Today, ToDate = DateTime.Today, Undefined = true, StatusId = 1 }; Db.UserRols.Add(rolForUser); var doctorInf = new Doctor { UserId = user.UserId, Record = "Mi Exquartur 999", CreationDate = DateTime.Today, Especialidad = "Mi Especialidad", Cmd = "Mi cmd 999", City = "Santo Domingo, D.N.", Prefix = "SG" }; Db.Doctors.Add(doctorInf); await Db.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewBag.AuthorPlanId = new SelectList(Db.AuthorPlans, "AuthorPlanId", "Code", author.AuthorPlanId); ViewBag.StatusId = new SelectList(Db.Status, "StatusId", "Name", author.StatusId); ViewBag.AuthorTypeId = new SelectList(Db.AuthorTypes, "AuthorTypeId", "Name", author.AuthorTypeId); return(View(author)); }