Пример #1
0
        public static CustomerDto NewCustomer(string nickName, string fullName, string facebook,
                                              string phone, DateTime birthDate, string address, float firstTimeAmount)
        {
            using (JiewStoreEntities context = new JiewStoreEntities())
            {
                float pointPerAmount = ParameterFacade.GetPointPerAmount(context);
                float pointMultipler = BirthMonthBonusPointCondition.GetMultipler(birthDate);;

                IPointCalculator pointCalculator = new JiewPointCalculator(pointPerAmount, pointMultipler);

                Customer newCustomer = CustomerHelper.NewCustomer(nickName,
                                                                  fullName,
                                                                  facebook,
                                                                  phone,
                                                                  birthDate,
                                                                  address);

                Transaction newTransaction = TransactionHelper.MakeNewAmountTransaction(newCustomer,
                                                                                        firstTimeAmount,
                                                                                        pointPerAmount,
                                                                                        pointMultipler,
                                                                                        "new customer",
                                                                                        pointCalculator);

                try
                {
                    context.Customers.Add(newCustomer);
                    context.Transactions.Add(newTransaction);
                    context.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                            ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    throw;
                }

                return(AutoMapper.Mapper.Map <CustomerDto>(newCustomer));
            } //using (JiewStoreEntities context = new JiewStoreEntities())
        }
Пример #2
0
        public static AddBuyAmountResultDto AddBuyAmount(string code, float amount)
        {
            float pointPerAmount = 50f;
            float pointMultipler = 1f;

            if (amount <= 0)
            {
                return(new AddBuyAmountResultDto()
                {
                    IsSuccess = false, Message = $"Amount is less than minimum amount[{pointPerAmount}]", Code = code, Amount = amount
                });
            }


            using (JiewStoreEntities context = new JiewStoreEntities())
            {
                try
                {
                    var userQuery = context.Customers.Where(c => c.Code == code);

                    if (!userQuery.Any())
                    {
                        return(new AddBuyAmountResultDto()
                        {
                            IsSuccess = false, Message = $"Customer code [{code}] was not found", Code = code, Amount = amount
                        });
                    }

                    var customer = userQuery.First();

                    pointMultipler = BirthMonthBonusPointCondition.GetMultipler(customer.BirthDate);

                    IPointCalculator pointCalculator = new JiewPointCalculator(pointPerAmount, pointMultipler);

                    Transaction newTransaction = TransactionHelper.MakeNewAmountTransaction(customer,
                                                                                            amount,
                                                                                            pointPerAmount,
                                                                                            pointMultipler,
                                                                                            "buy",
                                                                                            pointCalculator);

                    context.Transactions.Add(newTransaction);
                    context.SaveChanges();

                    CustomerDto customerDto = AutoMapper.Mapper.Map <CustomerDto>(customer);

                    customerDto.RemainingPoint = context.Transactions.Where(t => t.CustomerId == customer.ID).Sum(t => t.Point);

                    return(new AddBuyAmountResultDto()
                    {
                        IsSuccess = true, Amount = amount, Code = code, Customer = customerDto, Message = ""
                    });
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                            ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    throw;
                }
            }
        }