Move contact endpoint from Program.cs into ContactController
Deploy Staging / deploy (push) Successful in 1m11s
Deploy Staging / deploy (push) Successful in 1m11s
This commit is contained in:
@@ -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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
+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);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user