protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            if (modelBuilder == null)
            {
                throw new ArgumentNullException(nameof(modelBuilder));
            }

            modelBuilder.Entity <BankAccountEntity>().ToTable("BankAccount").Property(x => x.Id).HasAnnotation("MySql:ValueGeneratedOnAdd", MySqlValueGenerationStrategy.IdentityColumn).ValueGeneratedOnAdd();
            modelBuilder.Entity <SinanceUserEntity>().ToTable("Users").Property(x => x.Id).HasAnnotation("MySql:ValueGeneratedOnAdd", MySqlValueGenerationStrategy.IdentityColumn).ValueGeneratedOnAdd();
            modelBuilder.Entity <CategoryEntity>().ToTable("Category").Property(x => x.Id).HasAnnotation("MySql:ValueGeneratedOnAdd", MySqlValueGenerationStrategy.IdentityColumn).ValueGeneratedOnAdd();

            modelBuilder.Entity <CategoryMappingEntity>().ToTable("CategoryMapping").Property(x => x.Id).HasAnnotation("MySql:ValueGeneratedOnAdd", MySqlValueGenerationStrategy.IdentityColumn).ValueGeneratedOnAdd();
            modelBuilder.Entity <TransactionEntity>().ToTable("Transaction").Property(x => x.Id).HasAnnotation("MySql:ValueGeneratedOnAdd", MySqlValueGenerationStrategy.IdentityColumn).ValueGeneratedOnAdd();
            modelBuilder.Entity <TransactionCategoryEntity>().ToTable("TransactionCategory").Property(x => x.Id).HasAnnotation("MySql:ValueGeneratedOnAdd", MySqlValueGenerationStrategy.IdentityColumn).ValueGeneratedOnAdd();
            modelBuilder.Entity <CustomReportEntity>().ToTable("CustomReport").Property(x => x.Id).HasAnnotation("MySql:ValueGeneratedOnAdd", MySqlValueGenerationStrategy.IdentityColumn).ValueGeneratedOnAdd();
            modelBuilder.Entity <CustomReportCategoryEntity>().ToTable("CustomReportCategory").Property(x => x.Id).HasAnnotation("MySql:ValueGeneratedOnAdd", MySqlValueGenerationStrategy.IdentityColumn).ValueGeneratedOnAdd();

            modelBuilder.Entity <BankAccountEntity>().HasQueryFilter(x => x.UserId == _userIdProvider.GetCurrentUserId());
            modelBuilder.Entity <CategoryEntity>().HasQueryFilter(x => x.UserId == _userIdProvider.GetCurrentUserId());
            modelBuilder.Entity <CategoryMappingEntity>().HasQueryFilter(x => x.UserId == _userIdProvider.GetCurrentUserId());
            modelBuilder.Entity <TransactionEntity>().HasQueryFilter(x => x.UserId == _userIdProvider.GetCurrentUserId());
            modelBuilder.Entity <CustomReportEntity>().HasQueryFilter(x => x.UserId == _userIdProvider.GetCurrentUserId());

            modelBuilder.Entity <CategoryEntity>().HasIndex(x => x.ShortName).IsUnique(true);
            modelBuilder.Entity <CategoryEntity>().HasIndex(x => x.Name).IsUnique(true);

            modelBuilder.Entity <BankAccountEntity>().HasIndex(x => x.Name).IsUnique(true);

            base.OnModelCreating(modelBuilder);
        }
Exemple #2
0
        public async Task <ImportModel> CreateImportPreview(Stream fileStream, ImportModel model)
        {
            var importCacheKey = CreateImportCacheKey(_userIdProvider.GetCurrentUserId());

            SinanceCacheHandler.ClearCache(importCacheKey);

            using var unitOfWork = _unitOfWork();

            var bankAccount = await unitOfWork.BankAccountRepository.FindSingle(x => x.Id == model.BankAccountId);

            if (bankAccount == null)
            {
                throw new NotFoundException(nameof(BankAccountEntity));
            }

            try
            {
                model.ImportRows = await _bankFileImportHandler.CreateImportRowsFromFile(
                    unitOfWork,
                    fileInputStream : fileStream,
                    userId : bankAccount.UserId,
                    fileImporterId : model.BankFileImporterId,
                    bankAccountId : bankAccount.Id);

                // Place the import model in the cache for easy acces later
                SinanceCacheHandler.Cache(key: importCacheKey,
                                          contentAction: () => model,
                                          slidingExpiration: false,
                                          expirationTimeSpan: new TimeSpan(0, 0, 15, 0));

                return(model);
            }
            catch (Exception exc)
            {
                throw new ImportFileException("Unexpected error while importing", exc);
            }
        }
        public async Task <CustomReportModel> CreateCustomReport(CustomReportModel model)
        {
            using var unitOfWork = _unitOfWork();

            await ValidateModelCategories(model, unitOfWork);

            var entity = model.ToNewEntity(_userIdProvider.GetCurrentUserId());

            unitOfWork.CustomReportRepository.Insert(entity);
            await unitOfWork.SaveAsync();

            // Reselect to get the categories included
            entity = await FindCustomReportWithCategories(entity.Id, unitOfWork);

            return(entity.ToDto());
        }
Exemple #4
0
        public async Task <CategoryModel> CreateCategoryForCurrentUser(CategoryModel categoryModel)
        {
            using var unitOfWork = _unitOfWork();
            var category = await unitOfWork.CategoryRepository.FindSingleTracked(item => item.Name == categoryModel.Name);

            if (category != null)
            {
                throw new AlreadyExistsException(nameof(CategoryEntity));
            }

            var newCategory = categoryModel.ToNewEntity(_userIdProvider.GetCurrentUserId());

            unitOfWork.CategoryRepository.Insert(newCategory);
            await unitOfWork.SaveAsync();

            return(newCategory.ToDto());
        }
Exemple #5
0
        public async Task <BankAccountModel> CreateBankAccountForCurrentUser(BankAccountModel model)
        {
            using var unitOfWork = _unitOfWork();
            var bankAccount = await unitOfWork.BankAccountRepository.FindSingle(x => x.Name == model.Name);

            if (bankAccount != null)
            {
                throw new AlreadyExistsException(nameof(BankAccountEntity));
            }

            var bankAccountEntity = model.ToNewEntity(_userIdProvider.GetCurrentUserId());

            unitOfWork.BankAccountRepository.Insert(bankAccountEntity);
            await unitOfWork.SaveAsync();

            return(bankAccountEntity.ToDto());
        }
        public async Task <TransactionModel> CreateTransactionForCurrentUser(TransactionModel transactionModel)
        {
            using var unitOfWork = _unitOfWork();

            var bankAccount = await unitOfWork.BankAccountRepository.FindSingleTracked(x => x.Id == transactionModel.BankAccountId);

            if (bankAccount == null)
            {
                throw new NotFoundException(nameof(BankAccountEntity));
            }

            var transactionEntity = transactionModel.ToNewEntity(_userIdProvider.GetCurrentUserId());

            unitOfWork.TransactionRepository.Insert(transactionEntity);
            await unitOfWork.SaveAsync();

            // First save the transaction, then recalculate the balance.
            bankAccount.CurrentBalance = await _bankAccountCalculationService.CalculateCurrentBalanceForBankAccount(unitOfWork, bankAccount);

            await unitOfWork.SaveAsync();

            return(transactionEntity.ToDto());
        }
        public async Task <CategoryMappingModel> CreateCategoryMappingForCurrentUser(CategoryMappingModel model)
        {
            using var unitOfWork = _unitOfWork();

            var existingCategoryMapping = await unitOfWork.CategoryMappingRepository.FindSingle(findQuery : x =>
                                                                                                x.ColumnTypeId == model.ColumnTypeId &&
                                                                                                x.MatchValue == model.MatchValue &&
                                                                                                x.CategoryId == model.CategoryId);

            if (existingCategoryMapping?.MatchValue.Equals(model.MatchValue, StringComparison.InvariantCultureIgnoreCase) == true)
            {
                throw new AlreadyExistsException(nameof(CategoryMappingEntity));
            }

            var entity = model.ToNewEntity(_userIdProvider.GetCurrentUserId());

            unitOfWork.CategoryMappingRepository.Insert(entity);

            await unitOfWork.SaveAsync();

            var insertedCategoryMapping = await FindCategoryMapping(entity.Id, unitOfWork);

            return(insertedCategoryMapping.ToDto());
        }