public async Task <bool> DeleteOrganizationAsync(User user, List <long> orgIds)
        {
            try
            {
                foreach (var orgid in orgIds)
                {
                    if (_context.Users.Any(c => c.OrganizationId == orgid))
                    {
                        throw new ExperienceManagementGlobalException(OrganizationServiceErrors.UserInOrganizationExistError);
                    }
                    var parent = _context.Organizations.Include(p => p.Children)
                                 .SingleOrDefault(p => p.Id == orgid);

                    foreach (var child in parent.Children.ToList())
                    {
                        if (!orgIds.Contains(child.Id))
                        {
                            throw new ExperienceManagementGlobalException(OrganizationServiceErrors.DeleteOrgNotselectedError);
                        }
                        await DeleteOrganizationByChildAsync(child, orgIds);

                        _context.Organizations.Remove(child);
                    }
                    _context.Organizations.Remove(parent);
                }
                _context.SaveChanges();
                return(true);
            }
            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(OrganizationServiceErrors.DeleteOrganizationError, ex);
            }
        }
        public async Task <bool> DeleteEquipmentAsync(User user, List <long> equipmentIds)
        {
            try
            {
                foreach (var equipmentid in equipmentIds)
                {
                    if (_context.EquipmentUserSubscriptions.Any(c => c.EquipmentId == equipmentid))
                    {
                        throw new ExperienceManagementGlobalException(EquipmentServiceErrors.UserInEquipmentExistError);
                    }
                    var parent = _context.Equipments.Include(p => p.Children)
                                 .SingleOrDefault(p => p.Id == equipmentid);

                    foreach (var child in parent.Children.ToList())
                    {
                        if (!equipmentIds.Contains(child.Id))
                        {
                            throw new ExperienceManagementGlobalException(EquipmentServiceErrors.DeleteEquipmentNotselectedError);
                        }
                        await DeleteEquipmentByChildAsync(child, equipmentIds);

                        _context.Equipments.Remove(child);
                    }
                    _context.Equipments.Remove(parent);
                }
                _context.SaveChanges();
                return(true);
            }

            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(EquipmentServiceErrors.DeleteEquipmentError, ex);
            }
        }
Example #3
0
        public async Task <Guid> AddUserAsync(User user, AddUserFormModel userForm)
        {
            try
            {
                if (_context.Users.Any(c => c.Username.ToLower() == userForm.Username.ToLower()))
                {
                    throw new ExperienceManagementGlobalException(UsersServiceErrors.UserExistError);
                }
                var groupRole = await _context.GroupAuthRoles.Where(c => c.GroupAuthId == userForm.GroupAuthId).ToListAsync();

                if (groupRole == null)
                {
                    throw new ExperienceManagementGlobalException(UsersServiceErrors.GroupNotFoundError);
                }

                var userReg = new User()
                {
                    GroupAuthId  = userForm.GroupAuthId,
                    FirstName    = userForm.FirstName,
                    Description  = userForm.Description,
                    IsActive     = userForm.IsActive,
                    IsPresident  = userForm.IsPresident,
                    LastName     = userForm.LastName,
                    Password     = _securityService.GetSha256Hash(userForm.Password),
                    SerialNumber = Guid.NewGuid().ToString("N"),
                    Location     = userForm.Location,
                    PhoneNumber  = userForm.PhoneNumber,
                    Username     = userForm.Username.ToLower()
                };
                _context.Users.Add(userReg);
                _context.UserRoles.AddRange(groupRole.Select(c => c.RoleId).ToList().Select(roleId =>
                                                                                            new UserRole
                {
                    User   = userReg,
                    RoleId = roleId
                }));
                _context.SaveChanges();
                return(userReg.Id);
            }
            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(UsersServiceErrors.AddUserError, ex);
            }
        }