/// <summary> /// Finds text input with identifier provided and returns its DTO /// </summary> /// <param name="performingUserId"></param> /// <param name="textInput"></param> /// <returns></returns> public ISelectionInputDto GetSelectionInput(int performingUserId, int textInputId) { using (var context = new PrometheusContext()) { return(ManualMapper.MapSelectionInputToDto(context.SelectionInputs.Find(textInputId))); } }
public void ServiceController_ModifyService_Update() { //Set up test int statusId = CreateFakeLifecycleStatus(LifecycleStatusName); int serviceId = CreateFakeService(ServiceName, statusId); IServiceDto fakeService; using (var context = new PrometheusContext()) { var service = context.Services.Find(serviceId); fakeService = ManualMapper.MapServiceToDto(service); } //Do Test Action string originalDescription = fakeService.Description; string fakeDescription = "Updated fake description"; fakeService.Description = fakeDescription; ServiceController controller = new ServiceController(); var result = controller.ModifyService(UserId, fakeService, EntityModification.Update); //Clean up before Assert in case the Assert Fails and you dont reach code beyond it... If you want RemoveFakeService(serviceId); RemoveFakeLifecycleStatus(statusId); //Assert Assert.NotEqual(originalDescription, result.Description); Assert.Equal(fakeDescription, result.Description); }
/// <summary> /// Retrieve a single department /// </summary> /// <param name="performingUserId">user making the request</param> /// <param name="departmentId">department to retrieve</param> /// <returns></returns> public IDepartmentDto GetDepartment(int performingUserId, int departmentId) { using (var context = new PrometheusContext()) { return(ManualMapper.MapDepartmentToDto(context.Departments.FirstOrDefault(x => x.Id == departmentId))); } }
/// <summary> /// Gets the required inputs for all supplied service options /// </summary> /// <param name="performingUserId"></param> /// <param name="serviceOptions">Service Options to get the inputs for</param> /// <returns></returns> public IInputGroupDto GetInputsForServiceOptions(int performingUserId, IEnumerable <IServiceOptionDto> serviceOptions) { if (serviceOptions == null) { base.ThrowArgumentNullError(nameof(serviceOptions)); } var inputGroup = new InputGroupDto(); //Initialize the lists for inputs List <IScriptedSelectionInputDto> scriptedInputs = new List <IScriptedSelectionInputDto>(); List <ISelectionInputDto> selectionInputs = new List <ISelectionInputDto>(); List <ITextInputDto> textInputs = new List <ITextInputDto>(); using (var context = new PrometheusContext()) { var options = serviceOptions.Select(x => context.ServiceOptions.Find(x.Id)); foreach (var option in options) { textInputs.AddRange(from t in option.TextInputs select ManualMapper.MapTextInputToDto(t)); scriptedInputs.AddRange(from t in option.ScriptedSelectionInputs select ManualMapper.MapScriptedSelectionInputToDto(t)); selectionInputs.AddRange(from t in option.SelectionInputs select ManualMapper.MapSelectionInputToDto(t)); } } inputGroup.TextInputs = textInputs; inputGroup.SelectionInputs = selectionInputs; inputGroup.ScriptedSelectionInputs = scriptedInputs; return(inputGroup); }
protected override IUserDto Update(int performingUserId, IUserDto userDto) { if (userDto.Id == AdministratorId) { throw new InvalidOperationException("Administrator account cannot be updated."); } if (userDto.Id == GuestId) { throw new InvalidOperationException("Guest account cannot be updated."); } using (var context = new PrometheusContext()) { if (!context.Users.Any(x => x.Id == userDto.Id)) { throw new InvalidOperationException(string.Format("User with ID {0} cannot be updated since it does not exist.", userDto.Id)); } var updatedUser = ManualMapper.MapDtoToUser(userDto); context.Users.Attach(updatedUser); context.Entry(updatedUser).State = EntityState.Modified; context.SaveChanges(); return(ManualMapper.MapUserToDto(updatedUser)); } }
/// <summary> /// Finds service WorkUnit with identifier provided and returns its DTO /// </summary> /// <param name="performingUserId"></param> /// <param name="serviceWorkUnitId"></param> /// <returns></returns> public IServiceWorkUnitDto GetServiceWorkUnit(int performingUserId, int serviceWorkUnitId) { using (var context = new PrometheusContext()) { return(ManualMapper.MapServiceWorkUnitToDto(context.ServiceWorkUnits.Find(serviceWorkUnitId))); } }
/// <summary> /// Finds service Measure with identifier provided and returns its DTO /// </summary> /// <param name="performingUserId"></param> /// <param name="serviceMeasureId"></param> /// <returns></returns> public IServiceMeasureDto GetServiceMeasure(int performingUserId, int serviceMeasureId) { using (var context = new PrometheusContext()) { return(ManualMapper.MapServiceMeasureToDto(context.ServiceMeasures.Find(serviceMeasureId))); } }
public void ServiceController_SetServiceBundleForServices() { //Set up test int statusId = CreateFakeLifecycleStatus(LifecycleStatusName); int bundleId = CreateFakeServiceBundle(BundleName); int serviceId = CreateFakeService(ServiceName, statusId, bundleId); IEnumerable <IServiceDto> services; using (var context = new PrometheusContext()) { var service = context.Services.Find(serviceId); services = new List <IServiceDto>() { ManualMapper.MapServiceToDto(service) }; } //Do Test Action ServiceController controller = new ServiceController(); var result = controller.SetServiceBundleForServices(UserId, bundleId, services); //Clean up before Assert in case the Assert Fails and you dont reach code beyond it... If you want RemoveFakeService(serviceId); RemoveFakeServiceBundle(bundleId); RemoveFakeLifecycleStatus(statusId); //Assert Assert.Equal(bundleId, result.First().ServiceBundleId); }
/// <summary> /// Adds the roles to the specified user if permission allows it /// </summary> /// <param name="performingUserId">User performing the role addition</param> /// <param name="adjustedUserId">User having roles added</param> /// <param name="rolesToAdd">Roles to be added to the user</param> /// <returns></returns> public IEnumerable <IRoleDto> AddRolesToUser(int performingUserId, int adjustedUserId, IEnumerable <IRoleDto> rolesToAdd) { using (var context = new PrometheusContext()) { List <Role> newRoles = new List <Role>(); foreach (var role in rolesToAdd) { newRoles.Add((from r in context.Roles where r.Id == role.Id select r).First()); /* attach context objects */ } if (!context.Users.Any(x => x.Id == adjustedUserId)) { throw new EntityNotFoundException("Could not add Roles to User.", typeof(User), adjustedUserId); } var updatedUser = context.Users.Find(adjustedUserId); updatedUser.Roles = new List <Role>(); context.Users.Attach(updatedUser); foreach (var role in newRoles) { updatedUser.Roles.Add(role); } context.Entry(updatedUser).State = EntityState.Modified; context.SaveChanges(); foreach (var updatedUserRole in updatedUser.Roles) { yield return(ManualMapper.MapRoleToDto(updatedUserRole)); } } }
/// <summary> /// Finds service goal with identifier provided and returns its DTO /// </summary> /// <param name="performingUserId"></param> /// <param name="serviceGoalId"></param> /// <returns></returns> public IServiceGoalDto GetServiceGoal(int performingUserId, int serviceGoalId) { using (var context = new PrometheusContext()) { return(ManualMapper.MapServiceGoalToDto(context.ServiceGoals.Find(serviceGoalId))); } }
/// <summary> /// Changes the state of a service request to Submitted if the action is possible. /// </summary> /// <param name="userId">ID of user Submitting the request</param> /// <param name="requestId">ID of Service Request to Submit</param> /// <returns>Service Request after Submition is attempted</returns> public IServiceRequestDto <IServiceRequestOptionDto, IServiceRequestUserInputDto> SubmitRequest(int userId, int requestId) { IServiceRequestDto <IServiceRequestOptionDto, IServiceRequestUserInputDto> request = RequestFromId(requestId); if (request.State != ServiceRequestState.Incomplete) { throw new ServiceRequestStateException( string.Format("Cannot change the state of a Service Request to \"{0}\". " + "Service Request is in the \"{1}\" state and must be in the " + "\"{2}\" state to perform this action.", ServiceRequestState.Submitted, request.State, ServiceRequestState.Incomplete)); } if (UserCanSubmitRequest(userId, requestId)) { using (var context = new PrometheusContext()) { var requestEntity = context.ServiceRequests.Find(requestId); //Change state of the entity requestEntity.State = ServiceRequestState.Submitted; requestEntity.SubmissionDate = DateTime.UtcNow; //Save context.Entry(requestEntity).State = EntityState.Modified; context.SaveChanges(userId); request = ManualMapper.MapServiceRequestToDto(requestEntity); } } return(request); }
/// <summary> /// Retrieves the service packages that the service option id exists in /// </summary> /// <param name="performingUserId"></param> /// <param name="serviceOptionId"></param> /// <returns></returns> public IEnumerable <IServiceRequestPackageDto> GetServiceRequestPackagesForServiceOption(int performingUserId, int serviceOptionId, ServiceRequestAction action) { using (var context = new PrometheusContext()) { var option = context.ServiceOptions.Find(serviceOptionId); if (option == null) { throw new InvalidOperationException(string.Format("Service Option with ID {0} does not exist. Cannot retrieve service package with option identifier {0}.", serviceOptionId)); } //All packages where the service option exists in the first category of the package // OR the service option exists in the first service of the package var packages = context.ServiceRequestPackages.Where( x => x.Action == action && (x.ServiceOptionCategoryTags.Any( y => y.Order == 1 && y.ServiceOptionCategory.ServiceOptions.Any( z => z.Id == serviceOptionId)) || x.ServiceTags.Any( y => y.Order == 1 && y.Service.ServiceOptionCategories.Any( z => z.Id == serviceOptionId)))); //Sweet baby jesus if (!packages.Any()) { throw new InvalidOperationException(string.Format("Service Request Package with Service Option ID {0} does not exist.", serviceOptionId)); } foreach (var package in packages) { yield return(ManualMapper.MapServiceRequestPackageToDto(package)); } } }
protected override IServiceRequestPackageDto Create(int performingUserId, IServiceRequestPackageDto entity) { using (var context = new PrometheusContext()) { var servicePackage = context.ServiceRequestPackages.Find(entity.Id); if (servicePackage != null) { throw new InvalidOperationException(string.Format("Service Request Package with ID {0} already exists.", entity.Id)); } var savedPackage = context.ServiceRequestPackages.Add(ManualMapper.MapDtoToServiceRequestPackage(entity)); //Set tags to match DTO tags var categoryTags = new List <ServiceOptionCategoryTag>(); foreach (var tag in entity.ServiceOptionCategoryTags) { categoryTags.Add(ManualMapper.MapDtoToServiceOptionCategoryTag(tag)); } savedPackage.ServiceOptionCategoryTags = categoryTags; var serviceTags = new List <ServiceTag>(); foreach (var tag in entity.ServiceTags) { serviceTags.Add((ManualMapper.MapDtoToServiceTag(tag))); } savedPackage.ServiceTags = serviceTags; context.SaveChanges(performingUserId); return(ManualMapper.MapServiceRequestPackageToDto(savedPackage)); } }
/// <summary> /// Creates the entity in the database /// </summary> /// <param name="performingUserId">User creating the entity</param> /// <param name="entity">Entity to be created</param> /// <returns>Created entity DTO</returns> protected override ILifecycleStatusDto Create(int performingUserId, ILifecycleStatusDto lifecycleStatus) { using (var context = new PrometheusContext()) { var existingStatus = context.LifecycleStatuses.Find(lifecycleStatus.Id); if (existingStatus == null) { //Insert at correct Position foreach (var status in context.LifecycleStatuses) { if (status.Position >= lifecycleStatus.Position) { status.Position++; context.LifecycleStatuses.Attach(status); context.Entry(status).State = EntityState.Modified; } } var savedStatus = context.LifecycleStatuses.Add(ManualMapper.MapDtoToLifecycleStatus(lifecycleStatus)); context.SaveChanges(performingUserId); return(ManualMapper.MapLifecycleStatusToDto(savedStatus)); } else { throw new InvalidOperationException(string.Format("Lifecycle Status with ID {0} already exists.", lifecycleStatus.Id)); } } }
/// <summary> /// Finds SWOT activity with identifier provided and returns its DTO /// </summary> /// <param name="performingUserId"></param> /// <param name="swotActivityId"></param> /// <returns></returns> public ISwotActivityDto GetSwotActivity(int performingUserId, int swotActivityId) { using (var context = new PrometheusContext()) { return(ManualMapper.MapSwotActivityToDto(context.SwotActivities.Find(swotActivityId))); } }
public void ServiceController_ModifyService_Delete() { //Set up test int statusId = CreateFakeLifecycleStatus(LifecycleStatusName); int serviceId = CreateFakeService(ServiceName, statusId); IServiceDto fakeService; using (var context = new PrometheusContext()) { var service = context.Services.Find(serviceId); fakeService = ManualMapper.MapServiceToDto(service); } //Do Test Action ServiceController controller = new ServiceController(); var result = controller.ModifyService(UserId, fakeService, EntityModification.Delete); //Clean up before Assert in case the Assert Fails and you dont reach code beyond it... If you want //This test cleans service up if it works RemoveFakeLifecycleStatus(statusId); //Assert bool serviceExists; using (var context = new PrometheusContext()) { serviceExists = context.Services.Any(x => x.Id == serviceId); } Assert.Equal(false, serviceExists); }
/// <summary> /// returns a count of the number of Lifecycle statuses found /// </summary> /// <returns></returns> public int CountLifecycleStatuses() { using (var context = new PrometheusContext()) { return(context.LifecycleStatuses.Count()); } }
/// <summary> /// Finds service document with identifier provided and returns its DTO /// </summary> /// <param name="performingUserId"></param> /// <param name="serviceDocumentId"></param> /// <returns></returns> public IServiceDocumentDto GetServiceDocument(int performingUserId, int serviceDocumentId) { using (var context = new PrometheusContext()) { var document = context.ServiceDocuments.ToList().FirstOrDefault(x => x.Id == serviceDocumentId); return(ManualMapper.MapServiceDocumentToDto(document)); } }
/// <summary> /// Finds service bundle with identifier provided and returns its DTO /// </summary> /// <param name="serviceBundleId"></param> /// <returns></returns> public IServiceBundleDto GetServiceBundle(int serviceBundleId) { using (var context = new PrometheusContext()) { var serviceBundle = context.ServiceBundles.Find(serviceBundleId); return(ManualMapper.MapServiceBundleToDto(serviceBundle)); } }
/// <summary> /// Finds service with identifier provided and returns its DTO /// </summary> /// <param name="serviceId"></param> /// <returns></returns> public IServiceDto GetService(int serviceId) { using (var context = new PrometheusContext()) { var service = context.Services.Include(x => x.ServiceDocuments).FirstOrDefault(x => x.Id == serviceId); return(ManualMapper.MapServiceToDto(service)); } }
private void RemoveFakeServiceBundle(int bundleId) { using (var context = new PrometheusContext()) { var bundle = context.ServiceBundles.Find(bundleId); context.ServiceBundles.Remove(bundle); context.SaveChanges(); } }
private void RemoveFakeDocument(int documentId) { using (var context = new PrometheusContext()) { var document = context.ServiceDocuments.Find(documentId); context.ServiceDocuments.Remove(document); context.SaveChanges(); } }
private void RemoveFakeRequest(int requestId) { using (var context = new PrometheusContext()) { var request = context.ServiceRequests.Find(requestId); context.ServiceRequests.Remove(request); context.SaveChanges(); } }
/// <summary> /// Retrieves the unique identifier of the Department Script from ID provided /// </summary> /// <param name="scriptId"></param> /// <returns></returns> public Guid GetDepartmentScriptFromId(int scriptId) { using (var context = new PrometheusContext()) { var document = context.Scripts.ToList().FirstOrDefault(x => x.Id == scriptId); var department = document.ScriptFile; return(department); } }
private void RemoveFakeLifecycleStatus(int lifecycleStatusId) { using (var context = new PrometheusContext()) { var status = context.LifecycleStatuses.Find(lifecycleStatusId); context.LifecycleStatuses.Remove(status); context.SaveChanges(); } }
private void RemoveFakeService(int serviceId) { using (var context = new PrometheusContext()) { var service = context.Services.Find(serviceId); context.Services.Remove(service); context.SaveChanges(); } }
/// <summary> /// Changes the state of a service request to the result of the Approval if the action is possible. /// </summary> /// <param name="userId">ID of user Approving the request</param> /// <param name="requestId">ID of Service Request to Approve</param> /// <param name="approvalResult">Result of the approval transaction (approved or denied)</param> /// <param name="comments">Optional: Comments tied to the Approval if applicable</param> /// <returns>Service Request after Approval is attempted</returns> public IServiceRequestDto <IServiceRequestOptionDto, IServiceRequestUserInputDto> ApproveRequest(int userId, int requestId, ApprovalResult approvalResult, string comments) { IServiceRequestDto <IServiceRequestOptionDto, IServiceRequestUserInputDto> request = RequestFromId(requestId); if (request.State != ServiceRequestState.Submitted) { throw new ServiceRequestStateException( string.Format("Cannot change the state of a Service Request to \"{0}\". " + "Service Request is in the \"{1}\" state and must be in the " + "\"{2}\" state to perform this action.", approvalResult, request.State, ServiceRequestState.Submitted)); } if (UserCanApproveRequest(userId, requestId)) { using (var context = new PrometheusContext()) { //Build and save approval transaction Approval approval = new Approval() { ApproverId = userId, Comments = comments, RequestorId = request.RequestedByUserId, ServiceRequestId = request.Id, Result = approvalResult }; context.Approvals.Add(approval); context.SaveChanges(userId); //Change state of the entity var requestEntity = context.ServiceRequests.Find(requestId); ClearTemporaryFields(requestEntity); //Approve or deny if (approvalResult == ApprovalResult.Approved) { requestEntity.State = ServiceRequestState.Approved; requestEntity.ApprovedDate = DateTime.UtcNow; } else { requestEntity.State = ServiceRequestState.Denied; requestEntity.DeniedDate = DateTime.UtcNow; } requestEntity.FinalUpfrontPrice = requestEntity.UpfrontPrice; requestEntity.FinalMonthlyPrice = requestEntity.MonthlyPrice; context.Entry(requestEntity).State = EntityState.Modified; context.SaveChanges(userId); request = ManualMapper.MapServiceRequestToDto(requestEntity); } } return(request); }
/// <summary> /// Returns all service bundles /// </summary> /// <returns></returns> public IEnumerable <IServiceBundleDto> GetServiceBundles() { using (var context = new PrometheusContext()) { foreach (var bundle in context.ServiceBundles) { yield return(ManualMapper.MapServiceBundleToDto(bundle)); } } }
/// <summary> /// Deletes the entity in the database /// </summary> /// <param name="performingUserId">User deleting the entity</param> /// <param name="entity">Entity to be deleted</param> /// <returns>Deleted entity. null if deletion was successfull</returns> protected override ISelectionInputDto Delete(int performingUserId, ISelectionInputDto entity) { using (var context = new PrometheusContext()) { var toDelete = context.SelectionInputs.Find(entity.Id); context.SelectionInputs.Remove(toDelete); context.SaveChanges(performingUserId); } return(null); }
protected override IServiceDocumentDto Delete(int performingUserId, IServiceDocumentDto document) { using (var context = new PrometheusContext()) { var toDelete = context.ServiceDocuments.ToList().FirstOrDefault(x => x.Id == document.Id); context.ServiceDocuments.Remove(toDelete); context.SaveChanges(performingUserId); } return(null); }