public ActionResult Registo()
        {
            var model = new RegistoModel
            {
                ListaLocalizacoes = CustomHelper.GetListaLocalizacoes(ConnectionString)
            };

            return(View(model));
        }
        public ActionResult Registo(RegistoModel model)
        {
            if (!ModelState.IsValid)
            {
                model.ListaLocalizacoes = CustomHelper.GetListaLocalizacoes(ConnectionString);
                return(PartialView(model));
            }

            string sql = $@"INSERT INTO utilizador (
                                    [username],
                                    [password],
                                    [nome_completo],
                                    [email],
                                    [morada],
                                    [codigo_postal],
                                    [id_localizacao]
                                   ) VALUES (
                                    @username,
                                    @password,
                                    @nome_completo,
                                    @email,
                                    @morada,
                                    @codigo_postal,
                                    @id_localizacao)";

            using (var connection = new SqlConnection(ConnectionString))
                using (var command = new SqlCommand(sql, connection))
                {
                    connection.Open();

                    command.Parameters.AddWithValue("@username", model.Username);
                    command.Parameters.AddWithValue("@password", model.Password);
                    command.Parameters.AddWithValue("@nome_completo", model.Nome);
                    command.Parameters.AddWithValue("@email", model.Email);
                    command.Parameters.AddWithValue("@morada", model.Morada);
                    command.Parameters.AddWithValue("@codigo_postal", model.CodigoPostal);
                    command.Parameters.AddWithValue("@id_localizacao", model.IdLocalizacao);

                    command.ExecuteNonQuery();

                    connection.Close();
                }

            return(Json(new { }));
        }
        public ActionResult EditarPerfil(RegistoModel model)
        {
            ModelState.Remove("Password");

            if (!ModelState.IsValid)
            {
                model.ListaLocalizacoes = CustomHelper.GetListaLocalizacoes(ConnectionString);
                return(PartialView(model));
            }

            var sql = $@"update utilizador set 
                                    [username] = @userName,
                                    [nome_completo] = @nome_completo,
                                    [email] = @email,
                                    [morada] = @morada,
                                    [codigo_postal] = @codigo_postal,
                                    [id_localizacao] = @id_localizacao

                                    where id_user = @id_user";

            using (var connection = new SqlConnection(ConnectionString))
                using (var command = new SqlCommand(sql, connection))
                {
                    connection.Open();

                    command.Parameters.AddWithValue("@id_user", model.IdUser);
                    command.Parameters.AddWithValue("@username", model.Username);
                    command.Parameters.AddWithValue("@nome_completo", model.Nome);
                    command.Parameters.AddWithValue("@email", model.Email);
                    command.Parameters.AddWithValue("@morada", model.Morada);
                    command.Parameters.AddWithValue("@codigo_postal", model.CodigoPostal);
                    command.Parameters.AddWithValue("@id_localizacao", model.IdLocalizacao);

                    command.ExecuteNonQuery();

                    connection.Close();
                }

            return(Json(new {
                Notify = true,
                Type = CustomHelper.Constantes.TiposNotificacao.Success,
                Message = "Alterações gravadas com sucesso!",
                Time = 7500
            }));
        }
        public ActionResult EditarPerfil()
        {
            ViewBag.LoggedIn = IsLoggedIn();

            var utilizador = (LoginModel)Session[SessionUtilizador];

            var model = new RegistoModel
            {
                ListaLocalizacoes = CustomHelper.GetListaLocalizacoes(ConnectionString),
                IdUser            = utilizador.IdUser
            };

            var sql = "SELECT * from utilizador where id_user = @userId";

            using (var connection = new SqlConnection(ConnectionString))
                using (var command = new SqlCommand(sql, connection))
                {
                    connection.Open();

                    command.Parameters.AddWithValue("@userId", utilizador.IdUser);

                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            model.Username      = reader["UserName"].ToString();
                            model.Password      = reader["password"].ToString();
                            model.Nome          = reader["nome_completo"].ToString();
                            model.Email         = reader["Email"].ToString();
                            model.Morada        = reader["morada"].ToString();
                            model.CodigoPostal  = reader["codigo_postal"].ToString();
                            model.IdLocalizacao = Convert.ToInt16(reader["id_localizacao"]);
                        }
                    }

                    connection.Close();
                }

            return(View(model));
        }