public async Task <int> Handle(EditOrganization message) { var org = await _context.Organizations .Include(l => l.Location) .Include(tc => tc.OrganizationContacts) .SingleOrDefaultAsync(t => t.Id == message.Organization.Id) ?? _context.Add(new Organization()).Entity; org.Name = message.Organization.Name; org.LogoUrl = message.Organization.LogoUrl; org.WebUrl = message.Organization.WebUrl; org.DescriptionHtml = message.Organization.Description; org.Summary = message.Organization.Summary; //TODO: mgmccarthy: pull code from ContactExtension.UpdateOrganizationContact into this handler as this the only code that uses it and it should not be in an extension method... espeically doing database access org = await org.UpdateOrganizationContact(message.Organization, _context); org.Location = org.Location.UpdateModel(message.Organization.Location); org.Location.PostalCode = message.Organization.Location.PostalCode; org.PrivacyPolicy = message.Organization.PrivacyPolicy; org.PrivacyPolicyUrl = message.Organization.PrivacyPolicyUrl; await _context.SaveChangesAsync(); return(org.Id); }
public async Task <int> Handle(EditVolunteerTaskCommand message) { var volunteerTask = await _context.VolunteerTasks.Include(t => t.RequiredSkills).SingleOrDefaultAsync(t => t.Id == message.VolunteerTask.Id) ?? new VolunteerTask(); volunteerTask.Name = message.VolunteerTask.Name; volunteerTask.Description = message.VolunteerTask.Description; volunteerTask.Event = _context.Events.SingleOrDefault(a => a.Id == message.VolunteerTask.EventId); volunteerTask.Organization = _context.Organizations.SingleOrDefault(t => t.Id == message.VolunteerTask.OrganizationId); volunteerTask.StartDateTime = message.VolunteerTask.StartDateTime; volunteerTask.EndDateTime = message.VolunteerTask.EndDateTime; volunteerTask.NumberOfVolunteersRequired = message.VolunteerTask.NumberOfVolunteersRequired; volunteerTask.IsLimitVolunteers = volunteerTask.Event.IsLimitVolunteers; volunteerTask.IsAllowWaitList = volunteerTask.Event.IsAllowWaitList; if (volunteerTask.Id > 0) { var volunteerTaskSkillsToRemove = _context.VolunteerTaskSkills.Where(ts => ts.VolunteerTaskId == volunteerTask.Id && (message.VolunteerTask.RequiredSkills == null || message.VolunteerTask.RequiredSkills.All(ts1 => ts1.SkillId != ts.SkillId))); _context.VolunteerTaskSkills.RemoveRange(volunteerTaskSkillsToRemove); } if (message.VolunteerTask.RequiredSkills != null) { volunteerTask.RequiredSkills.AddRange(message.VolunteerTask.RequiredSkills.Where(mt => volunteerTask.RequiredSkills.All(ts => ts.SkillId != mt.SkillId))); } _context.AddOrUpdate(volunteerTask); // Delete existing attachments if (message.VolunteerTask.DeleteAttachments.Count > 0) { var attachmentsToDelete = _context.Attachments.Where(a => a.Task.Id == volunteerTask.Id && message.VolunteerTask.DeleteAttachments.Contains(a.Id)).ToList(); _context.RemoveRange(attachmentsToDelete); } // Add new attachment if (message.VolunteerTask.NewAttachment != null && !string.IsNullOrEmpty(message.VolunteerTask.NewAttachment.FileName)) { var attachmentModel = message.VolunteerTask.NewAttachment; var attachmentUrl = await _taskAttachmentService.UploadAsync(message.VolunteerTask.Id, attachmentModel); var attachment = new FileAttachment { Name = attachmentModel.FileName, Description = message.VolunteerTask.NewAttachmentDescription, Url = attachmentUrl, Task = volunteerTask, }; _context.Add(attachment); } await _context.SaveChangesAsync(); return(volunteerTask.Id); }
public async Task<int> Handle(DuplicateEventCommand message) { var @event = await GetEvent(message.DuplicateEventModel.Id); var newEvent = CloneEvent(@event); ApplyUpdates(newEvent, message.DuplicateEventModel); _context.Add(newEvent.Location); _context.Events.Add(newEvent); await _context.SaveChangesAsync(); return newEvent.Id; }
public async Task <int> Handle(CreateOrEditResourceCommand message) { var resource = await GetResource(message) ?? _context.Add(new AllReady.Models.Resource()).Entity; resource.Name = message.Resource.Name; resource.Description = message.Resource.Description; resource.CampaignId = message.Resource.CampaignId; resource.ResourceUrl = message.Resource.ResourceUrl; await _context.SaveChangesAsync(); return(resource.Id); }
void CreateUpdateOrDeleteCampaignPrimaryContact(Campaign campaign, IPrimaryContactModel contactModel) { var hasPrimaryContact = campaign.CampaignContacts != null && campaign.CampaignContacts.Any(campaignContact => campaignContact.ContactType == (int)ContactTypes.Primary); var addOrUpdatePrimaryContact = !string.IsNullOrWhiteSpace(string.Concat(contactModel.PrimaryContactEmail?.Trim() + contactModel.PrimaryContactFirstName?.Trim(), contactModel.PrimaryContactLastName?.Trim(), contactModel.PrimaryContactPhoneNumber?.Trim())); // Update existing Primary Campaign Contact if (hasPrimaryContact && addOrUpdatePrimaryContact) { var contactId = campaign.CampaignContacts.Single(campaignContact => campaignContact.ContactType == (int)ContactTypes.Primary).ContactId; var contact = _context.Contacts.Single(c => c.Id == contactId); contact.Email = contactModel.PrimaryContactEmail; contact.FirstName = contactModel.PrimaryContactFirstName; contact.LastName = contactModel.PrimaryContactLastName; contact.PhoneNumber = contactModel.PrimaryContactPhoneNumber; } // Delete existing Primary Campaign Contact else if (hasPrimaryContact && !addOrUpdatePrimaryContact) { var campaignContact = campaign.CampaignContacts.Single(cc => cc.ContactType == (int)ContactTypes.Primary); var contact = _context.Contacts.Single(c => c.Id == campaignContact.ContactId); _context.Remove(contact); _context.Remove(campaignContact); } // Add a Primary Campaign Contact else if (!hasPrimaryContact && addOrUpdatePrimaryContact) { var campaignContact = new CampaignContact() { ContactType = (int)ContactTypes.Primary, Contact = new Contact() { Email = contactModel.PrimaryContactEmail, FirstName = contactModel.PrimaryContactFirstName, LastName = contactModel.PrimaryContactLastName, PhoneNumber = contactModel.PrimaryContactPhoneNumber }, }; if (campaign.CampaignContacts == null) { campaign.CampaignContacts = new List <CampaignContact>(); } campaign.CampaignContacts.Add(campaignContact); _context.Add(campaignContact); } }
public async Task <int> Handle(SkillEditCommand message) { var msgSkill = message.Skill; var skill = await _context.Skills.SingleOrDefaultAsync(s => s.Id == msgSkill.Id) ?? _context.Add(new Skill()).Entity; skill.OwningOrganizationId = msgSkill.OwningOrganizationId; skill.Name = msgSkill.Name; skill.ParentSkillId = msgSkill.ParentSkillId; skill.Description = msgSkill.Description; await _context.SaveChangesAsync(); return(skill.Id); }
public async Task <int> Handle(GoalEditCommand message) { var msgGoal = message.Goal; var goal = await _context.CampaignGoals.SingleOrDefaultAsync(s => s.Id == msgGoal.Id) ?? _context.Add(new CampaignGoal()).Entity; goal.CampaignId = msgGoal.CampaignId; goal.GoalType = msgGoal.GoalType; goal.TextualGoal = msgGoal.TextualGoal; goal.NumericGoal = msgGoal.NumericGoal; goal.CurrentGoalLevel = msgGoal.CurrentGoalLevel; goal.Display = msgGoal.Display; await _context.SaveChangesAsync(); return(goal.Id); }
public void Process(RequestApiViewModel viewModel) { //since this is Hangfire job code, it needs to be idempotent, this could be re-tried if there is a failure if (context.Requests.Any(x => x.ProviderRequestId == viewModel.ProviderRequestId)) { return; } var requestIsFromApprovedRegion = RequestIsFromApprovedRegion(viewModel.ProviderData); if (requestIsFromApprovedRegion) { var request = new Request { RequestId = NewRequestId(), //TODO mgmccarthy: this is hard-coded for now to 1, which is HTBox Org's Id in dev b/c SampleDataGenerator always creates it first. We'll need something more robust when we go to production. OrganizationId = 1, ProviderRequestId = viewModel.ProviderRequestId, ProviderData = viewModel.ProviderData, Address = viewModel.Address, City = viewModel.City, DateAdded = DateTimeUtcNow(), Email = viewModel.Email, Name = viewModel.Name, Phone = viewModel.Phone, State = viewModel.State, PostalCode = viewModel.PostalCode, Status = RequestStatus.Unassigned, Source = RequestSource.Api }; //this is a web service call var coordinates = geocoder.GetCoordinatesFromAddress(request.Address, request.City, request.State, request.PostalCode, string.Empty).Result; request.Latitude = coordinates?.Latitude ?? 0; request.Longitude = coordinates?.Longitude ?? 0; context.Add(request); context.SaveChanges(); } //acceptance is true if we can service the Request or false if can't service it backgroundJobClient.Enqueue <ISendRequestStatusToGetASmokeAlarm>(x => x.Send(viewModel.ProviderRequestId, GasaStatus.New, requestIsFromApprovedRegion)); }
public async Task <int> Handle(EditItineraryCommand message) { try { var itinerary = await GetItinerary(message) ?? _context.Add(new Itinerary()).Entity; itinerary.Name = message.Itinerary.Name; itinerary.Date = message.Itinerary.Date; itinerary.EventId = message.Itinerary.EventId; await _context.SaveChangesAsync().ConfigureAwait(false); return(itinerary.Id); } catch (Exception) { // There was an error somewhere return(0); } }
public void Process(RequestApiViewModel viewModel) { //since this is job code now, it needs to be idempotent, this could be re-tried by Hangire if it fails var requestExists = context.Requests.Any(x => x.ProviderRequestId == viewModel.ProviderRequestId); if (!requestExists) { var request = new Request { RequestId = NewRequestId(), //TODO mgmccarthy: this is hard-coded for now to 1, which is HTBox Org's Id in dev b/c SampleDataGenerator always creates it first. We'll need something more robust when we go to production. OrganizationId = 1, ProviderRequestId = viewModel.ProviderRequestId, ProviderData = viewModel.ProviderData, Address = viewModel.Address, City = viewModel.City, DateAdded = DateTimeUtcNow(), Email = viewModel.Email, Name = viewModel.Name, Phone = viewModel.Phone, State = viewModel.State, Zip = viewModel.Zip, Status = RequestStatus.Unassigned, Source = RequestSource.Api }; //this is a web service call var address = geocoder.Geocode(viewModel.Address, viewModel.City, viewModel.State, viewModel.Zip, string.Empty).FirstOrDefault(); request.Latitude = address?.Coordinates.Latitude ?? 0; request.Longitude = address?.Coordinates.Longitude ?? 0; context.Add(request); context.SaveChanges(); mediator.Publish(new ApiRequestProcessedNotification { ProviderRequestId = viewModel.ProviderRequestId }); } }
public async Task <Guid> Handle(EditRequestCommand message) { var request = await _context.Requests .Include(l => l.Event) .SingleOrDefaultAsync(t => t.RequestId == message.RequestModel.Id) ?? _context.Add(new Request { Source = RequestSource.UI }).Entity; bool addressChanged = DetermineIfAddressChanged(message, request); request.EventId = message.RequestModel.EventId; request.Address = message.RequestModel.Address; request.City = message.RequestModel.City; request.Name = message.RequestModel.Name; request.State = message.RequestModel.State; request.Zip = message.RequestModel.Zip; request.Email = message.RequestModel.Email; request.Phone = message.RequestModel.Phone; //If lat/long not provided or we detect the address changed, then use geocoding API to get the lat/long if ((request.Latitude == 0 && request.Longitude == 0) || addressChanged) { //Assume the first returned address is correct var address = _geocoder.Geocode(request.Address, request.City, request.State, request.Zip, string.Empty) .FirstOrDefault(); request.Latitude = address?.Coordinates.Latitude ?? 0; request.Longitude = address?.Coordinates.Longitude ?? 0; } _context.AddOrUpdate(request); await _context.SaveChangesAsync(); return(request.RequestId); }
public async Task <Guid> Handle(EditRequestCommand message) { var request = await _context.Requests .Include(l => l.Event) .SingleOrDefaultAsync(t => t.RequestId == message.RequestModel.Id) ?? _context.Add(new Request { Source = RequestSource.UI }).Entity; var addressChanged = DetermineIfAddressChanged(message, request); var orgId = await _context.Events.AsNoTracking() .Include(rec => rec.Campaign) .Where(rec => rec.Id == message.RequestModel.EventId) .Select(rec => rec.Campaign.ManagingOrganizationId) .FirstOrDefaultAsync(); request.EventId = message.RequestModel.EventId; request.OrganizationId = orgId; request.Address = message.RequestModel.Address; request.City = message.RequestModel.City; request.Name = message.RequestModel.Name; request.State = message.RequestModel.State; request.Zip = message.RequestModel.Zip; request.Email = message.RequestModel.Email; request.Phone = message.RequestModel.Phone; //If lat/long not provided or we detect the address changed, then use geocoding API to get the lat/long if ((request.Latitude == 0 && request.Longitude == 0) || addressChanged) { var coordinates = await _geocoder.GetCoordinatesFromAddress(request.Address, request.City, request.State, request.Zip, string.Empty); request.Latitude = coordinates?.Latitude ?? 0; request.Longitude = coordinates?.Longitude ?? 0; } _context.AddOrUpdate(request); await _context.SaveChangesAsync(); return(request.RequestId); }
public async Task <int> Handle(EditItineraryCommand message) { try { var itinerary = await GetItinerary(message) ?? _context.Add(new Itinerary()).Entity; itinerary.Name = message.Itinerary.Name; itinerary.Date = message.Itinerary.Date; if (itinerary.EventId == 0) { itinerary.EventId = message.Itinerary.EventId; } if (!string.IsNullOrWhiteSpace(message.Itinerary.StartAddress1)) { var startLocation = itinerary.StartLocation ?? _context.Add(new Location()).Entity; startLocation.Address1 = message.Itinerary.StartAddress1; startLocation.Address2 = message.Itinerary.StartAddress2; startLocation.City = message.Itinerary.StartCity; startLocation.State = message.Itinerary.StartState; startLocation.PostalCode = message.Itinerary.StartPostalCode; startLocation.Country = message.Itinerary.StartCountry; itinerary.StartLocation = startLocation; if (message.Itinerary.UseStartAddressAsEndAddress) { if (itinerary.EndLocation != null) { _context.Locations.Remove(itinerary.EndLocation); } itinerary.EndLocation = startLocation; } else { if (itinerary.EndLocation == itinerary.StartLocation) { _context.Locations.Remove(itinerary.EndLocation); itinerary.EndLocation = null; } if (!string.IsNullOrWhiteSpace(message.Itinerary.EndAddress1)) { var endLocation = itinerary.EndLocation ?? _context.Add(new Location()).Entity; endLocation.Address1 = message.Itinerary.EndAddress1; endLocation.Address2 = message.Itinerary.EndAddress2; endLocation.City = message.Itinerary.EndCity; endLocation.State = message.Itinerary.EndState; endLocation.PostalCode = message.Itinerary.EndPostalCode; endLocation.Country = message.Itinerary.EndCountry; itinerary.EndLocation = endLocation; } } } else { if (itinerary.StartLocation != null) { // remove the existing start location _context.Locations.Remove(itinerary.StartLocation); itinerary.StartLocation = null; } if (itinerary.EndLocation != null) { // remove the existing end location _context.Locations.Remove(itinerary.EndLocation); itinerary.EndLocation = null; } } itinerary.UseStartAddressAsEndAddress = message.Itinerary.UseStartAddressAsEndAddress; await _context.SaveChangesAsync(); return(itinerary.Id); } catch (Exception) { // There was an error somewhere return(0); } }
public async Task <Guid> Handle(EditRequestCommand message) { var request = await _context.Requests .Include(l => l.Event) .SingleOrDefaultAsync(t => t.RequestId == message.RequestModel.Id).ConfigureAwait(false) ?? _context.Add(new Request()).Entity; request.EventId = message.RequestModel.EventId; request.Address = message.RequestModel.Address; request.City = message.RequestModel.City; request.Name = message.RequestModel.Name; request.State = message.RequestModel.State; request.Zip = message.RequestModel.Zip; request.Email = message.RequestModel.Email; request.Phone = message.RequestModel.Phone; // todo - longitude and latitude lookup await _context.SaveChangesAsync(); return(request.RequestId); }
protected override async Task HandleCore(AddVolunteerTaskCommand message) { dataContext.Add(message.VolunteerTask); await dataContext.SaveChangesAsync(); }
public async Task <int> Handle(EditOrganizationCommand message) { var org = await _context.Organizations .Include(l => l.Location) .Include(tc => tc.OrganizationContacts) .SingleOrDefaultAsync(t => t.Id == message.Organization.Id) ?? _context.Add(new Organization()).Entity; org.Name = message.Organization.Name; org.LogoUrl = message.Organization.LogoUrl; org.WebUrl = message.Organization.WebUrl; org.DescriptionHtml = message.Organization.Description; org.Summary = message.Organization.Summary; if (org.OrganizationContacts == null) { org.OrganizationContacts = new List <OrganizationContact>(); } var primaryCampaignContact = org.OrganizationContacts.SingleOrDefault(tc => tc.ContactType == (int)ContactTypes.Primary); var contactId = primaryCampaignContact?.ContactId; Contact primaryContact; var contactInfo = string.Concat(message.Organization.PrimaryContactEmail?.Trim() + message.Organization.PrimaryContactFirstName?.Trim(), message.Organization.PrimaryContactLastName?.Trim(), message.Organization.PrimaryContactPhoneNumber?.Trim()); if (contactId == null) { primaryContact = new Contact(); _context.Contacts.Add(primaryContact); } else { primaryContact = await _context.Contacts.SingleAsync(c => c.Id == contactId); } if (string.IsNullOrWhiteSpace(contactInfo) && primaryCampaignContact != null) { //Delete _context.OrganizationContacts.Remove(primaryCampaignContact); _context.Remove(primaryCampaignContact.Contact); } else { primaryContact.Email = message.Organization.PrimaryContactEmail; primaryContact.FirstName = message.Organization.PrimaryContactFirstName; primaryContact.LastName = message.Organization.PrimaryContactLastName; primaryContact.PhoneNumber = message.Organization.PrimaryContactPhoneNumber; if (primaryCampaignContact == null) { primaryCampaignContact = new OrganizationContact { Contact = primaryContact, Organization = org, ContactType = (int)ContactTypes.Primary }; org.OrganizationContacts.Add(primaryCampaignContact); } } org.Location = org.Location.UpdateModel(message.Organization.Location); org.PrivacyPolicy = message.Organization.PrivacyPolicy; org.PrivacyPolicyUrl = message.Organization.PrivacyPolicyUrl; await _context.SaveChangesAsync(); return(org.Id); }