DDD實戰進階第一波(十一):開發通常業務的大健康行業直銷系統(實現經銷商代註冊用例與登陸令牌分發)

前兩篇文章主要實現了經銷商代註冊的倉儲與領域邏輯、經銷商登陸的倉儲與相關邏輯,這篇文章主要講述經銷商代註冊的用例與經銷商登陸的查詢功能。微信

一.經銷商代註冊用例ssh

在經銷商代註冊用例中,咱們須要傳遞經銷商的基本註冊信息,這個信息是作成了DTO對象。ide

1.經銷商註冊的DTO對象:ui

 public class AddDealerDTO
    {
        public string Name { get; set; }
        public string Tel { get; set; }
        public decimal EleMoney { get; set; }
        public Guid? Parentid { get; set; }
        public List<string> ContactNames { get; set; }
        public List<string> ContactTels { get; set; }
        public List<string> ContactProvinces { get; set; }
        public List<string> ContactCities { get; set; }
        public List<string> ContactZeors { get; set; }
        public List<string> ContactStreets { get; set; }
        public List<int> IsDefaultContact { get; set; }
    }

在上述DTO對象中,Name和Tel屬性是子經銷商的名字和電話號碼;EleMoney表明子經銷商註冊時的電子幣,從領域邏輯中能夠看到,電子幣會從父經銷商扣除並附加到子經銷this

商,而且根據EleMoney肯定了子經銷商的級別;Parentid是當前登陸的父經銷商的Id;其餘剩餘屬性是子經銷商的聯繫信息。idea

 

2.經銷商註冊用例:spa

public class AddDealerUseCase:BaseAppSrv
    {
        private readonly IRepository irepository;
        private readonly IDealerRepository idealerrepository;
        private readonly ILoginRepository iloginrepository;
        public AddDealerUseCase(IRepository irepository, IDealerRepository idealerrepository,
            ILoginRepository iloginrepository)
        {
            this.irepository = irepository;
            this.idealerrepository = idealerrepository;
            this.iloginrepository = iloginrepository;
        }
        public ResultEntity<bool> AddDealer(AddDealerDTO adddealerdto)
        {
            var dealerid = Guid.NewGuid();
            var dealercontacts = new List<Contact>();
            for(int i = 0; i < adddealerdto.ContactNames.Count; i++)
            {
                var dealercontact = new Contact().CreateContact(dealerid, adddealerdto.ContactNames[i],
                    adddealerdto.ContactTels[i], adddealerdto.ContactProvinces[i],
                    adddealerdto.ContactCities[i], adddealerdto.ContactZeors[i], adddealerdto.ContactStreets[i],
                    adddealerdto.IsDefaultContact[i]);
                dealercontacts.Add(dealercontact);
            }
            var dealer = new Dealers(idealerrepository).RegisterDealer(dealerid, adddealerdto.Name,
                adddealerdto.Tel, adddealerdto.EleMoney, dealercontacts, adddealerdto.Parentid);
            var login = new Login().CreateLogin(dealer.Tel, dealerid);
            try
            {
                using (irepository)
                {
                    idealerrepository.CreateDealer(dealer);
                    if (adddealerdto.Parentid != null)
                    {
                        idealerrepository.SubParentEleMoney(Guid.Parse(adddealerdto.Parentid.ToString()),
                            adddealerdto.EleMoney);
                    }
                    idealerrepository.AddParentSubCount(adddealerdto.Parentid);
                    iloginrepository.CreateLogin(login);

                    irepository.Commit();
                }
                return GetResultEntity(true);
            }
            catch (OverTowSubException error)
            {
                throw error;
            }
            catch(Exception error)
            {
                throw error;
            }
        }
    }

從上面代碼能夠看到,調用了註冊經銷商的邏輯,而且調用了相關倉儲進行父經銷商電子幣扣除、將父經銷商子個數加一、並持久化子經銷商信息,若是父經銷商的子code

經銷商個數超過2個,則拋出OverTowSubException異常。orm

這裏再也不講述經銷商註冊的WebApi實現,能夠參考前面的文章實現相應的WebApi。視頻

 

二.經銷商登陸功能

經銷商登陸時,只須要傳遞電話號碼和密碼就能夠了,因此先定義相關的DTO對象。

1.經銷商登陸的DTO對象

public class UserLoginDTO
    {
        public string Telphone { get; set; }
        public string Password { get; set; }
    }

2.經銷商登陸查詢

經銷商登陸這個功能不是一個用例,應該叫查詢,調用前面文章實現的查詢邏輯便可實現。

 public class UserLoginQuery:BaseAppSrv
    {
        private readonly IRepository irepository;
        private readonly ILoginRepository iloginrepository;
        public UserLoginQuery(IRepository irepository, ILoginRepository iloginrepository)
        {
            this.iloginrepository = iloginrepository;
            this.irepository = irepository;
        }
        public Guid Login(UserLoginDTO userlogindto)
        {
            try
            {
                using (irepository)
                {
                    return iloginrepository.UserLogin(userlogindto.Telphone, userlogindto.Password);
                }
            }
            catch(Exception error)
            {
                throw error;
            }
        }
    }

3.經銷商登陸的WebApi

在前一篇文章中,咱們實現了登陸成功時令牌生成的邏輯,因此在WebApi中,若是經銷商登陸成功,咱們將返回經銷商的Id、電話號碼與Token信息,這些

信息存儲到一個DTO對象中。

 public class UserLoginResultDTO
    {
        public string Tel { get; set; }
        public Guid DealerId { get; set; }
        public string Token { get; set; }
    }

而後實現WebApi接口

[AllowAnonymous]
        [HttpPost]
        [Route("UserLogin")]
        public ResultEntity<UserLoginResultDTO> UserLogin([FromBody] UserLoginDTO userlogindto)
        {
            var result = new ResultEntity<UserLoginResultDTO>();
            var idealercontext = servicelocator.GetService<IDealerContext>();
            var irepository =
                servicelocator.GetService<IRepository>(new ParameterOverrides { { "context", idealercontext } });
            var iloginrepository = servicelocator.GetService<ILoginRepository>(new ParameterOverrides { { "context", idealercontext } });
            UserLoginQuery userloginquery = new UserLoginQuery(irepository, iloginrepository);
            try
            {
                var dealerid = userloginquery.Login(userlogindto);
                if (dealerid != Guid.Empty)
                {
                    var token = new JwtTokenBuilder()
                        .AddSecurityKey(JwtSecurityKey.Create("msshcjsecretmsshcjsecret"))
                        .AddSubject(userlogindto.Telphone)
                        .AddIssuer("DDD1ZXSystem")
                        .AddAudience("DDD1ZXSystem")
                        .AddClaim("role", "NormalUser")                        
                        .AddExpiry(600)
                        .Build();

                    var userloginresultdto = new UserLoginResultDTO();
                    userloginresultdto.Tel = userlogindto.Telphone;
                    userloginresultdto.DealerId = dealerid;
                    userloginresultdto.Token = token.Value;

                    result.IsSuccess = true;
                    result.Data = userloginresultdto;
                    result.Msg = "登陸成功!";
                }
                else
                {
                    result.ErrorCode = 300;
                    result.Msg = "登陸失敗!";
                }

            }
            catch (Exception error)
            {
                result.ErrorCode = 200;
                result.Msg = error.Message;
            }
            return result;
        }

 上述代碼中JwtTokenBuilder的相關方法說明請參考前一篇文章。

 

QQ討論羣:309287205

DDD實戰進階視頻請關注微信公衆號:

相關文章
相關標籤/搜索