コード例 #1
0
        public async Task <IActionResult> PostDoctor(DoctorData doctor)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            var hasedPassword = Encrypt.EncryptString(doctor.Doctor.Password);
            var user          = await _context.Doctors
                                .Where(u => u.Password == hasedPassword || u.Email == doctor.Doctor.Email)
                                .FirstOrDefaultAsync();

            if (user != null)
            {
                return(BadRequest());
            }
            doctor.Doctor.Password = hasedPassword;
            await _context.Doctors.AddAsync(doctor.Doctor);

            foreach (var id in doctor.Specialty)
            {
                var specialtyDoctor = new SpecialtyDoctor
                {
                    DoctorID    = doctor.Doctor.ID,
                    SpecialtyID = id
                };
                await _context.SpecialtyDoctors.AddAsync(specialtyDoctor);
            }
            await _context.SaveChangesAsync();

            return(Ok(doctor));
        }
コード例 #2
0
 public ActionResult AddSpecialty(SpecialtyDoctor specialtyDoctor)
 {
     if (specialtyDoctor.SpecialtyId != 0)
     {
         if (_db.SpecialtyDoctors.Where(x => x.SpecialtyId == specialtyDoctor.SpecialtyId && x.DoctorId == specialtyDoctor.DoctorId).ToHashSet().Count == 0)
         {
             _db.SpecialtyDoctors.Add(specialtyDoctor);
         }
     }
     _db.SaveChanges();
     return(RedirectToAction("Index"));
 }
コード例 #3
0
        public async Task <IActionResult> PutDoctor(int id, DoctorData item)
        {
            if (!ModelState.IsValid || id != item.Doctor.ID)
            {
                return(BadRequest());
            }
            var doctor = await _context.Doctors.FindAsync(id);

            var hasedPassword = Encrypt.EncryptString(item.Doctor.Password);
            var user          = await _context.Doctors
                                .Where(u => u.Password == hasedPassword || u.Email == item.Doctor.Email)
                                .FirstOrDefaultAsync();

            if (doctor == null || (user != null && user.ID != doctor.ID))
            {
                return(BadRequest());
            }
            doctor.Name        = item.Doctor.Name;
            doctor.Email       = item.Doctor.Email;
            doctor.Password    = hasedPassword;
            doctor.City        = item.Doctor.City;
            doctor.Address     = item.Doctor.Address;
            doctor.Information = item.Doctor.Information;
            var specialtyDoctors = await _context.SpecialtyDoctors
                                   .Where(d => d.DoctorID == doctor.ID)
                                   .Include(s => s.Specialty)
                                   .ToListAsync();

            _context.SpecialtyDoctors.RemoveRange(specialtyDoctors);
            foreach (var ids in item.Specialty)
            {
                var specialtyDoctor = new SpecialtyDoctor
                {
                    DoctorID    = doctor.ID,
                    SpecialtyID = ids
                };
                await _context.SpecialtyDoctors.AddAsync(specialtyDoctor);
            }
            _context.Doctors.Update(doctor);
            await _context.SaveChangesAsync();

            return(Ok(doctor));
        }