public ActionResult Love(int?id, string items, string history) { var model = new LoveModel(); var hash = new Hashids("GodisLove") { Order = true }; var ids = hash.Decode(items).ToList(); if (id == null) { var lights = this.Database.Light.All(q => q.OrderBy(l => Guid.NewGuid())).Take(25); foreach (var light in lights) { model.ToAdd.Add(new SdwItem(light)); } } else { var histIds = hash.Decode(history).ToList(); histIds.Add(id.Value); history = hash.Encode(histIds); var light = this.Database.Light.Get(id.Value); ids.Add(id.Value); var loves = (from peace in light.Peaces where peace.Love.Peaces.All(p => ids.Contains(p.Light.Id)) select peace.Love).ToList(); if (loves.Count > 0) { var max = loves.Where(l => l.Truths.Any()).Max(l => l.Peaces.Count); foreach (var love in loves.Where(l => l.Peaces.Count == max)) { foreach (var truth in love.Truths) { Love truthLove = null; var truthIds = new List <int> (); if (truth.Light != null) { truthIds.Add(truth.Light.Id); } if (truth.ParentId.HasValue && (!truth.Order.HasValue || truth.Order.Value > 0)) { truthLove = this.Database.Love.Get(truth.ParentId.Value); truthIds.AddRange(truthLove.Peaces.Select(p => p.Light.Id)); } if ((truthLove == null && truthIds.All(ids.Contains)) || (truthIds.All(histIds.Contains) && !truth.Number.HasValue && (!truth.Order.HasValue || truth.Order.Value > 0)) || (truth.Light != null && truth.Order == null && model.ToAdd.Any(i => i.Id == truth.Light.Id))) { continue; } if (truthLove == null && truthIds.Count <= 0) { continue; } var item = new SdwItem(truth) { History = history }; if (truthLove != null) { item.IsLink = true; var text = GetTitle(truthLove.Peaces); if (truth.Light != null) { item.Title = text; } else { item.Text = text; item.Id = truthLove.Peaces.OrderBy(p => p.Order).Last().Light.Id; } item.Parents = hash.Encode(truthLove.Peaces.Select(p => p.Light.Id)); } else if (truth.Light != null) { var parents = new List <Peace> (); var tempParents = (from peace in truth.Light.Peaces from p in peace.Love.Peaces where ids.Contains(p.Light.Id) select p).ToList(); foreach (var tp in tempParents) { if (parents.Any(p => p.Light.Id == tp.Light.Id)) { continue; } parents.Add(tp); } var parentIds = parents.Select(p => p.Light.Id).ToList(); item.Parents = hash.Encode(parentIds); item.IsSelected = histIds.Contains(truth.Light.Id); item.Title = GetTitle((parents.Count > 0) ? parents : truth.Number.HasValue || love.Peaces.Count > 1 ? love.Peaces : new List <Peace> ()); if (string.IsNullOrEmpty(item.Title)) { item.History = string.Empty; } } model.ToAdd.Add(item); } } SetHeadersAndFooters(model); } } return(PartialView(model)); }
/// <summary> /// Finds or creates the love with the given peaces. /// </summary> /// <param name="db">The database connection.</param> /// <param name="lights">The list of light ids.</param> /// <param name="create">True (default) to create a new love if non-existant, otherwise false.</param> /// <returns>The love.</returns> public static Love FindLove(ISdwDatabase db, IEnumerable <int> lights, bool create = true) { var hash = new Hashids("GodisLove"); return(FindLove(db, hash.Encode(lights), create)); }