mirror of
https://github.com/MvDevsUnion/WPetition.git
synced 2026-02-28 13:00:39 +00:00
i frogor to save some file before pushing
This commit is contained in:
@@ -189,98 +189,6 @@ namespace Submission.Api.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
// New endpoint: form-based petition upload
|
||||
[HttpPost("upload-petition-form", Name = "UploadPetitionForm")]
|
||||
public async Task<IActionResult> UploadPetitionForm([FromForm] PetitionFormDto form)
|
||||
{
|
||||
// Check if petition creation is allowed
|
||||
if (!_petitionSettings.AllowPetitionCreation)
|
||||
{
|
||||
return StatusCode(403, new { message = "Petition creation is disabled. Set 'PetitionSettings:AllowPetitionCreation' to true in appsettings.json" });
|
||||
}
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Parse start date (format: dd-MM-yyyy)
|
||||
DateOnly startDate;
|
||||
try
|
||||
{
|
||||
startDate = DateOnly.ParseExact(form.StartDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
return BadRequest(new { message = "StartDate must be in format dd-MM-yyyy" });
|
||||
}
|
||||
|
||||
// Check for duplicate slug
|
||||
var existingPetition = await _petitionRepository.FindOneAsync(x => x.Slug == form.Slug);
|
||||
if (existingPetition != null)
|
||||
{
|
||||
return Conflict(new { message = $"A petition with slug '{form.Slug}' already exists" });
|
||||
}
|
||||
|
||||
var petitionId = Guid.NewGuid();
|
||||
|
||||
// Create or get author
|
||||
var author = await _authorRepository.FindOneAsync(x => x.NID == form.AuthorNid);
|
||||
if (author == null)
|
||||
{
|
||||
author = new Author
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = form.AuthorName,
|
||||
NID = form.AuthorNid
|
||||
};
|
||||
await _authorRepository.InsertOneAsync(author);
|
||||
}
|
||||
|
||||
// Create petition
|
||||
var petition = new PetitionDetail
|
||||
{
|
||||
Id = petitionId,
|
||||
Slug = form.Slug,
|
||||
StartDate = startDate,
|
||||
NameDhiv = form.NameDhiv,
|
||||
NameEng = form.NameEng,
|
||||
AuthorId = author.Id,
|
||||
PetitionBodyDhiv = form.PetitionBodyDhiv,
|
||||
PetitionBodyEng = form.PetitionBodyEng,
|
||||
SignatureCount = 0
|
||||
};
|
||||
|
||||
await _petitionRepository.InsertOneAsync(petition);
|
||||
|
||||
// Build markdown file content and save to Petitions folder
|
||||
var frontmatter = $"---\nslug: \"{EscapeYaml(form.Slug)}\"\nstartDate: {form.StartDate}\nnameDhiv: \"{EscapeYaml(form.NameDhiv)}\"\nnameEng: \"{EscapeYaml(form.NameEng)}\"\nauthor:\n name: \"{EscapeYaml(form.AuthorName)}\"\n nid: \"{EscapeYaml(form.AuthorNid)}\"\n---\n";
|
||||
var body = $"## Petition Body (Dhivehi)\n\n{form.PetitionBodyDhiv}\n\n## Petition Body (English)\n\n{form.PetitionBodyEng}\n";
|
||||
var fileContent = frontmatter + "\n" + body;
|
||||
|
||||
Directory.CreateDirectory("Petitions");
|
||||
var newFileName = $"{Guid.NewGuid()}.md";
|
||||
var filePath = Path.Combine("Petitions", newFileName);
|
||||
|
||||
await System.IO.File.WriteAllTextAsync(filePath, fileContent);
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
message = "Petition created successfully",
|
||||
petitionId = petitionId,
|
||||
slug = form.Slug,
|
||||
fileName = newFileName,
|
||||
filePath = filePath,
|
||||
authorId = author.Id
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return Problem(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("svg-debug", Name = "SvgDebug")]
|
||||
public async Task<IActionResult> SVG_TEST([FromForm]string svg)
|
||||
@@ -356,10 +264,6 @@ namespace Submission.Api.Controllers
|
||||
return (dhivehiBody, englishBody);
|
||||
}
|
||||
|
||||
private static string EscapeYaml(string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value)) return "";
|
||||
return value.Replace("\"", "\\\"");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user