예제 #1
0
        /// <summary>
        /// Add a spending for a user
        /// </summary>
        /// <param name="userId">Id of the user owning the spending we want to create</param>
        /// <param name="dateInUtc">Date of the spending in utc</param>
        /// <param name="amount">Amount of the spending</param>
        /// <param name="isoCurrencySymbol">Three-character ISO 4217 currency symbol of this spending</param>
        /// <param name="nature">Nature of the spending</param>
        /// <param name="comment">Description of the comment</param>
        /// <returns>A 400 error if the request was incorrect, else return 200</returns>
        public IActionResult AddSpending(int userId, DateTime dateInUtc, decimal amount, string isoCurrencySymbol, string nature, string comment)
        {
            Nature?natureEnum = nature.ToNature();

            // Unknown nature
            if (natureEnum == null)
            {
                return(BadRequest());
            }

            SpendingCreationError verificationResult = this._spendingService.TryCreateSpending(userId, dateInUtc, amount, isoCurrencySymbol, natureEnum.Value, comment);

            if (verificationResult != SpendingCreationError.None)
            {
                return(BadRequest());
            }

            return(Ok());
        }
예제 #2
0
        /// <summary>
        /// Checks if the data we have are correct for a spending creation
        /// </summary>
        /// <param name="user">User owning the spending we want to create</param>
        /// <param name="dateInUtc">Date of the spending</param>
        /// <param name="amount">Amount of the spending</param>
        /// <param name="isoCurrencySymbol">Three-character ISO 4217 currency symbol of the spending</param>
        /// <param name="comment">Description of the comment</param>
        /// <returns>A flag indicating errors (if any)</returns>
        private SpendingCreationError VerifySpendingBeforeCreation(User user, DateTime dateInUtc, decimal amount, string isoCurrencySymbol, string comment)
        {
            SpendingCreationError returnValue = SpendingCreationError.None;

            if (dateInUtc.IsInTheFuture())
            {
                returnValue |= SpendingCreationError.SpendingDateInTheFuture;
            }
            if (dateInUtc.IsExpired())
            {
                returnValue |= SpendingCreationError.SpendingDateExpired;
            }
            if (string.IsNullOrEmpty(comment))
            {
                returnValue |= SpendingCreationError.MissingOrEmptyComment;
            }
            if (amount <= 0)
            {
                returnValue |= SpendingCreationError.SpendingAmountBelow0;
            }

            if (user == null)
            {
                returnValue |= SpendingCreationError.UserNotFound;
            }

            if (user != null && isoCurrencySymbol != user?.ISOCurrencySymbol)
            {
                returnValue |= SpendingCreationError.SpendingCurrencyDiscrepancy;
            }

            if (user != null && this._spendingRepository.SpendingExists(user.UserId, dateInUtc, amount))
            {
                returnValue |= SpendingCreationError.DuplicateSpending;
            }

            return(returnValue);
        }
예제 #3
0
        /// <summary>
        /// Add a spending for a user
        /// </summary>
        /// <param name="userId">Id of the user owning the spending we want to create</param>
        /// <param name="dateInUtc">Date of the spending in utc</param>
        /// <param name="amount">Amount of the spending</param>
        /// <param name="isoCurrencySymbol">Three-character ISO 4217 currency symbol of this spending</param>
        /// <param name="nature">Nature of the spending</param>
        /// <param name="comment">Description of the comment</param>
        /// <returns>A flag indicating errors (if any) in the query</returns>
        public SpendingCreationError TryCreateSpending(int userId, DateTime dateInUtc, decimal amount, string isoCurrencySymbol, Nature nature, string comment)
        {
            User     user            = this._spendingRepository.LoadUserById(userId);
            DateTime meaningfullDate = ExtractDate(dateInUtc);

            SpendingCreationError verificationResult = VerifySpendingBeforeCreation(user, meaningfullDate, amount, isoCurrencySymbol, comment);

            if (verificationResult == SpendingCreationError.None)
            {
                Spending spendingToCreate = new Spending();
                spendingToCreate.User   = user;
                spendingToCreate.UserId = user.UserId;

                spendingToCreate.Amount            = amount;
                spendingToCreate.Comment           = comment;
                spendingToCreate.DateInUtc         = meaningfullDate;
                spendingToCreate.ISOCurrencySymbol = isoCurrencySymbol;
                spendingToCreate.Nature            = nature;

                this._spendingRepository.AddSpending(spendingToCreate);
            }

            return(verificationResult);
        }
예제 #4
0
        public void AddSpending(int userId, int monthToAdd, decimal amount, string currency, Nature nature, string comment, SpendingCreationError expectedOutcome)
        {
            // Arrange
            User ironMan      = new User("Stark", "Anthony", new RegionInfo("US").ISOCurrencySymbol);
            User userNotFound = null;

            Mock <ISpendingRepository> spendingRepositoryMock = new Mock <ISpendingRepository>(MockBehavior.Strict);

            spendingRepositoryMock.Setup(r => r.AddSpending(It.IsAny <Spending>())).Callback <Spending>(s => CheckDateAreCorrect(s.DateInUtc));
            spendingRepositoryMock.Setup(r => r.LoadUserById(1)).Returns(ironMan);
            spendingRepositoryMock.Setup(r => r.LoadUserById(3)).Returns(userNotFound);

            spendingRepositoryMock.Setup(r => r.SpendingExists(It.IsAny <int>(), It.IsAny <DateTime>(), 42)).Returns(true);
            spendingRepositoryMock.Setup(r => r.SpendingExists(It.IsAny <int>(), It.IsAny <DateTime>(), 100)).Returns(false);
            spendingRepositoryMock.Setup(r => r.SpendingExists(It.IsAny <int>(), It.IsAny <DateTime>(), -100)).Returns(false);


            // Act
            // Adding/removing months to today
            DateTime              spendingDate = DateTime.Today.AddMonths(monthToAdd);
            ISpendingService      sut          = new SpendingService(spendingRepositoryMock.Object);
            SpendingCreationError outcome      = sut.TryCreateSpending(userId, spendingDate, amount, currency, nature, comment);

            // Assert
            Assert.Equal(expectedOutcome, outcome);

            Assert.Equal(expectedOutcome, outcome);
            if (expectedOutcome == SpendingCreationError.None)
            {
                spendingRepositoryMock.Verify(s => s.AddSpending(It.IsAny <Spending>()), Times.Once);
            }
            else
            {
                spendingRepositoryMock.Verify(s => s.AddSpending(It.IsAny <Spending>()), Times.Never);
            }
        }