コード例 #1
0
        public async Task <ActionResult <ClientForProfileDto> > UpdateClient([FromBody] ClientForEditDto editedClient)
        {
            var client = clientService.GetClientById(editedClient.Id);

            if (client == null)
            {
                return(NotFound());
            }

            if (client.Email != editedClient.Email)
            {
                var emailExists = await _userManager.FindByEmailAsync(editedClient.Email);

                if (emailExists != null)
                {
                    return(BadRequest(new { code = "DuplicatedEmail", message = "Cette adresse email est déjà utilisée." }));
                }
            }

            //Consider the case where the image has been updated
            var imagePath   = client.PicturePath;
            var imageBase64 = client.ImageBase64;

            //If the edited client image is different from the old one we have to upload it
            if (!UsefulMethods.ByteArrayCompare(editedClient.ImageBase64, client.ImageBase64))
            {
                //Transform the image base64 String
                ImageModel uploadedImage = FileUploader.Base64ToImage(FileUploader.BytesToBase64String(editedClient.ImageBase64), "ClientsPictures");
                imagePath   = uploadedImage.Path;
                imageBase64 = uploadedImage.ImageBytes;
            }

            //Update the location
            var location = locationService.GetLocationById(editedClient.Location.Id);

            if (location == null)
            {
                return(NotFound());
            }

            var newLocation = locationService.UpdateLocation(new Location
            {
                Id      = location.Id,
                Address = editedClient.Location.Address,
                City    = editedClient.Location.City,
                ZipCode = editedClient.Location.ZipCode,
                Lat     = editedClient.Location.Lat,
                Long    = editedClient.Location.Long
            });



            var hasValidatedEmail = client.HasValidatedEmail;

            //Consider the case where the email has been updated
            if (client.Email != editedClient.Email)
            {
                //Update the email in asp identity
                var user = await _userManager.FindByIdAsync(client.IdentityId);

                user.Email    = editedClient.Email;
                user.UserName = editedClient.Email;
                await _userManager.UpdateAsync(user);

                client.Email = editedClient.Email;

                //Set the column HasValidatedEmail to False
                hasValidatedEmail = false;

                //Send verification email
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                SendVerificationEmail(client, user.Id, code, true);
            }

            var newClient = clientService.UpdateClient(new Client
            {
                Id                = editedClient.Id,
                FirstName         = editedClient.FirstName,
                LastName          = editedClient.LastName,
                DateOfBirth       = editedClient.DateOfBirth,
                Email             = editedClient.Email,
                IdentityId        = client.IdentityId,
                ImageBase64       = imageBase64,
                Location          = newLocation,
                Phone             = editedClient.Phone,
                PicturePath       = imagePath,
                HasValidatedEmail = hasValidatedEmail
            });

            return(Ok(_mapper.Map <ClientForProfileDto>(newClient)));
        }