public async Task <Portal> Get(string guid, Location location = default, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(guid)) { return(null); } var portalSet = myContext.Set <Portal>(); // check local & remote var portal = await portalSet.FindAsync(new object[] { guid }, cancellationToken); var now = myClock.GetCurrentInstant().ToDateTimeOffset(); if ((now - portal?.Modified)?.TotalDays < 1) // refresh every day { return(portal); } var portals = await Search(guid, location ?? myDefaultLocation, near : false, cancellationToken); var remotePortal = portals.FirstOrDefault(p => p.Guid == guid); if (remotePortal != null && portal != null) { myContext.Entry(portal).SetNotNullProperties(remotePortal); } return(remotePortal); }
public async Task <PollMessage> GetOrCreatePollAndMessage(PollMessage pollMessage, IUrlHelper urlHelper, VoteEnum?format = null, CancellationToken cancellationToken = default) { bool exRaidGym = false; var pollId = pollMessage.PollId; if (pollId < 0) { pollId = pollMessage.PollId = -pollId; exRaidGym = true; } var poll = pollMessage.Poll ?? await myContext .Set <Poll>() .Where(_ => _.Id == pollId) .IncludeRelatedData() .Include(poll => poll.Notifications) .FirstOrDefaultAsync(cancellationToken); if (poll != null) { var existingMessage = poll.Messages.FirstOrDefault(_ => _.InlineMessageId == pollMessage.InlineMessageId && _.ChatId == pollMessage.ChatId && _.MessageId == pollMessage.MessageId); if (existingMessage != null) { return(existingMessage); } pollMessage.Poll = poll; return(await AddPollMessage(pollMessage, urlHelper, cancellationToken)); } var pollData = GetTemporaryPoll(pollId); if (pollData == null) { return(null); } pollMessage.Poll = new Poll { Id = pollId, Title = pollData.Title, AllowedVotes = format, Owner = pollData.Owner, Portal = pollData.Portal, ExRaidGym = exRaidGym, Time = pollData.Time, TimeZoneId = pollData.TimeZoneId, Votes = pollData.Votes, Limits = pollData.Limits }; if (pollMessage.UserId is { } userId) { pollMessage.Poll.InitImplicitVotes(GetCachedUser(userId), pollMessage.BotId); } // MUST be set before portal myContext.Set <Poll>().Attach(pollMessage.Poll).State = EntityState.Added; if (pollData.Portal is { } portal) { var portalSet = myContext.Set <Portal>(); // always check remote var existingPortal = await portalSet.AsNoTracking().FirstOrDefaultAsync(p => p.Guid == portal.Guid, cancellationToken); if (existingPortal == null) { portalSet.Attach(portal).State = EntityState.Added; } else { myContext.Entry(existingPortal).SetNotNullProperties(portal); myContext.Entry(portal).SetNotNullProperties(existingPortal); } } return(await AddPollMessage(pollMessage, urlHelper, cancellationToken, withLog : true)); }