private static void GetUsersAndProducts(ProductShopContext dbContext)
        {
            UserAllDto user = new UserAllDto()
            {
                UserCount = dbContext.Users.Count(us => us.ProductSold.Count >= 1),
                Users     = dbContext
                            .Users
                            .Where(u => u.ProductSold.Count >= 1)
                            .OrderByDescending(u => u.ProductSold.Count)
                            .Select(u => new UserProductDto()
                {
                    FirstName    = u.FirstName,
                    LastName     = u.LastName,
                    Age          = u.Age,
                    SoldProducts = new ProductSoldDto()
                    {
                        Count    = u.ProductSold.Count,
                        Products = u.ProductSold
                                   .Select(p => new ProductAllDto()
                        {
                            Name  = p.Name,
                            Price = p.Price
                        })
                                   .ToArray()
                    }
                })
                            .ToArray()
            };

            string jsonString = JsonConvert.SerializeObject(user, Formatting.Indented);

            File.WriteAllText("../../../Files/Export/users-and-products.json", jsonString);
        }
Beispiel #2
0
        public async Task AddUser(UserAllDto newUser)
        {
            if (newUser != null)
            {
                var mapper = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap <UserAllDto, UserDetails>();
                    cfg.CreateMap <UserAllDto, User>();
                }).CreateMapper();

                var userDetails = mapper.Map <UserAllDto, UserDetails>(newUser);

                if (newUser.Image != null)
                {
                    var imageUri = await _fileService.SaveFile(newUser.Image.OpenReadStream(),
                                                               Path.GetExtension(newUser.Image.FileName));

                    userDetails.ImageURl = imageUri.ToString();
                }

                var user = mapper.Map <UserAllDto, User>(newUser);
                user.RoleId       = (await _unitOfWork.Repository <Role>().Get(r => r.Name == "Unconfirmed")).Id;
                user.PasswordHash = PasswordHasher.HashPassword(newUser.Password);
                user.Details      = userDetails;

                _unitOfWork.Repository <UserDetails>().Add(userDetails);
                _unitOfWork.Repository <User>().Add(user);
                await _unitOfWork.SaveChangesAsync();

                user.Confirmation = await  AddConfirmation(user.Id);

                await _unitOfWork.SaveChangesAsync();

                await _confirmation.SendEmail(user.Confirmation.Number.ToString(), user.Email, "welcome.html");
            }
        }