Exemple #1
0
 private ApplicationUserDetailDTO CreateTraderToRetun(ApplicationUser passedTrader)
 {
     try
     {
         var dtoTrader = new ApplicationUserDetailDTO()
         {
             traderId = passedTrader.Id,
             //personalDetailsId = pd,
             //personalDetails = (PersonalDetails)db.PersonalDetails.Where(personal => personal.pdId == pd),
             // to come here personal details
             // security details
             // contact details
             //TeamName = te.TeamName
         };
         return(dtoTrader);
     }
     catch (Exception)
     {
         // the exception will be bubled up
         throw;
     }
 }
Exemple #2
0
        public async Task <IHttpActionResult> PostTrader(ApplicationUser passedTrader)
        {
            string message = string.Empty;

            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("Message", "The data provided is not valid!");
                return(BadRequest(ModelState));
            }

            try
            {
                // check is author in database as author or member
                var exist = UserManager.FindByEmail(passedTrader.Email);
                if (exist == null)
                {
                    // does not exists
                    // we haven't created new app user so we have no id and we have no username
                    passedTrader.UserName = passedTrader.Email;

                    // Business Rule: An account can be created if there no existing one
                    IdentityResult resultCreate = await UserManager.CreateAsync(passedTrader);

                    if (!resultCreate.Succeeded)
                    {
                        foreach (string err in resultCreate.Errors)
                        {
                            message += err;
                        }
                        ModelState.AddModelError("Message", "Trader Create Error: " + message + " Please contact the app admin!");
                        return(BadRequest(ModelState));
                    }
                    // add the role
                    IdentityResult resultRole = UserManager.AddToRole(passedTrader.Id, "Trader");
                    if (!resultRole.Succeeded)
                    {
                        foreach (string err in resultRole.Errors)
                        {
                            message += err;
                        }
                        ModelState.AddModelError("Message", "Trader Role Error: " + message + " Please contact the app admin!");
                        return(BadRequest(ModelState));
                    }
                    // Add DUMMY PASSWORD for the author
                    IdentityResult resultPassword = await UserManager.AddPasswordAsync(passedTrader.Id, "July2015!");

                    if (!resultPassword.Succeeded)
                    {
                        foreach (string err in resultPassword.Errors)
                        {
                            message += err;
                        }
                        ModelState.AddModelError("Message", "Trader Password Error: " + message + " Please contact the application administrator.");
                        return(BadRequest(ModelState));
                    }
                    // TODO SEND THE EMAIL WITH THE DUMMY PASSWORD TO THE AUTHOR

                    // create result dto to be sent back
                    ApplicationUserDetailDTO resultdto = CreateTraderToRetun(passedTrader);
                    await db.SaveChangesAsync();

                    // return ok if everything OK
                    return(Ok(resultdto));
                }
                else
                {
                    // does exists
                    // Business Rule: add the role to the account if there is no existing role
                    if (UserManager.IsInRole(exist.Id, "Trader"))
                    {
                        ModelState.AddModelError("Message", "Trader with the credentials provided already exist!");
                        return(BadRequest(ModelState));
                    }
                    // add the role now
                    IdentityResult resultRole = UserManager.AddToRole(exist.Id, "Trader");
                    if (!resultRole.Succeeded)
                    {
                        foreach (string err in resultRole.Errors)
                        {
                            message += err;
                        }
                        ModelState.AddModelError("Message", "Trader Role Error: " + message + " Please contact the app admin!");
                        return(BadRequest(ModelState));
                    }
                    // TODO SEND THE EMAIL WITH THE DUMMY PASSWORD TO THE AUTHOR

                    // Add DUMMY PASSWORD for the author
                    IdentityResult resultPassword = await UserManager.AddPasswordAsync(exist.Id, "July2015!");

                    if (!resultPassword.Succeeded)
                    {
                        foreach (string err in resultPassword.Errors)
                        {
                            message += err;
                        }
                        ModelState.AddModelError("Trader Password Error", "Trader Password Error: " + message + " Please contact the application administrator.");
                        return(BadRequest(ModelState));
                    }

                    ApplicationUserDetailDTO resultdto = CreateTraderToRetun(exist);
                    await db.SaveChangesAsync();

                    // return Ok if everything is OK
                    return(Ok(resultdto));
                }
            }
            catch (Exception)
            {
                RollBackDatabaseChanges();
                // log the exception
                ModelState.AddModelError("Message", "An unexpected error occured during the creation" +
                                         " of the account. Please contact the application administrator.");
                return(BadRequest(ModelState));
            }
        }