public override Response Add(Dto.Transaction dto)
        {
            var resp = _customerBusiness.Get(dto.CustomerId);

            if (resp.Type != ResponseType.Success)
            {
                return(resp);
            }

            var customer = resp.Data;

            SetCustomerBalance(customer, dto.IsDebt, dto.Amount);

            customer.User = null; // prevent updating users table

            _customerBusiness.OwnerId = OwnerId;

            using (var tx = Uow.BeginTransaction())
            {
                //update customer's remaining balance
                _customerBusiness.Edit(customer);

                base.Add(dto);

                tx.Commit();
            }

            _dashboardBusiness.RefreshUserCache(OwnerId);

            return(new Response
            {
                Type = ResponseType.Success
            });
        }
        public async Task <IActionResult> Put(Guid id, [FromBody] TResponse response)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != response.Id)
            {
                return(BadRequest());
            }

            try
            {
                Uow.BeginTransaction();
                BaseService.Update(Mapper1.Map <TEntity>(response));
                Uow.Commit();
            }
            catch (DbUpdateException)
            {
                if ((await BaseService.FindAsync(id)) == null)
                {
                    return(NotFound());
                }

                throw;
            }

            return(NoContent());
        }
        public override DataResponse <int> Edit(Dto.Transaction dto)
        {
            var businessResp = new DataResponse <int>
            {
                Type = ResponseType.Fail
            };

            var entity = Repository.GetById(dto.Id);

            if (entity == null)
            {
                businessResp.ErrorCode = ErrorCode.RecordNotFound;
                return(businessResp);
            }

            //do not let transaction customerId changes
            dto.CustomerId = entity.CustomerId;

            var resp = _customerBusiness.Get(entity.CustomerId);

            if (resp.Type != ResponseType.Success)
            {
                businessResp.ErrorCode = resp.ErrorCode;
                return(businessResp);
            }

            var customer = resp.Data;

            //rollback the existing amount from customer's remaining balance
            DeleteCustomerBalance(customer, entity.IsDebt, entity.Amount);

            //now add the new balance to customer's remaining balance
            SetCustomerBalance(customer, dto.IsDebt, dto.Amount);

            _customerBusiness.OwnerId = OwnerId;

            using (var tx = Uow.BeginTransaction())
            {
                //update customer's remaining balance
                _customerBusiness.Edit(customer);

                businessResp = base.Edit(dto);

                if (businessResp.Type != ResponseType.Success)
                {
                    return(businessResp);
                }

                tx.Commit();
            }

            _dashboardBusiness.RefreshUserCache(OwnerId);

            businessResp.Type = ResponseType.Success;
            return(businessResp);
        }
Пример #4
0
        public async Task <Users> CreateAsync(RegisterCustomer model)
        {
            IRepository <Users> repo = null;

            var scope = await Uow.BeginTransaction();

            var result = await Service.FindByEmailAsync(model.EmailAddress);

            var customerValidatorFactory = customerValidatorFactoryFunc();

            await customerValidatorFactory.ValidateCreateCustomer(model);

            var createdResult = await Service.CreateAsync(new SpecialAppUsers
            {
                Email       = model.EmailAddress,
                UserName    = model.UserName,
                PhoneNumber = model.PhoneNumber
            }, password : model.Password);

            var newUser = await Service.FindByEmailAsync(model.EmailAddress);

            if (createdResult.Succeeded)
            {
                repo = Uow.GetRepository <Users>();

                var users = new Users
                {
                    DOB               = model.DateOfBirth,
                    FirstName         = model.FirstName,
                    LastName          = model.LastName,
                    SpecialAppUsersId = newUser.Id,
                    State             = State.Added
                };

                repo.Add(users.SetDefaults(loggedInUser: string.Empty));

                await Uow.CommitAsync();

                var addedUsers = await repo.GetAll().Include(x => x.SpecialAppUsers).FirstOrDefaultAsync();

                scope.Commit();

                return(addedUsers);
            }
            return(null);
        }
        public override Response Delete(int id)
        {
            var deleteResp = new Response
            {
                Type = ResponseType.Fail
            };

            var entity = Repository.GetById(id);

            var resp = _customerBusiness.Get(entity.CustomerId);

            if (resp.Type != ResponseType.Success)
            {
                return(resp);
            }

            var customer = resp.Data;

            //rollback the existing amount from customer's remaining balance
            DeleteCustomerBalance(customer, entity.IsDebt, entity.Amount);

            _customerBusiness.OwnerId = OwnerId;

            using (var tx = Uow.BeginTransaction())
            {
                //update customer's remaining balance
                _customerBusiness.Edit(customer);

                deleteResp = base.Delete(id);

                if (deleteResp.Type != ResponseType.Success)
                {
                    return(deleteResp);
                }

                tx.Commit();
            }

            _dashboardBusiness.RefreshUserCache(OwnerId);

            return(deleteResp);
        }
Пример #6
0
        public void UserRepository_RollBackTransaction_NoNewEntitiesAdded()
        {
            var user = new User
            {
                Name = "Test User",
                Id   = 1
            };

            using (var transaction = Uow.BeginTransaction())
            {
                Uow.UserRepository.Add(user);

                Uow.Save();

                transaction.Rollback();
            }

            var users = Uow.UserRepository.Get();

            Assert.AreEqual(0, users.Count());
        }
        public async Task <IActionResult> Delete(Guid id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var entityViewModel = await BaseService.FindAsync(id);

            if (entityViewModel == null)
            {
                return(NotFound());
            }

            Uow.BeginTransaction();
            var entity = Mapper1.Map <TEntity>(entityViewModel);

            BaseService.Remove(entity);
            await Uow.CommitAsync();

            return(Ok(entityViewModel));
        }
        public void Intercept(IInvocation invocation)
        {
            if (Uow == null)
            {
                invocation.Proceed();
                return;
            }

            var method = invocation.MethodInvocationTarget;

            if (method != null && method.GetCustomAttribute <TransactionalAttribute>() != null)
            {
                Uow.BeginTransaction();
            }
            else
            {
                invocation.Proceed();
                return;
            }

            var callMethodSuccess = true;

            try
            {
                invocation.Proceed();
            }
            catch (Exception e)
            {
                callMethodSuccess = false;
                Uow.RollBack();
                Logger.LogDebug(e.Message);
            }

            if (callMethodSuccess && method.GetCustomAttribute <TransactionalAttribute>() != null)
            {
                Uow.Commit();
            }
        }