private LookupCategory GetLookupCategory(LookupCategory request) { var id = request?.Id; LookupCategory ret = null; var query = DocQuery.ActiveQuery ?? Execute; DocPermissionFactory.SetSelect <LookupCategory>(currentUser, "LookupCategory", request.Select); DocEntityLookupCategory entity = null; if (id.HasValue) { entity = DocEntityLookupCategory.Get(id.Value); } if (null == entity) { throw new HttpError(HttpStatusCode.NotFound, $"No LookupCategory found for Id {id.Value}"); } if (!DocPermissionFactory.HasPermission(entity, currentUser, DocConstantPermission.VIEW)) { throw new HttpError(HttpStatusCode.Forbidden, "You do not have VIEW permission for this route."); } ret = entity?.ToDto(); return(ret); }
public void Delete(LookupCategory request) { using (Execute) { Execute.Run(ssn => { if (!(request?.Id > 0)) { throw new HttpError(HttpStatusCode.NotFound, $"No Id provided for delete."); } var en = DocEntityLookupCategory.Get(request?.Id); if (null == en) { throw new HttpError(HttpStatusCode.NotFound, $"No LookupCategory could be found for Id {request?.Id}."); } if (en.IsRemoved) { return; } if (!DocPermissionFactory.HasPermission(en, currentUser, DocConstantPermission.DELETE)) { throw new HttpError(HttpStatusCode.Forbidden, "You do not have DELETE permission for this route."); } en.Remove(); DocCacheClient.RemoveSearch(DocConstantModelName.LOOKUPCATEGORY); DocCacheClient.RemoveById(request.Id); }); } }
public LookupCategory Post(LookupCategoryCopy request) { LookupCategory ret = null; using (Execute) { Execute.Run(ssn => { var entity = DocEntityLookupCategory.Get(request?.Id); if (null == entity) { throw new HttpError(HttpStatusCode.NoContent, "The COPY request did not succeed."); } if (!DocPermissionFactory.HasPermission(entity, currentUser, DocConstantPermission.ADD)) { throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route."); } var pCategory = entity.Category; if (!DocTools.IsNullOrEmpty(pCategory)) { pCategory += " (Copy)"; } var pEnum = entity.Enum; var pLookups = entity.Lookups.ToList(); var pParentCategory = entity.ParentCategory; var copy = new DocEntityLookupCategory(ssn) { Hash = Guid.NewGuid() , Category = pCategory , Enum = pEnum , ParentCategory = pParentCategory }; foreach (var item in pLookups) { entity.Lookups.Add(item); } copy.SaveChanges(DocConstantPermission.ADD); ret = copy.ToDto(); }); } return(ret); }
private LookupCategory _AssignValues(LookupCategory request, DocConstantPermission permission, Session session) { if (permission != DocConstantPermission.ADD && (request == null || request.Id <= 0)) { throw new HttpError(HttpStatusCode.NotFound, $"No record"); } if (permission == DocConstantPermission.ADD && !DocPermissionFactory.HasPermissionTryAdd(currentUser, "LookupCategory")) { throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route."); } request.Select = request.Select ?? new List <string>(); LookupCategory ret = null; request = _InitAssignValues <LookupCategory>(request, permission, session); //In case init assign handles create for us, return it if (permission == DocConstantPermission.ADD && request.Id > 0) { return(request); } var cacheKey = GetApiCacheKey <LookupCategory>(DocConstantModelName.LOOKUPCATEGORY, nameof(LookupCategory), request); //First, assign all the variables, do database lookups and conversions var pCategory = request.Category; var pEnum = DocEntityLookupTableEnum.Get(request.Enum); var pLookups = GetVariable <Reference>(request, nameof(request.Lookups), request.Lookups?.ToList(), request.LookupsIds?.ToList()); var pParentCategory = DocEntityLookupCategory.Get(request.ParentCategory?.Id, true, Execute) ?? DocEntityLookupCategory.Get(request.ParentCategoryId, true, Execute); var pArchived = true == request.Archived; var pLocked = request.Locked; var entity = InitEntity <DocEntityLookupCategory, LookupCategory>(request, permission, session); if (AllowPatchValue <LookupCategory, bool>(request, DocConstantModelName.LOOKUPCATEGORY, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived)) { entity.Archived = pArchived; } if (AllowPatchValue <LookupCategory, string>(request, DocConstantModelName.LOOKUPCATEGORY, pCategory, permission, nameof(request.Category), pCategory != entity.Category)) { entity.Category = pCategory; } if (AllowPatchValue <LookupCategory, DocEntityLookupTableEnum>(request, DocConstantModelName.LOOKUPCATEGORY, pEnum, permission, nameof(request.Enum), pEnum != entity.Enum)) { entity.Enum = pEnum; } if (AllowPatchValue <LookupCategory, DocEntityLookupCategory>(request, DocConstantModelName.LOOKUPCATEGORY, pParentCategory, permission, nameof(request.ParentCategory), pParentCategory != entity.ParentCategory)) { entity.ParentCategory = pParentCategory; } if (request.Locked && AllowPatchValue <LookupCategory, bool>(request, DocConstantModelName.LOOKUPCATEGORY, pArchived, permission, nameof(request.Locked), pLocked != entity.Locked)) { entity.Archived = pArchived; } entity.SaveChanges(permission); var idsToInvalidate = new List <int>(); idsToInvalidate.AddRange(PatchCollection <LookupCategory, DocEntityLookupCategory, Reference, DocEntityLookupTable>(request, entity, pLookups, permission, nameof(request.Lookups))); if (idsToInvalidate.Any()) { idsToInvalidate.Add(entity.Id); DocCacheClient.RemoveByEntityIds(idsToInvalidate); DocCacheClient.RemoveSearch(DocConstantModelName.LOOKUPCATEGORY); } entity.SaveChanges(permission); DocPermissionFactory.SetSelect <LookupCategory>(currentUser, nameof(LookupCategory), request.Select); ret = entity.ToDto(); var cacheExpires = DocResources.Metadata.GetCacheExpiration(DocConstantModelName.LOOKUPCATEGORY); DocCacheClient.Set(key: cacheKey, value: ret, entityId: request.Id, entityType: DocConstantModelName.LOOKUPCATEGORY, cacheExpires); return(ret); }