コード例 #1
0
        public IHttpActionResult GetUsers(string searchText = null, string selectedSearchItem = null,
                                          bool onlyDefault  = false)
        {
            var result = default(IHttpActionResult);

            try
            {
                var usersInfo = new List <UserInfo>();

                // Get only the default users or..
                if (!onlyDefault)
                {
                    // ..get all users if no searchtext is provided or filtered users if a searchtext is provided.
                    usersInfo = searchText == null
                        ? this.GetAllUsers()
                        : this.GetFilteredUsers(searchText, selectedSearchItem);

                    usersInfo = this.SortUsers(usersInfo);
                }

                this.AddDefaultUsers(usersInfo);

                var selectedUserId = this.UserInfo.UserID;

                var resultData = new UserCollectionDto
                {
                    Users = usersInfo.Select(userInfo => new UserDto
                    {
                        Id                 = userInfo.UserID,
                        UserName           = userInfo.Username,
                        UserAndDisplayName = userInfo.DisplayName != null
                                ? $"{userInfo.DisplayName} - {userInfo.Username}"
                                : userInfo.Username
                    })
                            .ToList(),
                    SelectedUserId = selectedUserId
                };

                result = this.Ok(resultData);
            }
            catch (Exception exception)
            {
                Exceptions.LogException(exception);

                result = this.InternalServerError(exception);
            }

            return(result);
        }
コード例 #2
0
        private void ExportUsersAndProducts()
        {
            var users = new UserCollectionDto()
            {
                Count = this._dbContext
                        .Users
                        .Count(u => u.ProductsSold.Count > 0),
                Users = this._dbContext
                        .Users
                        .Select(u => new UserDto()
                {
                    FirstName    = u.FirstName,
                    LastName     = u.LastName,
                    Age          = u.Age,
                    SoldProducts = new SoldProductCollectionDto()
                    {
                        Count    = u.ProductsSold.Count,
                        Products = u.ProductsSold
                                   .Select(ps => new SoldProductDto()
                        {
                            Name  = ps.Name,
                            Price = ps.Price
                        }).ToArray()
                    }
                })
                        .OrderByDescending(x => x.SoldProducts.Count)
                        .ThenBy(x => x.LastName)
                        .ToArray()
            };

            var jsonSettings = new JsonSerializerSettings()
            {
                DefaultValueHandling = DefaultValueHandling.Ignore
            };

            var serializedUsersString = JsonConvert.SerializeObject(users, Formatting.Indented, jsonSettings);

            File.WriteAllText(string.Format(ExportPath, UsersAndProductsPath), serializedUsersString);
        }
コード例 #3
0
        private void ExportUsersAndProducts()
        {
            var users = new UserCollectionDto()
            {
                Count    = this._dbContext.Users.Count(),
                UserDtos = this._dbContext
                           .Users
                           .Where(u => u.ProductsSold.Count > 0)
                           .OrderByDescending(u => u.ProductsSold.Count)
                           .Select(x => new DTOs.Export.UserDto()
                {
                    FirstName    = x.FirstName,
                    LastName     = x.LastName,
                    Age          = x.Age.ToString(),
                    SoldProducts = new SoldProductsCollectionDto()
                    {
                        Count           = x.ProductsSold.Count,
                        SoldProductDtos = x.ProductsSold
                                          .Select(z => new SoldProductAttributesDto()
                        {
                            Name  = z.Name,
                            Price = z.Price
                        }).ToArray()
                    }
                }).ToArray()
            };

            var sb            = new StringBuilder();
            var xmlNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });

            var serializer = new XmlSerializer(typeof(UserCollectionDto), new XmlRootAttribute("users"));

            serializer.Serialize(new StringWriter(sb), users, xmlNamespaces);

            File.WriteAllText(string.Format(XmlExportPath, UsersAndProductsPath), sb.ToString().Trim());
        }