diff --git a/Frontend/fonts/utheem.ttf b/Frontend/fonts/utheem.ttf new file mode 100644 index 0000000..2f244dc Binary files /dev/null and b/Frontend/fonts/utheem.ttf differ diff --git a/Frontend/index.html b/Frontend/index.html new file mode 100644 index 0000000..eb1cfcd --- /dev/null +++ b/Frontend/index.html @@ -0,0 +1,408 @@ + + + + + + Petition Details + + + + + +
+
Loading petition...
+ + +
+ + + + diff --git a/Frontend/style.css b/Frontend/style.css new file mode 100644 index 0000000..ba5395a --- /dev/null +++ b/Frontend/style.css @@ -0,0 +1,334 @@ +@font-face { + font-family: 'Utheem'; + src: url('fonts/utheem.woff') format('woff'), + url('fonts/utheem.ttf') format('truetype'); + font-weight: normal; + font-style: normal; +} + +@font-face { + font-family: 'Shangu'; + src: url('fonts/shangu.woff') format('woff'), + url('fonts/shangu.ttf') format('truetype'); + font-weight: normal; + font-style: normal; +} + +* { + margin: 0; + padding: 0; + box-sizing: border-box; +} + +body { + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif; + background-color: #f5f5f5; + color: #333; + line-height: 1.6; + padding: 20px; +} + +.container { + max-width: 900px; + margin: 0 auto; + background-color: white; + border-radius: 8px; + box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); + padding: 40px; +} + +.lang-switcher { + display: flex; + gap: 10px; + justify-content: flex-end; + margin-bottom: 20px; +} + +.lang-btn { + padding: 8px 20px; + border: 2px solid #007bff; + background-color: white; + color: #007bff; + border-radius: 6px; + cursor: pointer; + font-size: 14px; + font-weight: 500; + transition: all 0.3s ease; +} + +.lang-btn:hover { + background-color: #f0f8ff; +} + +.lang-btn.active { + background-color: #007bff; + color: white; +} + +.lang-btn:focus { + outline: none; + box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25); +} + +.loading { + text-align: center; + font-size: 18px; + color: #666; + padding: 40px; +} + +.error { + background-color: #fee; + border: 1px solid #fcc; + color: #c33; + padding: 20px; + border-radius: 4px; + margin-bottom: 20px; +} + +.petition-header { + border-bottom: 2px solid #007bff; + padding-bottom: 20px; + margin-bottom: 30px; +} + +.petition-header h1 { + font-size: 32px; + color: #222; + margin-bottom: 10px; +} + +.petition-header h2 { + font-size: 24px; + color: #555; + margin-bottom: 15px; +} + +.petition-header h2.dhivehi { + font-family: 'Shangu', 'Faruma', 'MV Faseyha', 'Waheed', 'Noto Sans Thaana', sans-serif; +} + +.dhivehi { + font-family: 'Utheem', 'Faruma', 'MV Faseyha', 'Waheed', 'Noto Sans Thaana', sans-serif; + direction: rtl; + text-align: right; +} + +.metadata { + display: flex; + gap: 20px; + flex-wrap: wrap; + margin-top: 15px; + font-size: 14px; + color: #666; +} + +.metadata span { + background-color: #f0f0f0; + padding: 6px 12px; + border-radius: 4px; +} + +.metadata span span { + background-color: transparent; + padding: 0; + font-weight: bold; + color: #333; +} + +.author-details { + background-color: #f9f9f9; + padding: 20px; + border-radius: 6px; + margin-bottom: 30px; +} + +.author-details h3 { + font-size: 18px; + color: #007bff; + margin-bottom: 12px; +} + +.author-details p { + margin-bottom: 8px; +} + +.petition-body { + margin-top: 30px; +} + +.petition-body h3 { + font-size: 20px; + color: #007bff; + margin-top: 30px; + margin-bottom: 15px; + padding-bottom: 10px; + border-bottom: 1px solid #e0e0e0; +} + +.body-content { + padding: 15px; + background-color: #fafafa; + border-radius: 4px; + line-height: 1.8; + white-space: pre-wrap; +} + +.body-content strong { + color: #222; +} + +.signature-count { + font-weight: bold; +} + +.signature-count span { + color: #007bff !important; +} + +.signature-section { + margin-top: 40px; + padding-top: 30px; + border-top: 2px solid #e0e0e0; +} + +.signature-section h3 { + font-size: 24px; + color: #007bff; + margin-bottom: 25px; +} + +.form-group { + margin-bottom: 20px; +} + +.form-group label { + display: block; + font-weight: 600; + margin-bottom: 8px; + color: #333; +} + +.form-group input[type="text"] { + width: 100%; + padding: 12px; + border: 2px solid #ddd; + border-radius: 6px; + font-size: 16px; + transition: border-color 0.3s ease; +} + +.form-group input[type="text"]:focus { + outline: none; + border-color: #007bff; +} + +.signature-pad-container { + border: 2px solid #ddd; + border-radius: 6px; + background-color: white; + display: inline-block; + cursor: crosshair; + margin-bottom: 10px; +} + +#signature-pad { + display: block; + touch-action: none; +} + +.signature-actions { + margin-bottom: 15px; +} + +.form-buttons { + display: flex; + gap: 12px; + align-items: center; +} + +.btn-primary, +.btn-secondary { + padding: 12px 24px; + border: none; + border-radius: 6px; + font-size: 16px; + font-weight: 600; + cursor: pointer; + transition: all 0.3s ease; +} + +.btn-primary { + background-color: #007bff; + color: white; +} + +.btn-primary:hover { + background-color: #0056b3; +} + +.btn-secondary { + background-color: #6c757d; + color: white; +} + +.btn-secondary:hover { + background-color: #545b62; +} + +.form-message { + padding: 12px; + border-radius: 6px; + margin-bottom: 15px; + display: none; +} + +.form-message.success { + background-color: #d4edda; + border: 1px solid #c3e6cb; + color: #155724; +} + +.form-message.error { + background-color: #f8d7da; + border: 1px solid #f5c6cb; + color: #721c24; +} + +@media (max-width: 768px) { + .container { + padding: 20px; + } + + .petition-header h1 { + font-size: 24px; + } + + .petition-header h2 { + font-size: 18px; + } + + .metadata { + flex-direction: column; + gap: 10px; + } + + #signature-pad { + width: 100%; + max-width: 100%; + } + + .signature-pad-container { + width: 100%; + overflow-x: auto; + } + + .form-buttons { + flex-direction: column; + width: 100%; + } + + .form-buttons button { + width: 100%; + } +} diff --git a/README.md b/README.md index 17d7361..d72f328 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,10 @@ # WPetition Submission API + +a self hostable e petition system to collect signatures for your cause. + +## why make this maldives parliment promised the release of a e-petition system powered by efass will be released months ago and then never released it i said fuck it i want data protection bill so i made this simple signature collection system since the law doesnt care if youre signature is signed digitally or via wet ink. -made it in 5 hours and didnt even vibe code it ## nerd shit A petition signing API built with ASP.NET Core 9.0 that allows users to sign petitions and retrieve petition details. Features rate limiting to prevent spam and duplicate signature detection. @@ -26,11 +29,7 @@ A petition signing API built with ASP.NET Core 9.0 that allows users to sign pet ### MongoDB Setup -1. Create a MongoDB database for the petition system -2. Create the following collections: - - `signatures` - stores petition signatures - - `petitions` - stores petition details - - `authors` - stores petition author information +refer to the details below ### Application Configuration @@ -38,6 +37,9 @@ Update `appsettings.json` with your MongoDB connection settings: ```json { + "PetitionSettings": { + "AllowPetitionCreation": true + }, "MongoDbSettings": { "ConnectionString": "mongodb://localhost:27017", "DatabaseName": "petition_database" @@ -52,6 +54,10 @@ Update `appsettings.json` with your MongoDB connection settings: } ``` +by default `AllowPetitionCreation` is true. you must upload your Petition to the debug controller and then shut down the server and set this value to false and reboot or anyone will be able to submit petitions. + +check out `sample.Petition.md` on how to structure your petition so it will be accepted by the server + ### Rate Limiting Configuration The API is configured with rate limiting to prevent spam. Default settings in `Program.cs`: @@ -197,41 +203,6 @@ Retrieves details of a specific petition including author information. {} ``` -## Data Models - -### Signature (Widget) -```csharp -{ - "id": "ObjectId", - "name": "string", - "idCard": "string", - "signature_SVG": "string", - "timestamp": "DateTime" -} -``` - -### Petition Details -```csharp -{ - "id": "Guid", - "startDate": "DateOnly", - "nameDhiv": "string", - "nameEng": "string", - "petitionBodyDhiv": "string", - "petitionBodyEng": "string", - "authorId": "Guid", - "signatureCount": "int" -} -``` - -### Author -```csharp -{ - "id": "Guid", - "name": "string", - "nid": "string" -} -``` ## Security Features diff --git a/Submission.Api/Configuration/PetitionSettings.cs b/Submission.Api/Configuration/PetitionSettings.cs new file mode 100644 index 0000000..bcb7336 --- /dev/null +++ b/Submission.Api/Configuration/PetitionSettings.cs @@ -0,0 +1,6 @@ +namespace Submission.Api.Configuration; + +public class PetitionSettings +{ + public bool AllowPetitionCreation { get; set; } +} diff --git a/Submission.Api/Controllers/DebugController.cs b/Submission.Api/Controllers/DebugController.cs new file mode 100644 index 0000000..fa2389c --- /dev/null +++ b/Submission.Api/Controllers/DebugController.cs @@ -0,0 +1,241 @@ +using Ashi.MongoInterface.Service; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using Submission.Api.Configuration; +using Submission.Api.Models; +using System.Globalization; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace Submission.Api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class DebugController : ControllerBase + { + private readonly PetitionSettings _petitionSettings; + private readonly IMongoRepository _authorRepository; + private readonly IMongoRepository _petitionRepository; + + public DebugController( + IOptions petitionSettings, + IMongoRepository authorRepository, + IMongoRepository petitionRepository) + { + _petitionSettings = petitionSettings.Value; + _authorRepository = authorRepository; + _petitionRepository = petitionRepository; + } + + [HttpGet("petitions", Name = "GetPetitions")] + public IActionResult GetPetitions() + { + try + { + var files = Directory.EnumerateFiles("Petitions"); + return Ok(files); + } + catch (Exception e) + { + return Problem("Petitions Folder not found"); + } + + } + + + [HttpGet("create-petition-folder", Name = "CreatePetitionFolder")] + public IActionResult create_petition_folder() + { + try + { + Directory.CreateDirectory("Petitions"); + return Ok("Petitions folder created"); + } + catch (Exception e) + { + return Problem(e.Message); + } + } + + [HttpPost("upload-petition", Name = "UploadPetition")] + public async Task UploadPetition(IFormFile file) + { + // 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" }); + } + + // Validate file exists + if (file == null || file.Length == 0) + { + return BadRequest(new { message = "No file uploaded" }); + } + + // Validate file extension + if (!file.FileName.EndsWith(".md", StringComparison.OrdinalIgnoreCase)) + { + return BadRequest(new { message = "Only .md files are allowed" }); + } + + try + { + // Read file content + string fileContent; + using (var reader = new StreamReader(file.OpenReadStream())) + { + fileContent = await reader.ReadToEndAsync(); + } + + // Parse frontmatter and body + var (frontmatter, body) = ParseMarkdownFile(fileContent); + + if (frontmatter == null) + { + return BadRequest(new { message = "Invalid markdown format. Frontmatter is required." }); + } + + // Parse YAML frontmatter + var deserializer = new DeserializerBuilder() + .WithNamingConvention(CamelCaseNamingConvention.Instance) + .Build(); + + var metadata = deserializer.Deserialize>(frontmatter); + + // Extract values + var petitionId = Guid.NewGuid(); + var startDateStr = metadata["startDate"].ToString(); + var nameDhiv = metadata["nameDhiv"].ToString(); + var nameEng = metadata["nameEng"].ToString(); + var authorData = metadata["author"] as Dictionary; + + var authorName = authorData["name"].ToString(); + var authorNid = authorData["nid"].ToString(); + + // Parse start date (format: dd-MM-yyyy) + var startDate = DateOnly.ParseExact(startDateStr, "dd-MM-yyyy", CultureInfo.InvariantCulture); + + // Parse petition bodies from markdown + var (petitionBodyDhiv, petitionBodyEng) = ParsePetitionBodies(body); + + // Check if petition already exists + var existingPetition = await _petitionRepository.FindByIdAsync(petitionId); + if (existingPetition != null) + { + return Conflict(new { message = $"A petition with ID '{petitionId}' already exists in the database" }); + } + + // Create or get author + var author = await _authorRepository.FindOneAsync(x => x.NID == authorNid); + if (author == null) + { + author = new Author + { + Id = Guid.NewGuid(), + Name = authorName, + NID = authorNid + }; + await _authorRepository.InsertOneAsync(author); + } + + // Create petition + var petition = new PetitionDetail + { + Id = petitionId, + StartDate = startDate, + NameDhiv = nameDhiv, + NameEng = nameEng, + AuthorId = author.Id, + PetitionBodyDhiv = petitionBodyDhiv, + PetitionBodyEng = petitionBodyEng, + SignatureCount = 0 + }; + + await _petitionRepository.InsertOneAsync(petition); + + // Save file with GUID prefix + Directory.CreateDirectory("Petitions"); + var newFileName = $"{Guid.NewGuid()}_{file.FileName}"; + var filePath = Path.Combine("Petitions", newFileName); + + await System.IO.File.WriteAllTextAsync(filePath, fileContent); + + return Ok(new + { + message = "Petition created successfully", + petitionId = petitionId, + fileName = newFileName, + filePath = filePath, + authorId = author.Id + }); + } + catch (Exception e) + { + return Problem(e.Message); + } + } + + private (string frontmatter, string body) ParseMarkdownFile(string content) + { + var lines = content.Split('\n'); + if (lines.Length < 3 || lines[0].Trim() != "---") + { + return (null, null); + } + + var frontmatterLines = new List(); + var bodyLines = new List(); + var inFrontmatter = true; + var frontmatterClosed = false; + + for (int i = 1; i < lines.Length; i++) + { + if (lines[i].Trim() == "---" && inFrontmatter) + { + inFrontmatter = false; + frontmatterClosed = true; + continue; + } + + if (inFrontmatter) + { + frontmatterLines.Add(lines[i]); + } + else + { + bodyLines.Add(lines[i]); + } + } + + if (!frontmatterClosed) + { + return (null, null); + } + + return (string.Join("\n", frontmatterLines), string.Join("\n", bodyLines)); + } + + private (string dhivehiBody, string englishBody) ParsePetitionBodies(string body) + { + var dhivehiBody = ""; + var englishBody = ""; + + var sections = body.Split("##", StringSplitOptions.RemoveEmptyEntries); + + foreach (var section in sections) + { + var trimmed = section.Trim(); + if (trimmed.StartsWith("Petition Body (Dhivehi)", StringComparison.OrdinalIgnoreCase)) + { + dhivehiBody = trimmed.Replace("Petition Body (Dhivehi)", "").Trim(); + } + else if (trimmed.StartsWith("Petition Body (English)", StringComparison.OrdinalIgnoreCase)) + { + englishBody = trimmed.Replace("Petition Body (English)", "").Trim(); + } + } + + return (dhivehiBody, englishBody); + } + } +} diff --git a/Submission.Api/Controllers/SignController.cs b/Submission.Api/Controllers/SignController.cs index f152f8c..3ce765e 100644 --- a/Submission.Api/Controllers/SignController.cs +++ b/Submission.Api/Controllers/SignController.cs @@ -1,10 +1,8 @@ -using System.CodeDom; -using System.Runtime.InteropServices; using Ashi.MongoInterface.Service; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.RateLimiting; using Microsoft.Extensions.Caching.Memory; +using MongoDB.Driver; using Submission.Api.Dto; using Submission.Api.Models; @@ -14,16 +12,16 @@ namespace Submission.Api.Controllers [ApiController] public class SignController : ControllerBase { - + private readonly IMongoRepository _authorRepository; private readonly IMongoRepository _detailRepository; - private readonly IMongoRepository _signatureRepository; + private readonly IMongoRepository _signatureRepository; private readonly IMemoryCache _cache; public SignController( IMongoRepository authorRepository, IMongoRepository detailRepository, - IMongoRepository signatureRepository, + IMongoRepository signatureRepository, IMemoryCache cache) { _authorRepository = authorRepository; @@ -32,32 +30,53 @@ namespace Submission.Api.Controllers _cache = cache; } - [HttpPost(Name = "petition/{id}")] + [HttpPost("petition/{petition_id}", Name = "SignPetition")] [EnableRateLimiting("SignPetitionPolicy")] - public async Task SignDisHoe([FromRoute]Guid petition_id,[FromBody] WidgetsDto body) + public async Task SignDisHoe([FromRoute] Guid petition_id, [FromBody] WidgetsDto body) { + var cacheKey = $"petition_{petition_id}"; + + var pet = await _detailRepository.FindByIdAsync(petition_id); + + if (pet == null) + return NotFound(); + + //TODO : add svg validation + + //check to see if the same person signed the petition already //if dupe send error saying user already signed - var dupe = await _signatureRepository.FindOneAsync(x => x.IdCard == body.IdCard); - if (dupe != null) - return Problem("You already signed this petition"); - + var dupe = await _signatureRepository.FindOneAsync(x => x.IdCard == body.IdCard); + if (dupe != null) + return Problem("You already signed this petition"); + //add signature to the db - await _signatureRepository.InsertOneAsync(new Widget + await _signatureRepository.InsertOneAsync(new Signature { IdCard = body.IdCard, Name = body.Name, Signature_SVG = body.Signature, - Timestamp = DateTime.Now + Timestamp = DateTime.Now, + PetitionId = petition_id }); - + //update signature count - + if (pet.SignatureCount == null) + { + pet.SignatureCount = 0; + } + + var count_update_filter = Builders.Filter.Eq("_id", petition_id); + var Countupdate = Builders.Update.Inc("SignatureCount", 1); + await _detailRepository.UpdateOneAsync(count_update_filter, Countupdate); + + _cache.Remove(cacheKey); + return Ok("your signature has been submitted"); } - [HttpGet(Name = "petition/{id}")] - public async Task GetDisHoe([FromRoute] Guid petition_id) + [HttpGet("petition/{petition_id}", Name = "GetPetition")] + public async Task GetDisHoe([FromRoute] Guid petition_id) { var cacheKey = $"petition_{petition_id}"; @@ -68,7 +87,7 @@ namespace Submission.Api.Controllers } // Not in cache, fetch from database - var pet = await _detailRepository.FindByIdAsync(petition_id); + var pet = await _detailRepository.FindByIdAsync(petition_id); if (pet == null) return NotFound(); @@ -88,12 +107,14 @@ namespace Submission.Api.Controllers { Name = author.Name, NID = author.NID, - } + }, + + SignatureCount = pet.SignatureCount }; // Store in cache with 5 minute expiration var cacheOptions = new MemoryCacheEntryOptions() - .SetAbsoluteExpiration(TimeSpan.FromHours(12)); + .SetAbsoluteExpiration(TimeSpan.FromHours(1)); _cache.Set(cacheKey, dto, cacheOptions); diff --git a/Submission.Api/Models/Widget.cs b/Submission.Api/Models/Signature.cs similarity index 78% rename from Submission.Api/Models/Widget.cs rename to Submission.Api/Models/Signature.cs index d7803bc..1e81a71 100644 --- a/Submission.Api/Models/Widget.cs +++ b/Submission.Api/Models/Signature.cs @@ -3,10 +3,12 @@ namespace Submission.Api.Models; [BsonCollection("signatures")] -public class Widget : Document +public class Signature : Document { public string Name { get; set; } public string IdCard { get; set; } public string Signature_SVG { get; set; } public DateTime Timestamp { get; set; } + + public Guid PetitionId { get; set; } } \ No newline at end of file diff --git a/Submission.Api/Program.cs b/Submission.Api/Program.cs index baf5e08..6bf1f98 100644 --- a/Submission.Api/Program.cs +++ b/Submission.Api/Program.cs @@ -1,13 +1,14 @@ -using System.Configuration; using Ashi.MongoInterface; using Ashi.MongoInterface.Service; using Microsoft.AspNetCore.RateLimiting; using Microsoft.Extensions.Options; +using Submission.Api.Configuration; -var builder = WebApplication.CreateBuilder(args); +var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.Configure(builder.Configuration.GetSection("MongoDbSettings")); +builder.Services.Configure(builder.Configuration.GetSection("PetitionSettings")); builder.Services.AddSingleton(serviceProvider => serviceProvider.GetRequiredService>().Value); @@ -17,8 +18,9 @@ builder.Services.AddScoped((typeof(IMongoRepository<>)), typeof(MongoRepository< builder.Services.AddMemoryCache(); builder.Services.AddControllers(); -// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi -builder.Services.AddOpenApi(); +// Add Swagger/OpenAPI +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); // Add rate limiting builder.Services.AddRateLimiter(options => @@ -37,7 +39,8 @@ var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - app.MapOpenApi(); + app.UseSwagger(); + app.UseSwaggerUI(); } app.UseHttpsRedirection(); diff --git a/Submission.Api/Properties/launchSettings.json b/Submission.Api/Properties/launchSettings.json index 26e3ab5..4fb9e15 100644 --- a/Submission.Api/Properties/launchSettings.json +++ b/Submission.Api/Properties/launchSettings.json @@ -4,7 +4,7 @@ "http": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": false, + "launchBrowser": true, "applicationUrl": "http://localhost:5299", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/Submission.Api/Submission.Api.csproj b/Submission.Api/Submission.Api.csproj index cb00eb4..7c6d60a 100644 --- a/Submission.Api/Submission.Api.csproj +++ b/Submission.Api/Submission.Api.csproj @@ -8,8 +8,9 @@ - + + diff --git a/Submission.Api/Submission.Api.csproj.user b/Submission.Api/Submission.Api.csproj.user index 9ff5820..e5a2ec0 100644 --- a/Submission.Api/Submission.Api.csproj.user +++ b/Submission.Api/Submission.Api.csproj.user @@ -1,6 +1,11 @@  - https + http + ApiControllerEmptyScaffolder + root/Common/Api + + + ProjectDebugger \ No newline at end of file diff --git a/Submission.Api/appsettings.json b/Submission.Api/appsettings.json index 10f68b8..ede0085 100644 --- a/Submission.Api/appsettings.json +++ b/Submission.Api/appsettings.json @@ -1,4 +1,11 @@ { + "PetitionSettings": { + "AllowPetitionCreation": true + }, + "MongoDbSettings": { + "ConnectionString": "mongodb://localhost:27017", + "DatabaseName": "petition_database" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/sample.Petition.md b/sample.Petition.md new file mode 100644 index 0000000..82b96f1 --- /dev/null +++ b/sample.Petition.md @@ -0,0 +1,17 @@ +--- +startDate: 14-12-2025 +nameDhiv: "މިސާލު ނަން" +nameEng: "Sample Petition Name" +author: + name: "Fishie" + nid: "AAAAA12345" +--- + +## Petition Body (Dhivehi) + +މިއީ ދިވެހި ބަސްނުވަތައް ލިޔެވިފައިވާ ބައިތައް. + +## Petition Body (English) + +This is the English version of the petition body written in clear paragraphs. +You can use normal Markdown formatting here such as **bold**, lists, and links. \ No newline at end of file