public FileStreamResult Export()
        {
            List <NewsletterSubscriber> newsLettersubscribers = _context.Users
                                                                .Where(x => x.IsNewsletterSubscriber == true)
                                                                .Select(x => new NewsletterSubscriber()
            {
                Email    = x.Email,
                Forename = x.Forename,
                Surname  = x.Surname
            }).ToList();

            var result       = CsvUtilities.WriteCsvToMemory(newsLettersubscribers);
            var memoryStream = new MemoryStream(result);

            return(new FileStreamResult(memoryStream, "text/csv")
            {
                FileDownloadName = "mailchimpexport.csv"
            });
        }
Beispiel #2
0
        public async Task <FileStreamResult> DownloadPersonalData()
        {
            // get user
            var user = await _userManager.GetUserAsync(this.User);

            var applicationUser = _context.Users
                                  .Include(x => x.Member)
                                  .Include(x => x.Staff)
                                  .ThenInclude(staff => staff.EmergencyContact)
                                  .Include(x => x.Decoration)
                                  .Include(x => x.Regimental)
                                  .ThenInclude(regimental => regimental.Regiment)
                                  .Include(x => x.Volunteer)
                                  .ThenInclude(volunteer => volunteer.Expertise)
                                  .Include(x => x.Volunteer)
                                  .ThenInclude(volunteer => volunteer.EmergencyContact)
                                  .FirstOrDefault(x => x.Id == user.Id);

            var personalData = new PersonalData()
            {
                Title                  = applicationUser.Title,
                Forename               = applicationUser.Forename,
                Initial                = applicationUser.Initial,
                Surname                = applicationUser.Surname,
                Decoration             = applicationUser.Decoration.Name,
                AddressLine1           = applicationUser.AddressLine1,
                AddressLine2           = applicationUser.AddressLine2,
                AddressLine3           = applicationUser.AddressLine3,
                County                 = applicationUser.County,
                Country                = applicationUser.Country,
                Postcode               = applicationUser.Postcode,
                PhoneNumber            = applicationUser.PhoneNumber,
                MobileTelNo            = applicationUser.MobileTelNo,
                Email                  = applicationUser.Email,
                WorkEmail              = applicationUser.WorkEmail,
                SubscribedToNewsLetter = applicationUser.IsNewsletterSubscriber ? "Yes" : "No",
            };

            if (applicationUser.Member != null)
            {
                personalData.MemberType       = applicationUser.Member.Type;
                personalData.MemberNumber     = applicationUser.Member.Number;
                personalData.MemberStartDate  = applicationUser.Member.StartDate;
                personalData.MemberExpiryDate = applicationUser.Member.ExpiryDate;
            }

            if (applicationUser.Staff != null)
            {
                personalData.EmergencyContactName     = applicationUser.Staff.EmergencyContact.Name;
                personalData.EmergencyContactRelation = applicationUser.Staff.EmergencyContact.Relation;
                personalData.EmergencyContactTelNo    = applicationUser.Staff.EmergencyContact.TelNo;
                personalData.EndOfEmployment          = applicationUser.Staff.LeaveDate ?? null;
            }

            if (applicationUser.Volunteer != null)
            {
                // override staff emergency contact
                personalData.EmergencyContactName     = applicationUser.Volunteer.EmergencyContact.Name;
                personalData.EmergencyContactRelation = applicationUser.Volunteer.EmergencyContact.Relation;
                personalData.EmergencyContactTelNo    = applicationUser.Volunteer.EmergencyContact.TelNo;

                personalData.Expertise = applicationUser.Volunteer.Expertise.Name;
            }

            if (applicationUser.Regimental != null)
            {
                personalData.Regiment = applicationUser.Regimental.Regiment.Name;
            }

            List <PersonalData> list = new List <PersonalData>()
            {
                personalData
            };

            var result       = CsvUtilities.WriteCsvToMemory(list);
            var memoryStream = new MemoryStream(result);

            return(new FileStreamResult(memoryStream, "text/csv")
            {
                FileDownloadName = "export.csv"
            });
        }