예제 #1
0
 public async Task<ActionResult> Index(string pdfName)
 {
     using (db = new VfsCustomerServiceEntities())
     {
         Report rp = await db.Reports.FirstOrDefaultAsync(r => r.UploadDir == pdfName);
         CustomerLog cl = await db.CustomerLogs.FirstOrDefaultAsync(log => log.CustomerId == User.Identity.Name);
         if (rp!=null && cl!=null)
         {
             rp.TotalDownload += 1;
             cl.Total_Download += 1;
             try // save databse
             {
                 db.Entry(rp).State = EntityState.Modified;
                 db.Entry(cl).State = EntityState.Modified;
                 await db.SaveChangesAsync();
             }
             catch (Exception)
             {
                 
             }
         }
     }
     return Redirect("~/upload/" + pdfName + ".pdf");
 }
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {

            if (ModelState.IsValid)
            {
                bool signInSucess =  await SignInFrontAsync(model.UserName, model.Password);
                
                if (signInSucess)
                {
                    //var user = await UserManager.FindAsync(model.UserName, model.Password);
                    var user = new UserCustom { UserName = model.UserName, PasswordHash = model.Password };
                    if (user != null)
                    {
                        await SignInAsync(user, model.RememberMe);
                        using (customerDb = new VfsCustomerServiceEntities())
                        {
                            #region kiem tra khach hang VIP
                            var customer = await customerDb.Customers.FirstOrDefaultAsync(cs => cs.CustomerId == user.UserName);               
                            if (customer.VType == true)
                            {
                                Helper.SetCookieOfVIP();
                            }
                            #endregion

                            #region thong ke khach hang

                            var customerlog = await customerDb.CustomerLogs.FirstOrDefaultAsync(cl => cl.CustomerId == user.UserName);
                            if (customerlog == null)
                            {
                                // insert log
                                customerlog = new CustomerLog();
                                customerlog.CreateDate = DateTime.Now;
                                customerlog.CustomerId = user.UserName;
                                customerlog.Total_Download = 0;
                                customerlog.Total_Login = 1;
                                customerDb.CustomerLogs.Add(customerlog);                                
                            }
                            else
                            {
                                //update log                                
                                customerlog.Total_Download +=1;
                                customerlog.Total_Login +=1;
                                customerDb.Entry(customerlog).State = EntityState.Modified;

                            }
                            await customerDb.SaveChangesAsync(); // save database
                            #endregion
                        }
                        return RedirectToLocal(returnUrl);
                    }
                }
                
                else
                {
                    ModelState.AddModelError("", "Invalid username or password.");
                }
            }
            ViewBag.ReturnUrl = returnUrl;
            // If we got this far, something failed, redisplay form
            return View(model);
        }