31 lines
1.2 KiB
C#
31 lines
1.2 KiB
C#
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||
|
|
using SoroushAsadi.Services;
|
||
|
|
|
||
|
|
namespace SoroushAsadi.Pages;
|
||
|
|
|
||
|
|
/// <summary>POST /contact — JSON endpoint for the contact form.</summary>
|
||
|
|
[IgnoreAntiforgeryToken]
|
||
|
|
public class ContactModel(EmailService email) : PageModel
|
||
|
|
{
|
||
|
|
public record ContactBody(
|
||
|
|
string? Name, string? Company, string? Service,
|
||
|
|
string? Budget, string? Message, string? Locale);
|
||
|
|
|
||
|
|
public async Task<IActionResult> OnPostAsync([FromBody] ContactBody body)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(body.Name) || body.Name.Length < 2 ||
|
||
|
|
string.IsNullOrWhiteSpace(body.Service) || body.Service.Length < 2 ||
|
||
|
|
string.IsNullOrWhiteSpace(body.Budget) || body.Budget.Length < 2 ||
|
||
|
|
string.IsNullOrWhiteSpace(body.Message) || body.Message.Length < 2)
|
||
|
|
return BadRequest(new { error = "Missing required fields" });
|
||
|
|
|
||
|
|
var err = await email.SendContactAsync(new EmailService.ContactForm(
|
||
|
|
body.Name!, body.Company ?? "", body.Service!, body.Budget!, body.Message!, body.Locale ?? "en"));
|
||
|
|
|
||
|
|
return err is null
|
||
|
|
? new JsonResult(new { ok = true })
|
||
|
|
: StatusCode(502, new { error = err });
|
||
|
|
}
|
||
|
|
}
|