Skip to content

Commit

Permalink
feat: Get reviews assigned to a reviewer (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
taustad authored Oct 6, 2023
1 parent 0638c48 commit b35de94
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 10 deletions.
1 change: 1 addition & 0 deletions datasheetapi/Adapters/TagDataReviewAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ private static TagDataReviewDto ToDto(this TagDataReview tagDataReview)
CommentResponsible = tagDataReview.CommentResponsible,
Approved = tagDataReview.Approved,
TagDataVersion = tagDataReview.TagDataVersion,
Reviewer = tagDataReview.Reviewers.ToDto(),
};
}

Expand Down
16 changes: 14 additions & 2 deletions datasheetapi/Controllers/TagDataReviewsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public TagDataReviewsController(ILoggerFactory loggerFactory,
}

[HttpGet(Name = "GetReviews")]
public async Task<ActionResult<List<TagDataReviewDto>>> GetReviews()
public async Task<ActionResult<List<TagDataReviewDto>>> GetReviews(Guid? reviewerId)
{
var reviews = await _reviewService.GetTagDataReviews();
var reviews = await _reviewService.GetTagDataReviews(reviewerId);
return reviews.ToDto();
}

Expand All @@ -63,4 +63,16 @@ public async Task<ActionResult<List<TagDataReviewDto>>> GetReviews()

return result.ToDto();
}

[HttpPut("{reviewId}/reviewers/{reviewerId}", Name = "UpdateReview")]
public async Task<ActionResult<ReviewerDto?>> UpdateReview(
[NotEmptyGuid] Guid reviewId,
[NotEmptyGuid] Guid reviewerId,
[Required] UpdateReviewerDto updateReviewerDto)
{
var reviewStatus = updateReviewerDto.ReviewStatus.MapReviewStatusDtoToModel();
var result = await _reviewerService.UpdateReviewer(reviewId, reviewerId, Utils.GetAzureUniqueId(HttpContext.User), reviewStatus);

return result.ToDtoOrNull();
}
}
1 change: 1 addition & 0 deletions datasheetapi/Dtos/TagDataReviewDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public record TagDataReviewDto : BaseEntityDto
public Guid CommentResponsible { get; set; }
public bool Approved { get; set; }
public int TagDataVersion { get; init; }
public List<ReviewerDto> Reviewer { get; set; } = new List<ReviewerDto>();
}
8 changes: 8 additions & 0 deletions datasheetapi/Dtos/UpdateReviewerDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.ComponentModel.DataAnnotations;

namespace datasheetapi.Dtos;
public record UpdateReviewerDto
{
[Required]
public ReviewStatusDto ReviewStatus { get; set; }
}
2 changes: 2 additions & 0 deletions datasheetapi/Repositories/IReviewerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ namespace datasheetapi.Repositories
public interface IReviewerRepository
{
Task<List<Reviewer>> CreateReviewers(List<Reviewer> review);
Task<Reviewer> UpdateReviewer(Reviewer reviewer);
Task<Reviewer?> GetReviewer(Guid reviewId, Guid reviewerId);
}
}
2 changes: 1 addition & 1 deletion datasheetapi/Repositories/ITagDataReviewRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public interface ITagDataReviewRepository
{
Task<TagDataReview?> GetTagDataReview(Guid id);
Task<bool> AnyTagDataReview(Guid id);
Task<List<TagDataReview>> GetTagDataReviews();
Task<List<TagDataReview>> GetTagDataReviews(Guid? reviewerId);
Task<List<TagDataReview>> GetTagDataReviewsForTag(string tagNo);
Task<List<TagDataReview>> GetTagDataReviewsForTags(List<string> tagNos);
Task<TagDataReview> AddTagDataReview(TagDataReview review);
Expand Down
19 changes: 19 additions & 0 deletions datasheetapi/Repositories/ReviewerRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using api.Database;

using Microsoft.EntityFrameworkCore;

namespace datasheetapi.Repositories;

public class ReviewerRepository : IReviewerRepository
Expand All @@ -26,4 +28,21 @@ public async Task<List<Reviewer>> CreateReviewers(List<Reviewer> reviewers)

return reviewers;
}

public async Task<Reviewer?> GetReviewer(Guid reviewId, Guid reviewerId)
{
var reviewer = await _context.Reviewers.FirstOrDefaultAsync(r => r.TagDataReviewId == reviewId && r.ReviewerId == reviewerId);
return reviewer;
}

public async Task<Reviewer> UpdateReviewer(Reviewer reviewer)
{
reviewer.ModifiedDate = DateTime.UtcNow;

_context.Reviewers.Update(reviewer);

await _context.SaveChangesAsync();

return reviewer;
}
}
14 changes: 12 additions & 2 deletions datasheetapi/Repositories/TagDataReviewRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,19 @@ public async Task<bool> AnyTagDataReview(Guid id)
return exists;
}

public async Task<List<TagDataReview>> GetTagDataReviews()
public async Task<List<TagDataReview>> GetTagDataReviews(Guid? reviewerId)
{
var reviews = await _context.TagDataReviews.ToListAsync();
if (reviewerId == null)
{
return await _context.TagDataReviews.ToListAsync();
}

var reviews = await _context.TagDataReviews
.Include(t => t.Reviewers)
.Where(t => t.Reviewers
.Any(x => x.ReviewerId == reviewerId))
.ToListAsync();

return reviews;
}

Expand Down
1 change: 1 addition & 0 deletions datasheetapi/Services/IReviewerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ namespace datasheetapi.Services
public interface IReviewerService
{
Task<List<Reviewer>> CreateReviewers(Guid reviewId, List<Reviewer> reviewers);
Task<Reviewer> UpdateReviewer(Guid reviewId, Guid reviewerId, Guid userFromToken, ReviewStatusEnum reviewStatus);
}
}
2 changes: 1 addition & 1 deletion datasheetapi/Services/ITagDataReviewService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public interface ITagDataReviewService
{
Task<TagDataReview> GetTagDataReview(Guid id);
Task<bool> AnyTagDataReview(Guid reviewId);
Task<List<TagDataReview>> GetTagDataReviews();
Task<List<TagDataReview>> GetTagDataReviews(Guid? reviewerId);
Task<List<TagDataReview>> GetTagDataReviewsForTag(string tagNo);
Task<List<TagDataReview>> GetTagDataReviewsForTags(List<string> tagNos);
Task<TagDataReview> CreateTagDataReview(TagDataReview review, Guid azureUniqueId);
Expand Down
15 changes: 15 additions & 0 deletions datasheetapi/Services/ReviewerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ public async Task<List<Reviewer>> CreateReviewers(Guid reviewId, List<Reviewer>

return result;
}

public async Task<Reviewer> UpdateReviewer(Guid reviewId, Guid reviewerId, Guid userFromToken, ReviewStatusEnum reviewStatus)
{
if (reviewerId != userFromToken) { throw new BadRequestException("Reviewer cannot update other people's review"); }

if (!await _reviewService.AnyTagDataReview(reviewId)) { throw new NotFoundException($"Invalid reviewId - {reviewId}."); }

var existingReviewer = await _reviewerRepository.GetReviewer(reviewId, reviewerId)
?? throw new NotFoundException($"Reviewer with reviewId {reviewId} and reviewerId {reviewerId} not found");

existingReviewer.Status = reviewStatus;

var result = await _reviewerRepository.UpdateReviewer(existingReviewer);
return result;
}
}
4 changes: 2 additions & 2 deletions datasheetapi/Services/TagDataReviewService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public async Task<bool> AnyTagDataReview(Guid reviewId)
return exists;
}

public async Task<List<TagDataReview>> GetTagDataReviews()
public async Task<List<TagDataReview>> GetTagDataReviews(Guid? reviewerId)
{
var reviews = await _reviewRepository.GetTagDataReviews();
var reviews = await _reviewRepository.GetTagDataReviews(reviewerId);
return reviews;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Services/TagDataReviewServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public async Task GetTagDataReviews_ReturnsReviews()
{
// Arrange
var reviews = new List<TagDataReview> { new TagDataReview(), new TagDataReview() };
_reviewRepositoryMock.Setup(x => x.GetTagDataReviews()).ReturnsAsync(reviews);
_reviewRepositoryMock.Setup(x => x.GetTagDataReviews(null)).ReturnsAsync(reviews);

// Act
var result = await _tagDataReviewService.GetTagDataReviews();
var result = await _tagDataReviewService.GetTagDataReviews(null);

// Assert
Assert.NotNull(result);
Expand Down

0 comments on commit b35de94

Please sign in to comment.