|
| 1 | +using DevExcelerateApi.Models; |
| 2 | + |
| 3 | +namespace DevExcelerateApi.Data |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Repository for Issue Request entities. |
| 7 | + /// Demonstrates how easy it is to create repositories for new entity types. |
| 8 | + /// </summary> |
| 9 | + /// <param name="storageContext">The storage context for IssueRequestEntity</param> |
| 10 | + public class IssueRequestRepository( |
| 11 | + IStorageContext<IssueRequestEntity> storageContext, |
| 12 | + GitHubWebhookRepository? webhookRepository = null) |
| 13 | + : Repository<IssueRequestEntity>(storageContext) |
| 14 | + { |
| 15 | + private readonly GitHubWebhookRepository? _webhookRepository = webhookRepository; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Saves an issue request with additional business logic. |
| 19 | + /// </summary> |
| 20 | + /// <param name="repositoryName">The repository name</param> |
| 21 | + /// <param name="issueNumber">The issue number</param> |
| 22 | + /// <param name="title">The issue title</param> |
| 23 | + /// <param name="body">The issue body</param> |
| 24 | + /// <param name="createdBy">The user who created the issue</param> |
| 25 | + /// <returns>The created issue request entity</returns> |
| 26 | + public async Task<IssueRequestEntity> CreateIssueRequestAsync( |
| 27 | + string repositoryName, |
| 28 | + long issueNumber, |
| 29 | + string? issueUrl, |
| 30 | + string? title, |
| 31 | + string? body = null, |
| 32 | + string? createdBy = null) |
| 33 | + { |
| 34 | + var issueRequest = new IssueRequestEntity |
| 35 | + { |
| 36 | + RepositoryName = repositoryName, |
| 37 | + IssueNumber = issueNumber, |
| 38 | + IssueUrl = issueUrl, |
| 39 | + Title = title, |
| 40 | + Body = body, |
| 41 | + CreatedBy = createdBy, |
| 42 | + State = "open" |
| 43 | + }; |
| 44 | + |
| 45 | + await CreateAsync(issueRequest); |
| 46 | + return issueRequest; |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Finds all issue requests for a specific repository. |
| 51 | + /// </summary> |
| 52 | + /// <param name="repositoryName">The repository name</param> |
| 53 | + /// <returns>List of issue requests for the repository</returns> |
| 54 | + public async Task<IEnumerable<IssueRequestEntity>> FindByRepositoryAsync(string repositoryName) |
| 55 | + { |
| 56 | + return await StorageContext.QueryEntitiesAsync(entity => |
| 57 | + entity.RepositoryName?.Equals(repositoryName, StringComparison.OrdinalIgnoreCase) == true); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Finds issue requests by state. |
| 62 | + /// </summary> |
| 63 | + /// <param name="state">The issue state (open, closed, etc.)</param> |
| 64 | + /// <returns>List of issue requests with the specified state</returns> |
| 65 | + public async Task<IEnumerable<IssueRequestEntity>> FindByStateAsync(string state) |
| 66 | + { |
| 67 | + return await StorageContext.QueryEntitiesAsync(entity => |
| 68 | + entity.State?.Equals(state, StringComparison.OrdinalIgnoreCase) == true); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Updates the state of an issue request. |
| 73 | + /// </summary> |
| 74 | + /// <param name="issueRequestId">The issue request ID</param> |
| 75 | + /// <param name="newState">The new state</param> |
| 76 | + /// <param name="partition">The partition key (optional)</param> |
| 77 | + public async Task UpdateStateAsync(string issueRequestId, string newState, string? partition = null) |
| 78 | + { |
| 79 | + var issueRequest = await FindByIdAsync(issueRequestId, partition); |
| 80 | + issueRequest.State = newState; |
| 81 | + issueRequest.UpdatedOn = DateTimeOffset.UtcNow; |
| 82 | + |
| 83 | + await UpsertAsync(issueRequest); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Finds issue requests created within a date range. |
| 88 | + /// </summary> |
| 89 | + /// <param name="startDate">Start date</param> |
| 90 | + /// <param name="endDate">End date</param> |
| 91 | + /// <returns>List of issue requests created within the date range</returns> |
| 92 | + public async Task<IEnumerable<IssueRequestEntity>> FindByDateRangeAsync(DateTimeOffset startDate, DateTimeOffset endDate) |
| 93 | + { |
| 94 | + return await StorageContext.QueryEntitiesAsync(entity => |
| 95 | + entity.CreatedOn >= startDate && entity.CreatedOn <= endDate); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Gets webhooks for an issue request by repository name and issue number. |
| 100 | + /// </summary> |
| 101 | + /// <param name="repositoryName">The repository name</param> |
| 102 | + /// <param name="issueNumber">The issue number</param> |
| 103 | + /// <returns>Collection of webhooks for the issue</returns> |
| 104 | + public async Task<IEnumerable<GitHubWebhookEntity>> GetWebhooksByIssueAsync(string repositoryName, long issueNumber) |
| 105 | + { |
| 106 | + if (_webhookRepository == null) |
| 107 | + return Enumerable.Empty<GitHubWebhookEntity>(); |
| 108 | + |
| 109 | + return await _webhookRepository.GetWebhooksByIssueAsync(repositoryName, issueNumber); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments