示例#1
0
        public Result Register(DatabaseContext db,HttpPostedFileBase file)
        {
            Result res = new Result();

            using (var transaction = db.Database.BeginTransaction())
            {
                try
                {
                    // check email exist
                    if (db.Accounts.Any(x => x.Email == this.Account.Email))
                        return res.Fail("This Email has already exist");

                    // if have file's Certificate  , save file
                    if (file != null)
                    {
                        string extension = Path.GetExtension(file.FileName);
                        string fileName = "";
                        string path = "";
                        bool fileInvalid = true;
                        // check if file's exist . If not : save , else : generate another file name
                        do
                        {
                            // generate file's name by guid
                            fileName = Config.CertificateUrl + Guid.NewGuid().ToString() + extension;
                            path = HttpContext.Current.Server.MapPath("~");
                            path = path + fileName;
                            fileInvalid = File.Exists(path + fileName);

                        } while (fileInvalid);

                        file.SaveAs(path);
                        this.Certificate = fileName;
                    }

                    // create account login
                    this.Account.Role = eRole.Investor;
                    db.Accounts.Add(this.Account);
                    db.SaveChanges();
                    if (this.Account.ID == 0)
                    {
                        transaction.Rollback();
                        return res.Fail("Create account fail");
                    }
                    // create trustee
                    db.Trustees.Add(this.Trustee);
                    db.SaveChanges();
                    if (this.Account.ID == 0)
                    {
                        transaction.Rollback();
                        return res.Fail("Create trustee fail");
                    }
                    // create investor information
                    this.UserId = Account.ID;
                    this.TrusteeId = Trustee.ID;
                    db.Investors.Add(this);
                    db.SaveChanges();

                    if (this.ID == 0)
                    {
                        transaction.Rollback();
                        return res.Fail("Create Investor information fail");
                    }
                    transaction.Commit();
                    // send email congratulations for investor
                    Email_Service es = new Email_Service();
                    es.InvestorRegister(this.Account.Email);
                    es.Notification_Register(this.Account.Email,this.Account.Role);

                    return res.Success(this);

                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return res.Fail(ex.Message);
                }
            }
        }
示例#2
0
        public Result Upsert(DatabaseContext db)
        {
            Result res = new Result();
            ParentService parentService = new ParentService(db);
            using (var transaction = db.Database.BeginTransaction())
            {
                try
                {
                    // Get parent by parent's id
                    var resultParent = parentService.GetParentInformationByParentId(ParentId);
                    if (!resultParent.State)
                        return res.Fail(resultParent.Message);

                    Parent parent = (Parent)resultParent.RetVal;
                    // check loan complete
                    if (parent.Status == eBorroweStatus.Funded || parent.Status == eBorroweStatus.Fulfilled)
                        return res.Fail("This investment complete");

                    // get investor in investments for parent
                    var investment = parent.Investments.Where(x => x.InvestorId == InvestorId).FirstOrDefault();
                    // total loan amount
                    var loanWithRate = parent.LoanWithRate;
                    // calculator amount left
                    var amountLeft = loanWithRate
                        - parent.Investments.Sum(x => x.BidAmount)
                        + (investment == null ? 0 : investment.BidAmount);

                    if (this.BidAmount > amountLeft)
                        return res.Fail("Bid amount so large");

                    if (investment == null)
                    {
                        Random ran = new Random();
                        this.BidRate = (decimal)ran.Next(500, 700) / 100;
                        this.LastBidDate = DateTime.Now;
                        this.Status = this.BidAmount == amountLeft ? eInvestmentStatus.Success : eInvestmentStatus.Funding;
                        db.Investments.Add(this);
                        db.SaveChanges();
                    }
                    else
                    {
                        if (this.BidAmount == 0)
                        {
                            db.Entry<Investment>(investment).State = EntityState.Deleted;
                            db.SaveChanges();
                        }
                        else
                        {
                            investment.LastBidDate = DateTime.Now;
                            investment.BidAmount = this.BidAmount;
                            investment.Status = this.BidAmount == amountLeft ? eInvestmentStatus.Success : eInvestmentStatus.Funding;
                            db.Entry<Investment>(investment).State = EntityState.Modified;
                            db.SaveChanges();
                        }

                    }
                    /*Update borrower status*/
                    if (this.BidAmount == amountLeft)
                    {
                        parent.Status = eBorroweStatus.Fulfilled; // funded
                        parent.Investments.ToList().ForEach(a => a.Status = eInvestmentStatus.Success);

                        var listEmail = db.Investments.Where(x=>x.ParentId == parent.ID).Select(b => b.Investor.Account.Email).ToList();

                        es.Notification_InvesmentSuccess(parent.Account.Email, listEmail);
                    }

                    db.SaveChanges();
                    transaction.Commit();
                    return res.Success(this);
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return res.Fail(ex.Message);
                }
            }
        }