Example #1
0
        public async Task <IActionResult> EditUserAsync([FromRoute] Guid id, [FromBody] RegisterEditDto editDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var identity = HttpContext.User.Identity as ClaimsIdentity;

            var userIdFromToken = identity.Claims.FirstOrDefault(x => x.Type == "id");

            var claimValue = userIdFromToken.Value;

            if (claimValue != id.ToString())
            {
                return(this.StatusCode(403));
            }

            var response = await this.userService.EditUserAsync(id, identity, editDto).ConfigureAwait(false);

            if (response.Errors.Any())
            {
                return(this.BadRequest(response.Errors));
            }

            return(Ok());
        }
        public async Task <RegisterEditResponseDto> EditUserAsync(Guid customerId, ClaimsIdentity identity, RegisterEditDto user)
        {
            if (user == null)
            {
                return(new RegisterEditResponseDto
                {
                    Errors = new List <Error>
                    {
                        new Error("Invalid register dto", "Register dto cannot be null")
                    }
                });
            }

            var identityUser = await this.userManager.FindByIdAsync(customerId.ToString()).ConfigureAwait(false);

            if (identityUser == null)
            {
                return(new RegisterEditResponseDto
                {
                    Errors = new List <Error>
                    {
                        new Error("Invalid user email", "Email not found")
                    }
                });
            }

            var customer = this.identityDbContext
                           .Customers
                           .Include(x => x.Genders)
                           .Include(x => x.Roles)
                           .Include(x => x.Tracks)
                           .Include(x => x.SocialNetworks)
                           .FirstOrDefault(x => x.IdentityId == identityUser.Id);

            if (customer == null)
            {
                return(new RegisterEditResponseDto
                {
                    Errors = new List <Error>
                    {
                        new Error("Customer not found", "Customer not found")
                    }
                });
            }

            customer.Description  = user.Description;
            customer.CustomerType = (CustomerType)user.AccountType;
            customer.ProfileName  = user.ProfileName;
            customer.Price        = user.Price;

            try
            {
                var baseProfilePhotosPath = $@"C:\TurnItUp\ProfilePhotos";
                var baseHeaderPhotosPath  = $@"C:\TurnItUp\HeaderPhotos";

                byte[] profilePhotoBytes = System.Convert.FromBase64String(user.ProfilePhoto.Content);

                Directory.CreateDirectory($@"{baseProfilePhotosPath}\{customer.IdentityId}");

                File.WriteAllBytes($@"{baseProfilePhotosPath}\{customer.IdentityId}\{user.ProfilePhoto.Name}.{user.ProfilePhoto.Extension}", profilePhotoBytes);

                customer.ProfilePhoto = new Domain.Model.Images.Image
                {
                    Name      = user.ProfilePhoto.Name,
                    Extension = user.ProfilePhoto.Extension
                };

                Directory.CreateDirectory($@"{baseHeaderPhotosPath}\{customer.IdentityId}");

                byte[] headerPhotoBytes = System.Convert.FromBase64String(user.HeaderPhoto.Content);

                File.WriteAllBytes($@"{baseHeaderPhotosPath}\{customer.IdentityId}\{user.HeaderPhoto.Name}.{user.HeaderPhoto.Extension}", headerPhotoBytes);

                customer.HeaderPhoto = new Domain.Model.Images.Image
                {
                    Name      = user.HeaderPhoto.Name,
                    Extension = user.HeaderPhoto.Extension
                };
            }
            catch (Exception ex)
            {
                return(new RegisterEditResponseDto
                {
                    Errors = new List <Error>
                    {
                        new Error(string.Empty, ex.Message)
                    }
                });
            }

            foreach (var role in user.RoleGroup)
            {
                if (!customer.Roles.Select(x => x.GroupId).Contains(role.RoleGroupId))
                {
                    customer.Roles.Add(new Domain.Model.ValueObjects.Role
                    {
                        GroupId = role.RoleGroupId
                    });
                }
            }

            foreach (var genre in user.GenresGroup)
            {
                if (!customer.Genders.Select(x => x.GroupId).Contains(genre.GenreGroupId))
                {
                    customer.Genders.Add(new Domain.Model.ValueObjects.Gender
                    {
                        GroupId = genre.GenreGroupId
                    });
                }
            }

            customer.SocialNetworks.Clear();
            foreach (var socialNetwork in user.SocialNetworks)
            {
                customer.SocialNetworks.Add(new Domain.Model.SocialMedia.SocialNetwork
                {
                    Name = socialNetwork.Name,
                    Url  = socialNetwork.Url
                });
            }

            if (customer.Location == null)
            {
                customer.Location = new Domain.Model.ValueObjects.Location
                {
                    CountryGroupId = user.Location.CountryGroupId,
                    City           = user.Location.City
                };
            }
            else
            {
                customer.Location.CountryGroupId = user.Location.CountryGroupId;
                customer.Location.City           = user.Location.City;
            }

            this.identityDbContext.Customers.Update(customer);
            await this.identityDbContext.SaveChangesAsync();

            return(new RegisterEditResponseDto
            {
                Errors = new List <Error>()
            });
        }