Example #1
0
        public async Task <IActionResult> SubmeterPratoExistente(int?id)
        {
            string Status = CheckStatus();

            switch (Status)
            {
            case "Restaurante":
                if (id == null)
                {
                    return(NotFound());
                }

                PratoIndividual Prato = await(from prato in _context.Pratos
                                              join restaurantePrato in _context.RestaurantePratos on prato.Id equals restaurantePrato.PratoId
                                              join restaurantes in _context.Restaurantes on restaurantePrato.RestauranteId equals restaurantes.Id
                                              where id == prato.Id && restaurantes.UtilizadorId == Convert.ToInt32(HttpContext.Session.GetString("Id"))
                                              select new PratoIndividual
                {
                    Id                = prato.Id,
                    Nome              = prato.Nome,
                    Preco             = restaurantePrato.Preco,
                    TipoPratoId       = prato.TipoPratoId,
                    TipoPrato         = prato.TipoPrato,
                    RestaurantePratos = prato.RestaurantePratos,
                    Descricao         = restaurantePrato.Descricao,
                    Foto              = restaurantePrato.Foto,
                }).FirstOrDefaultAsync();



                return(View(Prato));

            case "Admin":
            case "Cliente":
            case "NaoAutenticado":
                return(RedirectToAction("Login", "Utilizador"));

            default:
                return(RedirectToAction("Bloqueado", new RouteValueDictionary(
                                            new { controller = "Utilizador", action = "Bloqueado", Motivo = Status })));
            }
        }
Example #2
0
        public async Task <IActionResult> AddHojeNovo([Bind("Id", "Foto", "TipoPratoId", "Nome", "Descricao", "Dia", "Preco")] PratoIndividual pratoIndividual, IFormFile files)
        {
            //if (ModelState.IsValid)
            //{
            try
            {
                try
                {
                    if (files != null)
                    {
                        Random numAleatorio = new Random();
                        int    valorInteiro = numAleatorio.Next(10000, 100000);
                        string NomeFicheiro = HttpContext.Session.GetString("Id") + valorInteiro + Path.GetFileName(files.FileName);

                        string uploads = Path.Combine(_he.ContentRootPath, "wwwroot/Images/Pratos/", NomeFicheiro);

                        FileStream fs = new FileStream(uploads, FileMode.Create);

                        files.CopyTo(fs);
                        fs.Close();

                        pratoIndividual.Foto = NomeFicheiro; // opiniao dar id + nome da imagem pq as imagens podem ter nomes iguais
                    }
                    else
                    {
                        pratoIndividual.Foto = "FOTOTEMPORARIA";
                    }
                }
                catch (Exception)
                {
                }



                int restauranteID = Convert.ToInt32((from Restaurante in _context.Restaurantes
                                                     where Restaurante.UtilizadorId == Convert.ToInt32(HttpContext.Session.GetString("Id"))
                                                     select Restaurante.Id).FirstOrDefault());

                Prato NovoPrato = new Prato();
                var   Existe    = _context.Pratos.Where(u => u.Nome.ToUpper() == pratoIndividual.Nome.ToUpper());

                if (Existe.Any())
                {
                    NovoPrato = Existe.FirstOrDefault();
                }
                else
                {
                    NovoPrato.Foto        = pratoIndividual.Foto;
                    NovoPrato.Nome        = pratoIndividual.Nome;
                    NovoPrato.TipoPratoId = pratoIndividual.TipoPratoId;

                    _context.Pratos.Add(NovoPrato);
                    await _context.SaveChangesAsync();
                }



                RestaurantePrato NovoPrato1 = new RestaurantePrato();
                NovoPrato1.Descricao     = pratoIndividual.Descricao;
                NovoPrato1.Foto          = pratoIndividual.Foto;
                NovoPrato1.Preco         = pratoIndividual.Preco;
                NovoPrato1.PratoId       = NovoPrato.Id;
                NovoPrato1.RestauranteId = restauranteID;
                NovoPrato1.Dia           = pratoIndividual.Dia;

                _context.RestaurantePratos.Add(NovoPrato1);
                await _context.SaveChangesAsync();


                // ENVIA EMAIL!!!!

                var Emails = (from utilizadores in _context.Utilizadors
                              join cliente in _context.Clientes on utilizadores.Id equals cliente.UtilizadorId
                              join pratoClientes in _context.PratoClientes on cliente.Id equals pratoClientes.ClienteId
                              where pratoClientes.PratoId == NovoPrato1.PratoId && utilizadores.Notificacao
                              select utilizadores);

                foreach (Utilizador u in Emails)
                {
                    var          fromAddress  = new MailAddress("*****@*****.**", "Jiro");
                    var          toAddress    = new MailAddress(u.Email, u.Name);
                    const string fromPassword = "******";
                    const string subject      = "Verificação Email";
                    string       body         = "O Seu prato favorito vai ser servido!";

                    var smtp = new SmtpClient
                    {
                        Host                  = "smtp.gmail.com",
                        Port                  = 587,
                        EnableSsl             = true,
                        DeliveryMethod        = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false,
                        Credentials           = new NetworkCredential(fromAddress.Address, fromPassword)
                    };
                    using (var message = new MailMessage(fromAddress, toAddress)
                    {
                        Subject = subject,
                        Body = body
                    })
                    {
                        smtp.Send(message);
                    }
                }
            }
            catch (DbUpdateConcurrencyException)
            {
            }
            return(RedirectToAction("Index", "Home"));
            //}

            //return RedirectToAction("Index", "Home");
        }