public static void GeneraterNewBadges(Guid userId) { using (var dbContext = new ActivityTrackerDbContext()) { var badges = dbContext.Badges.ToList(); var userBadges = dbContext.UserBadges.Where(x => x.UserID == userId).ToList(); foreach (var badge in badges) { if (userBadges.FirstOrDefault(x => x.BadgeID == badge.BadgeID) == null) { if (CheckIfUserWonThisBadge(userId, badge)) { var userBadge = new UserBadge(); userBadge.UserID = userId; userBadge.BadgeID = badge.BadgeID; userBadge.Date = DateTime.UtcNow; dbContext.UserBadges.Add(userBadge); } } } dbContext.SaveChanges(); } }
private static bool NewJoiner(Guid userId, Badge badge) { using (var dbContext = new ActivityTrackerDbContext()) { return(dbContext.UserBadges.FirstOrDefault(x => x.UserID == userId && x.Badge.Name == badge.Name) == null); } }
private bool ActivityAlreadyExist(string externalId) { using (var dbContext = new ActivityTrackerDbContext()) { return(externalId != null && dbContext.UserActivities.Any(activity => externalId == activity.ExternalID)); } }
private static bool HasBadge(Guid userId, int Quantity) { using (var dbContext = new ActivityTrackerDbContext()) { return(dbContext.UserBadges.FirstOrDefault(x => x.UserID == userId && x.Badge.RuleName == "Walk" + Quantity) != null); } }
public static bool GenerateNewCommunications(Guid userId) { var newComm = false; using (var dbContext = new ActivityTrackerDbContext()) { var comms = dbContext.Communications.ToList(); var userComms = dbContext.UserCommunications.Where(x => x.UserID == userId).ToList(); foreach (var comm in comms) { if (checkIfUserHasCommunication(userId, comm, userComms)) { var userComm = new UserCommunication(); userComm.CommunicationID = comm.CommunicationID; userComm.UserID = userId; userComm.Date = DateTime.UtcNow; dbContext.UserCommunications.Add(userComm); newComm = true; } } dbContext.SaveChanges(); } return(newComm); }
private ActivityType GetActivityTypeByCode(string activityTypeCode) { using (var dbContext = new ActivityTrackerDbContext()) { return(dbContext.ActivityTypes.FirstOrDefault(type => type.Code == activityTypeCode)); } }
private User GetUserBytUtn(int utn) { using (var dbContext = new ActivityTrackerDbContext()) { return(dbContext.Users.FirstOrDefault(u => u.UTN == utn)); } }
private static bool WalkCount(Guid userId, Badge badge, int nbFootSteps) { using (var dbContext = new ActivityTrackerDbContext()) { var maxFootStepsPerDay = dbContext.UserActivities.Where(x => x.UserID == userId && x.ActivityTypeID == 1).GroupBy(x => x.Date.DayOfYear).Select(x => new { x.Key, FootSteps = x.Sum(y => y.Quantity) }).ToList(); return(maxFootStepsPerDay.FirstOrDefault(x => x.FootSteps >= nbFootSteps) != null); } }
/// <summary> /// Assume we do calculation on the same day of the activity was made. Activities in the past isn't possible, /// the activity should be sent on the same day or retrieved on the same day in an external system /// </summary> /// <param name="activity"></param> /// <param name="type"></param> private void AdjustHealthPoints(UserActivity activity, ActivityType type) { using (var dbContext = new ActivityTrackerDbContext()) { var todayUserActivites = dbContext.UserActivities.Where(activity => activity.UserID.Equals(activity.UserID) && activity.Date > DateTime.UtcNow.Date); int todayAccumulatedHealthPoints = todayUserActivites.Sum(activity => activity.HealthPoints); if (todayAccumulatedHealthPoints < Constants.MAX_HEALTH_POINTS_PER_DAY) { // Max HealthPoints per day not reatch, add points up to the maximum remaining (Hackathon doesn't care about parrallel calls that could go over maximum) int calculatedPointsForTheActivity = (int)Math.Floor(activity.Quantity * type.QuantityMultiplier); int maximumAllowableHealthPoints = Constants.MAX_HEALTH_POINTS_PER_DAY - todayAccumulatedHealthPoints; activity.HealthPoints = Math.Min(calculatedPointsForTheActivity, maximumAllowableHealthPoints); } } }
private static bool NoActivityFor3Days(Guid userId, Communication comm, List <UserCommunication> userComms) { using (var dbContext = new ActivityTrackerDbContext()) { var userActivities = dbContext.UserActivities.Where(x => x.UserID == userId && x.Date > DateTime.UtcNow.AddDays(-3)).ToList(); if (userActivities == null || userActivities.Count == 0) { var alreadyNotified = userComms.FirstOrDefault(x => x.CommunicationID == comm.CommunicationID && x.Date > DateTime.UtcNow.AddDays(-14)) != null; if (!alreadyNotified) { return(true); } } } return(false); }
/// <summary> /// Méthode réutilisable pour créer une activité /// </summary> /// <param name="user"></param> /// <param name="createActivityParameter"></param> private void CreateUserActivity(User user, CreateActivityParameter createActivityParameter) { var type = GetActivityTypeByCode(createActivityParameter.ActivityTypeCode); ValidateUserActivityCreation(user, type, createActivityParameter); var activity = new UserActivity { UserID = user.UserID, ActivityTypeID = type.ActivityTypeID, Date = createActivityParameter.CreationDate ?? DateTime.UtcNow, Quantity = createActivityParameter.Quantity, ExternalID = createActivityParameter.ExternalId }; AdjustHealthPoints(activity, type); using (var dbContext = new ActivityTrackerDbContext()) { dbContext.UserActivities.Add(activity); dbContext.SaveChanges(); } }
public ActivityTrackerController(ActivityTrackerDbContext dbContext) { this._dbContext = dbContext; }