예제 #1
0
        public async Task <IActionResult> UpdateUser(UserProfileViewModel model)
        {
            if (ModelState.IsValid)
            {
                if ((this.User.Identity.Name != model.UserName) && !this.User.IsInRole("Admin"))
                {
                    return(NotFound());
                }

                var user = await _userHelper.GetUserByEmailAsync(model.UserName);

                if (user != null)
                {
                    user = _converterHelper.ChangerUserProfileViewModelToUser(model, user);

                    var response = await _userHelper.UpdateUserAsync(user);

                    if (model.Doctor != null)
                    {
                        model.Doctor.SpecializationId = model.SpecializationId;
                        model.Doctor.Specialization   = await _specializationRepository.GetByIdAsync(model.SpecializationId);

                        await _doctorRepository.UpdateAsync(model.Doctor);
                    }

                    if (response.Succeeded)
                    {
                        ViewBag.UserMessage = "User update!";
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, response.Errors.FirstOrDefault().Description);
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "User not found!");
                }
            }

            return(RedirectToAction("Profile", new { username = model.UserName }));
        }
        // GET: Specializations/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var specialization = await _specializationRepository.GetByIdAsync(id.Value);

            if (specialization == null)
            {
                return(NotFound());
            }
            return(View(specialization));
        }
예제 #3
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);
            //    }
            //}
        }
예제 #4
0
        public async Task<JsonResult> UpdateData([FromBody] EditParams param)
        {
            if (param==null)
            {
                return Json(NotFound());
            }

            if (param.action == "insert" || (param.action == "batch" && param.added.Count != 0)) 
            {
                var value = (param.action == "insert") ? param.value : param.added[0];

                var client = await _clientRepository.GetClientWithUserAsync(value.ClientUsername);

                var doctor = await _doctorRepository.GetDoctorWithUserAsync(value.DoctorId);

                var animal = await _animalRepository.GetAnimalWithUserAsync(value.AnimalId);

                var specialization = await _specializationRepository.GetByIdAsync(value.SpecializationId);

                Appointment appointment = new Appointment
                {
                    Client = client,
                    Animal = animal,
                    Doctor = doctor,
                    Specialization = specialization,
                    Status = "Waiting",
                    StartTime = value.StartTime,
                    ClientDescription = value.ClientDescription
                };

                await _appointmentRepository.CreateAsync(appointment);

                try
                {
                    _mailHelper.SendMail(appointment.Client.User.UserName, "Appointment Requested", $"<h1>You have requested an appointment</h1>" +
                      $"Please wait while we analyze your request!");
                }
                catch (Exception)
                {
                }
          
            }
            if (param.action == "update" || (param.action == "batch" && param.changed.Count != 0))
            {
                var value = param.changed[0];

                var appointment = await _appointmentRepository.GetByIdWithModelsAsync(value.Id);

                if (appointment!=null)
                {
                    appointment.Animal = await _animalRepository.GetAnimalWithUserAsync(value.AnimalId);

                    appointment.Doctor = await _doctorRepository.GetDoctorWithUserAsync(value.DoctorId);

                    appointment.Specialization = await _specializationRepository.GetByIdAsync(value.SpecializationId);

                    appointment.ClientDescription = value.ClientDescription;

                    await _appointmentRepository.UpdateAsync(appointment);

                    try
                    {
                        _mailHelper.SendMail(appointment.Client.User.UserName, "Appointment Updated", $"<h1>Your Appointment request has been updated.</h1>" +
                          $"Please wait while we analyze your request!");
                    }
                    catch (Exception)
                    {
                    }
                }             

            }
            if (param.action == "remove" || (param.action == "batch" && param.deleted.Count != 0))
            {
                if (param.action == "remove")
                {

                }
                else
                {
                    foreach (var apps in param.deleted)
                    {
                        var appointment = await _appointmentRepository.GetByIdWithModelsAsync(apps.Id);

                        if (appointment!=null && appointment.Status=="Waiting")
                        {
                           await _appointmentRepository.DeleteAsync(appointment);

                            try
                            {
                                _mailHelper.SendMail(appointment.Client.User.UserName, "Appointment Canceled", $"<h1>Your appointment request has been canceled!</h1>" +
                                 $"Your appointment for { appointment.Animal.Name} with the date { appointment.StartTime} has been canceled.");
                            }
                            catch (Exception)
                            {
                            }
                        }

                    }
                }

            }

            var data = _appointmentRepository.GetAllWithModels().ToList();

            return Json(data, new Newtonsoft.Json.JsonSerializerSettings());
        }