Compare commits
3 Commits
028e62407a
...
df781eaf84
| Author | SHA1 | Date | |
|---|---|---|---|
| df781eaf84 | |||
| 10737b8272 | |||
| a8f664a6ba |
@@ -0,0 +1,3 @@
|
|||||||
|
namespace RazorPagesMovie.Api.V1.Contact;
|
||||||
|
|
||||||
|
public record ContactDto(string? Name, string? Email, string? Phone, string? Subject, string? Message);
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using RazorPagesMovie.Api.V1.Contact;
|
||||||
|
using RazorPagesMovie.Data;
|
||||||
|
using RazorPagesMovie.Models;
|
||||||
|
|
||||||
|
namespace RazorPagesMovie.Controllers.Api.V1;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/v1/[controller]")]
|
||||||
|
public class ContactController(RazorPagesMovieContext context) : ControllerBase
|
||||||
|
{
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<IActionResult> Post(ContactDto request)
|
||||||
|
{
|
||||||
|
var contactMessage = new ContactMessage
|
||||||
|
{
|
||||||
|
Name = request.Name ?? string.Empty,
|
||||||
|
Email = request.Email ?? string.Empty,
|
||||||
|
Phone = request.Phone,
|
||||||
|
Subject = request.Subject,
|
||||||
|
Message = request.Message ?? string.Empty
|
||||||
|
};
|
||||||
|
|
||||||
|
var validationResults = new List<ValidationResult>();
|
||||||
|
if (!Validator.TryValidateObject(contactMessage, new ValidationContext(contactMessage), validationResults, validateAllProperties: true))
|
||||||
|
{
|
||||||
|
return BadRequest(new { error = "Bitte alle Pflichtfelder korrekt ausfüllen." });
|
||||||
|
}
|
||||||
|
|
||||||
|
context.ContactMessage.Add(contactMessage);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return Ok(new { success = true });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -958,7 +958,7 @@
|
|||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
name="name"
|
name="name"
|
||||||
placeholder="Dein Name*"
|
placeholder="Name*"
|
||||||
required>
|
required>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -972,7 +972,7 @@
|
|||||||
<input
|
<input
|
||||||
type="email"
|
type="email"
|
||||||
name="email"
|
name="email"
|
||||||
placeholder="Deine E-Mail*"
|
placeholder="E-Mail*"
|
||||||
required>
|
required>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -1013,7 +1013,7 @@
|
|||||||
name="message"
|
name="message"
|
||||||
cols="30"
|
cols="30"
|
||||||
rows="10"
|
rows="10"
|
||||||
placeholder="Deine Nachricht*"
|
placeholder="Nachricht*"
|
||||||
required></textarea>
|
required></textarea>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -1082,27 +1082,38 @@
|
|||||||
messageBox.style.display = "block";
|
messageBox.style.display = "block";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isValidPhone(value) {
|
||||||
|
if (!value) return true;
|
||||||
|
var digitCount = (value.match(/\d/g) || []).length;
|
||||||
|
return /^[0-9+\-\s()]+$/.test(value) && digitCount >= 7 && digitCount <= 15;
|
||||||
|
}
|
||||||
|
|
||||||
form.addEventListener("submit", function (event) {
|
form.addEventListener("submit", function (event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
submitButton.disabled = true;
|
|
||||||
|
|
||||||
var payload = {
|
var payload = {
|
||||||
name: form.querySelector("[name=name]").value,
|
name: form.querySelector("[name=name]").value,
|
||||||
email: form.querySelector("[name=email]").value,
|
email: form.querySelector("[name=email]").value,
|
||||||
phone: form.querySelector("[name=phone]").value,
|
phone: form.querySelector("[name=phone]").value.trim(),
|
||||||
subject: form.querySelector("[name=subject]").value,
|
subject: form.querySelector("[name=subject]").value,
|
||||||
message: form.querySelector("[name=message]").value
|
message: form.querySelector("[name=message]").value
|
||||||
};
|
};
|
||||||
|
|
||||||
fetch("/api/contact", {
|
if (!isValidPhone(payload.phone)) {
|
||||||
|
showMessage("Bitte gib eine gültige Telefonnummer ein.", false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
submitButton.disabled = true;
|
||||||
|
|
||||||
|
fetch("/api/v1/contact", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify(payload)
|
body: JSON.stringify(payload)
|
||||||
})
|
})
|
||||||
.then(function (response) {
|
.then(function (response) {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
showMessage("Danke für deine Nachricht! Ich melde mich so schnell wie möglich.", true);
|
showMessage("Danke für die Nachricht! Ich melde mich so schnell wie möglich.", true);
|
||||||
form.reset();
|
form.reset();
|
||||||
} else {
|
} else {
|
||||||
showMessage("Beim Senden ist ein Fehler aufgetreten. Bitte versuche es erneut.", false);
|
showMessage("Beim Senden ist ein Fehler aufgetreten. Bitte versuche es erneut.", false);
|
||||||
|
|||||||
+10
-27
@@ -1,4 +1,3 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using RazorPagesMovie.Data;
|
using RazorPagesMovie.Data;
|
||||||
@@ -6,7 +5,12 @@ using RazorPagesMovie.Models;
|
|||||||
using RazorPagesMovie.Services;
|
using RazorPagesMovie.Services;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
builder.Configuration.AddJsonFile("appsettings.Local.json", optional: true, reloadOnChange: true);
|
|
||||||
|
if (builder.Environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
builder.Configuration.AddJsonFile("appsettings.Local.json", optional: true, reloadOnChange: true);
|
||||||
|
}
|
||||||
|
|
||||||
builder.Services.AddDbContext<RazorPagesMovieContext>(options =>
|
builder.Services.AddDbContext<RazorPagesMovieContext>(options =>
|
||||||
options.UseSqlServer(builder.Configuration.GetConnectionString("RazorPagesMovieContext") ?? throw new InvalidOperationException("Connection string 'RazorPagesMovieContext' not found.")));
|
options.UseSqlServer(builder.Configuration.GetConnectionString("RazorPagesMovieContext") ?? throw new InvalidOperationException("Connection string 'RazorPagesMovieContext' not found.")));
|
||||||
|
|
||||||
@@ -22,6 +26,7 @@ builder.Services.AddRazorPages(options =>
|
|||||||
{
|
{
|
||||||
options.Conventions.AuthorizeFolder("/Admin");
|
options.Conventions.AuthorizeFolder("/Admin");
|
||||||
});
|
});
|
||||||
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
builder.Services.AddHttpClient();
|
builder.Services.AddHttpClient();
|
||||||
builder.Services.AddHostedService<ContactMessageNotificationService>();
|
builder.Services.AddHostedService<ContactMessageNotificationService>();
|
||||||
@@ -32,7 +37,9 @@ using (var scope = app.Services.CreateScope())
|
|||||||
{
|
{
|
||||||
var services = scope.ServiceProvider;
|
var services = scope.ServiceProvider;
|
||||||
var context = services.GetRequiredService<RazorPagesMovieContext>();
|
var context = services.GetRequiredService<RazorPagesMovieContext>();
|
||||||
|
|
||||||
context.Database.Migrate();
|
context.Database.Migrate();
|
||||||
|
|
||||||
SeedData.Initialize(services);
|
SeedData.Initialize(services);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,30 +55,6 @@ app.UseRouting();
|
|||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
app.MapRazorPages();
|
app.MapRazorPages();
|
||||||
|
app.MapControllers();
|
||||||
app.MapPost("/api/contact", async (ContactMessageRequest request, RazorPagesMovieContext context) =>
|
|
||||||
{
|
|
||||||
var contactMessage = new ContactMessage
|
|
||||||
{
|
|
||||||
Name = request.Name ?? string.Empty,
|
|
||||||
Email = request.Email ?? string.Empty,
|
|
||||||
Phone = request.Phone,
|
|
||||||
Subject = request.Subject,
|
|
||||||
Message = request.Message ?? string.Empty
|
|
||||||
};
|
|
||||||
|
|
||||||
var validationResults = new List<ValidationResult>();
|
|
||||||
if (!Validator.TryValidateObject(contactMessage, new ValidationContext(contactMessage), validationResults, validateAllProperties: true))
|
|
||||||
{
|
|
||||||
return Results.BadRequest(new { error = "Bitte alle Pflichtfelder korrekt ausfüllen." });
|
|
||||||
}
|
|
||||||
|
|
||||||
context.ContactMessage.Add(contactMessage);
|
|
||||||
await context.SaveChangesAsync();
|
|
||||||
|
|
||||||
return Results.Ok(new { success = true });
|
|
||||||
});
|
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
record ContactMessageRequest(string? Name, string? Email, string? Phone, string? Subject, string? Message);
|
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ public class ContactMessageNotificationService : BackgroundService
|
|||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(botToken) || string.IsNullOrWhiteSpace(chatId))
|
if (string.IsNullOrWhiteSpace(botToken) || string.IsNullOrWhiteSpace(chatId))
|
||||||
{
|
{
|
||||||
|
_logger.LogWarning("Telegram:BotToken oder Telegram:ChatId ist nicht konfiguriert – Benachrichtigung wird übersprungen.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user