Example #1
0
        public async Task <IActionResult> Register(UserFormViewModel model)
        {
            if (User.Identity.IsAuthenticated)
            {
                TempData.AddErrorMessage(WebConstants.AlreadyRegistered);
                return(RedirectToHome());
            }

            if (!ModelState.IsValid || model.ImageFile == null)
            {
                TempData.AddErrorMessage(WebConstants.ErrorTryAgain);
                return(View(model));
            }

            Picture profilePicture      = new Picture();                                                  // Creates instance of Picture entity
            Image   profilePictureImage = PictureServiceHelpers.ConvertIFormFileToImage(model.ImageFile); // Converts the uploaded image to System.Drawing.Image

            if (profilePictureImage == null)                                                              // The uploaded file is not a picture
            {
                TempData.AddErrorMessage(WebConstants.ErrorTryAgain);
                return(View(model));
            }

            bool imageInsertSuccess = await pictureService.InsertAsync(profilePicture, profilePictureImage); // inserts image into database and file system

            if (!imageInsertSuccess)                                                                         // if something with the image goes wrong return error
            {
                TempData.AddErrorMessage(WebConstants.ErrorTryAgain);
                return(View(model));
            }

            User user = new User
            {
                UserName               = AuthenticationHelpers.GenerateUniqueUsername(model.FirstName, model.LastName),
                Email                  = model.Email,
                FirstName              = model.FirstName,
                LastName               = model.LastName,
                PhoneNumber            = model.PhoneNumber,
                ProfilePictureFileName = profilePicture.FileName
            };

            IdentityResult result = await userManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)                                // If something with the insert of a user goes wrong return an error
            {
                await pictureService.DeleteAsync(profilePicture); // In that case delete the already inserted profile picture

                AddErrors(result);
                return(View(model));
            }

            await signInManager.SignInAsync(user, false); // Signs the new user

            TempData.AddSuccessMessage(WebConstants.SuccessfulRegistration);
            return(RedirectToHome());
        }
Example #2
0
        public async Task <bool> InsertAsync(Picture pictureEntity, Image profilePicture)
        {
            try
            {
                Bitmap resizedPicture = PictureServiceHelpers.ResizePicture(profilePicture,
                                                                            ServiceConstants.ProfilePictureWidth,
                                                                            ServiceConstants.ProfilePictureHeight); // Resizes the uploaded picture to a specific size
                await repo.InsertIntoDatabaseAsync(pictureEntity);                                                  // send the entity to the repo

                repo.InsertIntoFileSystem(pictureEntity, resizedPicture);                                           // send the bitmap to the repo

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #3
0
        protected override async Task <Car> GetEntityAsync(CarFormViewModel viewModel, int id)
        {
            Car car = await service.GetByIdAsync(id);

            if (car == null)
            {
                car = new Car();

                if (viewModel.ImageFile == null)
                {
                    return(null);
                }

                Picture carPicture      = new Picture();                                                      // Creates instance of Picture entity
                Image   carPictureImage = PictureServiceHelpers.ConvertIFormFileToImage(viewModel.ImageFile); // Converts the uploaded image to System.Drawing.Image

                if (carPictureImage == null)                                                                  // The uploaded file is not a picture
                {
                    return(null);
                }

                bool imageInsertSuccess = await pictureService.InsertAsync(carPicture, carPictureImage); // inserts image into database and file system

                if (!imageInsertSuccess)
                {
                    return(null);
                }

                // Properties which can't be updated
                car.PictureFileName = carPicture.FileName;
                car.OwnerId         = GetCurrentUserAsync().Result.Id;
            }

            // Properties which can be updated
            car.Colour  = viewModel.Colour;
            car.ModelId = viewModel.ModelId;

            return(car);
        }