Example #1
0
        public async Task <Object> NewDeliveryMan([FromBody] UserForCreationDto newDeliveryMan)
        {
            var identityUser = new IdentityUser
            {
                Email    = newDeliveryMan.Email,
                UserName = newDeliveryMan.Email
            };

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

            try
            {
                var result = await _userManager.CreateAsync(identityUser, newDeliveryMan.Password);

                if (result.Succeeded)
                {
                    IdentityUser user = _userManager.Users.Where(u => u.Email == newDeliveryMan.Email).FirstOrDefault();


                    //Insert in the table location
                    var location = locationService.AddLocation(newDeliveryMan.Location);

                    //Transform the image base64 String
                    ImageModel uploadedImage = FileUploader.Base64ToImage(newDeliveryMan.ImageBase64String, "DeliveryMenPictures");

                    //Create the client entity
                    var entityDeliveryMan = new DeliveryMan
                    {
                        IdentityId        = user.Id,
                        Email             = newDeliveryMan.Email,
                        FirstName         = newDeliveryMan.FirstName,
                        LastName          = newDeliveryMan.LastName,
                        Phone             = newDeliveryMan.Phone,
                        DateOfBirth       = newDeliveryMan.DateOfBirth,
                        ImageBase64       = uploadedImage.ImageBytes,
                        PicturePath       = uploadedImage.Path,
                        Location          = location,
                        HasValidatedEmail = false,
                        IsValidated       = false
                    };

                    //Insert the new user in the DB
                    var addedDeliveryMan = deliveryMenService.AddDeliveryMan(entityDeliveryMan);

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

                    SendVerificationEmail(addedDeliveryMan, user.Id, code);
                }
                return(Ok(result));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #2
0
        public ActionResult <OrdersHistoryForDeliveryManDto> CompleteDelivery([FromBody] OrderToCompleteDeliveryDto orderToUpdate)
        {
            var order = orderService.GetOrderById(orderToUpdate.IdOrder);

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

            var deliveryInfos = deliveryInfoService.GetOrderDeliveryInfo(orderToUpdate.IdOrder);

            order.Status = EnumOrderStatus.Delivered;
            deliveryInfos.RealDeliveryTime = DateTime.Now;

            //Upload signature Image
            //Signatures
            ImageModel uploadedImage = FileUploader.Base64ToImage(orderToUpdate.SignatureImageBase64String, "Signatures");

            deliveryInfos.SignatureImageBase64 = uploadedImage.ImageBytes;
            deliveryInfos.SignatureImagePath   = uploadedImage.Path;

            deliveryInfoService.EditDeliveryInfo(deliveryInfos);

            orderService.EditOrder(order);

            var client = clientService.GetClientById(order.IdClient);

            var orderToReturn = new OrdersHistoryForDeliveryManDto
            {
                OrderId               = order.Id,
                OrderTime             = order.OrderTime,
                ClientId              = order.IdClient,
                ClientName            = $"{client.FirstName} {client.LastName}",
                ClientPicture         = client.ImageBase64,
                OrderPrice            = order.OrderPrice,
                DeliveryPrice         = order.DeliveryPrice,
                EstimatedDeliveryTime = deliveryInfos.EstimatedDeliveryTime,
                RealDeliveryTime      = deliveryInfos.RealDeliveryTime
            };

            return(Ok(orderToReturn));
        }
        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)));
        }