public void UpdateUser(PersonEditDomainView personEditDomainView, UserEditDomainView userEditDomainView, int idSpecialization) { Users existing = GetById(userEditDomainView.Id); Persons existingperson = _context.Persons.Find(personEditDomainView.Id); if (existing.RoleId == (int)EnumRole.Doctor && existing.RoleId != userEditDomainView.RoleId) { var query = from doctors in _context.Doctors where doctors.PersonId == existingperson.Id select new Doctors { Id = doctors.Id, SpecializationId = doctors.SpecializationId }; Doctors doctor = query.FirstOrDefault(); _context.Doctors.Remove(doctor); } else if (userEditDomainView.RoleId == (int)EnumRole.Doctor) { var query = from doctors in _context.Doctors where doctors.PersonId == existingperson.Id select new Doctors { Id = doctors.Id, SpecializationId = doctors.SpecializationId }; Doctors doctor = query.FirstOrDefault(); if (doctor == null) { doctor = new Doctors() { PersonId = existingperson.Id, SpecializationId = idSpecialization }; _context.Doctors.Add(doctor); } else { doctor.SpecializationId = idSpecialization; _context.Doctors.Update(doctor); } } existing.RoleId = userEditDomainView.RoleId != null? userEditDomainView.RoleId: existing.RoleId; //_context.Roles.Find(userEditDomainView.RoleId); existing.UserName = userEditDomainView.UserName; existing.UserPassword = userEditDomainView.UserPassword; existingperson.FirstName = personEditDomainView.FirstName; existingperson.LastName = personEditDomainView.LastName; existingperson.MiddleName = personEditDomainView.MiddleName; existingperson.Phone = personEditDomainView.Phone; existingperson.Age = personEditDomainView.Age; existingperson.HomeAddress = personEditDomainView.HomeAddress; existingperson.ImageUrl = personEditDomainView.ImageUrl; Update(existing); _context.Persons.Update(existingperson); Save(); }
public void AddUser(PersonEditDomainView personEditDomainView, UserEditDomainView userEditDomainView, int id) { Persons newpersons = new Persons() { FirstName = personEditDomainView.FirstName, MiddleName = personEditDomainView.MiddleName, LastName = personEditDomainView.LastName, Age = personEditDomainView.Age, HomeAddress = personEditDomainView.HomeAddress, Phone = personEditDomainView.Phone, ImageUrl = personEditDomainView.ImageUrl }; newpersons = _context.Persons.Add(newpersons).Entity; Save(); Users newuser = new Users() { UserName = userEditDomainView.UserName, UserPassword = userEditDomainView.UserPassword, IsActive = true, RoleId = userEditDomainView.RoleId, PersonId = newpersons.Id }; Insert(newuser); if (userEditDomainView.RoleId == (int)EnumRole.Doctor) { Doctors newdoctor = new Doctors() { PersonId = newpersons.Id, SpecializationId = id }; _context.Doctors.Add(newdoctor); } Save(); }