Skip to content

Commit

Permalink
feat: Add displayname to reviewerdto (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
taustad authored Oct 17, 2023
1 parent 1681391 commit a78d167
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 215 deletions.
22 changes: 11 additions & 11 deletions datasheetapi/Adapters/ConversationAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ namespace datasheetapi.Adapters;
public static class ConversationAdapter
{
public static Conversation ToModel(this ConversationDto conversationDto,
Guid projectId, string tagNo,
Guid azureUniqueId)
Guid projectId, string tagNo,
Guid azureUniqueId)
{
MessageDto messageDto = new()
{
Expand All @@ -22,7 +22,7 @@ public static Conversation ToModel(this ConversationDto conversationDto,
}

public static GetConversationDto ToDto(this Conversation conversation,
Dictionary<Guid, string> userIdNameMap)
Dictionary<Guid, string> userIdNameMap)
{
return new GetConversationDto
{
Expand All @@ -38,8 +38,8 @@ public static GetConversationDto ToDto(this Conversation conversation,
}

public static Message ToMessageModel(
this MessageDto messageDto,
Guid azureUniqueId)
this MessageDto messageDto,
Guid azureUniqueId)
{
return new Message
{
Expand All @@ -51,15 +51,15 @@ public static Message ToMessageModel(
}

public static List<GetMessageDto> ToMessageDtos(this List<Message> messages,
Dictionary<Guid, string> userIdNameMap)
Dictionary<Guid, string> userIdNameMap)
{
return messages.Select(message =>
ToMessageDto(message, userIdNameMap[message.UserId])).ToList();
}

public static GetMessageDto ToMessageDto(
this Message message,
string commenterName)
this Message message,
string commenterName)
{
return new GetMessageDto
{
Expand Down Expand Up @@ -129,15 +129,15 @@ private static Participant ToParticipantModel(Guid azureUniqueId)
}

private static List<UserDto> ToParticipantDtos(this List<Participant> participants,
Dictionary<Guid, string> userIdNameMap)
Dictionary<Guid, string> userIdNameMap)
{
return participants.Select(user =>
ToParticipantDto(user, userIdNameMap[user.UserId])).ToList();
}

private static UserDto ToParticipantDto(
Participant participant,
string commenterName)
Participant participant,
string commenterName)
{
return new UserDto
{
Expand Down
18 changes: 8 additions & 10 deletions datasheetapi/Adapters/ReviewerAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
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)
public static ReviewerDto ToDto(
this Reviewer tagDataReview,
string displayName)
{
return new ReviewerDto
{
Expand All @@ -16,13 +12,15 @@ private static ReviewerDto ToDto(this Reviewer tagDataReview)
TagDataReviewId = tagDataReview.TagDataReviewId,
CreatedDate = tagDataReview.CreatedDate,
ModifiedDate = tagDataReview.ModifiedDate,
DisplayName = displayName,
};
}

public static List<ReviewerDto> ToDto(this List<Reviewer>? tagDataReviews)
public static List<ReviewerDto> ToDto(
this List<Reviewer> tagDataReviews,
Dictionary<Guid, string> userIdNameMap)
{
if (tagDataReviews is null) { return new List<ReviewerDto>(); }
return tagDataReviews.Select(ToDto).ToList();
return tagDataReviews.Select(review => ToDto(review, userIdNameMap[review.ReviewerId])).ToList();
}

public static Reviewer ToModel(this CreateReviewerDto tagDataReviewDto)
Expand Down
18 changes: 8 additions & 10 deletions datasheetapi/Adapters/TagDataReviewAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
namespace datasheetapi.Adapters;
public static class TagDataReviewAdapter
{
public static TagDataReviewDto? ToDtoOrNull(this TagDataReview? tagDataReview)
{
if (tagDataReview is null) { return null; }
return tagDataReview.ToDto();
}

private static TagDataReviewDto ToDto(this TagDataReview tagDataReview)
public static TagDataReviewDto ToDto(
this TagDataReview tagDataReview,
Dictionary<Guid, string> userIdNameMap)
{
return new TagDataReviewDto
{
Expand All @@ -20,7 +16,7 @@ private static TagDataReviewDto ToDto(this TagDataReview tagDataReview)
CommentResponsible = tagDataReview.CommentResponsible,
Approved = tagDataReview.Approved,
TagDataVersion = tagDataReview.TagDataVersion,
Reviewer = tagDataReview.Reviewers.ToDto(),
Reviewer = tagDataReview.Reviewers.ToDto(userIdNameMap),
};
}

Expand Down Expand Up @@ -56,10 +52,12 @@ public static ReviewStatusEnum MapReviewStatusDtoToModel(this ReviewStatusDto dt
};
}

public static List<TagDataReviewDto> ToDto(this List<TagDataReview>? tagDataReviews)
public static List<TagDataReviewDto> ToDto(
this List<TagDataReview>? tagDataReviews,
Dictionary<Guid, string> userIdNameMap)
{
if (tagDataReviews is null) { return new List<TagDataReviewDto>(); }
return tagDataReviews.Select(ToDto).ToList();
return tagDataReviews.Select(r => ToDto(r, userIdNameMap)).ToList();
}

public static TagDataReview ToModel(this CreateTagDataReviewDto tagDataReviewDto)
Expand Down
15 changes: 10 additions & 5 deletions datasheetapi/Controllers/ConversationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ namespace datasheetapi.Controllers;
public class ConversationsController : ControllerBase
{
private readonly IConversationService _conversationService;
private readonly IUserService _userService;
private readonly ILogger<ConversationsController> _logger;

public ConversationsController(ILoggerFactory loggerFactory,
IConversationService conversationService)
public ConversationsController(
ILoggerFactory loggerFactory,
IConversationService conversationService,
IUserService userService
)
{
_logger = loggerFactory.CreateLogger<ConversationsController>();
_conversationService = conversationService;
_userService = userService;
}

[HttpPost(Name = "CreateConversation")]
Expand All @@ -51,7 +56,7 @@ public async Task<ActionResult<GetConversationDto>> CreateConversation(
_logger.LogInformation(
"Created new conversation in tag {tagNo} & project {projectId}.", tagNo, projectId);

var userIdNameMap = await _conversationService.GetUserIdUserName(
var userIdNameMap = await _userService.GetDisplayNames(
savedConversation.Participants.Select(p => p.UserId).ToList());
return savedConversation.ToDto(userIdNameMap);
}
Expand All @@ -63,7 +68,7 @@ public async Task<ActionResult<GetConversationDto>> GetConversation(
_logger.LogDebug("Fetching conversation for tagNo {tagNo} & project {projectId}", tagNo, projectId);
var conversation = await _conversationService.GetConversation(conversationId);

var userIdNameMap = await _conversationService.GetUserIdUserName(
var userIdNameMap = await _userService.GetDisplayNames(
conversation.Participants.Select(p => p.UserId).ToList());

return conversation.ToDto(userIdNameMap);
Expand All @@ -86,7 +91,7 @@ public async Task<ActionResult<List<GetConversationDto>>> GetConversations([NotE

var userIds = conversations.SelectMany(conversation =>
conversation.Participants.Select(p => p.UserId)).ToList();
var userIdNameMap = await _conversationService.GetUserIdUserName(userIds);
var userIdNameMap = await _userService.GetDisplayNames(userIds);

return conversations.Select(conversation => conversation.ToDto(userIdNameMap)).ToList();
}
Expand Down
17 changes: 11 additions & 6 deletions datasheetapi/Controllers/MessagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ namespace datasheetapi.Controllers;
public class MessagesController : ControllerBase
{
private readonly IConversationService _conversationService;
private readonly IUserService _userService;
private readonly ILogger<MessagesController> _logger;

public MessagesController(ILoggerFactory loggerFactory,
IConversationService conversationService)
public MessagesController(
ILoggerFactory loggerFactory,
IConversationService conversationService,
IUserService userService
)
{
_logger = loggerFactory.CreateLogger<MessagesController>();
_conversationService = conversationService;
_userService = userService;
}

[HttpPost("{conversationId}/messages", Name = "AddMessage")]
Expand All @@ -38,7 +43,7 @@ public async Task<ActionResult<GetMessageDto>> AddMessage(
var savedMessage = await _conversationService.AddMessage(conversationId, message);
_logger.LogInformation("Added new message in the conversation {conversationId}.", conversationId);

return savedMessage.ToMessageDto(await _conversationService.GetUserName(savedMessage.UserId));
return savedMessage.ToMessageDto(await _userService.GetDisplayName(savedMessage.UserId));
}

[HttpGet("{conversationId}/messages/{messageId}", Name = "GetMessage")]
Expand All @@ -47,7 +52,7 @@ public async Task<ActionResult<GetMessageDto>> GetMessage(
{
_logger.LogDebug("Fetching message on the conversation {conversationId}.", conversationId);
var message = await _conversationService.GetMessage(messageId);
var username = await _conversationService.GetUserName(message.UserId);
var username = await _userService.GetDisplayName(message.UserId);

return message.ToMessageDto(username);
}
Expand All @@ -58,7 +63,7 @@ public async Task<ActionResult<List<GetMessageDto>>> GetMessages([NotEmptyGuid]
_logger.LogDebug("Fetching messages on the conversation {conversationId}.", conversationId);
var messges = await _conversationService.GetMessages(conversationId);

var userIdNameMap = await _conversationService.GetUserIdUserName(
var userIdNameMap = await _userService.GetDisplayNames(
messges.Select(c => c.UserId).ToList());

return messges.ToMessageDtos(userIdNameMap);
Expand All @@ -76,7 +81,7 @@ public async Task<ActionResult<GetMessageDto>> UpdateMessage(
_logger.LogInformation("Updated the message {messageId} on the conversation {conversationId}.",
messageId, conversationId);

var userName = await _conversationService.GetUserName(message.UserId);
var userName = await _userService.GetDisplayName(message.UserId);
return message.ToMessageDto(userName);

}
Expand Down
44 changes: 36 additions & 8 deletions datasheetapi/Controllers/TagDataReviewsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,43 @@ public class TagDataReviewsController : ControllerBase
private readonly ILogger<TagDataReviewsController> _logger;
private readonly ITagDataReviewService _reviewService;
private readonly IReviewerService _reviewerService;
private readonly IUserService _userService;

public TagDataReviewsController(ILoggerFactory loggerFactory,
ITagDataReviewService reviewService, IReviewerService reviewerService)
public TagDataReviewsController(
ILoggerFactory loggerFactory,
ITagDataReviewService reviewService,
IReviewerService reviewerService,
IUserService userService
)
{
_logger = loggerFactory.CreateLogger<TagDataReviewsController>();
_reviewService = reviewService;
_reviewerService = reviewerService;
_userService = userService;
}

[HttpGet("{reviewId}", Name = "GetReviewById")]
public async Task<ActionResult<TagDataReviewDto?>> GetReview([NotEmptyGuid] Guid reviewId)
{
var review = await _reviewService.GetTagDataReview(reviewId);
return review.ToDtoOrNull();
var result = await _reviewService.GetTagDataReview(reviewId);

var reviewerIds = result.Reviewers.Select(r => r.ReviewerId).ToList();

var displayNameMap = await _userService.GetDisplayNames(reviewerIds);

return result.ToDto(displayNameMap);
}

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

var userIds = reviews.SelectMany(tagReview =>
tagReview.Reviewers.Select(p => p.ReviewerId)).ToList();
var userIdNameMap = await _userService.GetDisplayNames(userIds);

return reviews.Select(review => review.ToDto(userIdNameMap)).ToList();
}

[HttpPost(Name = "CreateReview")]
Expand All @@ -50,7 +66,12 @@ public async Task<ActionResult<List<TagDataReviewDto>>> GetReviews(Guid? reviewe
{
var result = await _reviewService.CreateTagDataReview(
reviewDto.ToModel(), Utils.GetAzureUniqueId(HttpContext.User));
return result.ToDtoOrNull();

var reviewerIds = result.Reviewers.Select(r => r.ReviewerId).ToList();

var displayNameMap = await _userService.GetDisplayNames(reviewerIds);

return result.ToDto(displayNameMap);
}

[HttpPost("{reviewId}/reviewers", Name = "CreateReviewers")]
Expand All @@ -61,7 +82,12 @@ public async Task<ActionResult<List<TagDataReviewDto>>> GetReviews(Guid? reviewe
var result = await _reviewerService.CreateReviewers(
reviewId, reviewDtos.ToModel());

return result.ToDto();
var userIds = result.Select(tagReview =>
tagReview.ReviewerId).ToList();

var userIdNameMap = await _userService.GetDisplayNames(userIds);

return result.ToDto(userIdNameMap);
}

[HttpPut("{reviewId}/reviewers/{reviewerId}", Name = "UpdateReview")]
Expand All @@ -73,6 +99,8 @@ public async Task<ActionResult<List<TagDataReviewDto>>> GetReviews(Guid? reviewe
var reviewStatus = updateReviewerDto.ReviewStatus.MapReviewStatusDtoToModel();
var result = await _reviewerService.UpdateReviewer(reviewId, reviewerId, Utils.GetAzureUniqueId(HttpContext.User), reviewStatus);

return result.ToDtoOrNull();
var displayName = await _userService.GetDisplayName(result.ReviewerId);

return result.ToDto(displayName);
}
}
1 change: 1 addition & 0 deletions datasheetapi/Dtos/ReviewerDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public record ReviewerDto
{
[Required]
public ReviewStatusDto Status { get; set; }
public string DisplayName { get; set; } = string.Empty;

public DateTime CreatedDate { get; set; }
public DateTime ModifiedDate { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions datasheetapi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
builder.Services.AddScoped<IFusionPeopleService, FusionPeopleService>();
builder.Services.AddScoped<IReviewerService, ReviewerService>();
builder.Services.AddScoped<IReviewerRepository, ReviewerRepository>();
builder.Services.AddScoped<IUserService, UserService>();

builder.Services.AddSingleton<IFAMService, DummyFAMService>();

Expand All @@ -166,6 +167,7 @@
builder.Services.AddSingleton<IAuthorizationHandler, ApplicationRoleAuthorizationHandler>();
builder.Services.AddSingleton<IAuthorizationPolicyProvider, ApplicationRolePolicyProvider>();
builder.Services.AddSingleton<IAzureUserCacheService, AzureUserCacheService>();

builder.Services.Configure<IConfiguration>(builder.Configuration);

//Swagger config
Expand Down
Loading

0 comments on commit a78d167

Please sign in to comment.