Skip to content

Commit

Permalink
feat: Add endpoint for updating conversation state (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
taustad authored Nov 14, 2023
1 parent 6162ee1 commit 988c0df
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion datasheetapi/Adapters/ConversationAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static ConversationLevel MapConversationLevelDTOToModel(ConversationLeve
};
}

private static ConversationStatus MapConversationStatusDTOToModel(ConversationStatusDto dto)
public static ConversationStatus MapConversationStatusDTOToModel(ConversationStatusDto dto)
{
return dto switch
{
Expand Down
15 changes: 15 additions & 0 deletions datasheetapi/Controllers/ConversationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ public async Task<ActionResult<GetConversationDto>> CreateConversation(
return savedConversation.ToDto(userIdNameMap);
}

[HttpPut("{conversationId}", Name = "UpdateConversation")]
public async Task<ActionResult<GetConversationDto>> UpdateConversation(
Guid projectId,
[Required] string tagNo,
Guid conversationId,
[Required] UpdateConversationDto conversation)
{
var savedConversation = await _conversationService.UpdateConversation(conversationId, ConversationAdapter.MapConversationStatusDTOToModel(conversation.ConversationStatus));

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

return savedConversation.ToDto(userIdNameMap);
}

[HttpGet("{conversationId}", Name = "GetConversation")]
public async Task<ActionResult<GetConversationDto>> GetConversation(
[NotEmptyGuid] Guid projectId, [Required] string tagNo, [NotEmptyGuid] Guid conversationId)
Expand Down
7 changes: 7 additions & 0 deletions datasheetapi/Dtos/Conversation/UpdateConversationDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.ComponentModel.DataAnnotations;

namespace datasheetapi.Dtos.Conversation;
public record UpdateConversationDto
{
public ConversationStatusDto ConversationStatus { get; set; }
}
10 changes: 10 additions & 0 deletions datasheetapi/Repositories/ConversationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ public async Task<Conversation> CreateConversation(Conversation conversation)
return savedConversation.Entity;
}

public async Task<Conversation> UpdateConversation(Conversation entity)
{
entity.ModifiedDate = DateTime.UtcNow;

var updatedConversation = _context.Conversations.Update(entity);
await _context.SaveChangesAsync();

return updatedConversation.Entity;
}

public async Task<Conversation?> GetConversation(Guid conversationId)
{
return await _context.Conversations
Expand Down
1 change: 1 addition & 0 deletions datasheetapi/Repositories/IConversationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace datasheetapi.Repositories;
public interface IConversationRepository
{
Task<Conversation> CreateConversation(Conversation conversation);
Task<Conversation> UpdateConversation(Conversation entity);
Task<Conversation?> GetConversation(Guid conversationId);
Task<List<Conversation>> GetConversationsForTagNos(ICollection<string> tagNos);
Task<List<Conversation>> GetConversationsWithLatestMessage(Guid projectId,
Expand Down
11 changes: 10 additions & 1 deletion datasheetapi/Services/ConversationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ public ConversationService(ILoggerFactory loggerFactory,
public async Task<Conversation> CreateConversation(Conversation conversation)
{
// TODO: Not sure, how to verify the project from the tag.
// This needs to be relook when we have integration to FAM
// This needs to be looked at when we have integration to FAM
_ = await _famService.GetTagData(conversation.TagNo)
?? throw new NotFoundException("Invalid tag data");
return await _conversationRepository.CreateConversation(conversation);
}

public async Task<Conversation> UpdateConversation(Guid conversationId, ConversationStatus status)
{
var existingConversation = await _conversationRepository.GetConversation(conversationId)
?? throw new NotFoundException($"Conversation with id {conversationId} not found");

existingConversation.ConversationStatus = status;
return await _conversationRepository.UpdateConversation(existingConversation);
}

public async Task<Conversation> GetConversation(Guid conversationId)
{
var conversation = await _conversationRepository.GetConversation(conversationId);
Expand Down
1 change: 1 addition & 0 deletions datasheetapi/Services/IConversationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace datasheetapi.Services
public interface IConversationService
{
Task<Conversation> CreateConversation(Conversation conversation);
Task<Conversation> UpdateConversation(Guid conversationId, ConversationStatus status);
Task<Conversation> GetConversation(Guid conversationId);
Task<List<Conversation>> GetConversations(Guid projectId, string tagNo, bool includeLatestMessage);
Task<List<Conversation>> GetConversationsForTagNos(ICollection<string> tagNos);
Expand Down

0 comments on commit 988c0df

Please sign in to comment.