示例#1
0
        public Domain.UserAccount CreateAccount(Domain.UserAccount account)
        {
            var newAccount          = EntityMapper.ToDatabaseModel(account);
            var newUserData         = new DB.Accounts.UserData();
            var newPantry           = new DB.Items.Pantry();
            var newMealPlanCalendar = new DB.MealPlans.MealPlanCalendar();
            var newRecipeCatalog    = new DB.Recipes.RecipeCatalog();

            // Hash password
            passwordHelper.CreatePasswordHashAndSalt(account.Password, out var hash, out var salt);
            newAccount.PasswordHash = hash;
            newAccount.PasswordSalt = salt;

            // Set user data fields
            newUserData.FirstName   = account.UserData?.FirstName ?? "";
            newUserData.LastName    = account.UserData?.LastName ?? "";
            newUserData.DateOfBirth = account.UserData?.DateOfBirth ?? DateTime.UtcNow;
            newUserData.Gender      = account.UserData?.Gender ?? "";

            // Create Pantry, MealPlanCalendar and RecipeCatalog
            newPantry.Items               = new List <DB.Items.PantryItem>();
            newPantry.UserData            = newUserData;
            newMealPlanCalendar.MealPlans = new List <DB.MealPlans.MealPlan>();
            newMealPlanCalendar.UserData  = newUserData;
            newRecipeCatalog.Recipes      = new List <DB.Recipes.Recipe>();
            newRecipeCatalog.UserData     = newUserData;

            // Set account fields
            newUserData.Pantry           = newPantry;
            newUserData.MealPlanCalendar = newMealPlanCalendar;
            newUserData.RecipeCatalog    = newRecipeCatalog;
            newAccount.UserData          = newUserData;

            // Add account to database
            context.Pantries.Add(newPantry);
            context.MealPlanCalendars.Add(newMealPlanCalendar);
            context.RecipeCatalogs.Add(newRecipeCatalog);
            context.UsersData.Add(newUserData);
            context.UserAccounts.Add(newAccount);

            // Commit changes and return result
            context.SaveChanges();
            return(EntityMapper.ToDomainModel(newAccount));
        }