public Badge Update(Badge item)
 {
     Repo.Update(item);
     Unit.SaveChanges();
     return item;
     
 }
        // PUT api/Activity/5
        public IHttpActionResult Put(Badge item)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                return Ok(Logic.Update(item));
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!Logic.Exists(item.Id))
                {
                    return NotFound();
                }

            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        public void BadgeRepository()
        {
            Mock<IDbSetFactory> factory = new Mock<IDbSetFactory>();
            Mock<DbSet<Badge>> dbSet = new Mock<DbSet<Badge>>();

            factory.Setup(m => m.CreateDbSet<Badge>()).Returns(dbSet.Object);

            BadgeRepository repo = new BadgeRepository(factory.Object);

            var badge = new Badge();

            var sequence = new MockSequence();
            dbSet.InSequence(sequence).Setup(e => e.Add(badge));
            dbSet.InSequence(sequence).Setup(e => e.Find(badge.Id));
            dbSet.InSequence(sequence).Setup(e => e.Find(badge.Id));
            dbSet.InSequence(sequence).Setup(e => e.Find(badge.Id));
            repo.Create(badge);
            repo.Get(badge.Id);
            repo.Update(badge);
            repo.Delete(badge.Id);
        }
        public void BadgeLogic()
        {
            Mock<IUnitOfWork> uow = new Mock<IUnitOfWork>();
            Mock<IBadgeRepository> repo = new Mock<IBadgeRepository>();

            BadgeLogic logic = new BadgeLogic(uow.Object, repo.Object);

            var badge = new Badge();
            var sequence = new MockSequence();
            repo.InSequence(sequence).Setup(r => r.Create(badge));
            repo.InSequence(sequence).Setup(r => r.Update(badge));
            repo.InSequence(sequence).Setup(r => r.Get(badge.Id));
            repo.InSequence(sequence).Setup(r => r.Delete(badge.Id));
            logic.Create(badge);
            logic.Update(badge);
            logic.Get(badge.Id);
            logic.Delete(badge.Id);
        }
 public Badge Create(Badge item)
 {
     Repo.Create(item);
     Unit.SaveChanges();
     return item;
 }
        private bool Evaluate(string accountId, Badge b, long activityId)
        {
            Target t = tRepo.Get(b.TargetId);
            Activity activity = actRepo.Get(activityId);
            List<Activity> activities = new List<Activity>();
            DateTime begin = activity.EndTime;
            DateTime end = begin;
            if (EqualActivityType(t.ActivityType, activity.Type))
            {
                if (b.Timeline == BadgeTimeline.SingleActivity)
                {
                    activities.Add(activity);
                }
                else if (b.Timeline == BadgeTimeline.Cumulative)
                {
                    activities = actRepo.GetByAccountAndType(accountId, activity.Type).ToList();
                }
                else
                {
                    if (b.Timeline == BadgeTimeline.Daily)
                    {
                        begin.AddDays(-1);
                    }
                    else if (b.Timeline == BadgeTimeline.Weekly)
                    {
                        begin.AddDays(-7);
                    }
                    else if (b.Timeline == BadgeTimeline.Monthly)
                    {
                        begin.AddMonths(-1);
                    }

                    if (t.ActivityType == TargetActivityType.General)
                    {
                        activities = actRepo.GetByDate(accountId, begin, end).ToList();
                    }
                    else
                    {
                        activities = actRepo.GetByDateAndType(accountId, begin, end, activity.Type).ToList();
                    }
                }
            }
            else
            {
                return false;
            }

            double total = 0;

            if (t.Type == TargetType.Duration)
            {
                foreach (Activity a in activities)
                {
                    total += a.Duration.TotalHours;
                }
            }
            else if (t.Type == TargetType.Distance)
            {
                foreach (Activity a in activities)
                {
                    total += a.Distance;
                }
            }
            else if (t.Type == TargetType.Steps)
            {
                foreach (Activity a in activities)
                {
                    total += a.Steps;
                }
            }

            if (total >= t.TargetNumber)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 public IHttpActionResult Post(Badge item)
 {
     Badge Created = Logic.Create(item);
     return Ok(Created);
 }