/// <summary> /// /// </summary> /// <remarks>Replaces an Owner's Contacts</remarks> /// <param name="id">id of Owner to replace Contacts for</param> /// <param name="items">Replacement Owner contacts.</param> /// <response code="200">OK</response> public virtual IActionResult OwnersIdContactsPutAsync(int id, Contact[] items) { var exists = _context.Owners.Any(a => a.Id == id); if (exists && items != null) { Owner owner = _context.Owners .Include(x => x.LocalArea.ServiceArea.District.Region) .Include(x => x.EquipmentList) .ThenInclude(y => y.DistrictEquipmentType) .Include(x => x.Notes) .Include(x => x.Attachments) .Include(x => x.History) .Include(x => x.Contacts) .First(x => x.Id == id); // adjust the incoming list. for (int i = 0; i < items.Count(); i++) { Contact item = items[i]; if (item != null) { bool contact_exists = _context.Contacts.Any(x => x.Id == item.Id); if (contact_exists) { items[i] = _context.Contacts .First(x => x.Id == item.Id); } else { _context.Add(item); items[i] = item; } } } // remove contacts that are no longer attached. foreach (Contact contact in owner.Contacts) { if (contact != null && !items.Any(x => x.Id == contact.Id)) { _context.Remove(contact); } } // replace Contacts. owner.Contacts = items.ToList(); _context.Update(owner); _context.SaveChanges(); return(new ObjectResult(items)); } else { // record not found return(new StatusCodeResult(404)); } }
/// <summary> /// Create owner contacts /// </summary> /// <remarks>Replaces an Owner's Contacts</remarks> /// <param name="id">id of Owner to replace Contacts for</param> /// <param name="items">Replacement Owner contacts.</param> /// <response code="200">OK</response> public virtual IActionResult OwnersIdContactsPutAsync(int id, Contact[] items) { bool exists = _context.Owners.Any(a => a.Id == id); if (exists && items != null) { Owner owner = _context.Owners .Include(x => x.LocalArea.ServiceArea.District.Region) .Include(x => x.EquipmentList) .ThenInclude(y => y.DistrictEquipmentType) .Include(x => x.Notes) .Include(x => x.Attachments) .Include(x => x.History) .Include(x => x.Contacts) .First(x => x.Id == id); // adjust the incoming list for (int i = 0; i < items.Length; i++) { Contact item = items[i]; if (item != null) { bool contactExists = _context.Contacts.Any(x => x.Id == item.Id); if (contactExists) { items[i] = _context.Contacts .First(x => x.Id == item.Id); } else { _context.Add(item); items[i] = item; } } } // remove contacts that are no longer attached. foreach (Contact contact in owner.Contacts) { if (contact != null && items.All(x => x.Id != contact.Id)) { _context.Remove(contact); } } // replace contacts owner.Contacts = items.ToList(); _context.Update(owner); _context.SaveChanges(); return(new ObjectResult(new HetsResponse(items))); } // record not found return(new ObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration)))); }
/// <summary> /// Update contacts associated with a project /// </summary> /// <remarks>Replaces an Project's Contacts</remarks> /// <param name="id">id of Project to replace Contacts for</param> /// <param name="items">Replacement Project contacts.</param> /// <response code="200">OK</response> public virtual IActionResult ProjectsIdContactsPutAsync(int id, Contact[] items) { bool exists = _context.Projects.Any(a => a.Id == id); if (exists && items != null) { Project project = _context.Projects .Include(x => x.District.Region) .Include(x => x.Notes) .Include(x => x.Attachments) .Include(x => x.History) .Include(x => x.Contacts) .First(x => x.Id == id); // adjust the incoming list for (int i = 0; i < items.Count(); i++) { Contact item = items[i]; if (item != null) { bool contactExists = _context.Contacts.Any(x => x.Id == item.Id); if (contactExists) { items[i] = _context.Contacts .First(x => x.Id == item.Id); } else { _context.Add(item); items[i] = item; } } } // remove contacts that are no longer attached. foreach (Contact contact in project.Contacts) { if (contact != null && items.All(x => x.Id != contact.Id)) { _context.Remove(contact); } } // replace Contacts project.Contacts = items.ToList(); _context.Update(project); _context.SaveChanges(); return(new ObjectResult(items)); } // record not found return(new StatusCodeResult(404)); }
/// <summary> /// /// </summary> /// <remarks>Updates the active set of groups for a user</remarks> /// <param name="id">id of User to update</param> /// <param name="items"></param> /// <response code="200">OK</response> /// <response code="404">User not found</response> public virtual IActionResult UsersIdGroupsPutAsync(int id, GroupMembershipViewModel[] items) { bool exists = _context.Users.Any(x => x.Id == id); if (exists) { User user = _context.Users .Include(x => x.District) .Include(x => x.GroupMemberships) .ThenInclude(y => y.Group) .Include(x => x.UserRoles) .ThenInclude(y => y.Role) .ThenInclude(z => z.RolePermissions) .ThenInclude(z => z.Permission) .First(x => x.Id == id); if (user.GroupMemberships == null) { user.GroupMemberships = new List <GroupMembership>(); } else { // existing data, clear it. foreach (var groupMembership in user.GroupMemberships) { if (_context.GroupMemberships.Any(x => x.Id == groupMembership.Id)) { GroupMembership delete = _context.GroupMemberships.First(x => x.Id == groupMembership.Id); _context.Remove(delete); } } user.GroupMemberships.Clear(); } foreach (var item in items) { if (item != null) { // check the role id bool group_exists = _context.Groups.Any(x => x.Id == item.GroupId); if (group_exists) { // create a new UserRole based on the view model. GroupMembership groupMembership = new GroupMembership(); Group group = _context.Groups.First(x => x.Id == item.GroupId); groupMembership.Group = group; groupMembership.User = user; _context.Add(groupMembership); if (!user.GroupMemberships.Contains(groupMembership)) { user.GroupMemberships.Add(groupMembership); } } } } _context.Update(user); _context.SaveChanges(); return(new StatusCodeResult(201)); } else { return(new StatusCodeResult(400)); } }
/// <summary> /// Update all roles associated with a user /// </summary> /// <remarks>Updates the roles for a user</remarks> /// <param name="id">id of User to update</param> /// <param name="items"></param> /// <response code="200">OK</response> public virtual IActionResult UsersIdRolesPutAsync(int id, UserRoleViewModel[] items) { bool exists = _context.Users.Any(x => x.Id == id); // not found if (!exists || items == null) { return(new StatusCodeResult(400)); } User user = _context.Users .Include(x => x.District) .Include(x => x.UserRoles) .ThenInclude(y => y.Role) .ThenInclude(z => z.RolePermissions) .ThenInclude(z => z.Permission) .First(x => x.Id == id); if (user.UserRoles == null) { user.UserRoles = new List <UserRole>(); } else { // existing data, clear it foreach (UserRole userRole in user.UserRoles) { if (_context.UserRoles.Any(x => x.Id == userRole.Id)) { UserRole delete = _context.UserRoles.First(x => x.Id == userRole.Id); _context.Remove(delete); } } user.UserRoles.Clear(); } foreach (UserRoleViewModel item in items) { // check the role id bool roleExists = _context.Roles.Any(x => x.Id == item.RoleId); if (roleExists) { // create a new UserRole based on the view model. UserRole userRole = new UserRole(); Role role = _context.Roles.First(x => x.Id == item.RoleId); userRole.Role = role; userRole.EffectiveDate = item.EffectiveDate; userRole.ExpiryDate = item.ExpiryDate; _context.Add(userRole); if (!user.UserRoles.Contains(userRole)) { user.UserRoles.Add(userRole); } } } _context.Update(user); _context.SaveChanges(); return(new StatusCodeResult(200)); }