Exemple #1
0
        public async void Add_Duplicate_Ledger_WithCode()
        {
            using (var scope = Factory.Services.CreateScope())
            {
                var mapper          = scope.ServiceProvider.GetRequiredService <IMapper>();
                var responseService = scope.ServiceProvider.GetRequiredService <IResponseService <LedgerDto> >();
                var ledgerDto       = new LedgerDto
                {
                    Code     = RandomData.IntGenerate().ToString(),
                    Title    = RandomData.StringGenerator(),
                    IsActive = true
                };


                var legerRepositoryMock = new Mock <ILegerRepository>();
                var unitOfWorkMock      = new Mock <IUnitOfWork>();

                legerRepositoryMock.Setup(c => c.ExistsAsync(It.IsAny <Expression <Func <Ledger, bool> > >()))
                .ReturnsAsync((Expression <Func <Ledger, bool> > pre) => { return(true); });


                unitOfWorkMock.Setup(c => c.SaveChangesAsync()).ReturnsAsync(1);

                var ledgerService = new LedgerService(legerRepositoryMock.Object, mapper,
                                                      responseService, unitOfWorkMock.Object);

                var result = await ledgerService.AddAsync(ledgerDto);

                result.ApplicationMessage.Should().BeEquivalentTo(UserMessage.Duplicated);
            }
        }
        public async Task <ResponseDto <LedgerDto> > AddAsync(LedgerDto ledgerDto)
        {
            var ledger = _mapper.Map <LedgerDto, Ledger>(ledgerDto);

            var duplicateCode = await _legerRepository.ExistsAsync(c => c.Code == ledger.Code);

            if (duplicateCode)
            {
                return(_responseService.Response(ledgerDto, UserMessage.Duplicated));
            }


            var duplicateTitle = await _legerRepository.ExistsAsync(c => c.Title == ledger.Title);

            if (duplicateTitle)
            {
                throw new DuplicateException(UserMessage.Duplicated + " Title");
            }


            try
            {
                await _legerRepository.AddAsync(ledger);

                await _unitOfWork.SaveChangesAsync();

                return(_responseService.Response(ledgerDto, UserMessage.Success));
            }
            catch (Exception e)
            {
                var lastException = e.GetLastException();
                return(_responseService.Response(ledgerDto, lastException.ReturnList(), UserMessage.Failed));
            }
        }
Exemple #3
0
        public async void Add_New_Ledger()
        {
            using (var scope = Factory.Services.CreateScope())
            {
                var mapper          = scope.ServiceProvider.GetRequiredService <IMapper>();
                var responseService = scope.ServiceProvider.GetRequiredService <IResponseService <LedgerDto> >();
                var ledgerDto       = new LedgerDto
                {
                    Code     = RandomData.IntGenerate().ToString(),
                    Title    = RandomData.StringGenerator(),
                    IsActive = true
                };
                var ledger = mapper.Map <LedgerDto, Ledger>(ledgerDto);
                var legerRepositoryMock = new Mock <ILegerRepository>();

                var unitOfWorkMock = new Mock <IUnitOfWork>();

                legerRepositoryMock.Setup(c => c.AddAsync(ledger)).Returns(Task.CompletedTask);
                unitOfWorkMock.Setup(c => c.SaveChangesAsync()).ReturnsAsync(1);


                var ledgerService = new LedgerService(legerRepositoryMock.Object, mapper,
                                                      responseService, unitOfWorkMock.Object);

                var result = await ledgerService.AddAsync(ledgerDto);

                result.ApplicationMessage.Should().BeEquivalentTo(UserMessage.Success);
            }
        }
Exemple #4
0
        public SingleObjectModel GetLedger([FromUri] string ledgerUID)
        {
            using (var usecases = LedgerUseCases.UseCaseInteractor()) {
                LedgerDto ledger = usecases.GetLedger(ledgerUID);

                return(new SingleObjectModel(base.Request, ledger));
            }
        }
Exemple #5
0
        IEnumerable <LedgerDto> ILedgerParser.Parse(TextReader reader)
        {
            var configuration = new Configuration(CultureInfo.InvariantCulture);

            configuration.HasHeaderRecord = true;

            int lineIndex = 0;

            using (var csv = new CsvReader(reader, configuration)) {
                csv.Read();
                csv.ReadHeader();

                while (csv.Read())
                {
                    lineIndex++;

                    string transField = csv.GetField("Trans #");
                    if (!int.TryParse(transField, NumberStyles.Integer | NumberStyles.AllowThousands, NumberFormatInfo.InvariantInfo, out int trans))
                    {
                        Log.Logger.Write(LogEventLevel.Error, $"CSV error in line: {lineIndex}. Unable to parse Trans #:'{transField}' to number.");
                    }

                    string dateField = csv.GetField("Date");
                    if (!DateTime.TryParse(dateField, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out DateTime date))
                    {
                        Log.Logger.Write(LogEventLevel.Error, $"CSV error in line: {lineIndex}. Unable to parse Date:'{dateField}' to date.");
                    }

                    string numField = csv.GetField("Num");
                    int    num      = 0;
                    if (!string.IsNullOrEmpty(numField))
                    {
                        if (!int.TryParse(numField, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out num))
                        {
                            Log.Logger.Write(LogEventLevel.Error, $"CSV error in line: {lineIndex}. Unable to parse Num:'{numField}' to number.");
                        }
                    }

                    string debitField = csv.GetField("Debit");
                    if (!double.TryParse(debitField, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out double debit))
                    {
                        Log.Logger.Write(LogEventLevel.Error, $"CSV error in line: {lineIndex}. Unable to parse Debit:'{debitField}' to decimal.");
                    }

                    string creditField = csv.GetField("Credit");
                    if (!double.TryParse(creditField, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out double credit))
                    {
                        Log.Logger.Write(LogEventLevel.Error, $"CSV error in line: {lineIndex}. Unable to parse Credit:'{creditField}' to decimal.");
                    }

                    LedgerType type = LedgerTypeHelper.Parse(csv.GetField("Type"));

                    var dto = new LedgerDto(
                        trans: trans,
                        type: type,
                        date: date,
                        num: num,
                        name: csv.GetField("Name"),
                        memo: csv.GetField("Memo"),
                        account: csv.GetField("Account"),
                        debit: debit,
                        credit: credit
                        );

                    yield return(dto);
                }
            }
        }
Exemple #6
0
        public void Should_Get_A_Ledger()
        {
            LedgerDto ledger = _usecases.GetLedger(TestingConstants.LEDGER_UID);

            Assert.Equal(TestingConstants.LEDGER_UID, ledger.UID);
        }
 internal static bool IsFirstLine(this LedgerDto dto)
 => dto.Trans > 0;
 public async Task <ResponseDto <LedgerDto> > PostAsync(LedgerDto ledgerDto)
 {
     return(await _ledgerService.AddAsync(ledgerDto));
 }