-
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.
- Loading branch information
Eliran Turgeman
authored and
Eliran Turgeman
committed
Dec 14, 2024
1 parent
cd7fdb4
commit c3112f2
Showing
9 changed files
with
260 additions
and
15 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,49 @@ | ||
@page "{id:guid}" | ||
@model EmailCollector.Api.Pages.CustomEmailTemplates.Edit | ||
|
||
@{ | ||
ViewData["Title"] = "Edit Custom Email Template"; | ||
} | ||
|
||
<h1>Edit Custom Email Template</h1> | ||
|
||
<form method="post"> | ||
<input type="hidden" asp-for="CustomEmailTemplate.FormId" /> | ||
|
||
<div class="form-group"> | ||
<label asp-for="CustomEmailTemplate.FormId" class="control-label">Form</label> | ||
<select asp-for="CustomEmailTemplate.FormId" class="form-control" asp-items="Model.FormOptions" disabled> | ||
<option value="">-- Select Form --</option> | ||
</select> | ||
<span asp-validation-for="CustomEmailTemplate.FormId" class="text-danger"></span> | ||
<small class="form-text text-muted">Form cannot be changed.</small> | ||
</div> | ||
<div class="form-group"> | ||
<label asp-for="CustomEmailTemplate.Event" class="control-label">Event</label> | ||
<select asp-for="CustomEmailTemplate.Event" class="form-control" asp-items="Model.EventOptions"> | ||
<option value="">-- Select Event --</option> | ||
</select> | ||
<span asp-validation-for="CustomEmailTemplate.Event" class="text-danger"></span> | ||
</div> | ||
<div class="form-group"> | ||
<label asp-for="CustomEmailTemplate.TemplateSubject" class="control-label">Subject</label> | ||
<input asp-for="CustomEmailTemplate.TemplateSubject" class="form-control" /> | ||
<span asp-validation-for="CustomEmailTemplate.TemplateSubject" class="text-danger"></span> | ||
</div> | ||
<div class="form-group"> | ||
<label asp-for="CustomEmailTemplate.TemplateBody" class="control-label">HTML Content</label> | ||
<textarea asp-for="CustomEmailTemplate.TemplateBody" class="form-control" rows="10"></textarea> | ||
<span asp-validation-for="CustomEmailTemplate.TemplateBody" class="text-danger"></span> | ||
<small class="form-text text-muted">Enter the HTML content for the email template.</small> | ||
</div> | ||
<div class="form-group form-check"> | ||
<input asp-for="CustomEmailTemplate.IsActive" class="form-check-input" /> | ||
<label asp-for="CustomEmailTemplate.IsActive" class="form-check-label">Is Active</label> | ||
</div> | ||
<button type="submit" class="btn btn-primary">Save</button> | ||
<a asp-page="./Index" class="btn btn-secondary">Cancel</a> | ||
</form> | ||
|
||
@section Scripts { | ||
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");} | ||
} |
158 changes: 158 additions & 0 deletions
158
EmailCollector.Api/Pages/CustomEmailTemplates/Edit.cshtml.cs
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,158 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using EmailCollector.Api.DTOs; | ||
using EmailCollector.Api.Services; | ||
using EmailCollector.Api.Services.CustomEmailTemplates; | ||
using EmailCollector.Domain.Entities; | ||
using EmailCollector.Domain.Enums; | ||
using Ganss.Xss; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
|
||
namespace EmailCollector.Api.Pages.CustomEmailTemplates; | ||
|
||
public class Edit : PageModel | ||
{ | ||
private readonly ICustomEmailTemplatesService _emailTemplatesService; | ||
private readonly IFormService _formService; | ||
private readonly UserManager<EmailCollectorApiUser> _userManager; | ||
|
||
|
||
public Edit(ICustomEmailTemplatesService emailTemplatesService, | ||
IFormService formService, | ||
UserManager<EmailCollectorApiUser> userManager) | ||
{ | ||
_emailTemplatesService = emailTemplatesService; | ||
_formService = formService; | ||
_userManager = userManager; | ||
} | ||
|
||
[BindProperty] | ||
public EditCustomEmailTemplateViewModel CustomEmailTemplate { get; set; } | ||
|
||
public IEnumerable<SelectListItem> FormOptions { get; set; } | ||
public IEnumerable<SelectListItem> EventOptions { get; set; } | ||
|
||
public async Task<IActionResult> OnGetAsync(Guid id) | ||
{ | ||
var currentUser = await _userManager.GetUserAsync(User); | ||
var userId = new Guid(currentUser?.Id!); | ||
|
||
// Fetch the template | ||
var customEmailTemplateDto = await _emailTemplatesService.GetCustomEmailTemplateById(id); | ||
|
||
if (customEmailTemplateDto == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
CustomEmailTemplate = new EditCustomEmailTemplateViewModel | ||
{ | ||
FormId = customEmailTemplateDto.FormId, | ||
Event = customEmailTemplateDto.Event, | ||
TemplateBody = customEmailTemplateDto.TemplateBody, | ||
TemplateSubject = customEmailTemplateDto.TemplateSubject, | ||
IsActive = customEmailTemplateDto.IsActive | ||
}; | ||
|
||
// Verify that the template's FormId belongs to the current user | ||
var userForms = await _formService.GetFormsByUserAsync(userId); | ||
var userFormIds = userForms.Select(f => f.Id).ToHashSet(); | ||
|
||
if (!userFormIds.Contains(CustomEmailTemplate.FormId)) | ||
{ | ||
return Forbid(); | ||
} | ||
|
||
// Populate FormOptions (disabled in UI) | ||
FormOptions = userForms.Select(f => new SelectListItem | ||
{ | ||
Value = f.Id.ToString(), | ||
Text = f.FormName, | ||
Selected = f.Id == CustomEmailTemplate.FormId | ||
}); | ||
|
||
// Populate event options | ||
PopulateEventOptions(); | ||
|
||
return Page(); | ||
} | ||
|
||
public async Task<IActionResult> OnPostAsync(Guid id) | ||
{ | ||
var currentUser = await _userManager.GetUserAsync(User); | ||
var userId = new Guid(currentUser?.Id!); | ||
|
||
// Fetch the existing template | ||
var existingTemplate = await _emailTemplatesService.GetCustomEmailTemplateById(id); | ||
if (existingTemplate == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
// Verify that the template's FormId belongs to the current user | ||
var userForms = await _formService.GetFormsByUserAsync(userId); | ||
var userFormIds = userForms.Select(f => f.Id).ToHashSet(); | ||
|
||
if (!userFormIds.Contains(existingTemplate.FormId)) | ||
{ | ||
return Forbid(); | ||
} | ||
|
||
// Since FormId is not editable, ensure it's not altered | ||
CustomEmailTemplate.FormId = existingTemplate.FormId; | ||
|
||
// Sanitize HTML content | ||
var sanitizer = new HtmlSanitizer(); | ||
CustomEmailTemplate.TemplateBody = sanitizer.Sanitize(CustomEmailTemplate.TemplateBody); | ||
|
||
// Update the template DTO | ||
var updatedDto = new CustomEmailTemplateDto | ||
{ | ||
Id = existingTemplate.Id, | ||
FormId = existingTemplate.FormId, | ||
Event = CustomEmailTemplate.Event, | ||
TemplateSubject = CustomEmailTemplate.TemplateSubject, | ||
TemplateBody = CustomEmailTemplate.TemplateBody, | ||
TemplateBodyUri = existingTemplate.TemplateBodyUri, // Assuming URI remains same | ||
IsActive = CustomEmailTemplate.IsActive, | ||
CreatedAt = existingTemplate.CreatedAt, | ||
UpdatedAt = DateTime.UtcNow | ||
}; | ||
|
||
Console.WriteLine("saving"); | ||
await _emailTemplatesService.SaveCustomEmailTemplate(updatedDto); | ||
Console.WriteLine("saved"); | ||
return RedirectToPage("./Index"); | ||
} | ||
|
||
private void PopulateEventOptions() | ||
{ | ||
EventOptions = Enum.GetValues(typeof(TemplateEvent)) | ||
.Cast<TemplateEvent>() | ||
.Select(e => new SelectListItem | ||
{ | ||
Value = e.ToString(), | ||
Text = e.ToString().Replace('_', ' ') | ||
}); | ||
} | ||
|
||
public class EditCustomEmailTemplateViewModel | ||
{ | ||
[Required(ErrorMessage = "Event is required.")] | ||
public TemplateEvent Event { get; set; } | ||
|
||
[Required(ErrorMessage = "Form is required.")] | ||
public Guid FormId { get; set; } | ||
|
||
[Required(ErrorMessage = "Template Subject is required.")] | ||
[StringLength(100, ErrorMessage = "Template Subject cannot exceed 100 characters.")] | ||
public string TemplateSubject { get; set; } | ||
|
||
[Required(ErrorMessage = "Template Body is required.")] | ||
public string TemplateBody { get; set; } | ||
|
||
public bool IsActive { 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
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