PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Atlassian.Jira/Remote/ProjectComponentService.cs

https://bitbucket.org/farmas/atlassian.net-sdk
C# | 69 lines | 58 code | 11 blank | 0 comment | 1 complexity | 125e834e38cef5576313e36a721a1592 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using RestSharp;
  9. namespace Atlassian.Jira.Remote
  10. {
  11. internal class ProjectComponentService : IProjectComponentService
  12. {
  13. private readonly Jira _jira;
  14. public ProjectComponentService(Jira jira)
  15. {
  16. _jira = jira;
  17. }
  18. public async Task<ProjectComponent> CreateComponentAsync(ProjectComponentCreationInfo projectComponent, CancellationToken token = default(CancellationToken))
  19. {
  20. var serializer = JsonSerializer.Create(_jira.RestClient.Settings.JsonSerializerSettings);
  21. var resource = "/rest/api/2/component";
  22. var requestBody = JToken.FromObject(projectComponent, serializer);
  23. var remoteComponent = await _jira.RestClient.ExecuteRequestAsync<RemoteComponent>(Method.POST, resource, requestBody, token).ConfigureAwait(false);
  24. remoteComponent.ProjectKey = projectComponent.ProjectKey;
  25. var component = new ProjectComponent(remoteComponent);
  26. _jira.Cache.Components.TryAdd(component);
  27. return component;
  28. }
  29. public async Task DeleteComponentAsync(string componentId, string moveIssuesTo = null, CancellationToken token = default(CancellationToken))
  30. {
  31. var resource = String.Format("/rest/api/2/component/{0}?{1}",
  32. componentId,
  33. String.IsNullOrEmpty(moveIssuesTo) ? null : "moveIssuesTo=" + Uri.EscapeDataString(moveIssuesTo));
  34. await _jira.RestClient.ExecuteRequestAsync(Method.DELETE, resource, null, token).ConfigureAwait(false);
  35. _jira.Cache.Components.TryRemove(componentId);
  36. }
  37. public async Task<IEnumerable<ProjectComponent>> GetComponentsAsync(string projectKey, CancellationToken token = default(CancellationToken))
  38. {
  39. var cache = _jira.Cache;
  40. if (!cache.Components.Values.Any(c => String.Equals(c.ProjectKey, projectKey)))
  41. {
  42. var resource = String.Format("rest/api/2/project/{0}/components", projectKey);
  43. var remoteComponents = await _jira.RestClient.ExecuteRequestAsync<RemoteComponent[]>(Method.GET, resource).ConfigureAwait(false);
  44. var components = remoteComponents.Select(remoteComponent =>
  45. {
  46. remoteComponent.ProjectKey = projectKey;
  47. return new ProjectComponent(remoteComponent);
  48. });
  49. cache.Components.TryAdd(components);
  50. return components;
  51. }
  52. else
  53. {
  54. return cache.Components.Values.Where(c => String.Equals(c.ProjectKey, projectKey));
  55. }
  56. }
  57. }
  58. }