-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from davishoang96/view-and-delete-message
View and delete message
- Loading branch information
Showing
13 changed files
with
193 additions
and
66 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
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,79 @@ | ||
@page "/text/{id}" | ||
@inject IApiClient ApiClient | ||
@using Newtonsoft.Json | ||
@using privatext.Client.HttpClient | ||
@using privatext.Common.Response | ||
|
||
|
||
<RadzenRow JustifyContent="JustifyContent.Center" AlignItems="AlignItems.Center"> | ||
<RadzenColumn Size="3" /> | ||
<RadzenColumn Size="6"> | ||
<RadzenStack Orientation="Orientation.Vertical" Visible="ShowViewTextBtn"> | ||
<h3>ViewText</h3> | ||
<RadzenText>The text will be automatically remove from the server once you view it.</RadzenText> | ||
<RadzenButton Click="@ExecuteViewText">View Text</RadzenButton> | ||
</RadzenStack> | ||
<RadzenStack Orientation="Orientation.Vertical" Visible="IsTextViewed"> | ||
<h3>Your text message</h3> | ||
<RadzenTextArea @bind-Value="@Content" Rows="15" Cols="100"></RadzenTextArea> | ||
</RadzenStack> | ||
<RadzenStack Orientation="Orientation.Vertical" Visible="ShowDeletedNotification"> | ||
<h3>@ErrorMessage</h3> | ||
</RadzenStack> | ||
</RadzenColumn> | ||
<RadzenColumn Size="3" /> | ||
</RadzenRow> | ||
|
||
@code { | ||
|
||
[Parameter] | ||
public string Id { get; set; } | ||
private bool IsTextViewed { get; set; } | ||
private bool ShowViewTextBtn { get; set; } | ||
private bool ShowDeletedNotification { get; set; } | ||
private string ErrorMessage { get; set; } | ||
|
||
private string Content { get; set; } | ||
private string DateCreated { get; set; } | ||
|
||
protected override void OnInitialized() | ||
{ | ||
base.OnInitialized(); | ||
ShowViewTextBtn = true; | ||
ShowDeletedNotification = false; | ||
} | ||
|
||
private async Task ExecuteViewText() | ||
{ | ||
ShowViewTextBtn = false; | ||
try | ||
{ | ||
var m = await ApiClient.GetMessageEndpointAsync(Id); | ||
if (m is not null) | ||
{ | ||
IsTextViewed = true; | ||
|
||
Content = m.Content; | ||
DateCreated = m.DateCreated.Value.ToString("dd/MM/yyyy"); | ||
} | ||
else | ||
{ | ||
IsTextViewed = false; | ||
ShowDeletedNotification = true; | ||
} | ||
} | ||
catch (ApiException apiEx) | ||
{ | ||
var err = JsonConvert.DeserializeObject<ErrorResponse>(apiEx.Response); | ||
ErrorMessage = err.Message; | ||
} | ||
catch (Exception ex) | ||
{ | ||
ErrorMessage = "Something wrong. Please contact administrator"; | ||
} | ||
finally | ||
{ | ||
ShowDeletedNotification = true; | ||
} | ||
} | ||
} |
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,6 @@ | ||
namespace privatext.Common; | ||
|
||
public class Constant | ||
{ | ||
public const string MessageDeleted = "The message has been deleted"; | ||
} |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
namespace privatext.Common.Request | ||
using FastEndpoints; | ||
|
||
namespace privatext.Common.Request; | ||
|
||
public class GetMessageRequest | ||
{ | ||
public class GetMessageRequest | ||
{ | ||
public string MessageId { get; set; } | ||
} | ||
[FromBody] | ||
public string MessageId { 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 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace privatext.Common.Response; | ||
|
||
public class ErrorResponse | ||
{ | ||
[JsonProperty("StatusCode")] | ||
public int StatusCode { get; set; } | ||
|
||
[JsonProperty("Message")] | ||
public string Message { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
using privatext.Common.DTO; | ||
namespace privatext.Common.Response; | ||
|
||
namespace privatext.Common.Response | ||
public class GetMessageResponse | ||
{ | ||
public class GetMessageResponse : BaseEndpointResponse | ||
{ | ||
public MessageDTO MessageDTO { get; set; } | ||
} | ||
public string Content { get; set; } | ||
public DateTime DateCreated { 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 |
---|---|---|
@@ -1,45 +1,57 @@ | ||
using FastEndpoints; | ||
using FluentValidation.Results; | ||
using privatext.Common.Request; | ||
using privatext.Common.Response; | ||
using privatext.Services; | ||
|
||
namespace privatext.Endpoints | ||
namespace privatext.Endpoints; | ||
|
||
public class GetMessageEndpoint : Endpoint<GetMessageRequest, GetMessageResponse> | ||
{ | ||
public class GetMessageEndpoint : Endpoint<GetMessageRequest, GetMessageResponse> | ||
private readonly ICryptoService cryptoService; | ||
private readonly IMessageService messageService; | ||
public GetMessageEndpoint(IMessageService messageService, ICryptoService cryptoService) | ||
{ | ||
private readonly ICryptoService cryptoService; | ||
private readonly IMessageService messageService; | ||
public GetMessageEndpoint(IMessageService messageService, ICryptoService cryptoService) | ||
{ | ||
this.messageService = messageService; | ||
this.cryptoService = cryptoService; | ||
} | ||
this.messageService = messageService; | ||
this.cryptoService = cryptoService; | ||
} | ||
|
||
public override void Configure() | ||
{ | ||
Post("/getMessage/"); | ||
AllowAnonymous(); | ||
} | ||
public override void Configure() | ||
{ | ||
Post("/getMessage/"); | ||
AllowAnonymous(); | ||
DontCatchExceptions(); | ||
} | ||
|
||
public override async Task HandleAsync(GetMessageRequest r, CancellationToken c) | ||
public override async Task HandleAsync(GetMessageRequest r, CancellationToken c) | ||
{ | ||
var res = new GetMessageResponse(); | ||
var midpoint = r.MessageId.Length / 2; | ||
var secondHalf = r.MessageId.Substring(midpoint); | ||
var model = messageService.GetMessage(secondHalf); | ||
if (model == null) | ||
{ | ||
var res = new GetMessageResponse(); | ||
var model = messageService.GetMessage(r.MessageId); | ||
if (model == null) | ||
ThrowError(new ValidationFailure | ||
{ | ||
res.AddError("Message has been deleted"); | ||
await SendAsync(res); | ||
} | ||
ErrorMessage = $"Message id = {r.MessageId} has been deleted", | ||
Severity = FluentValidation.Severity.Error, | ||
PropertyName = nameof(GetMessageEndpoint), | ||
}); | ||
} | ||
|
||
var decryptedMessage = await cryptoService.Decrypt(model.Content, r.MessageId); | ||
await SendAsync(new GetMessageResponse | ||
if (!await messageService.DeleteMessage(secondHalf)) | ||
{ | ||
ThrowError(new ValidationFailure | ||
{ | ||
MessageDTO = new Common.DTO.MessageDTO | ||
{ | ||
Content = decryptedMessage, | ||
DateCreated = model.DateCreated, | ||
} | ||
ErrorMessage = "Error when deleting a message", | ||
Severity = FluentValidation.Severity.Error, | ||
PropertyName = nameof(GetMessageEndpoint), | ||
}); | ||
} | ||
|
||
var decryptedMessage = await cryptoService.Decrypt(model.Content, r.MessageId); | ||
res.Content = decryptedMessage; | ||
res.DateCreated = model.DateCreated; | ||
await SendAsync(res); | ||
} | ||
} |
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