private static void onRemove(string key, object value, CacheItemRemovedReason reason) { if (!key.StartsWith("UserVisit->")) { return; } UserVisit visit = value as UserVisit; if (visit == null) { return; } using (ApplicationDbContext db = new ApplicationDbContext()) { UserStore <ApplicationUser> userStore = new UserStore <ApplicationUser>(db); ApplicationUserManager userManager = new ApplicationUserManager(userStore); ApplicationUser user = userManager.FindByIdAsync(visit.UserID).Result; if (user != null) { user.LastVisit = visit.VisitDate; user.LastPage = visit.Url; user.LastBrowser = visit.Browser; user.LastIP = visit.IpAddress; user.NumberOfVisits += visit.Count; IdentityResult result = userManager.UpdateAsync(user).Result; } } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!filterContext.HttpContext.Request.IsAjaxRequest()) { string userId = filterContext.HttpContext.User.Identity.GetId(); string key = "UserVisit->" + userId; if (userId != null) { UserVisit visit = HttpRuntime.Cache[key] as UserVisit; if (visit == null) { visit = new UserVisit { UserID = userId, VisitDate = DateTime.Now, Url = filterContext.HttpContext.Request.Url.ToString(), Browser = filterContext.HttpContext.Request.Browser.Browser, IpAddress = filterContext.HttpContext.Request.UserHostAddress }; HttpRuntime.Cache.Insert(key, visit, null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Default, onRemove); } else { visit.VisitDate = DateTime.Now; visit.Url = filterContext.HttpContext.Request.Url.ToString(); visit.Browser = filterContext.HttpContext.Request.Browser.Browser; visit.IpAddress = filterContext.HttpContext.Request.UserHostAddress; visit.Count++; } } } base.OnActionExecuting(filterContext); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var visit = new Visit { Id = request.Id, Title = request.Title, Description = request.Description, Category = request.Category, Date = request.Date, DocName = request.DocName, isEnded = request.isEnded, }; _context.Visits.Add(visit); var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername()); var doctor = await _context.Users.SingleOrDefaultAsync(d => d.UserName == request.DocName); var attendee = new UserVisit { AppUser = user, Visit = visit, IsHost = true, DateJoined = DateTime.Now }; var attendee2 = new UserVisit { AppUser = doctor, Visit = visit, IsHost = false, DateJoined = DateTime.Now }; _context.UserVisits.Add(attendee); _context.UserVisits.Add(attendee2); var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }
public static void ModifyUserVisits(string login, string doctorId, int anotherVisit) { var user = Mock._users.FirstOrDefault(m => m.Login == login); int count = 0; foreach (UserVisit visit in user.Visits) { if (visit.DoctorId == doctorId) { visit.VisitsTaken = visit.VisitsTaken + anotherVisit; } else { count++; } } if (count == user.Visits.Count) { UserVisit newVisit = new UserVisit { DoctorId = doctorId, VisitsTaken = 1 }; user.Visits.Add(newVisit); } int deletionIndex = -1; for (int i = 0; i < user.Visits.Count; i++) { if (user.Visits[i].VisitsTaken == 0) { deletionIndex = i; } } if (deletionIndex != -1) { user.Visits.RemoveAt(deletionIndex); } Mock.loggedUser.Visits = user.Visits; }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var visit = await _context.Visits.FindAsync(request.Id); if (visit == null) { throw new RestException(HttpStatusCode.NotFound, new { Visit = "Could not find visit" }); } var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername()); var attendance = await _context.UserVisits .SingleOrDefaultAsync(x => x.VisitId == visit.Id && x.AppUserId == user.Id); if (attendance != null) { throw new RestException(HttpStatusCode.BadRequest, new { Attendance = "Already attending this visit" }); } attendance = new UserVisit { Visit = visit, AppUser = user, IsHost = false, DateJoined = DateTime.Now }; _context.UserVisits.Add(attendance); var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; bool isSaveSuccess = true; try { string json = context.Request["jsonData"].ToString(); string s = "\\\""; string t = "#()#"; json = json.Replace(s, t); UserBehaviorDataContext userBehaviorDataContext = new UserBehaviorDataContext(); UserBehaviorEntity userBehavior = new UserBehaviorEntity(); userBehavior = JsonConvert.DeserializeObject<UserBehaviorEntity>(json); UserVisit users = userBehaviorDataContext.UserVisits.SingleOrDefault(u => u.UserIdentity == userBehavior.UserVisit.UserIdentity); if (users == null) { UserVisit userVisitTable = new UserVisit { UserIdentity = userBehavior.UserVisit.UserIdentity, browser = userBehavior.UserVisit.Browser, os = userBehavior.UserVisit.OS, ip = context.Request.ServerVariables["REMOTE_ADDR"] }; userBehaviorDataContext.UserVisits.InsertOnSubmit(userVisitTable); } foreach (PageVisitEntity pageVisitEntity in userBehavior.PageVisit) { PageVisit page = userBehaviorDataContext.PageVisits.SingleOrDefault(p => (p.UserIdentity == userBehavior.UserVisit.UserIdentity && p.PagePath.ToLower() == pageVisitEntity.PagePath.ToLower())); //insert when don't find the same page with the same user //otherwise update page actions if (page == null) { string actionArryStr = JsonConvert.SerializeObject(pageVisitEntity.Actions).ToString(); PageVisit pageVistTable = new PageVisit { PageIdentity = Guid.NewGuid(), UserIdentity = userBehavior.UserVisit.UserIdentity, PagePath = pageVisitEntity.PagePath, StartAccess = pageVisitEntity.StartAccess, LastActiveTime = pageVisitEntity.LastActiveTime, Actions = actionArryStr }; userBehaviorDataContext.PageVisits.InsertOnSubmit(pageVistTable); } else { List<ActionEntity> actionOldArray = null; try { actionOldArray = (!string.IsNullOrEmpty(page.Actions)) ? (List<ActionEntity>)JsonConvert.DeserializeObject<List<ActionEntity>>(page.Actions) : (List<ActionEntity>)JsonConvert.DeserializeObject<List<ActionEntity>>("[]"); } catch { actionOldArray = (List<ActionEntity>)JsonConvert.DeserializeObject<List<ActionEntity>>("[]"); } actionOldArray.AddRange(pageVisitEntity.Actions); page.LastActiveTime = pageVisitEntity.LastActiveTime; page.Actions = JsonConvert.SerializeObject(actionOldArray).ToString(); } } userBehaviorDataContext.SubmitChanges(); } catch { isSaveSuccess = false; } context.Response.Write(isSaveSuccess); }