public static async Task <int> CreateNewSession() { using ItsyBitsyDbContext context = new ItsyBitsyDbContext(); var session = new Session() { StartTime = DateTime.Now }; var dbWebsite = context.Session.Add(session); await context.SaveChangesAsync(); return(session.Id); }
public static async Task EndSession(int sessionId) { using ItsyBitsyDbContext context = new ItsyBitsyDbContext(); var session = new Session() { Id = sessionId, EndTime = DateTime.Now }; context.Entry(session).Property(x => x.EndTime).IsModified = true; await context.SaveChangesAsync(); }
internal static async Task AddPageRelation(IEnumerable <string> existingLinks, int parentId) { using ItsyBitsyDbContext context = new ItsyBitsyDbContext(); var pageRelations = from page in context.Page where existingLinks.Contains(page.Uri) select new PageRelation() { ChildPageId = page.Id, ParentPageId = parentId }; context.PageRelation.AddRange(pageRelations); await context.SaveChangesAsync(); }
public static async Task <Website> CreateWebsite(string seed) { if (!Uri.TryCreate(seed, UriKind.Absolute, out Uri uri)) { throw new InvalidCastException($"{seed} is not a valid uri."); } if (uri.Scheme != "http" && uri.Scheme != "https") { throw new Exception("Only http and https are accepted."); } var host = new Uri($"{uri.Scheme}://{uri.Host}"); var webResuest = WebRequest.CreateHttp(host); webResuest.AutomaticDecompression = DecompressionMethods.All; webResuest.AllowAutoRedirect = true; webResuest.Method = "GET"; var response = webResuest.GetResponse(); var websiteHomeUri = response.ResponseUri.ToString(); if (seed != websiteHomeUri) { throw new Exception($"{seed} redirects to {websiteHomeUri} please use this as the seed."); } using ItsyBitsyDbContext context = new ItsyBitsyDbContext(); if (context.Website.Any(x => x.Seed == websiteHomeUri)) { throw new InvalidCastException($"{websiteHomeUri} already exists."); } var dbWebsite = new Data.Website() { Seed = seed }; context.Website.Add(dbWebsite); await context.SaveChangesAsync(); return(new Website(dbWebsite)); }