Esempio n. 1
0
        public PagedResult <MemberListItem> GetMembers(
            int pageNumber           = 1,
            int pageSize             = 100,
            string orderBy           = "email",
            Direction orderDirection = Direction.Ascending,
            string filter            = "")
        {
            if (pageNumber <= 0 || pageSize <= 0)
            {
                throw new NotSupportedException("Both pageNumber and pageSize must be greater than zero");
            }

            var queryString     = Request.GetQueryNameValuePairs();
            var memberTypeAlias = GetMemberType(queryString);
            var filters         = GetFilters(queryString);

            var members = MemberSearch.PerformMemberSearch(filter, filters, out int totalRecords,
                                                           memberTypeAlias, pageNumber, pageSize,
                                                           orderBy, orderDirection);

            if (totalRecords == 0)
            {
                return(new PagedResult <MemberListItem>(0, 0, 0));
            }

            var pagedResult = new PagedResult <MemberListItem>(totalRecords, pageNumber, pageSize)
            {
                Items = members
                        .Select(x => AutoMapperExtensions.MapWithUmbracoContext <SearchResult, MemberListItem>(x, UmbracoContext))
            };

            return(pagedResult);
        }
Esempio n. 2
0
        public HttpResponseMessage GetMembersExport(FormDataCollection queryStrings)
        {
            // Base Query data
            string    memberType     = queryStrings.HasKey("memberType") ? queryStrings.GetValue <string>("memberType") : "";
            string    orderBy        = queryStrings.HasKey("orderBy") ? queryStrings.GetValue <string>("orderBy") : "email";
            Direction orderDirection = queryStrings.HasKey("orderDirection") ? queryStrings.GetValue <Direction>("orderDirection") : Direction.Ascending;

            string filter = queryStrings.HasKey("filter") ? queryStrings.GetValue <string>("filter") : "";

            int totalMembers = 0;

            var members = Mapper.Map <IEnumerable <MemberExportModel> >(MemberSearch.PerformMemberSearch(filter, queryStrings, out totalMembers,
                                                                                                         memberType,
                                                                                                         orderBy: orderBy,
                                                                                                         orderDirection: orderDirection));

            var content = members.CreateCSV();

            // see http://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api
            // & http://stackoverflow.com/questions/12975886/how-to-download-a-file-using-web-api-in-asp-net-mvc-4-and-jquery
            // We really should use an async version - the above reference includes an example.
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

            result.Content = new StringContent(content);
            result.Content.Headers.ContentType                 = new MediaTypeHeaderValue("application/octet-stream");
            result.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileName = string.Format("Members_{0:yyyyMMdd}.csv", DateTime.Now);

            return(result);
        }
Esempio n. 3
0
        public PagedResult <MemberListItem> GetMembers(FormDataCollection queryStrings)
        {
            // Base Query data
            int       pageNumber     = queryStrings.HasKey("pageNumber") ? queryStrings.GetValue <int>("pageNumber") : 1;
            int       pageSize       = queryStrings.HasKey("pageSize") ? queryStrings.GetValue <int>("pageSize") : 10;
            string    orderBy        = queryStrings.HasKey("orderBy") ? queryStrings.GetValue <string>("orderBy") : "email";
            Direction orderDirection = queryStrings.HasKey("orderDirection") ? queryStrings.GetValue <Direction>("orderDirection") : Direction.Ascending;
            string    memberType     = queryStrings.HasKey("memberType") ? queryStrings.GetValue <string>("memberType") : "";

            string filter = queryStrings.HasKey("filter") ? queryStrings.GetValue <string>("filter") : "";

            int totalMembers = 0;
            var members      = Mapper.Map <IEnumerable <MemberListItem> >(MemberSearch.PerformMemberSearch(filter, queryStrings, out totalMembers,
                                                                                                           memberType, pageNumber, pageSize,
                                                                                                           orderBy, orderDirection));

            if (totalMembers == 0)
            {
                return(new PagedResult <MemberListItem>(0, 0, 0));
            }

            var pagedResult = new PagedResult <MemberListItem>(
                totalMembers,
                pageNumber,
                pageSize);

            pagedResult.Items = members;

            return(pagedResult);
        }
Esempio n. 4
0
        public async Task <IHttpActionResult> GetMembersExport(
            string orderBy           = "email",
            Direction orderDirection = Direction.Ascending,
            string filter            = "",
            ExportFormat format      = ExportFormat.Excel)
        {
            // Only export if the current user has access to sensitive data.
            if (!Security.CurrentUser.HasAccessToSensitiveData())
            {
                return(Ok());
            }

            var queryString     = Request.GetQueryNameValuePairs();
            var memberTypeAlias = GetMemberType(queryString);
            var filters         = GetFilters(queryString);
            var columns         = GetColumns(queryString);

            var members = MemberSearch.PerformMemberSearch(filter, filters, out _,
                                                           memberTypeAlias,
                                                           orderBy: orderBy,
                                                           orderDirection: orderDirection)
                          .ToExportModel(columns);

            var name     = $"Members - {DateTime.Now:yyyy-MM-dd}";
            var filename = $"Members-{DateTime.Now:yyyy-MM-dd}";
            var stream   = new MemoryStream();

            string ext      = "csv";
            string mimeType = Constants.MimeTypes.CSV;

            switch (format)
            {
            case ExportFormat.CSV:
                await members.CreateCSVAsync(stream);

                break;

            case ExportFormat.Excel:
                ext      = "xlsx";
                mimeType = Constants.MimeTypes.Excel;
                await members.CreateExcelAsync(stream, name);

                break;
            }

            stream.Seek(0, SeekOrigin.Begin);
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(stream)
            };

            response.Content.Headers.ContentType        = new MediaTypeHeaderValue(mimeType);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = $"{filename}.{ext}"
            };

            return(ResponseMessage(response));
        }