示例#1
0
        public ExpenseDto GetExpense(string content)
        {
            ValidateContent(content);

            var totalTagContent   = GetTotalTagContent(content);
            var total             = decimal.Parse(totalTagContent); // It is safe to parse total directly. It has passed validation.
            var gstExclusiveTotal = _gstCalculateService.GetGstExclusiveTotal(total);

            var expenseTagContent = GetExpenseTagContent(content);
            var costCentre        = GetTagContent(Tags.CostCentre, expenseTagContent);
            var paymentMethod     = GetTagContent(Tags.PaymentMethod, expenseTagContent);

            var vendor      = GetTagContent(Tags.Vendor, content);
            var description = GetTagContent(Tags.Description, content);

            var expenseViewModel = new ExpenseDto
            {
                CostCentre        = string.IsNullOrEmpty(costCentre) ? CostCentres.Default : HttpUtility.HtmlEncode(costCentre),
                Total             = total,
                GstExclusiveTotal = gstExclusiveTotal,
                PaymentMethod     = HttpUtility.HtmlEncode(paymentMethod),
                Vendor            = HttpUtility.HtmlEncode(vendor),
                Description       = HttpUtility.HtmlEncode(description)
            };

            var dateTagContent = GetTagContent(Tags.Date, content);

            if (string.IsNullOrEmpty(dateTagContent))
            {
                return(expenseViewModel);
            }

            if (DateTime.TryParse(dateTagContent, out var date))
            {
                expenseViewModel.Date = date;
            }
            else
            {
                throw new InvalidContentException($"Invalid {Tags.Date}: {dateTagContent}");
            }
            return(expenseViewModel);
        }
        public void GetGstExclusiveTotal_ReturnsCorrectValue()
        {
            var total = _gstCalculateService.GetGstExclusiveTotal(115m);

            Assert.AreEqual(100, total);
        }