public IActionResult updateDoctor(int id, CreateDoctorDtoRequest doctor)
        {
            if (!_db.Doctors.Any(d => d.IdDoctor == id))
            {
                return(NotFound("Doktor o podanym numerze id nie istnieje."));
            }

            if (string.IsNullOrEmpty(doctor.FirstName) || string.IsNullOrEmpty(doctor.LastName) || string.IsNullOrEmpty(doctor.Email))
            {
                return(BadRequest("Jedna z przekazanych wartości jest pusta."));
            }

            Doctor d = new Doctor
            {
                IdDoctor  = id,
                FirstName = doctor.FirstName,
                LastName  = doctor.LastName,
                Email     = doctor.Email
            };

            _db.Attach(d);
            _db.Entry(d).Property("FirstName").IsModified = true;
            _db.Entry(d).Property("LastName").IsModified  = true;
            _db.Entry(d).Property("Email").IsModified     = true;
            _db.SaveChanges();

            return(Ok());
        }
Exemple #2
0
        public IActionResult UpdateDoctor(UpdateDoctorRequest request, int idDoctor)
        {
            var doctor = new Doctor {
                IdDoctor = idDoctor
            };

            _context.Attach(doctor);

            if (request.FirstName != null)
            {
                doctor.FirstName = request.FirstName;
                _context.Entry(doctor).Property("FirstName").IsModified = true;
            }

            if (request.LastName != null)
            {
                doctor.LastName = request.LastName;
                _context.Entry(doctor).Property("LastName").IsModified = true;
            }

            if (request.Email != null)
            {
                doctor.Email = request.Email;
                _context.Entry(doctor).Property("Email").IsModified = true;
            }

            _context.SaveChanges();

            string uri = $"/api/doctors/{doctor.IdDoctor}";

            return(Created(uri, doctor));
        }
        public async Task <IActionResult> DeleteDoctor(int id)
        {
            var doctor = _context.Doctor.FirstOrDefault(s => s.IdDoctor == id);

            if (doctor == null)
            {
                return(new BadRequestResult());
            }
            _context.Attach(doctor);
            _context.Remove(doctor);
            await _context.SaveChangesAsync();

            return(new OkObjectResult(doctor));
        }