Exemplo n.º 1
0
        //[Route("~/[controller]/[action]")]
        public IActionResult Inscription()
        {
            var model = new AccountInscriptionViewModel();

            model.GuardianDateOfBirth = DateTime.Now;
            model.KidDateOfBirth      = DateTime.Now;
            if (this._signInManager.IsSignedIn(User))
            {
                var user = this._userManager.FindByNameAsync(User.Identity.Name).Result;
                model.GuardianFirstName   = user.FirstName;
                model.GuardianLastName    = user.LastName;
                model.GuardianDateOfBirth = user.DateOfBirth;
                model.GuardianGendre      = user.Gendre;
                model.GuardianDateOfBirth = user.DateOfBirth;
                model.Email         = user.Email;
                model.PhoneNumber_1 = user.PhoneNumber;
                model.PhoneNumber_2 = user.PhoneNumber_2;
                model.AddressLine_1 = user.AddressLine_1;
                model.AddressLine_2 = user.AddressLine_2;
                model.PostalCode    = user.PostalCode;
            }
            return(View(model));
        }
Exemplo n.º 2
0
        //[Route("~/[controller]/[action]")]
        public async Task <IActionResult> Inscription(AccountInscriptionViewModel model)
        {
            if (this._signInManager.IsSignedIn(User))
            {
                var user = await this._userManager.FindByNameAsync(User.Identity.Name);

                if (user.Email != model.Email.Trim())
                {
                    ModelState.AddModelError(string.Empty, "Email Should Mathc Signed User!!");
                    return(View(model));
                }
            }

            if (ModelState.IsValid)
            {
                string fullName = null;
                string filePath = null;
                var    folders  = new string[] { "Guardians", "GuardianCINs", "Kids" };
                var    exts     = new String[] { ".jpg", ".png", ".jpeg" };

                // Fill Guardian Info
                var extFilePhoto = Path.GetExtension(model.GuardianPhoto.FileName).ToLower();
                if (!(exts.Contains(extFilePhoto)))
                {
                    ModelState.AddModelError(string.Empty, "Invalid Image Extention For (Guardian Photo)!!");
                    return(View(model));
                }
                fullName = $"{model.KidFirstName}_{model.KidLastName}";
                filePath = SerializeFile(model.KidPhoto, folders[0], fullName);

                var extFileCinCopy = Path.GetExtension(model.CinCopy.FileName).ToLower();
                if (!(extFileCinCopy.Contains(".pdf")))
                {
                    ModelState.AddModelError(string.Empty, "Invalid File Extention For (CIN Copy)!!");
                    return(View(model));
                }
                var cinCopyPath = SerializeFile(model.CinCopy, folders[1], fullName);

                AppUser user = null;

                if (this._signInManager.IsSignedIn(User))
                {
                    user = await this._userManager.FindByNameAsync(User.Identity.Name);

                    user.FirstName     = model.GuardianFirstName;
                    user.LastName      = model.GuardianLastName;
                    user.Gendre        = model.GuardianGendre;
                    user.CIN           = model.CIN;
                    user.CinCopyPath   = cinCopyPath;
                    user.DateOfBirth   = model.GuardianDateOfBirth;
                    user.PhotoPath     = filePath;
                    user.PhoneNumber   = model.PhoneNumber_1;
                    user.PhoneNumber_2 = model.PhoneNumber_2;
                    user.AddressLine_1 = model.AddressLine_1;
                    user.AddressLine_2 = model.AddressLine_2;
                    user.PostalCode    = model.PostalCode;

                    var result = await this._userManager.UpdateAsync(user);

                    if (!result.Succeeded)
                    {
                        foreach (var error in result.Errors)
                        {
                            ModelState.AddModelError("", error.Description);
                        }
                        return(View(model));
                    }
                }
                else
                {
                    user = new AppUser()
                    {
                        Email         = model.Email,
                        UserName      = model.Email,
                        CIN           = model.CIN,
                        CinCopyPath   = cinCopyPath,
                        FirstName     = model.GuardianFirstName,
                        LastName      = model.GuardianLastName,
                        DateOfBirth   = model.GuardianDateOfBirth,
                        Gendre        = model.GuardianGendre,
                        PhotoPath     = filePath,
                        PhoneNumber   = model.PhoneNumber_1,
                        PhoneNumber_2 = model.PhoneNumber_2,
                        AddressLine_1 = model.AddressLine_1,
                        AddressLine_2 = model.AddressLine_2,
                        PostalCode    = model.PostalCode
                    };
                    var result = await this._userManager.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        await this._signInManager.SignInAsync(user, isPersistent : false);
                    }
                    else
                    {
                        foreach (var error in result.Errors)
                        {
                            ModelState.AddModelError(string.Empty, error.Description);
                        }
                        return(View(model));
                    }
                }

                filePath = null;
                fullName = null;

                // Fill Kid Info
                extFilePhoto = Path.GetExtension(model.KidPhoto.FileName).ToLower();
                if (!(exts.Contains(extFilePhoto)))
                {
                    ModelState.AddModelError(string.Empty, "Invalid Image Extention For (Kid Photo)!!");
                    return(View(model));
                }
                fullName = $"{model.KidFirstName}_{model.KidLastName}";
                filePath = SerializeFile(model.KidPhoto, folders[2], fullName); // Kids

                var kid = new Kid()
                {
                    FirstName   = model.KidFirstName,
                    LastName    = model.KidLastName,
                    Gendre      = model.KidGendre,
                    DateofBirth = model.KidDateOfBirth,
                    PhotoPath   = filePath,
                    AppUser     = user
                };

                var addedKid = this._context.Add(kid);
                if (addedKid == null)
                {
                    ModelState.AddModelError(string.Empty, "The Kid Informations Did Not Register!!");
                    return(View(model));
                }

                this._db.Inscriptions.Add(new Inscription
                {
                    AppUser         = user,
                    Kid             = kid,
                    InscriptionDate = DateTime.Now
                });
                this._db.SaveChanges();

                if (ModelState.ErrorCount == 0)
                {
                    return(RedirectToAction("Index", "Home"));
                }
            }

            return(View(model));
        }