public async Task <IActionResult> Registar2([Bind("Id,Name,Email,Username,Password,Telefone,Morada,HoraAbertura,HoraFecho,DiaDescanso")] RestauranteCompleto restaurante, IFormFile files) { if (ModelState.IsValid) { List <Utilizador> existe = _context.Utilizadors.AsNoTracking().ToList(); //Parte com cookies var u = existe.FirstOrDefault(u => (u.Username.Equals(restaurante.Username)) || (u.Email.Equals(restaurante.Email))); if (u == null) { Utilizador utilizador = restaurante.GetUtilizador(); try { Random numAleatorio = new Random(); int valorInteiro = numAleatorio.Next(10000, 100000); string NomeFicheiro = valorInteiro + Path.GetFileName(files.FileName); string uploads = Path.Combine(_he.ContentRootPath, "wwwroot/Images/Utilizadores/", NomeFicheiro); FileStream fs = new FileStream(uploads, FileMode.Create); files.CopyTo(fs); fs.Close(); utilizador.Imagem = NomeFicheiro; } catch (Exception) { utilizador.Imagem = "default_img.jpg"; } HttpContext.Session.SetString("Imagem", utilizador.Imagem); _context.Add(utilizador); await _context.SaveChangesAsync(); int utilizadorId = utilizador.Id; Restaurante restaurante1 = restaurante.GetRestaurante(); restaurante1.UtilizadorId = utilizadorId; restaurante1.Aprovado = false; _context.Add(restaurante1); await _context.SaveChangesAsync(); return(RedirectToAction("Login", "Utilizador")); } else { ModelState.AddModelError("Username", "This user already exists"); } } return(View()); }
public async Task <IActionResult> EditRes([Bind("Id", "UtilizadorId", "Name", "Email", "Username", "Password", "Telefone", "Morada", "HoraAbertura", "HoraFecho", "DiaDescanso")] RestauranteCompleto restaurante, IFormFile files) { if (ModelState.IsValid) { List <Utilizador> existe = _context.Utilizadors.AsNoTracking().ToList(); var u = existe.FirstOrDefault(u => ((!(u.Id.Equals(restaurante.UtilizadorId)) && (u.Username.Equals(restaurante.Username)) || (!(u.Id.Equals(restaurante.UtilizadorId)) && (u.Email.Equals(restaurante.Email)))))); if (u == null) { try { try { if (files != null) { Random numAleatorio = new Random(); int valorInteiro = numAleatorio.Next(100, 1000); string NomeFicheiro = HttpContext.Session.GetString("Id") + valorInteiro + Path.GetFileName(files.FileName); string uploads = Path.Combine(_he.ContentRootPath, "wwwroot/Images/Utilizadores/", NomeFicheiro); FileStream fs = new FileStream(uploads, FileMode.Create); files.CopyTo(fs); fs.Close(); restaurante.Imagem = NomeFicheiro; // opiniao dar id + nome da imagem pq as imagens podem ter nomes iguais HttpContext.Session.SetString("Imagem", restaurante.Imagem); } } catch (Exception) { } restaurante.Imagem = HttpContext.Session.GetString("Imagem"); restaurante.Aprovado = true; HttpContext.Session.SetString("Email", restaurante.Email); HttpContext.Session.SetString("Name", restaurante.Name); _context.Update(restaurante.GetUtilizador()); await _context.SaveChangesAsync(); _context.Update(restaurante.GetRestaurante()); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RestauranteExists(restaurante.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("Username", "This user already exists"); } } return(View(restaurante)); }