// ************************************** // RegisterUser // ************************************** public static User RegisterUser(User user, Guid invitationCode) { using (var ctx = new SongSearchContext()) { var existing = ctx.GetUser(user); if (existing != null) { return existing; } else { var inv = ctx.Invitations.SingleOrDefault(i => i.InvitationId.Equals(invitationCode) && i.InvitationEmailAddress.Equals(user.UserName)); var pricingPlan = ctx.PricingPlans.SingleOrDefault(x => x.PricingPlanId == user.PricingPlanId); if (inv == null) { throw new ArgumentOutOfRangeException(String.Format("Invalid invitation {0}", inv.InvitationId), innerException: null); } else { // ---------------------------------- // CREATE USER // ---------------------------------- var newUser = ctx.Create(user, inv, pricingPlan); ctx.SaveChanges(); // ---------------------------------- // GET / CREATE PLAN SUBSCRIPTION // ---------------------------------- if (!inv.IsPlanInvitation) { // ---------------------------------- // GET / CREATE PLAN BALANCE // ---------------------------------- var balance = ctx.SubscribeUserTo(newUser, pricingPlan); } else { newUser.PlanBalanceId = inv.InvitedByUser.PlanBalance.PlanBalanceId; newUser.PlanUserId = inv.InvitedByUser.UserId; ctx.AddToUserBalance(newUser); ctx.AddToAdminBalance(newUser); } // ---------------------------------- // CATALOG ACCESS // ---------------------------------- // Get parent users catalog where parent user is at least a plugger and assign to new user in client role var catalogs = ctx.UserCatalogRoles.Where(x => x.UserId == inv.InvitedByUserId && x.RoleId <= (int)Roles.Admin); catalogs.ForEach(c => newUser.UserCatalogRoles.Add(new UserCatalogRole() { CatalogId = c.CatalogId, RoleId = (int)Roles.Client }) ); inv.InvitationStatus = (int)InvitationStatusCodes.Registered; ctx.SaveChanges(); inv = null; return newUser; } } } }
// ************************************** // Subscribe // ************************************** public static PlanBalance SubscribeTo(this User user, PricingPlan pricingPlan) { using (var ctx = new SongSearchContext()) { var balance = ctx.SubscribeUserTo(user, pricingPlan); ctx.SaveChanges(); return balance; } }