コード例 #1
0
        /// <summary>
        /// Adds a header to the given truth.
        /// </summary>
        /// <param name="id">The id of the truth to add a header to.</param>
        /// <param name="text">The header's text.</param>
        /// <param name="index">The index to use for footers.</param>
        /// <returns>The result.</returns>
        public ActionResult CreateHeaderFooter(int id, string text, int?index)
        {
            var truth = this.Database.Truth.Get(id);

            if (truth == null)
            {
                return(this.Fail("Unable to understand the truth."));
            }
            var light = this.Database.Light.Get(l => l.Text == text).FirstOrDefault();

            if (light == null)
            {
                light = new Light {
                    Text = text, Modified = DateTime.Now
                };
                this.Database.Save();
                LightSearch.AddOrUpdateIndex(light);
            }
            if (!truth.Love.Truths.Any(t => t.ParentId.HasValue && t.ParentId == id && t.Light != null && t.Light.Id == light.Id))
            {
                var headerFooter = new Truth {
                    Light = light, ParentId = id, Order = index ?? 0
                };
                truth.Love.Truths.Add(headerFooter);
                var hLove = Helper.FindLove(this.Database, light.Id);
                hLove.Truths.Add(new Truth {
                    ParentId = truth.Love.Id
                });
            }
            this.Database.Save();
            return(this.Success());
        }
コード例 #2
0
 public ActionResult ReIndex(int?id)
 {
     if (id == null)
     {
         LightSearch.AddOrUpdateIndex(this.Database.Light.All());
     }
     else
     {
         var light = this.Database.Light.Get(id.Value);
         if (light == null)
         {
             return(this.Fail("That light has not been illuminated."));
         }
         LightSearch.AddOrUpdateIndex(light);
     }
     return(this.Success());
 }
コード例 #3
0
        public ActionResult LightAddLight(int id, string truth)
        {
            var light = this.Database.Light.Get(l => l.Text == truth).FirstOrDefault() ?? new Light {
                Text = truth, Modified = DateTime.Now
            };
            var love = Helper.FindLove(this.Database, id);

            if (!love.Truths.Any(t => t.Light != null && t.Light.Id == id))
            {
                love.Truths.Add(new Truth {
                    Light = light
                });
            }
            this.Database.Save();
            LightSearch.AddOrUpdateIndex(light);
            return(this.Success());
        }
コード例 #4
0
        public ActionResult LightEdit(int id, string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(this.Fail("Text was not given and must be supplied."));
            }
            var light = this.Database.Light.Get(id);

            if (light == null)
            {
                return(this.Fail("That light has not yet been illuminated."));
            }
            light.Text     = text;
            light.Modified = DateTime.Now;
            this.Database.Save();
            LightSearch.AddOrUpdateIndex(light);
            return(this.Success());
        }
コード例 #5
0
        public ActionResult Illuminate(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(this.Fail("Text was not given and must be supplied."));
            }
            var existingLight = this.Database.Light.Get(l => l.Text == text).FirstOrDefault();

            if (existingLight != null)
            {
                return(this.Fail("That light has already been illuminated."));
            }
            var light = new Light {
                Text = text, Modified = DateTime.Now
            };

            this.Database.Light.Insert(light);
            this.Database.Save();
            LightSearch.AddOrUpdateIndex(light);
            return(Json(new { status = SUCCESS, id = light.Id, text }));
        }
コード例 #6
0
        public ActionResult Love(string light, string truth, string versions, string truthLinks)
        {
            if (string.IsNullOrWhiteSpace(truth))
            {
                return(this.Fail("No truth given"));
            }
            var love = Helper.FindLove(this.Database, light);

            if (love == null)
            {
                return(this.Fail("No light given"));
            }
            var truths = truth.Trim().Split(new [] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var t in truths)
            {
                var truthData  = t.Split('|');
                var lightText  = truthData [2].Trim();
                var order      = string.IsNullOrWhiteSpace(truthData [0]) ? null : (int?)Convert.ToInt32(truthData [0]);
                var number     = string.IsNullOrWhiteSpace(truthData [1]) ? null : (int?)Convert.ToInt32(truthData [1]);
                var truthLight = this.Database.Light.Get(l => l.Text == lightText).FirstOrDefault() ??
                                 new Light {
                    Text = lightText, Modified = DateTime.Now
                };
                love.Truths.Add(new Truth {
                    Light = truthLight, Order = order, Number = number
                });
            }
            this.Database.Save();
            foreach (var t in love.Truths)
            {
                LightSearch.AddOrUpdateIndex(t.Light);
            }
            var hash = new Hashids("GodisLove");

            if (!string.IsNullOrEmpty(truthLinks))
            {
                var links = truthLinks.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var t in love.Truths)
                {
                    var l = Helper.FindLove(this.Database, t.Light.Id);
                    foreach (var link in links)
                    {
                        var lightIds = hash.Decode(link).ToList();
                        if (lightIds [0] == 0)
                        {
                            lightIds.RemoveAt(0);
                            var li = this.Database.Light.Get(lightIds [0]);
                            lightIds.RemoveAt(0);
                            var lov = Helper.FindLove(this.Database, lightIds);
                            if (lov.Truths.Any(tr => tr.Light != null && tr.Light.Id == li.Id && !tr.ParentId.HasValue))
                            {
                                continue;
                            }
                            lov.Truths.Add(new Truth {
                                Light = li
                            });
                        }
                        else
                        {
                            if (lightIds.Count == 1)
                            {
                                if (l.Truths.Any(tr => tr.Light != null && tr.Light.Id == lightIds [0] && !tr.ParentId.HasValue))
                                {
                                    continue;
                                }
                                l.Truths.Add(new Truth {
                                    Light = this.Database.Light.Get(lightIds [0])
                                });
                            }
                            else
                            {
                                var lov = Helper.FindLove(this.Database, link);
                                if (l.Truths.Any(tr => tr.Light == null && tr.ParentId.HasValue && tr.ParentId == lov.Id))
                                {
                                    continue;
                                }
                                l.Truths.Add(new Truth {
                                    ParentId = lov.Id
                                });
                            }
                        }
                    }
                }
                this.Database.Save();
            }
            if (!string.IsNullOrEmpty(versions))
            {
                var loveIds = hash.Decode(versions);
                if (loveIds.Length > 0)
                {
                    foreach (var loveId in loveIds)
                    {
                        var linkedLove = this.Database.Love.Get(loveId);
                        foreach (var addTruth in love.Truths.Where(t => t.Number.HasValue))
                        {
                            var lt = linkedLove.Truths.FirstOrDefault(tr => tr.Number == addTruth.Number);
                            if (lt != null)
                            {
                                var truthLove = Helper.FindLove(this.Database, lt.Light.Id);
                                truthLove.Truths.Add(new Truth {
                                    Light = addTruth.Light, ParentId = love.Id
                                });
                                truthLove = Helper.FindLove(this.Database, addTruth.Light.Id);
                                truthLove.Truths.Add(new Truth {
                                    Light = lt.Light, ParentId = linkedLove.Id
                                });
                            }
                        }
                    }
                    this.Database.Save();
                }
            }
            return(this.Success());
        }
コード例 #7
0
        public ActionResult Love(string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                return(this.Fail("No data given to import!"));
            }
            var hash   = new Hashids("GodisLove");
            var import = JsonConvert.DeserializeObject <ImportList> (data);

            foreach (var group in import.Groups)
            {
                if (group.Lights.Count <= 0)
                {
                    return(this.Fail("The light was not given."));
                }
                var love = Helper.FindLove(this.Database, hash.Encode(Helper.GetLightIds(this.Database, group.Lights)));
                foreach (var t in group.Truths)
                {
                    var truth = t;
                    if (t.Parent.HasValue)
                    {
                        if (t.Id.HasValue)
                        {
                            if (!love.Truths.Any(tr => tr.ParentId.HasValue && tr.ParentId == t.Parent.Value && tr.Light != null && tr.Light.Id == truth.Id))
                            {
                                love.Truths.Add(new Truth {
                                    Light    = this.Database.Light.Get(t.Id.Value),
                                    ParentId = t.Parent,
                                    Number   = t.Number,
                                    Order    = t.Order
                                });
                            }
                        }
                        else
                        {
                            if (!love.Truths.Any(tr => tr.ParentId.HasValue && tr.ParentId == t.Parent.Value && tr.Light == null))
                            {
                                love.Truths.Add(new Truth {
                                    ParentId = t.Parent,
                                    Number   = t.Number,
                                    Order    = t.Order
                                });
                            }
                        }
                    }
                    else
                    {
                        if (t.Id.HasValue)
                        {
                            if (!love.Truths.Any(tr => !tr.ParentId.HasValue && tr.Light != null && tr.Light.Id == truth.Id))
                            {
                                love.Truths.Add(new Truth {
                                    Light  = this.Database.Light.Get(t.Id.Value),
                                    Number = t.Number,
                                    Order  = t.Order
                                });
                            }
                        }
                        else
                        {
                            var light = this.Database.Light.Get(l => l.Text == truth.Text).FirstOrDefault() ??
                                        new Light {
                                Text = truth.Text, Modified = DateTime.Now
                            };
                            love.Truths.Add(new Truth {
                                Light = light, Order = t.Order, Number = t.Number
                            });
                        }
                    }
                }
                this.Database.Save();
                foreach (var t in love.Truths)
                {
                    LightSearch.AddOrUpdateIndex(t.Light);
                }

                /*if (!string.IsNullOrEmpty (truthLinks)) {
                 * var links = truthLinks.Split (new [] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                 * foreach (var t in love.Truths) {
                 *    var l = this.FindLove (hash.Encode (t.Light.Id));
                 *    foreach (var link in links) {
                 *       var lightIds = hash.Decode (link).ToList ();
                 *       if (lightIds [0] == 0) {
                 *          lightIds.RemoveAt (0);
                 *          var li = this.Database.Light.Get (lightIds [0]);
                 *          lightIds.RemoveAt (0);
                 *          var lov = this.FindLove (hash.Encode (lightIds));
                 *          if (lov.Truths.Any (tr => tr.Light != null && tr.Light.Id == li.Id && !tr.ParentId.HasValue))
                 *             continue;
                 *          lov.Truths.Add (new Truth { Light = li });
                 *       } else {
                 *          if (lightIds.Count == 1) {
                 *             if (l.Truths.Any (tr => tr.Light != null && tr.Light.Id == lightIds [0] && !tr.ParentId.HasValue))
                 *                continue;
                 *             l.Truths.Add (new Truth { Light = this.Database.Light.Get (lightIds [0]) });
                 *          } else {
                 *             var lov = this.FindLove (link);
                 *             if (l.Truths.Any (tr => tr.Light == null && tr.ParentId.HasValue && tr.ParentId == lov.Id))
                 *                continue;
                 *             l.Truths.Add (new Truth { ParentId = lov.Id });
                 *          }
                 *       }
                 *    }
                 * }
                 * this.Database.Save ();
                 * }*/
            }
            //if (string.IsNullOrWhiteSpace (truth)) return this.Fail ("No truth given");
            //var love = FindLove (light);
            //if (love == null) return this.Fail ("No light given");
            //var truths = truth.Trim ().Split (new [] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            //foreach (var t in truths) {
            //   var truthData = t.Split ('|');
            //   var lightText = truthData [2].Trim ();
            //   var order = string.IsNullOrWhiteSpace (truthData [0]) ? null : (int?)Convert.ToInt32 (truthData [0]);
            //   var number = string.IsNullOrWhiteSpace (truthData [1]) ? null : (int?) Convert.ToInt32 (truthData [1]);
            //   var truthLight = this.Database.Light.Get (l => l.Text == lightText).FirstOrDefault () ??
            //                    new Light {Text = lightText, Modified = DateTime.Now};
            //   love.Truths.Add (new Truth { Light = truthLight, Order = order, Number = number });
            //}
            //var hash = new Hashids ("GodisLove");

            //if (!string.IsNullOrEmpty (versions)) {
            //   var loveIds = hash.Decode (versions);
            //   if (loveIds.Length > 0) {
            //      foreach (var loveId in loveIds) {
            //         var linkedLove = this.Database.Love.Get (loveId);
            //         foreach (var addTruth in love.Truths.Where (t => t.Number.HasValue)) {
            //            var lt = linkedLove.Truths.FirstOrDefault (tr => tr.Number == addTruth.Number);
            //            if (lt != null) {
            //               var truthLove = this.FindLove (hash.Encode (lt.Light.Id));
            //               truthLove.Truths.Add (new Truth {Light = addTruth.Light, ParentId = love.Id});
            //               truthLove = this.FindLove (hash.Encode (addTruth.Light.Id));
            //               truthLove.Truths.Add (new Truth {Light = lt.Light, ParentId = linkedLove.Id});
            //            }
            //         }
            //      }
            //      this.Database.Save ();
            //   }
            //}
            return(this.Success());
        }