public JsonResult CreateUtilizador([FromBody] Lib_Primavera.Model.Utilizador cliente)
        {
            try
            {
                String username   = cliente.Username;
                String password   = cliente.Pass;
                String email      = cliente.Email;
                String fullname   = cliente.Fullname;
                String codCliente = username;
                String address    = cliente.Address;

                string hash = GetHashString(password);

                var db   = new FirstREST.Models.StoreEntities();
                var blog = new FirstREST.Models.Utilizador {
                    Email = email, Pass = hash, Username = username, Fullname = fullname, CodCliente = codCliente, Address = address
                };
                db.Utilizadors.Add(blog);
                db.SaveChanges();
                return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new { success = false, msg = e.StackTrace }, JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult ListaCarrinho()
        {
            int userID = Int32.Parse(Request.Cookies["UserId"].Value);
            var db     = new FirstREST.Models.StoreEntities();
            var cart   = from m in db.Carts
                         where m.ClientId == userID
                         select m;
            double total = 0;

            foreach (var art in cart.ToList())
            {
                total += art.ProductPrice * art.Quantity;
            }

            FirstREST.Models.Utilizador myUser = db.Utilizadors
                                                 .FirstOrDefault(u => u.Id == userID);

            ViewBag.cliente = myUser;
            List <Models.Cart> carrinho = new List <Models.Cart>();

            if (cart != null)
            {
                carrinho = cart.ToList();
            }
            ViewBag.carrinho = carrinho;
            ViewBag.total    = total;
            return(View());
        }
        public ActionResult Cliente()
        {
            int userID = Int32.Parse(Request.Cookies["UserId"].Value);
            var db     = new FirstREST.Models.StoreEntities();

            FirstREST.Models.Utilizador myUser = db.Utilizadors
                                                 .FirstOrDefault(u => u.Id == userID);

            ViewBag.cliente           = myUser;
            ViewBag.encomendasCliente = Lib_Primavera.PriIntegration.getEncomendasCliente(myUser.Username);

            return(View());
        }
 public JsonResult RemoveArtigo([FromBody] string id)
 {
     try
     {
         int userID      = Int32.Parse(Request.Cookies["UserId"].Value);
         int rowID       = Int32.Parse(id);
         var db          = new FirstREST.Models.StoreEntities();
         var productCart = db.Carts
                           .FirstOrDefault(u => u.ClientId == userID && u.Id == rowID);
         db.Carts.Remove(productCart);
         db.SaveChanges();
         return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception e)
     {
         return(Json(new { success = false, msg = e.StackTrace }, JsonRequestBehavior.AllowGet));
     }
 }
        public JsonResult Put(Lib_Primavera.Model.Utilizador utilizador)
        {
            Lib_Primavera.Model.RespostaErro erro = new Lib_Primavera.Model.RespostaErro();

            try
            {
                int userID = Int32.Parse(Request.Cookies["UserId"].Value);
                var db     = new FirstREST.Models.StoreEntities();
                var myUser = db.Utilizadors
                             .FirstOrDefault(u => u.Id == userID);

                if (myUser == null)
                {
                    throw new InvalidOperationException();
                }

                //UPDATE local database
                myUser.Fullname = utilizador.Fullname;
                myUser.Email    = utilizador.Email;
                myUser.Address  = utilizador.Address;
                db.SaveChanges();

                Lib_Primavera.Model.Cliente cliente = new Cliente();
                cliente.CodCliente  = myUser.Username;
                cliente.Morada      = utilizador.Address;
                cliente.NomeCliente = utilizador.Fullname;

                erro = Lib_Primavera.PriIntegration.UpdCliente(cliente);
                if (erro.Erro != 0)
                {
                    throw new InvalidOperationException();
                }

                return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
            }

            catch (Exception exc)
            {
                return(Json(new { success = false }, JsonRequestBehavior.AllowGet));
            }
        }
        public JsonResult UpdatePassword(string oldPass, string newPass)
        {
            string hashOld = GetHashString(oldPass);
            string newHash = GetHashString(newPass);

            int userID = Int32.Parse(Request.Cookies["UserId"].Value);
            var db     = new FirstREST.Models.StoreEntities();
            var myUser = db.Utilizadors
                         .FirstOrDefault(u => u.Id == userID);

            if (myUser.Pass.Equals(hashOld))
            {
                myUser.Pass = newHash;
                db.SaveChanges();
                return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json(new { success = false }, JsonRequestBehavior.AllowGet));
            }
        }
        public JsonResult CheckLogIn([FromBody] Lib_Primavera.Model.Utilizador cliente)
        {
            try
            {
                String username = cliente.Username;
                String password = cliente.Pass;
                string hash     = GetHashString(password);

                if (username.Equals("admin") && password.Equals("admin1234"))
                {
                    string idvalue = "admin";
                    Response.Cookies["UserId"].Value = idvalue;
                    return(Json(new { success = true, msg = "admin" }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    //The ".FirstOrDefault()" method will return either the first matched
                    //result or null
                    var db     = new FirstREST.Models.StoreEntities();
                    var myUser = db.Utilizadors
                                 .FirstOrDefault(u => u.Username == username &&
                                                 u.Pass == hash);
                    if (myUser == null)
                    {
                        throw new InvalidOperationException();
                    }

                    string idvalue = myUser.Id.ToString();
                    Response.Cookies["UserId"].Value = idvalue;
                    return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception e)
            {
                return(Json(new { success = false }, JsonRequestBehavior.AllowGet));
            }
        }
        public JsonResult AdicionaArtigoCarrinho([FromBody] Lib_Primavera.Model.Artigo artigo)
        {
            try
            {
                int    clienteId    = Int32.Parse(Request.Cookies["UserId"].Value);
                String productId    = artigo.CodArtigo;
                String productName  = artigo.DescArtigo;
                float  productPrice = (float)artigo.Preco;
                int    quantity     = artigo.Quantidade;

                var db  = new FirstREST.Models.StoreEntities();
                var art = new FirstREST.Models.Cart {
                    ClientId = clienteId, ProductId = productId, ProductName = productName, ProductPrice = productPrice, Quantity = quantity
                };
                db.Carts.Add(art);
                db.SaveChanges();
                return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                var msg = e.StackTrace;
                return(Json(new { success = false, msg = e.StackTrace }, JsonRequestBehavior.AllowGet));
            }
        }