Exemplo n.º 1
0
        /// <summary>
        /// Actualiza los UserFlavors del usuario por los nuevos seleccionados.
        /// </summary>
        /// <param name="Flavor1Weight">Peso del primer Fashion Flavor seleccionado.</param>
        /// <param name="Flavor2Weight">Peso del segudno Fashion Falvor determinado.</param>
        /// <param name="values">Coleccion de objetos del formulario</param>
        /// <returns>Actualiza los flavors seleccionados y redirecciona a la Home.</returns>
        public ActionResult UpdateUserFlavors(string Flavor1Weight, string Flavor2Weight, string submit)
        {
            bool previous = (submit != null && submit.ToLower() == "previous");

            if (previous)
            {
                return(RedirectToAction("Index", "FlavorSelect"));
            }

            IList <FashionFlavor> flavors     = ClosetState.Flavors;
            IList <UserFlavor>    userFlavors = new List <UserFlavor>();

            if (flavors != null)
            {
                if (!string.IsNullOrEmpty(Flavor1Weight))
                {
                    userFlavors.Add(new UserFlavor(flavors[0], Convert.ToDecimal(Flavor1Weight)));
                }
                if (!string.IsNullOrEmpty(Flavor2Weight))
                {
                    userFlavors.Add(new UserFlavor(flavors[1], Convert.ToDecimal(Flavor2Weight)));
                }
            }

            RegisteredUser user = registeredUserRepository.Get(this.UserId);

            List <int> flavorsIds    = new List <int>();
            List <int> myGarmentsIds = new List <int>();

            foreach (FashionFlavor ff in flavors)
            {
                flavorsIds.Add(ff.Id);
            }

            IList <ClosetGarment> closetGarments = closetRepository.GetGarmentsForUser(user);

            foreach (ClosetGarment closetGarment in closetGarments)
            {
                myGarmentsIds.Add(closetGarment.Garment.Id);
            }

            if (!new OutfitEngineServiceClient().HasValidCombinations(myGarmentsIds, flavorsIds))
            {
                return(RedirectToAction("FlavorChangeResult", "FlavorSelect", new { flavorsChanged = false }));
            }

            user.SetFlavors(userFlavors);

            //Update UserFlavors
            registeredUserRepository.DbContext.BeginTransaction();
            registeredUserRepository.SaveOrUpdate(user);
            registeredUserRepository.DbContext.CommitTransaction();

            new OutfitEngineServiceClient().CreateOutfits(user.Closet.Id);
            UserDataHelper.LoadFromDatabase();

            return(RedirectToAction("FlavorChangeResult", "FlavorSelect", new { flavorsChanged = true }));
        }