-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
255 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace datasheetapi.Adapters; | ||
public static class ReviewerAdapter | ||
{ | ||
public static ReviewerDto? ToDtoOrNull(this Reviewer? tagDataReview) | ||
{ | ||
if (tagDataReview is null) { return null; } | ||
return tagDataReview.ToDto(); | ||
} | ||
|
||
private static ReviewerDto ToDto(this Reviewer tagDataReview) | ||
{ | ||
return new ReviewerDto | ||
{ | ||
Status = tagDataReview.Status.MapReviewStatusModelToDto(), | ||
ReviewerId = tagDataReview.ReviewerId, | ||
TagDataReviewId = tagDataReview.TagDataReviewId, | ||
CreatedDate = tagDataReview.CreatedDate, | ||
ModifiedDate = tagDataReview.ModifiedDate, | ||
}; | ||
} | ||
|
||
public static List<ReviewerDto> ToDto(this List<Reviewer>? tagDataReviews) | ||
{ | ||
if (tagDataReviews is null) { return new List<ReviewerDto>(); } | ||
return tagDataReviews.Select(ToDto).ToList(); | ||
} | ||
|
||
public static Reviewer ToModel(this CreateReviewerDto tagDataReviewDto) | ||
{ | ||
return new Reviewer | ||
{ | ||
Status = ReviewStatusEnum.New, | ||
ReviewerId = tagDataReviewDto.ReviewerId, | ||
}; | ||
} | ||
|
||
public static List<Reviewer> ToModel(this List<CreateReviewerDto> tagDataReviewDtos) | ||
{ | ||
return tagDataReviewDtos.Select(x => ToModel(x)).ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace datasheetapi.Dtos; | ||
public record CreateReviewerDto | ||
{ | ||
[Required] | ||
[NotEmptyGuid] | ||
public Guid ReviewerId { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace datasheetapi.Dtos; | ||
public record ReviewerDto | ||
{ | ||
[Required] | ||
public ReviewStatusDto Status { get; set; } | ||
|
||
public DateTime CreatedDate { get; set; } | ||
public DateTime ModifiedDate { get; set; } | ||
|
||
public Guid ReviewerId { get; set; } | ||
public Guid TagDataReviewId { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace datasheetapi.Models; | ||
|
||
public class Reviewer | ||
{ | ||
public ReviewStatusEnum Status { get; set; } | ||
|
||
public DateTime CreatedDate { get; set; } | ||
public DateTime ModifiedDate { get; set; } | ||
|
||
public Guid TagDataReviewId { get; set; } | ||
public Guid ReviewerId { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace datasheetapi.Repositories | ||
{ | ||
public interface IReviewerRepository | ||
{ | ||
Task<List<Reviewer>> CreateReviewers(List<Reviewer> review); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using api.Database; | ||
|
||
namespace datasheetapi.Repositories; | ||
|
||
public class ReviewerRepository : IReviewerRepository | ||
{ | ||
private readonly DatabaseContext _context; | ||
private readonly ILogger<ReviewerRepository> _logger; | ||
|
||
public ReviewerRepository(ILoggerFactory loggerFactory, DatabaseContext context) | ||
{ | ||
_logger = loggerFactory.CreateLogger<ReviewerRepository>(); | ||
_context = context; | ||
} | ||
|
||
public async Task<List<Reviewer>> CreateReviewers(List<Reviewer> reviewers) | ||
{ | ||
reviewers.ForEach(r => | ||
{ | ||
r.CreatedDate = DateTime.UtcNow; | ||
r.ModifiedDate = DateTime.UtcNow; | ||
}); | ||
|
||
_context.Reviewers.AddRange(reviewers); | ||
await _context.SaveChangesAsync(); | ||
|
||
return reviewers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace datasheetapi.Services | ||
{ | ||
public interface IReviewerService | ||
{ | ||
Task<List<Reviewer>> CreateReviewers(Guid reviewId, List<Reviewer> reviewers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using datasheetapi.Exceptions; | ||
using datasheetapi.Repositories; | ||
|
||
namespace datasheetapi.Services; | ||
|
||
public class ReviewerService : IReviewerService | ||
{ | ||
private readonly ITagDataReviewService _reviewService; | ||
|
||
private readonly IReviewerRepository _reviewerRepository; | ||
|
||
public ReviewerService( | ||
ITagDataReviewService reviewService, | ||
IReviewerRepository reviewerRepository) | ||
{ | ||
_reviewService = reviewService; | ||
_reviewerRepository = reviewerRepository; | ||
} | ||
|
||
public async Task<List<Reviewer>> CreateReviewers(Guid reviewId, List<Reviewer> reviewers) | ||
{ | ||
if (!await _reviewService.AnyTagDataReview(reviewId)) { throw new NotFoundException($"Invalid reviewId - {reviewId}."); } | ||
|
||
reviewers.ForEach(r => r.TagDataReviewId = reviewId); | ||
|
||
var result = await _reviewerRepository.CreateReviewers(reviewers); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using datasheetapi.Exceptions; | ||
using datasheetapi.Models; | ||
using datasheetapi.Repositories; | ||
using datasheetapi.Services; | ||
|
||
using Moq; | ||
|
||
namespace tests.Services; | ||
public class ReviewerServiceTests | ||
{ | ||
private readonly Mock<ITagDataReviewService> _reviewService = new(); | ||
private readonly Mock<IReviewerRepository> _reviewerRepository = new(); | ||
|
||
private readonly ReviewerService _reviewerService; | ||
|
||
public ReviewerServiceTests() | ||
{ | ||
_reviewerService = new ReviewerService( | ||
_reviewService.Object, | ||
_reviewerRepository.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateReviewerTagDataReview_ThrowsIfReviewDoesNotExist() | ||
{ | ||
List<Reviewer> reviewers = new() | ||
{ | ||
new Reviewer { Status = 0, ReviewerId = Guid.NewGuid() } | ||
}; | ||
var reviewId = Guid.NewGuid(); | ||
|
||
_reviewService.Setup(x => x.AnyTagDataReview(reviewId)).ThrowsAsync(new NotFoundException($"Invalid reviewId - {reviewId}.")); | ||
|
||
await Assert.ThrowsAsync<NotFoundException>(() => _reviewerService.CreateReviewers(reviewId, reviewers)); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateReviewerTagDataReview_RunsOkayWithCorrectInput() | ||
{ | ||
List<Reviewer> reviewers = new() | ||
{ | ||
new Reviewer { Status = 0, ReviewerId = Guid.NewGuid() }, | ||
new Reviewer { Status = 0, ReviewerId = Guid.NewGuid() } | ||
}; | ||
|
||
var reviewId = Guid.NewGuid(); | ||
|
||
_reviewService.Setup(x => x.AnyTagDataReview(reviewId)).ReturnsAsync(true); | ||
|
||
_reviewerRepository.Setup(x => x.CreateReviewers(reviewers)).ReturnsAsync(reviewers); | ||
|
||
var result = await _reviewerService.CreateReviewers(reviewId, reviewers); | ||
|
||
Assert.NotNull(result); | ||
Assert.Equal(reviewers, result); | ||
_reviewService.Verify(x => x.AnyTagDataReview(reviewId), Times.Once); | ||
_reviewerRepository.Verify(x => x.CreateReviewers(reviewers), Times.Once); | ||
} | ||
} |