public async Task <IHttpActionResult> CreateAttemptAsync(GoalAttemptData attempt)
        {
            _log.LogDebug($"Request: {JsonConvert.SerializeObject(attempt)}");

            User user;

            try
            {
                user = await ControllerUtils.GetUserForRequestHeaderTokenAsync(this, _tokenStore, _userStore, _log);
            }
            catch (AuthenticationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (InternalServerException)
            {
                return(InternalServerError());
            }

            Goal goal = await _goalStore.GetGoalAsync(attempt.GoalId);

            if (goal == null || goal.UserId != user.Id)
            {
                _log.LogDebug($"Goal not found with id {attempt.GoalId} for user {user.Id}.");
                return(NotFound());
            }

            GoalAttempt newAttempt = await _attemptStore.CreateAttemptAsync(attempt.GoalId);

            _log.LogInfo($"Created goal-attempt for user {user.Id} : {newAttempt}");

            return(StatusCode(HttpStatusCode.Created));
        }
        //---------------------------------------------------------------------------------------------

        public async Task RemoveAttempt(int attemptId)
        {
            GoalAttempt attempt = await GoalAttempts.FirstOrDefaultAsync(a => a.Id == attemptId);

            if (attempt == null)
            {
                return;
            }

            GoalAttempts.Remove(attempt);
        }
Example #3
0
        //---------------------------------------------------------------------------------------------

        public async Task <GoalAttempt> CreateAttemptAsync(int goalId)
        {
            await ValidateGoalExists(goalId);

            var attempt = new GoalAttempt
            {
                GoalId    = goalId,
                Timestamp = _dateTimeSource.Now
            };

            _goalAttemptDb.AddAttempt(attempt);

            await _goalAttemptDb.SaveAsync();

            return(attempt);
        }
Example #4
0
        public async Task RemoveAttemptAsync_GivenAttempt_ShouldRemoveFromDb()
        {
            // Arrange.
            var attemptDb       = Substitute.For <IGoalAttemptDb>();
            var goalStore       = Substitute.For <IGoalStore>();
            var dateTimeSource  = Substitute.For <IDateTimeSource>();
            var testObject      = new GoalAttemptStore(attemptDb, goalStore, dateTimeSource);
            var attemptToRemove = new GoalAttempt();

            // Act.
            await testObject.RemoveAttemptAsync(123);

            // Assert.
            await attemptDb.Received(1).RemoveAttempt(123);

            await attemptDb.Received(1).SaveAsync();
        }
        public async Task <IHttpActionResult> DeleteAttemptAsync(int attemptId)
        {
            _log.LogDebug($"Request: attemptId={attemptId}");

            User user;

            try
            {
                user = await ControllerUtils.GetUserForRequestHeaderTokenAsync(this, _tokenStore, _userStore, _log);
            }
            catch (AuthenticationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (InternalServerException)
            {
                return(InternalServerError());
            }

            GoalAttempt attempt = await _attemptStore.GetAttemptAsync(attemptId);

            if (attempt == null)
            {
                _log.LogDebug($"Goal-attempt not found with id {attemptId} for user {user.Id}.");
                return(NotFound());
            }

            Goal goal = await _goalStore.GetGoalAsync(attempt.GoalId);

            if (goal == null || goal.UserId != user.Id)
            {
                _log.LogDebug($"Goal not found with id {attempt.GoalId} for user {user.Id}.");
                return(NotFound());
            }

            await _attemptStore.RemoveAttemptAsync(attemptId);

            _log.LogInfo($"Deleted goal-attempt {attemptId} for user {user.Id}.");

            return(Ok());
        }
Example #6
0
        public async Task CreateAttemptAsync_GivenValidAttempt_ShouldAddToDb()
        {
            // Arrange.
            var attemptDb      = Substitute.For <IGoalAttemptDb>();
            var goalStore      = Substitute.For <IGoalStore>();
            var dateTimeSource = Substitute.For <IDateTimeSource>();
            var testObject     = new GoalAttemptStore(attemptDb, goalStore, dateTimeSource);

            goalStore.GetGoalAsync(123).Returns(new Goal());
            dateTimeSource.Now.Returns(DateTime.Now);

            // Act.
            GoalAttempt attempt = await testObject.CreateAttemptAsync(123);

            // Assert.
            attemptDb.Received(1).AddAttempt(Arg.Any <GoalAttempt>());

            await attemptDb.Received(1).SaveAsync();

            Assert.AreEqual(123, attempt.GoalId);
            Assert.AreEqual(dateTimeSource.Now, attempt.Timestamp);
        }
        public async Task <IHttpActionResult> DeleteLastAttemptAsync(int goalId)
        {
            _log.LogDebug($"Request: goalId={goalId}");

            User user;

            try
            {
                user = await ControllerUtils.GetUserForRequestHeaderTokenAsync(this, _tokenStore, _userStore, _log);
            }
            catch (AuthenticationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (InternalServerException)
            {
                return(InternalServerError());
            }

            Goal goal = await _goalStore.GetGoalAsync(goalId);

            if (goal == null || goal.UserId != user.Id)
            {
                _log.LogDebug($"Goal not found with id {goalId} for user {user.Id}.");
                return(NotFound());
            }

            GoalAttempt lastAttempt =
                await _attemptStore.GetAttempts(goalId)
                .OrderByDescending(a => a.Timestamp)
                .FirstOrDefaultAsync();

            await _attemptStore.RemoveAttemptAsync(lastAttempt.Id);

            _log.LogInfo($"Deleted goal-attempt {lastAttempt.Id} for user {user.Id}.");

            return(Ok());
        }
        // IGoalAttemptDb =============================================================================

        public void AddAttempt(GoalAttempt goal)
        {
            GoalAttempts.Add(goal);
        }