Esempio n. 1
0
        private void AdicionarRestaurante(User utilizador)
        {
            string restaurante = txt_nome_restaurante.Text;
            string cidade      = txt_cidade.Text;
            string cp          = txt_cp.Text;
            string morada      = txt_morada.Text;

            if (restaurante == String.Empty || restaurante.Trim().Length <= 3)
            {
                throw new Exception("O nome indicado não é válido. Deve ter pelo menos 3 letras.");
            }

            if (DateTime.Now.Year - utilizador.data_nasc.Year < 18)
            {
                throw new Exception("Deves ter no mímimo 18 anos para poderes ser responsável por um restaurante na Food4U");
            }

            if (cidade == String.Empty || cidade.Trim().Length <= 3)
            {
                throw new Exception("A cidade indicada não é válida. Deve ter pelo menos 3 letras.");
            }

            if (cp == String.Empty || Regex.Match(cp.Trim(), @"^\d{4}(-\d{3})?$").Success == false)
            {
                throw new Exception("O código postal indicado não é válido. Deve ter o seguinte formato xxxx-xxx.");
            }

            if (morada == String.Empty || morada.Trim().Length <= 3)
            {
                throw new Exception("A morada indicada não é válida. Deve ter pelo menos 3 letras.");
            }


            if (FileUpload1.HasFile == false)
            {
                throw new Exception("Tem de indicar o ficheiro da foto do restaurante");
            }
            if (FileUpload1.PostedFile.ContentType != "image/jpeg" &&
                FileUpload1.PostedFile.ContentType != "image/jpg" &&
                FileUpload1.PostedFile.ContentType != "image/png")
            {
                throw new Exception("O formato do ficheiro da foto do restaurante não é suportado.");
            }
            if (FileUpload1.PostedFile.ContentLength == 0 ||
                FileUpload1.PostedFile.ContentLength > 5000000)
            {
                throw new Exception("O tamanho da foto do restaurante não é válido.");
            }

            Restaurant restaurant = new Restaurant();

            restaurant.name    = restaurante;
            restaurant.city    = cidade;
            restaurant.cp      = cp;
            restaurant.address = morada;

            int id_restaurant = restaurant.AdicionarRestauranteOwner(utilizador);

            string ficheiro = Server.MapPath(@"~\Public\images\restaurants\");

            ficheiro += id_restaurant + ".jpg";
            FileUpload1.SaveAs(ficheiro);
        }