private static int HandleList(IJournalrService service, ListOptions opts) { var query = new Dictionary <string, Object>(); if (opts.Count != null) { query["count"] = opts.Count.Value; } if (opts.DateTime != null) { var span = new Chronic.Parser().Parse(opts.DateTime); if (span != null) { query["start"] = span.Start; query["end"] = span.End; } } var entries = service.ListEntries(query); if (entries.Count > 0) { entries.ForEach(e => PrintEntry(e)); } else { Console.WriteLine("No entries found"); } return(0); }
private static int HandleAdd(IJournalrService service, AddOptions opts) { var datetime = new Chronic.Parser().Parse(opts.DateTime ?? "now").Start; if (datetime == null) { Console.WriteLine($"Could not parse {opts.DateTime}"); return(1); } var entry = new Entry { EntryId = GenerateId(), Text = opts.Body, EntryDate = datetime.Value, CreatedDate = DateTime.Now, Tags = string.IsNullOrWhiteSpace(opts.Tags) ? new List <string>() : opts.Tags.Split(',').ToList() }; if (service.AddEntry(entry)) { Console.WriteLine($"Added entry: { entry.EntryId }"); } else { Console.WriteLine("Failed to add entry"); } return(0); }
private static int HandleRemoveTags(IJournalrService service, RemoveTagsOptions opts) { var entry = service.GetEntry(opts.Id); if (string.IsNullOrWhiteSpace(opts.Tags)) { entry.Tags = new List <string>(); } else { var tags = opts.Tags.Split(',').ToList(); entry.Tags = entry.Tags.Where(t => !opts.Tags.Contains(t)).ToList(); } if (service.UpdateEntry(entry)) { Console.WriteLine("Edit successful"); } else { Console.WriteLine("Edit failed"); } return(0); }
private static int HandleEdit(IJournalrService service, EditOptions opts) { var entry = service.GetEntry(opts.Id); if (opts.Body != null) { entry.Text = opts.Body; } if (opts.DateTime != null) { entry.EntryDate = new Chronic.Parser().Parse(opts.DateTime).Start.Value; } if (opts.Tags != null) { var newTags = new List <string>(); foreach (var tag in opts.Tags.Split(',')) { if (!entry.Tags.Contains(tag)) { newTags.Add(tag); } } entry.Tags = newTags; } if (service.UpdateEntry(entry)) { Console.WriteLine("Edit successful"); } else { Console.WriteLine("Edit failed"); } return(0); }
private static int HandleTag(IJournalrService service, TagOptions opts) { var success = false; if (opts.Id == null) { success = service.TagEntry(opts.Count, opts.Tags.Split(',').ToList()); } else { success = service.TagEntry(opts.Id, opts.Tags.Split(',').ToList()); } if (success) { Console.WriteLine("Tagged"); } else { Console.WriteLine("Tag failed"); } return(0); }
private static int HandleRemove(IJournalrService service, RemoveOptions opts) { var success = false; if (opts.Id != null) { success = service.RemoveEntry(opts.Id); } else { success = service.RemoveEntry(opts.Count); } if (success) { Console.WriteLine("Deleted"); } else { Console.WriteLine("Delete failed"); } return(0); }