Exemple #1
0
        public async Task <IActionResult> Create(DoctorViewModel doctorViewModel)
        {
            if (ModelState.IsValid)
            {
                var path = string.Empty;

                if (doctorViewModel.ImageFile != null && doctorViewModel.ImageFile.Length > 0)
                {
                    path = await _imageHelper.UploadImageAsync(doctorViewModel.ImageFile, "Doctors");
                }

                try
                {
                    var doctor = _converterHelper.ToDoctor(doctorViewModel, path, true);

                    doctor.CreatedBy = await _userHelper.GetUserByEmailAsync(User.Identity.Name);

                    await _doctorRepository.CreateAsync(doctor);

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError(string.Empty, exception.Message);
                }
            }
            return(View(doctorViewModel));
        }
Exemple #2
0
        public async Task<IActionResult> Create( DoctorViewModel doctorVM )
        {
            if ( ModelState.IsValid )
            {
                var doc = doctorVM.GetNewDoctor();
                doc.UserName = doc.Email;
                doc.NormalizedEmail = doc.Email.ToUpper();
                doc.NormalizedUserName = doc.UserName.ToUpper();
                doc = await _doctorRepository.CreateAsync( doc );
                await _userManager.AddToRoleAsync( doc, Roles.DOCTOR );
                return RedirectToAction( nameof( List ) );
            }
            return View( doctorVM );

        } // Create
Exemple #3
0
        public async Task <IActionResult> Create(DoctorViewModel model)
        {
            var path = string.Empty;

            if (model.ImageFile != null && model.ImageFile.Length > 0)
            {
                path = await _imageHelper.UploadImageAsync(model.ImageFile, "Doctors");
            }

            var doctor = _converterHelper.ToDoctor(model, path, true);

            if (ModelState.IsValid)
            {
                doctor.User = await _userHelper.GetUserByEmailAsync(this.User.Identity.Name);

                await _doctorRepository.CreateAsync(doctor);

                return(RedirectToAction(nameof(Index)));
            }

            model.Specialities = _combosHelper.GetComboSpecialities();
            return(View(model));
        }
Exemple #4
0
        public async Task <Doctor> Handle(CreateDoctorCommand request, CancellationToken cancellationToken)
        {
            var doctor = new Doctor(request.Name, request.Crm, request.CrmUf);

            return(await _doctorRepository.CreateAsync(doctor));
        }
Exemple #5
0
        public async Task <IActionResult> Create(CreateViewModel model)
        {
            if (model.Role == "Doctor")
            {
                if (model.AppointmentDuration <= 0 || model.AppointmentDuration >= 720) //720 minutes == 12 hours which is max time the vet is open
                {
                    ModelState.AddModelError(nameof(model.AppointmentDuration), "Please select a value between 1 and 719.");
                }
                if (model.SpecialityId < 1)
                {
                    ModelState.AddModelError(nameof(model.SpecialityId), "Please select a Speciality.");
                }
                if (model.RoomId < 1)
                {
                    ModelState.AddModelError(nameof(model.RoomId), "Please select a Room.");
                }
            }
            if (ModelState.IsValid)
            {
                //Gets the user
                var user = await _userRepository.GetUserByEmailAsync(model.Email);

                if (user == null)
                {
                    // Initializes an empty path
                    string path = string.Empty;
                    if (model.Photo != null && model.Photo.Length > 0)
                    {
                        path = await _imageHelper.UploadImageAsync(model.Photo, "Users");
                    }
                    else
                    {
                        //Defines the Photo as the default
                        path = $"~/images/Users/Default_User_Image.png";
                    }

                    //Initializes a new User
                    user = _converterHelper.ToUserEntity(model, path);

                    // Adds the User to the DataBase
                    var result = await _userRepository.AddUserAsync(user, "Password");

                    if (result != IdentityResult.Success)
                    {
                        ModelState.AddModelError(string.Empty, "The User could not be created");
                        return(View(model));
                    }

                    // Add user to Role
                    await _userRepository.AddUserToRoleAsync(user, model.Role);

                    if (model.Role == "Doctor")
                    {
                        var doctor = _converterHelper.ToDoctorEntity(model, user.Id);

                        await _doctorRepository.CreateAsync(doctor);
                    }

                    // Creates a Token in order to confirm the email
                    var myToken = await _userRepository.GenerateEmailConfirmationTokenAsync(user);

                    // Defines the Link with its properties to be sent in the email
                    var tokenLink = this.Url.Action("ConfirmAccount", "Account", new
                    {
                        userId = user.Id,
                        token  = myToken,
                    }, protocol: HttpContext.Request.Scheme);

                    //Sends an Email to the User with the TokenLink
                    try
                    {
                        _mailHelper.SendMail(model.Email, "Account Confirmation", $"<h1>Account Confirmation</h1>" +
                                             $"To finish your account registration, " +
                                             $"please click this link:</br></br><a href = \"{tokenLink}\">Confirm Account</a>");
                        this.ViewBag.Message = "To finish account creation verify your email.";
                    }
                    catch (Exception)
                    {
                        this.ModelState.AddModelError(string.Empty, "Error sending the email, please try again in a few minutes");
                    }

                    model.Roles = await _comboHelper.GetComboRolesAsync();

                    model.Specialities = _comboHelper.GetComboSpecialities();
                    model.Rooms        = await _comboHelper.GetComboRoomsAsync(model.SpecialityId);

                    return(this.View(model));
                }

                ModelState.AddModelError(nameof(model.Email), "A User with this email is already registered");
            }


            model.Roles = await _comboHelper.GetComboRolesAsync();

            model.Specialities = _comboHelper.GetComboSpecialities();
            model.Rooms        = await _comboHelper.GetComboRoomsAsync(model.SpecialityId);

            return(View(model));
        }
Exemple #6
0
        public async Task SeedAsync()
        {
            await _context.Database.EnsureCreatedAsync();

            await _userHelper.CheckRoleAsync(RoleNames.Administrator);

            await _userHelper.CheckRoleAsync(RoleNames.Client);

            await _userHelper.CheckRoleAsync(RoleNames.Doctor);

            if (!_context.Specializations.Any())
            {
                this.AddSpecialization("General");
                this.AddSpecialization("Vaccination");
                this.AddSpecialization("Surgery");
                this.AddSpecialization("Others");
                await _context.SaveChangesAsync();
            }

            var user1 = await _userHelper.GetUserByEmailAsync("*****@*****.**");

            if (user1 == null)
            {
                user1 = new User
                {
                    Email          = "*****@*****.**",
                    UserName       = "******",
                    FirstName      = "Diogo",
                    LastName       = "Silva",
                    DateOfBirth    = DateTime.Now,
                    StreeName      = "Praceta das flores nº4",
                    PostalCode     = "2685-192",
                    Location       = "Loures",
                    PhoneNumber    = "916728644",
                    DocumentNumber = "123456789",
                };

                var result = await _userHelper.AddUserAsync(user1, "123456");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Could not create the user in seeder");
                }

                var admin = new Admin
                {
                    User = user1
                };

                await _adminRepository.CreateAsync(admin);

                var isInRole = await _userHelper.IsUserInRoleAsync(user1, RoleNames.Administrator);

                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user1);

                await _userHelper.ConfirmEmailAsync(user1, token);

                if (!isInRole)
                {
                    await _userHelper.AddUserToRoleAsync(user1, RoleNames.Administrator);
                }
            }

            var user3 = await _userHelper.GetUserByEmailAsync("*****@*****.**");

            if (user3 == null)
            {
                user3 = new User
                {
                    Email          = "*****@*****.**",
                    UserName       = "******",
                    FirstName      = "André",
                    LastName       = "Pina",
                    DateOfBirth    = DateTime.Now,
                    StreeName      = "TESTE",
                    PostalCode     = "TESTE",
                    Location       = "TESTE",
                    PhoneNumber    = "123456789",
                    DocumentNumber = "123456789",
                };

                var result = await _userHelper.AddUserAsync(user3, "123456");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Could not create the user in seeder");
                }

                var doctor = new Doctor
                {
                    User             = user3,
                    SpecializationId = _context.Specializations.FirstOrDefault().Id,
                    Specialization   = _context.Specializations.FirstOrDefault()
                };

                await _doctorRepository.CreateAsync(doctor);

                var isInRole = await _userHelper.IsUserInRoleAsync(user3, RoleNames.Doctor);

                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user3);

                await _userHelper.ConfirmEmailAsync(user3, token);

                if (!isInRole)
                {
                    await _userHelper.AddUserToRoleAsync(user3, RoleNames.Doctor);
                }
            }

            var user4 = await _userHelper.GetUserByEmailAsync("*****@*****.**");

            if (user4 == null)
            {
                user4 = new User
                {
                    Email          = "*****@*****.**",
                    UserName       = "******",
                    FirstName      = "Sidney",
                    LastName       = "Major",
                    DateOfBirth    = DateTime.Now,
                    StreeName      = "TESTE",
                    PostalCode     = "TESTE",
                    Location       = "TESTE",
                    PhoneNumber    = "123456789",
                    DocumentNumber = "123456789",
                };

                var result = await _userHelper.AddUserAsync(user4, "123456");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Could not create the user in seeder");
                }

                var doctor = new Doctor
                {
                    User             = user4,
                    SpecializationId = _context.Specializations.FirstOrDefault().Id,
                    Specialization   = _context.Specializations.FirstOrDefault()
                };

                await _doctorRepository.CreateAsync(doctor);

                var isInRole = await _userHelper.IsUserInRoleAsync(user4, RoleNames.Doctor);

                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user4);

                await _userHelper.ConfirmEmailAsync(user4, token);

                if (!isInRole)
                {
                    await _userHelper.AddUserToRoleAsync(user4, RoleNames.Doctor);
                }
            }



            var user2 = await _userHelper.GetUserByEmailAsync("*****@*****.**");

            if (user2 == null)
            {
                user2 = new User
                {
                    Email          = "*****@*****.**",
                    UserName       = "******",
                    FirstName      = "DIOGO",
                    LastName       = "SILVA",
                    DateOfBirth    = DateTime.Now,
                    StreeName      = "Praceta das flores nº4",
                    PostalCode     = "2685-192",
                    Location       = "Loures",
                    PhoneNumber    = "916728644",
                    DocumentNumber = "123456789",
                };

                var result = await _userHelper.AddUserAsync(user2, "123456");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Could not create the user in seeder");
                }

                var client = new Client
                {
                    User = user2
                };

                await _clientRepository.CreateAsync(client);

                var isInRole = await _userHelper.IsUserInRoleAsync(user2, RoleNames.Client);

                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user2);

                await _userHelper.ConfirmEmailAsync(user2, token);

                if (!isInRole)
                {
                    await _userHelper.AddUserToRoleAsync(user2, RoleNames.Client);
                }

                var animal = new Animal
                {
                    Name    = "TesteConsulta",
                    Sex     = "M",
                    Species = "CAO",
                    User    = user2
                };

                _context.Animals.Add(animal);

                var appointment = new Appointment
                {
                    Client         = client,
                    Animal         = animal,
                    Doctor         = await _doctorRepository.GetDoctorByUserAsync(await _userHelper.GetUserByEmailAsync("*****@*****.**")),
                    Specialization = await _specializationRepository.GetByIdAsync(1),
                    StartTime      = DateTime.Now,
                    DoctorNotes    = "Teste 1",
                    Status         = "Confirmed"
                };

                _context.Appointments.Add(appointment);

                var appointment2 = new Appointment
                {
                    Client         = client,
                    Animal         = animal,
                    Doctor         = await _doctorRepository.GetDoctorByUserAsync(await _userHelper.GetUserByEmailAsync("*****@*****.**")),
                    Specialization = await _specializationRepository.GetByIdAsync(1),
                    StartTime      = DateTime.Now,
                    DoctorNotes    = "Teste 2",
                    Status         = "Waiting"
                };

                _context.Appointments.Add(appointment2);

                var appointment3 = new Appointment
                {
                    Client         = client,
                    Animal         = animal,
                    Doctor         = await _doctorRepository.GetDoctorByUserAsync(await _userHelper.GetUserByEmailAsync("*****@*****.**")),
                    Specialization = await _specializationRepository.GetByIdAsync(1),
                    StartTime      = DateTime.Now,
                    DoctorNotes    = "Teste 3",
                    Status         = "Confirmed"
                };

                _context.Appointments.Add(appointment3);
            }

            if (!_context.Animals.Any())
            {
                this.AddAnimal("Pipas", "Male", "Bird", user1);
                this.AddAnimal("Whity", "Male", "Dog", user2);
                this.AddAnimal("Xaninha", "Female", "Cat", user1);
                await _context.SaveChangesAsync();
            }


            //for (int i = 0; i < 50; i++)
            //{
            //    var usertemp = new User
            //    {
            //        Email = $"tempuser{i}@abc.com",
            //        UserName = $"tempuser{i}@abc.com",
            //        FirstName = "TEMP",
            //        LastName = i.ToString(),
            //        DateOfBirth = Convert.ToDateTime("25/06/1992"),
            //        StreeName = "",
            //        PostalCode = "",
            //        Location = "",
            //        PhoneNumber = $"912{i}",
            //        DocumentNumber = "123456789",
            //    };

            //    var result = await _userHelper.AddUserAsync(usertemp, "123456");
            //    if (result != IdentityResult.Success)
            //    {
            //        throw new InvalidOperationException("Could not create the user in seeder");
            //    }

            //    var client = new Client
            //    {
            //        User = usertemp
            //    };

            //    await _clientRepository.CreateAsync(client);

            //    var isInRole = await _userHelper.IsUserInRoleAsync(usertemp, RoleNames.Client);
            //    if (!isInRole)
            //    {
            //        await _userHelper.AddUserToRoleAsync(usertemp, RoleNames.Client);
            //    }
            //}
        }
Exemple #7
0
        public async Task <IActionResult> Register(RegisterViewModel registerVM, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                _logger.LogInformation("********Role: " + registerVM.Role + "*************");

                //IdentityResult result = null;

                //var user = registerVM.Role == Roles.DOCTOR ? (ApplicationUser) doctor : (ApplicationUser) patient;
                switch (registerVM.Role)
                {
                case Roles.DOCTOR:
                    Doctor doctor = registerVM.GetNewDoctor();
                    doctor = await _doctorRepository.CreateAsync(doctor);

                    await _userManager.AddToRoleAsync(doctor, Roles.DOCTOR.ToUpper());

                    //await _users.AssignRole(doctor.UserName, Roles.DOCTOR.ToUpper());
                    await SetupUser(doctor, registerVM);

                    break;

                case Roles.PATIENT:
                    Patient patient = await registerVM.GetNewPatient(_doctorRepository);

                    patient = await _patientRepository.CreateAsync(patient);

                    await _userManager.AddToRoleAsync(patient, Roles.PATIENT);
                    await SetupUser(patient, registerVM);

                    break;

                case Roles.DEVELOPER:
                    Developer dev = registerVM.GetNewDeveloper();
                    //var result = await _userManager.CreateAsync(dev, registerVM.Password);
                    dev = await _developerRepository.CreateAsync(dev);

                    await _userManager.AddToRoleAsync(dev, Roles.DEVELOPER);
                    await SetupUser(dev, registerVM);

                    break;

                default:
                    break;
                }

                //var user = new ApplicationUser { UserName = registerVM.Email, Email = registerVM.Email };

                //if ( result != null && result.Succeeded )
                //{
                return(RedirectToLocal(returnUrl));

                //} // if

                //AddErrors( result );
            } // if

            // If we got this far, something failed, redisplay form
            return(View(registerVM));
        } // Register
Exemple #8
0
        public async Task <IActionResult> Register(RegisterNewUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userHelper.GetUserByEmailAsync(model.Username);

                if (user == null)
                {
                    if (model.RoleId >= 1 && !this.User.IsInRole("Admin"))
                    {
                        this.ModelState.AddModelError(string.Empty, "Stop trying to hack.");
                        model.Specializations = _specializationRepository.GetComboSpecializations();
                        return(this.View(model));
                    }

                    user = new User //TODO POR ISTO NO CONVERTERHELPER
                    {
                        Email       = model.Username,
                        UserName    = model.Username,
                        FirstName   = model.FirstName,
                        LastName    = model.LastName,
                        Gender      = model.Gender,
                        DateOfBirth = model.DateOfBirth
                    };

                    var result = await _userHelper.AddUserAsync(user, model.Password);

                    if (result != IdentityResult.Success)
                    {
                        this.ModelState.AddModelError(string.Empty, "The user couldn't be created.");
                        model.Specializations = _specializationRepository.GetComboSpecializations();
                        return(this.View(model));
                    }

                    if (model.RoleId <= 0)
                    {
                        await _userHelper.AddUserToRoleAsync(user, RoleNames.Client);

                        var client = new Client
                        {
                            User = user
                        };

                        await _clientRepository.CreateAsync(client);
                    }
                    else
                    {
                        Roles role = (Roles)model.RoleId;

                        switch (role) //TODO VER SE PONHO ESTE SWITCH NO USERHELPER
                        {
                        case Roles.Admin:
                            await _userHelper.AddUserToRoleAsync(user, RoleNames.Administrator);

                            var admin = new Admin
                            {
                                User = user
                            };

                            await _adminRepository.CreateAsync(admin);

                            break;

                        case Roles.Client:
                            await _userHelper.AddUserToRoleAsync(user, RoleNames.Client);

                            var client = new Client
                            {
                                User = user
                            };

                            await _clientRepository.CreateAsync(client);

                            break;

                        case Roles.Doctor:
                            await _userHelper.AddUserToRoleAsync(user, RoleNames.Doctor);

                            var doctor = new Doctor
                            {
                                User             = user,
                                SpecializationId = model.SpecializationId,
                                Specialization   = await _specializationRepository.GetByIdAsync(model.SpecializationId)
                            };

                            await _doctorRepository.CreateAsync(doctor);

                            break;

                        default:
                            this.ModelState.AddModelError(string.Empty, "The user couldn't be created.");
                            model.Specializations = _specializationRepository.GetComboSpecializations();
                            return(this.View(model));
                        }
                    }

                    var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                    var tokenLink = this.Url.Action("ConfirmEmail", "Account", new
                    {
                        userid = user.Id,
                        token  = myToken,
                    }, protocol: HttpContext.Request.Scheme);

                    try
                    {
                        _mailHelper.SendMail(model.Username, "Email confirmation", $"<h1>Email Confirmation</h1>" +
                                             $"Click this link to " +
                                             $"confirm your account:</br></br><a href = \"{tokenLink}\">Confirm Email</a>");

                        this.ViewBag.Message = "A confirmation Email has been sent to your account.";
                    }
                    catch (Exception e)
                    {
                        this.ViewBag.Message = e.Message;
                    }

                    model.Specializations = _specializationRepository.GetComboSpecializations();
                    return(this.View(model));
                }

                this.ModelState.AddModelError(string.Empty, "The user already exists.");
            }

            model.Specializations = _specializationRepository.GetComboSpecializations();
            return(View(model));
        }