public IActionResult LogInSyn([FromBody] DTO.Principal principal)
        {
            PrincipalDAO dao  = new PrincipalDAO(_context);
            string       role = dao.CheckLogin(principal.Username, principal.Password);

            if (role == "admin" || role == "user")
            {
                HttpContext.Session.SetString("USER", principal.Username);
                HttpContext.Session.SetString("ROLE", role);
                if (role == "user")
                {
                    ShoppingCartDAO      scDAO              = new ShoppingCartDAO(_context);
                    List <DTO.Accessory> listAccCart        = scDAO.FindAllAccCartByUsername(principal.Username);
                    Dictionary <string, DTO.Accessory> cart = new Dictionary <string, DTO.Accessory>();
                    foreach (var item in listAccCart)
                    {
                        DTO.ShoppingCart.AddCart(item, cart);
                    }
                    HttpContext.Session.SetCollectionAsJson("cart", cart);
                }
                TempData["msg"]         = "Register successful";
                TempData["msg-details"] = "System automatically log in your account.";
            }
            else
            {
                TempData["msg"] = "Register failed";
            }
            return(RedirectToAction("Index", "Home"));;
        }
        public IActionResult LogIn([FromBody] DTO.Principal principal)
        {
            PrincipalDAO dao  = new PrincipalDAO(_context);
            string       role = dao.CheckLogin(principal.Username, principal.Password);

            if (role == "admin" || role == "user")
            {
                HttpContext.Session.SetString("USER", principal.Username);
                HttpContext.Session.SetString("ROLE", role);
                if (role == "user")
                {
                    ShoppingCartDAO      scDAO              = new ShoppingCartDAO(_context);
                    List <DTO.Accessory> listAccCart        = scDAO.FindAllAccCartByUsername(principal.Username);
                    Dictionary <string, DTO.Accessory> cart = new Dictionary <string, DTO.Accessory>();
                    foreach (var item in listAccCart)
                    {
                        DTO.ShoppingCart.AddCart(item, cart);
                    }
                    HttpContext.Session.SetCollectionAsJson("cart", cart);
                    var a = HttpContext.Session.GetCollectionFromJson <Dictionary <string, DTO.Accessory> >("cart");
                }
                TempData["msg"] = "Login successfully";
            }
            else
            {
                TempData["msg"] = "Login failed";
            }
            return(new JsonResult(role));
        }
Пример #3
0
        public bool Update(DTO.Principal p)
        {
            var principal = _context.Principal.Find(p.Username);

            principal.Phone    = p.Phone;
            principal.Fullname = p.Fullname;
            principal.Address  = p.Address;
            return(_context.SaveChanges() != 0);
        }
        public IActionResult Register([Bind("Username,Password,Fullname,Phone,Address")] DTO.Principal principal)
        {
            PrincipalDAO dao = new PrincipalDAO(_context);

            principal.Role = "user";
            if (dao.Register(principal).Result)
            {
                return(LogInSyn(principal));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
        public IActionResult UpdateProfile([FromBody] DTO.Principal principal)
        {
            PrincipalDAO dao = new PrincipalDAO(_context);
            string       msg;

            if (dao.Update(principal))
            {
                msg = "Update profile success";
            }
            else
            {
                msg = "Nothing has changed or Update profile failed ";
            }
            return(new JsonResult(msg));
        }
Пример #6
0
 public async Task <bool> Register(DTO.Principal p)
 {
     _context.Principal.Add(p);
     return(await _context.SaveChangesAsync() != 0);
 }