public EntityResponse <PermissionsCategory> GetWithPermissionList(Guid id) { EntityResponse <PermissionsCategory> res = GetByID(id, PermissionNavigationOptions.IncludeAll); try { var permissionComparer = new PermissionComparer(); if (res.Success) { var category = res.Entity; var combinRes = GetAllPermissionsCombinations(category); if (combinRes.Success) { var currPerms = category.PermissionProperties .SelectMany(prop => prop.PropertyValues .SelectMany(val => val.PermissionPrices)) .Distinct(permissionComparer); //.Where(perm => category.PermissionProperties // .All(prop => perm.PropertyValues // .Any(val => val.PermissionsCategoryID == prop.ID))) // .ToList(); res.Entity.Permissions = combinRes.Entities.ToList(); for (int i = 0; i < res.Entity.Permissions.Count; i++) { var perm = res.Entity.Permissions[i]; var existPerm = currPerms.FirstOrDefault(currPerm => perm.PropertyValues.All(val => currPerm.PropertyValues.Any(currVal => currVal.ID == val.ID))); if (existPerm != null) { perm.ID = existPerm.ID; perm.PermissionCategoryID = existPerm.PermissionCategoryID; perm.PermissionCost = existPerm.PermissionCost; } } res.Success = true; } else { res.Success = false; res.Message = combinRes.Message; } } } catch (Exception e) { res.Success = false; res.Message = e.ToString(); } return(res); }
public ResponseBase UpdatePermissions(PermissionsCategory category) { ResponseBase res = new ResponseBase(); try { var getRes = GetByID(category.ID, PermissionNavigationOptions.IncludeAll); PermissionComparer permissionComparer = new PermissionComparer(); List <Permission> lstAdd = new List <Permission>(); if (getRes.Success) { var dbVals = getRes.Entity.PermissionProperties .SelectMany(prop => prop.PropertyValues); var dbPerms = dbVals .SelectMany(val => val.PermissionPrices) .Distinct(permissionComparer); foreach (var perm in category.Permissions) { var dbPerm = dbPerms.FirstOrDefault(currPerm => currPerm.ID == perm.ID); if (dbPerm == null) { if (perm.PermissionCost.HasValue) { perm.PropertyValues = perm.PropertyValues.Select(val => dbVals.First(dbVal => dbVal.ID == val.ID)).ToList(); lstAdd.Add(perm); } } else { dbPerm.PermissionCost = perm.PermissionCost; } } res = _permissionService.UpdateAndAddList(dbPerms, lstAdd); } else { res = getRes; } } catch (Exception e) { res.Success = false; res.Message = e.ToString(); } return(res); }
public EntityResponse <PurchaseDetails> CheckPermissionCost(IEnumerable <Guid> valueIds, int musicId, Guid categoryId, bool createPermissionIfNotExist = false) { var res = new EntityResponse <PurchaseDetails>(); res.Success = true; res.Entity = new PurchaseDetails(); res.Entity.Cost = 0; try { var music = _mscRep.GetByID(musicId); var getCatRes = GetByID(categoryId, PermissionNavigationOptions.IncludeAll); PermissionComparer permissionComperer = new PermissionComparer(); res.Success = getCatRes.Success; if (getCatRes.Success) { var properties = getCatRes.Entity.PermissionProperties.Where(prop => prop.IsActive); var permissions = properties .SelectMany(prop => prop.PropertyValues.Where(val => val.IsActive) .SelectMany(val => val.PermissionPrices)) .Distinct(permissionComperer); var costProp = properties.Where(prop => prop.IsCostLevel); var nonCostProp = properties.Except(costProp); var nonCostValues = nonCostProp.Select(prop => prop.PropertyValues .Single(val => val.IsActive && valueIds.Any(valId => valId == val.ID))); var costValues = GetCostLevelValuesFromMusic(music, costProp); if (costValues.Any()) { var allValues = costValues.Select(costVal => costVal.ID).Union(valueIds); var permission = permissions.FirstOrDefault(perm => allValues.All(valId => perm.PropertyValues.Any(permVal => valId == permVal.ID))); var cost = permission?.PermissionCost; res.Entity.Cost = cost.HasValue ? cost.Value : 0; res.Entity.Music = music; res.Entity.PermissionValueNames = nonCostValues.Select(val => val.Name); res.Entity.Permission = permission; res.Entity.IsExistPermission = permission == null && !createPermissionIfNotExist; if (res.Entity.Permission == null) { if (createPermissionIfNotExist) { var getOrAddRes = GetOrAdd(costValues.Union(nonCostValues).Select(val => val.ID), categoryId); res.Success = getOrAddRes.Success; res.Entity.Permission = getOrAddRes.Entity; } else { res.Entity.Permission = new Permission(categoryId, null); res.Entity.Permission.PropertyValues = costValues.Union(nonCostValues).ToList(); } } } } } catch (Exception e) { res.Message = "אירעה שגיאה בעת הבאת מחיר הרשיון"; res.Success = false; } return(res); }