コード例 #1
0
 public async Task AddAsync(Guid id, Guid userId,
                            string currency, string timeframe, IEnumerable <string> indicators,
                            string name, string description, DateTime createdAt)
 {
     var activity = new Entry(id, userId,
                              currency, timeframe, indicators,
                              name, description, createdAt);
     await _entryRepository.AddAsync(activity);
 }
コード例 #2
0
        [HttpPost] // @PostMapping
        public async Task <IActionResult> AddAsync([FromBody] Entry dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            // <>
            var temp = new Entry();

            temp.Name    = dto.Name;
            temp.Title   = dto.Title;
            temp.Content = dto.Content;
            temp.Created = DateTime.Now;
            // --TODO--
            // </>

            try
            {
                var model = await _repository.AddAsync(temp);

                if (model == null)
                {
                    return(BadRequest());
                }

                //[!] 다음 항목 중 원하는 방식 사용
                if (DateTime.Now.Second % 60 == 0)
                {
                    return(Ok(model)); // 200 OK
                }
                else if (DateTime.Now.Second % 3 == 0)
                {
                    return(CreatedAtRoute(nameof(GetEntryById), new { id = model.Id }, model)); // Status: 201 Created
                }
                else if (DateTime.Now.Second % 2 == 0)
                {
                    var uri = Url.Link(nameof(GetEntryById), new { id = model.Id });
                    return(Created(uri, model)); // 201 Created
                }
                else
                {
                    // GetById 액션 이름 사용해서 입력된 데이터 반환
                    return(CreatedAtAction(nameof(GetEntryById), new { id = model.Id }, model));
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(BadRequest());
            }
        }
コード例 #3
0
        public async Task HandleAsync(EntryCreated @event)
        {
            await _repository.AddAsync(new Entry
            {
                Id          = @event.Id,
                UserId      = @event.UserId,
                Currency    = @event.Currency,
                Timeframe   = @event.Timeframe,
                Indicators  = @event.Indicators,
                Name        = @event.Name,
                CreatedAt   = @event.CreatedAt,
                Description = @event.Description
            });

            Console.WriteLine($"Entry created: {@event.Name}");
        }
コード例 #4
0
        public async Task <Guid> Handle(CreateEntryCommand request, CancellationToken cancellationToken)
        {
            var validator        = new CreateEntryCommandValidator(_entryRepository);
            var validationResult = await validator.ValidateAsync(request);

            if (validationResult.Errors.Count > 0)
            {
                throw new Exceptions.ValidationException(validationResult);
            }

            var @entry = _mapper.Map <Entry>(request);

            @entry = await _entryRepository.AddAsync(@entry);


            return(@entry.EntryId);
        }
コード例 #5
0
        public async Task <EntryResponse> SaveAsync(Entry entry)
        {
            try
            {
                var existingPhonebook = await _phonebookRepository.FindByIdAsync(entry.PhoneBookId);

                if (existingPhonebook == null)
                {
                    return(new EntryResponse("Invalid phonebook."));
                }

                await _entryRepository.AddAsync(entry);

                await _unitOfWork.CompleteAsync();

                return(new EntryResponse(entry));
            }
            catch (Exception ex)
            {
                // Do some logging stuff
                return(new EntryResponse($"An error occurred when saving the entry: {ex.Message}"));
            }
        }
コード例 #6
0
ファイル: CreateEntryHandler.cs プロジェクト: tuga1975/Depot
        public async Task HandleAsync(CreateEntry command)
        {
            if (string.IsNullOrWhiteSpace(command.Key))
            {
                await PublishError(command.Key, "Entry key can not be empty.");

                return;
            }
            var entry = await _repository.GetAsync(command.Key);

            if (entry != null)
            {
                await PublishError(command.Key, $"Entry with key: '{command.Key}' already exists.");

                return;
            }
            await _repository.AddAsync(new Entry(command.Key, command.Value));

            await _cache.SetStringAsync(command.Key, JsonConvert.SerializeObject(command.Value),
                                        new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(10)));

            Console.WriteLine($"Created a new entry with key: '{command.Key}'.");
            await _busClient.PublishAsync(new EntryCreated(command.Key));
        }
コード例 #7
0
        public async Task <IActionResult> Post(long Id, [FromBody] AddEntryViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }
            if (model == null)
            {
                return(BadRequest());
            }

            if (_periodManager.GetPeriod() == Guid.Empty)
            {
                ModelState.AddModelError("", Resources.Global.Common.FinancialPeriodMustBeSpecified);
                return(BadRequest(ModelState.GetWithErrorsKey()));
            }

            var financialPeriod = await _financialPeriodRepo.FindAsync(_periodManager.GetPeriod());

            if (financialPeriod == null)
            {
                return(NotFound("FinancialPeriodNotFound"));
            }
            if (!financialPeriod.CheckIfDateInPeriod(model.Date))
            {
                ModelState.AddModelError("Date", Resources.Global.Common.DateOutCurrenctPeriod);
                return(BadRequest(ModelState.GetWithErrorsKey()));
            }

            #region checks

            Guid currencyId;
            var  currency = await _currencyRepo.GetAsync(model.CurrencyId);

            if (currency != null)
            {
                currencyId          = currency.Id;
                model.CurrencyValue = currency.Value;
            }
            else
            {
                currencyId          = _defaultKeysOptions.Value.CurrencyId;
                model.CurrencyValue = 1;
            }

            Guid?costCenterId = null;
            if (model.CostCenterId.HasValue)
            {
                var costCenter = await _costCenterRepo.GetAsync(model.CostCenterId.Value);

                if (costCenter == null)
                {
                    return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound));
                }
                costCenterId = costCenter.Id;
            }

            Guid?branchId = null;
            if (model.BranchId.HasValue)
            {
                var branch = await _branchRepo.GetAsync(model.BranchId.Value);

                if (branch == null)
                {
                    return(NotFound(Resources.Branchs.BranchResource.BranchNotFound));
                }
                branchId = branch.Id;
            }

            // get new number value if the value from model is exist
            if (!await _entryRepo.CheckIfNotExistAsync(model.Number))
            {
                model.Number = await _entryRepo.GetNextNumberAsync();
            }

            #endregion

            var entry = new Entry(model.Number, currencyId, model.CurrencyValue, branchId, model.Note);

            var itemsIndex = 0;
            // check form items if not found
            foreach (var item in model.Items)
            {
                itemsIndex++;

                Guid accountId;
                var  account = await _accountRepo.GetAsync(item.AccountId);

                if (account == null)
                {
                    return(NotFound($"account in entry item {itemsIndex} not found"));
                }
                accountId = account.Id;

                Guid itemCurrencyId;
                if (model.CurrencyId == item.CurrencyId)
                {
                    itemCurrencyId     = currencyId;
                    item.CurrencyValue = model.CurrencyValue;
                }
                else
                {
                    var itemCurrency = await _currencyRepo.GetAsync(item.CurrencyId.Value);

                    if (itemCurrency != null)
                    {
                        itemCurrencyId     = itemCurrency.Id;
                        item.CurrencyValue = itemCurrency.Value;
                    }
                }

                Guid?itemCostCenterId = null;
                if (model.CostCenterId.HasValue && item.CostCenterId.HasValue && model.CostCenterId == item.CostCenterId)
                {
                    itemCostCenterId = costCenterId;
                }
                else
                {
                    if (item.CostCenterId.HasValue)
                    {
                        var costCenter = await _costCenterRepo.GetAsync(item.CostCenterId.Value);

                        if (costCenter == null)
                        {
                            return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound));
                        }
                        itemCostCenterId = costCenter.Id;
                    }
                }

                var entryItem = new EntryItem(accountId, item.Debit, item.Credit, itemCurrencyId, item.CurrencyValue.Value, costCenterId, item.Date.Value.UtcDateTime, item.Note);
                entry.Items.Add(entryItem);
            }
            await _accountBalanceService.PostEntryToAccounts(entry);

            var affectedRows = await _entryRepo.AddAsync(entry);

            if (affectedRows > 0)
            {
                var viewModel = AutoMapper.Mapper.Map <EntryViewModel>(entry);
                return(CreatedAtRoute("GetEntry", new { id = entry.Number }, viewModel));
            }

            return(BadRequest());
        }
コード例 #8
0
        public async Task <IActionResult> Post(long id, [FromBody] AddPayEntryViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }
            if (id != model.PayTypeId)
            {
                return(BadRequest());
            }

            var payType = await _payTypeRep.GetAsync(id);

            if (payType == null)
            {
                return(NotFound("Not Found !"));
            }

            if (_periodManager.GetPeriod() == Guid.Empty)
            {
                ModelState.AddModelError("", Resources.Global.Common.FinancialPeriodMustBeSpecified);
                return(BadRequest(ModelState.GetWithErrorsKey()));
            }

            var financialPeriod = await _financialPeriodRepo.FindAsync(_periodManager.GetPeriod());

            if (financialPeriod == null)
            {
                return(NotFound("FinancialPeriodNotFound"));
            }
            if (!financialPeriod.CheckIfDateInPeriod(model.Date))
            {
                ModelState.AddModelError("Date", Resources.Global.Common.DateOutCurrenctPeriod);
                return(BadRequest(ModelState.GetWithErrorsKey()));
            }
            var debitSum  = payType.ShowItemDebitField ? model.Items.Sum(p => p.Debit) : 0;
            var creditSum = payType.ShowItemDebitField ? model.Items.Sum(p => p.Credit) : 0;

            Guid?payAccountId = null;
            var  payAccount   = await _accountRepo.GetAsync(model.PayAccountId.Value);

            if (payAccount != null)
            {
                payAccountId = payAccount.Id;
            }

            #region checks

            var ShowItemDebitField  = model.Items.ToList().All(e => e.Debit != 0);
            var ShowItemCreditField = model.Items.ToList().All(e => e.Credit != 0);

            if (payAccount != null && ((ShowItemDebitField && creditSum != 0) || (ShowItemCreditField && debitSum != 0)))
            {
                return(NotFound("Error in writing entry Details "));
            }
            if (ShowItemDebitField && ShowItemCreditField && payAccount != null)
            {
                return(NotFound("Error payAccount must be leaved empty in Daily or Beginning Entry ! "));
            }
            if ((payType.ShowItemDebitField && payType.ShowItemCreditField) && payAccount == null)
            {
                var isBalance = debitSum == creditSum ? true : false;
                return(NotFound("Entry is not Balanced ! "));
            }
            if ((ShowItemDebitField || ShowItemCreditField) && payAccount == null)
            {
                return(NotFound("Error payAccount must be Filled !! "));
            }

            Guid currencyId;
            var  currency = await _currencyRepo.GetAsync(model.CurrencyId);

            if (currency != null)
            {
                currencyId          = currency.Id;
                model.CurrencyValue = currency.Value;
            }
            else
            {
                currencyId          = _defaultKeysOptions.Value.CurrencyId;
                model.CurrencyValue = 1;
            }

            Guid?costCenterId = null;
            if (model.CostCenterId.HasValue)
            {
                var costCenter = await _costCenterRepo.GetAsync(model.CostCenterId.Value);

                if (costCenter == null)
                {
                    return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound));
                }
                costCenterId = costCenter.Id;
            }

            Guid?branchId = null;
            if (model.BranchId.HasValue)
            {
                var branch = await _branchRepo.GetAsync(model.BranchId.Value);

                if (branch == null)
                {
                    return(NotFound(Resources.Branchs.BranchResource.BranchNotFound));
                }
                branchId = branch.Id;
            }



            #endregion

            long entryNumber = await _entryRepo.GetNextNumberAsync();

            var entry = new Entry(entryNumber, currencyId, model.CurrencyValue, branchId, model.Note);


            if ((payType.ShowItemDebitField && payType.ShowItemCreditField))
            {
                var itemsIndex = 0;
                // check form items if not found
                foreach (var item in model.Items)
                {
                    itemsIndex++;

                    Guid account1Id;
                    var  account1 = await _accountRepo.GetAsync(item.AccountId);

                    if (account1 == null)
                    {
                        return(NotFound($"account in entry item {itemsIndex} not found"));
                    }
                    account1Id = account1.Id;



                    Guid itemCurrencyId;
                    if (model.CurrencyId == item.CurrencyId)
                    {
                        itemCurrencyId     = currencyId;
                        item.CurrencyValue = model.CurrencyValue;
                    }
                    else
                    {
                        var itemCurrency = await _currencyRepo.GetAsync(item.CurrencyId.Value);

                        if (itemCurrency != null)
                        {
                            itemCurrencyId     = itemCurrency.Id;
                            item.CurrencyValue = itemCurrency.Value;
                        }
                    }

                    Guid?itemCostCenterId = null;
                    if (model.CostCenterId.HasValue && item.CostCenterId.HasValue && model.CostCenterId == item.CostCenterId)
                    {
                        itemCostCenterId = costCenterId;
                    }
                    else
                    {
                        if (item.CostCenterId.HasValue)
                        {
                            var costCenter = await _costCenterRepo.GetAsync(item.CostCenterId.Value);

                            if (costCenter == null)
                            {
                                return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound));
                            }
                            itemCostCenterId = costCenter.Id;
                        }
                    }

                    var entryItem = new EntryItem(account1Id, item.Debit, item.Credit, itemCurrencyId, item.CurrencyValue.Value, costCenterId, item.Date.Value.UtcDateTime, item.Note);
                    entry.Items.Add(entryItem);
                }
            }
            else if (payType.ShowItemDebitField || payType.ShowItemCreditField)
            {
                var itemsIndex = 0;
                // check form items if not found
                foreach (var item in model.Items)
                {
                    itemsIndex++;

                    Guid account1Id;
                    var  account1 = await _accountRepo.GetAsync(item.AccountId);

                    if (account1 == null)
                    {
                        return(NotFound($"account in entry item {itemsIndex} not found"));
                    }
                    account1Id = account1.Id;



                    Guid itemCurrencyId;
                    if (model.CurrencyId == item.CurrencyId)
                    {
                        itemCurrencyId     = currencyId;
                        item.CurrencyValue = model.CurrencyValue;
                    }
                    else
                    {
                        var itemCurrency = await _currencyRepo.GetAsync(item.CurrencyId.Value);

                        if (itemCurrency != null)
                        {
                            itemCurrencyId     = itemCurrency.Id;
                            item.CurrencyValue = itemCurrency.Value;
                        }
                    }

                    Guid?itemCostCenterId = null;
                    if (model.CostCenterId.HasValue && item.CostCenterId.HasValue && model.CostCenterId == item.CostCenterId)
                    {
                        itemCostCenterId = costCenterId;
                    }
                    else
                    {
                        if (item.CostCenterId.HasValue)
                        {
                            var costCenter = await _costCenterRepo.GetAsync(item.CostCenterId.Value);

                            if (costCenter == null)
                            {
                                return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound));
                            }
                            itemCostCenterId = costCenter.Id;
                        }
                    }


                    var entryItem = new EntryItem(account1Id, item.Debit, item.Credit, itemCurrencyId, item.CurrencyValue.Value, costCenterId, item.Date.Value.UtcDateTime, item.Note);

                    entry.Items.Add(entryItem);
                }

                var entryItem1 = new EntryItem(payAccountId.Value, creditSum, debitSum, currencyId, model.CurrencyValue, costCenterId, model.Date.UtcDateTime, model.Note);
                entry.Items.Add(entryItem1);
            }
            entry.PayEntry = new PayEntry
            {
                Number       = await _payEntryRepo.GetNextNumberAsync(payType.Id),
                PayAccountId = payAccountId.Value,
                PayTypeId    = payType.Id,
            };

            if (payType.AutoPostToAccounts)
            {
                await _accountBalanceService.PostEntryToAccounts(entry);
            }

            var affectedRows = await _entryRepo.AddAsync(entry);

            if (affectedRows > 0)
            {
                var viewModel = AutoMapper.Mapper.Map <EntryViewModel>(entry);
                return(CreatedAtRoute("GetPayEntry", new { id = entry.Number }, viewModel));
            }

            return(BadRequest());
        }