private EoD GetEoD(EoD request) { var id = request?.Id; EoD ret = null; var query = DocQuery.ActiveQuery ?? Execute; DocPermissionFactory.SetSelect <EoD>(currentUser, "EoD", request.Select); DocEntityEoD entity = null; if (id.HasValue) { entity = DocEntityEoD.Get(id.Value); } if (null == entity) { throw new HttpError(HttpStatusCode.NotFound, $"No EoD 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 EoD Post(EoD request) { if (request == null) { throw new HttpError(HttpStatusCode.NotFound, "Request cannot be null."); } request.Select = request.Select ?? new List <string>(); EoD ret = null; using (Execute) { Execute.Run(ssn => { if (!DocPermissionFactory.HasPermissionTryAdd(currentUser, "EoD")) { throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route."); } ret = _AssignValues(request, DocConstantPermission.ADD, ssn); }); } return(ret); }
public void Delete(EoD request) { using (Execute) { Execute.Run(ssn => { if (!(request?.Id > 0)) { throw new HttpError(HttpStatusCode.NotFound, $"No Id provided for delete."); } var en = DocEntityEoD.Get(request?.Id); if (null == en) { throw new HttpError(HttpStatusCode.NotFound, $"No EoD 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.EOD); DocCacheClient.RemoveById(request.Id); }); } }
public EoD Post(EoDCopy request) { EoD ret = null; using (Execute) { Execute.Run(ssn => { var entity = DocEntityEoD.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 pAssignee = entity.Assignee; var pData = entity.Data; var pDescription = entity.Description; if (!DocTools.IsNullOrEmpty(pDescription)) { pDescription += " (Copy)"; } var pDocument = entity.Document; var pDueDate = entity.DueDate; var pReporter = entity.Reporter; var pStatus = entity.Status; var pType = entity.Type; var pWorkflow = entity.Workflow; var copy = new DocEntityEoD(ssn) { Hash = Guid.NewGuid() , Assignee = pAssignee , Data = pData , Description = pDescription , Document = pDocument , DueDate = pDueDate , Reporter = pReporter , Status = pStatus , Type = pType , Workflow = pWorkflow }; copy.SaveChanges(DocConstantPermission.ADD); ret = copy.ToDto(); }); } return(ret); }
public EoD Patch(EoD request) { if (true != (request?.Id > 0)) { throw new HttpError(HttpStatusCode.NotFound, "Please specify a valid Id of the EoD to patch."); } request.Select = request.Select ?? new List <string>(); EoD ret = null; using (Execute) { Execute.Run(ssn => { ret = _AssignValues(request, DocConstantPermission.EDIT, ssn); }); } return(ret); }
public EoD Put(EoD request) { return(Patch(request)); }
private EoD _AssignValues(EoD 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, "EoD")) { throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route."); } request.Select = request.Select ?? new List <string>(); EoD ret = null; request = _InitAssignValues <EoD>(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 <EoD>(DocConstantModelName.EOD, nameof(EoD), request); //First, assign all the variables, do database lookups and conversions var pAssignee = DocEntityUser.Get(request.Assignee?.Id, true, Execute) ?? DocEntityUser.Get(request.AssigneeId, true, Execute); var pData = request.Data; var pDescription = request.Description; var pDocument = DocEntityDocument.Get(request.Document?.Id, true, Execute) ?? DocEntityDocument.Get(request.DocumentId, true, Execute); var pDueDate = request.DueDate; var pReporter = DocEntityUser.Get(request.Reporter?.Id, true, Execute) ?? DocEntityUser.Get(request.ReporterId, true, Execute); var pStatus = request.Status; var pType = request.Type; var pWorkflow = DocEntityWorkflow.Get(request.Workflow?.Id, true, Execute) ?? DocEntityWorkflow.Get(request.WorkflowId, true, Execute); var pArchived = true == request.Archived; var pLocked = request.Locked; var entity = InitEntity <DocEntityEoD, EoD>(request, permission, session); if (AllowPatchValue <EoD, bool>(request, DocConstantModelName.EOD, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived)) { entity.Archived = pArchived; } if (AllowPatchValue <EoD, DocEntityUser>(request, DocConstantModelName.EOD, pAssignee, permission, nameof(request.Assignee), pAssignee != entity.Assignee)) { entity.Assignee = pAssignee; } if (AllowPatchValue <EoD, string>(request, DocConstantModelName.EOD, pData, permission, nameof(request.Data), pData != entity.Data)) { entity.Data = pData; } if (AllowPatchValue <EoD, string>(request, DocConstantModelName.EOD, pDescription, permission, nameof(request.Description), pDescription != entity.Description)) { entity.Description = pDescription; } if (AllowPatchValue <EoD, DocEntityDocument>(request, DocConstantModelName.EOD, pDocument, permission, nameof(request.Document), pDocument != entity.Document)) { entity.Document = pDocument; } if (AllowPatchValue <EoD, DateTime?>(request, DocConstantModelName.EOD, pDueDate, permission, nameof(request.DueDate), pDueDate != entity.DueDate)) { entity.DueDate = pDueDate; } if (AllowPatchValue <EoD, DocEntityUser>(request, DocConstantModelName.EOD, pReporter, permission, nameof(request.Reporter), pReporter != entity.Reporter)) { entity.Reporter = pReporter; } if (AllowPatchValue <EoD, EoDStatusEnm?>(request, DocConstantModelName.EOD, pStatus, permission, nameof(request.Status), pStatus != entity.Status)) { if (null != pStatus) { entity.Status = pStatus.Value; } } if (AllowPatchValue <EoD, TaskTypeEnm?>(request, DocConstantModelName.EOD, pType, permission, nameof(request.Type), pType != entity.Type)) { if (null != pType) { entity.Type = pType.Value; } } if (AllowPatchValue <EoD, DocEntityWorkflow>(request, DocConstantModelName.EOD, pWorkflow, permission, nameof(request.Workflow), pWorkflow != entity.Workflow)) { entity.Workflow = pWorkflow; } if (request.Locked && AllowPatchValue <EoD, bool>(request, DocConstantModelName.EOD, pArchived, permission, nameof(request.Locked), pLocked != entity.Locked)) { entity.Archived = pArchived; } entity.SaveChanges(permission); var idsToInvalidate = new List <int>(); if (idsToInvalidate.Any()) { idsToInvalidate.Add(entity.Id); DocCacheClient.RemoveByEntityIds(idsToInvalidate); DocCacheClient.RemoveSearch(DocConstantModelName.EOD); } entity.SaveChanges(permission); DocPermissionFactory.SetSelect <EoD>(currentUser, nameof(EoD), request.Select); ret = entity.ToDto(); var cacheExpires = DocResources.Metadata.GetCacheExpiration(DocConstantModelName.EOD); DocCacheClient.Set(key: cacheKey, value: ret, entityId: request.Id, entityType: DocConstantModelName.EOD, cacheExpires); return(ret); }
public object Get(EoD request) => GetEntityWithCache <EoD>(DocConstantModelName.EOD, request, GetEoD);