public void InviteUsers(BasicUser invitedBy, string comments, FriendProvider friendProvider, IList <IBasicUser> contacts, bool includeInvitationCode)
        {
            // Make sure we get the complete user.
            BasicUser user = basicUserRepository.Get(invitedBy.Id);

            // If we are including the invitation code is because we are in Beta, no more than 10 invites per user.
#if !DEBUG
            if (includeInvitationCode)
            {
                if (user.Friends.Count + contacts.Count >= MAX_TO_INVITE)
                {
                    if (MAX_TO_INVITE - user.Friends.Count > 0)
                    {
                        throw new LimitOfFriendsExceededException(string.Format("You can add up to {0} more friends in the Beta period.", MAX_TO_INVITE - user.Friends.Count));
                    }
                    else
                    {
                        throw new LimitOfFriendsExceededException(string.Format("You have reached the {0} contacts limit in the Beta period.", MAX_TO_INVITE));
                    }
                }
            }
#endif

            basicUserRepository.DbContext.BeginTransaction();
            foreach (IBasicUser contact in contacts)
            {
                if (contact.EmailAddress.Trim() == string.Empty || user.EmailAddress.Trim().ToLower() == contact.EmailAddress.Trim().ToLower())
                {
                    continue;
                }

                // Call the Friend Creator service
                Friend f = friendCreatorService.Create(contact.EmailAddress.Trim().ToLower(), contact.FirstName, contact.LastName, friendProvider);

                // Make sure it does not have it as a friend to send the email.
                if (!user.HasFriend(f))
                {
                    // Save the Friend into the user collection
                    f.BasicUser = user;
                    user.AddFriend(f);
                }

                // TODO: Review how to get the Url from the controller
                string     confirmUrl = string.Format("/Friend/InvitedMe/default.aspx");
                InviteData inviteData = new InviteData {
                    Friend = f, AcceptUrl = confirmUrl, InvitationCode = Guid.NewGuid().ToString()
                };

                if (f.User is InvitedUser)
                {
                    if (includeInvitationCode)
                    {
                        // Create an invitation code
                        InvitationCode ic = new InvitationCode();
                        ic.EmailAddress = f.User.EmailAddress;
                        ic.Code         = inviteData.InvitationCode;
                        ic.InvitedBy    = f.BasicUser;
                        invitationCodeRepository.SaveOrUpdate(ic);

                        messageSenderService.SendWithTemplate("invitation_with_code", user, inviteData, f.User.EmailAddress);
                    }
                    else
                    {
                        messageSenderService.SendWithTemplate("invitation", user, f, f.User.EmailAddress);
                    }
                }
                else
                {
                    messageSenderService.SendWithTemplate("acceptinvite", user, inviteData, f.User.EmailAddress);
                }
            }

            basicUserRepository.SaveOrUpdate(user);
            basicUserRepository.DbContext.CommitTransaction();
        }
示例#2
0
        /// <summary>
        /// Creates a new closet outfit in the system related with the current user.
        /// </summary>
        /// <param name="userId">User</param>
        /// <param name="garments">List of Garments</param>
        /// <param name="season">Season</param>
        /// <param name="visibility">Visibility</param>
        /// <exception cref="NotValidCombinationException">When a combination is not valid.</exception>
        public void CreateUserOutfit(int userId, IList <Garment> garments, Season season, ClosetOutfitVisibility visibility)
        {
            if (!OutfitValidationService.IsValidCombination(garments))
            {
                throw new NotValidCombinationException();
            }

            ClosetOutfit uo = new ClosetOutfit();

            // HACK: We need the garments saved with 0 in the non filled fields to make sure we have no duplicates.
            Garment hack = garmentRepository.Get(0);

            foreach (Garment g in garments)
            {
                uo.AddComponent(g, hack);
            }

            uo.Rating.CalculateEditorRating(uo.Components);

            BasicUser     bu = basicUserRepository.Get(userId);
            FashionFlavor ff = bu.GetPreferredFlavor();

            uo.FashionFlavor = ff;
            uo.Closet        = (bu as RegisteredUser).Closet;

            uo.SetSeason(season);
            uo.SetEventTypes(bu as RegisteredUser);
            uo.SetVisibility(visibility);

            //Agregar PreCombination
            PreCombination pc = preCombinationRepository.GetByGarments(uo.RetrieveCombinableComponents().ToList <Garment>(), ff);

            if (pc == null)
            {
                pc = new PreCombination();
                pc.FashionFlavor = ff;

                for (int j = 0; j < garments.Count; j++)
                {
                    Garment g = garments[j];
                    if (!OutfitValidationService.IsAccessory(g))
                    {
                        pc.AddPreGarment(garments[j]);
                    }
                }
            }

            uo.PreCombination = pc;
            uo.User           = bu as RegisteredUser;

            if (!uo.IsValid())
            {
                throw new NotValidCombinationException();
            }

            closetOutfitRepository.DbContext.BeginTransaction();

            // REVIEW: This may lead to orphaned records, given we cannot change the autonumeric for now because of the combination process.
            // TODO: Is better to check first if the Closet Outfit combination already exists and then proceed.
            preCombinationRepository.SaveOrUpdate(pc);

            try
            {
                closetOutfitRepository.SaveOrUpdate(uo);
                closetOutfitRepository.DbContext.BeginTransaction();
            }
            catch
            {
                closetOutfitRepository.DbContext.RollbackTransaction();
                throw new CombinationAlreadyExistsException();
            }
        }