Skip to content
Snippets Groups Projects
Commit c9206089 authored by Sara Agossou's avatar Sara Agossou
Browse files

successfully creating election

parent cc2103e1
No related branches found
No related tags found
No related merge requests found
Showing
with 237 additions and 68 deletions
......@@ -5,6 +5,8 @@ VisualStudioVersion = 25.0.1704.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "electronicvotingfyp", "electronicvotingfyp\electronicvotingfyp.csproj", "{A63B1935-D67D-4BA8-8E62-A6937637FEFD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChooseTest", "ChooseTest\ChooseTest.csproj", "{1BEE025A-4CC0-4790-A99F-C52C1C4DCB91}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -15,6 +17,10 @@ Global
{A63B1935-D67D-4BA8-8E62-A6937637FEFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A63B1935-D67D-4BA8-8E62-A6937637FEFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A63B1935-D67D-4BA8-8E62-A6937637FEFD}.Release|Any CPU.Build.0 = Release|Any CPU
{1BEE025A-4CC0-4790-A99F-C52C1C4DCB91}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1BEE025A-4CC0-4790-A99F-C52C1C4DCB91}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1BEE025A-4CC0-4790-A99F-C52C1C4DCB91}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1BEE025A-4CC0-4790-A99F-C52C1C4DCB91}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
......@@ -27,12 +27,13 @@ public class electronicvotingfypIdentityDbContext : IdentityDbContext<IdentityUs
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
builder.Entity<Dashboard>().HasOne(d => d.User);
builder.Entity<Election>().HasMany(e => e.CandidatesVoters);
//builder.Entity<Election>().HasMany(e => e.CandidatesVoters);
builder.Entity<Election>().HasOne(e => e.Creator);
builder.Entity<Participation>().HasOne(p => p.Election);
builder.Entity<Participation>().HasOne(p => p.Participant);
builder.Entity<Participation>().HasOne(p => p.Dashboard);
builder.Entity<Election>().HasMany(e => e.Dashboards)
.WithMany(e => e.Elections)
.UsingEntity<Participation>();
builder.Entity<ElectionOption>().HasOne(o => o.Election);
......
......@@ -2,10 +2,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using electronicvotingfyp.Areas.Identity.Data;
using electronicvotingfyp.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
......@@ -76,7 +78,15 @@ namespace electronicvotingfyp.Controllers
var token = await userManager.GenerateEmailConfirmationTokenAsync(user);
var confirmationLink = Url.Action("ConfimEmail", "Account", new { userId = user.Id, token = token }, Request.Scheme);
logger.Log(LogLevel.Warning, confirmationLink);
DbContextOptions<electronicvotingfypIdentityDbContext> options = new DbContextOptions<electronicvotingfypIdentityDbContext>();
using (var db = new electronicvotingfypIdentityDbContext(options))
{
var Id = Guid.NewGuid();
Dashboard board = new Dashboard();
board.User = db.Users.Single(u => u.Email == user.Email);
db.Dashboards.Add(board);
await db.SaveChangesAsync();
}
//await signInManager.SignInAsync(user, isPersistent: false);
return RedirectToAction("confirmation", "home");
}
......
......@@ -18,6 +18,7 @@ namespace electronicvotingfyp.Controllers
{
return View();
}
}
}
......@@ -38,7 +38,7 @@ namespace electronicvotingfyp.Controllers
[HttpGet]
public IActionResult Create()
{
ElectionCreateViewModel objElectModel = new ElectionCreateViewModel() { StartDate = DateTime.Now, EndDate = DateTime.Now.AddHours(24), SystemUsers = userManager.Users.AsEnumerable(), Question = "Which ones of these options do you choose?" };
ElectionCreateViewModel objElectModel = new ElectionCreateViewModel() { StartDate = DateTime.Now, EndDate = DateTime.Now.AddHours(24), SystemUsers = userManager.Users.AsEnumerable(), Question = "Which ones of these options do you choose?" , VotingOptions = new List<string>(), Voters = new List<string>() };
return View(objElectModel);
}
......@@ -54,53 +54,60 @@ namespace electronicvotingfyp.Controllers
}
[HttpPost]
public async Task<IActionResult> Create(ElectionCreateViewModel model)
public async Task<IActionResult> Create(ElectionCreateViewModel model, IFormFile Candidates, IFormFile Voters)
{
//model.VotersFile = Voters;
//model.VotingOptionsFile = Candidates;
FileUploadChecker fc = new FileUploadChecker();
if (ModelState.IsValid)
if (Candidates != null && Voters != null)
{
if (fc.UploadUserFile(model.VotersFile))
if (fc.UploadUserFile(Voters))
{
fc.AddVoters(model.Voters, model.VotersFile);
fc.AddVoters(model.Voters, Voters);
}
if (fc.UploadUserFile(model.VotingOptionsFile))
if (fc.UploadUserFile(Candidates))
{
fc.AddOptions(model.VotingOptions, model.VotingOptionsFile);
List<string> Opt = new List<string>();
fc.AddOptions(Opt, Candidates);
model.VotingOptions = Opt;
}
}
if (fc.ErrorMessage != null || !fc.validFile)
if (!fc.validFile)
{
ViewBag.ResultErrorMessage = fc.ErrorMessage;
return View();
TempData["Message"] = fc.ErrorMessage;
return RedirectToAction("Create", "Election");
}
else
{
DbContextOptions<electronicvotingfypIdentityDbContext> options = new DbContextOptions<electronicvotingfypIdentityDbContext>();
using (var db = new electronicvotingfypIdentityDbContext(options))
{
var participations = new List<Participation>();
foreach (string uid in model.Voters)
var user = await userManager.GetUserAsync(this.User);
var participations = new List<Dashboard>();
foreach (string uid in model.Voters)
{
var participant = await userManager.FindByIdAsync(uid);
var dashboard = db.Dashboards.Where(d => d.User == participant).ToList();
var participation = new Participation { Participant = participant, Dashboard = dashboard.First() };
participations.Add(participation);
var participant = await userManager.FindByEmailAsync(uid);
//var dashboard = db.Dashboards.Where(d => d.User.Id == participant.Id).ToList();
//var dashboard = db.Dashboards.Single(d => d.User.Id == participant.Id);
//var participation = new Participation();
var board = db.Dashboards.Single(d => d.User.Id == participant.Id);
//participation.Participant = db.Users.Single(u => u.Email == uid);
//db.Participations.Add(participation);
participations.Add(board);
}
var votingOptions = new List<ElectionOption>();
foreach (string option in model.VotingOptions)
{
ElectionOption electionOption = new ElectionOption { OptionName = option };
//db.Options.Add(electionOption);
votingOptions.Add(electionOption);
}
var user = await userManager.GetUserAsync(this.User);
var Election = new Election { StartDate = model.StartDate, EndDate = model.EndDate, Title = model.Title, Creator = user, isVisible = model.IsVisible, CandidatesVoters = participations, Options = votingOptions };
db.Elections.Add(Election);
db.Elections.Add(new Election {Title = model.Title, Creator = db.Users.Single(u => u.Email == user.Email), Dashboards = participations, StartDate = model.StartDate, EndDate = model.EndDate, Question = model.Question, Options = votingOptions });
await db.SaveChangesAsync();
return RedirectToAction("Index", "Dashboard");
}
......
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace electronicvotingfyp.Migrations
{
public partial class participationsDashBoardsElections : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Participations_AspNetUsers_ParticipantId",
table: "Participations");
migrationBuilder.DropForeignKey(
name: "FK_Participations_Dashboards_DashboardId",
table: "Participations");
migrationBuilder.DropForeignKey(
name: "FK_Participations_Elections_ElectionId",
table: "Participations");
migrationBuilder.DropIndex(
name: "IX_Participations_ParticipantId",
table: "Participations");
migrationBuilder.DropColumn(
name: "ParticipantId",
table: "Participations");
migrationBuilder.DropColumn(
name: "isCandidate",
table: "Participations");
migrationBuilder.RenameColumn(
name: "ElectionId",
table: "Participations",
newName: "ElectionId1");
migrationBuilder.RenameColumn(
name: "DashboardId",
table: "Participations",
newName: "DashboardId1");
migrationBuilder.RenameIndex(
name: "IX_Participations_ElectionId",
table: "Participations",
newName: "IX_Participations_ElectionId1");
migrationBuilder.RenameIndex(
name: "IX_Participations_DashboardId",
table: "Participations",
newName: "IX_Participations_DashboardId1");
migrationBuilder.AddForeignKey(
name: "FK_Participations_Dashboards_DashboardId1",
table: "Participations",
column: "DashboardId1",
principalTable: "Dashboards",
principalColumn: "DashboardId",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_Participations_Elections_ElectionId1",
table: "Participations",
column: "ElectionId1",
principalTable: "Elections",
principalColumn: "ElectionId",
onDelete: ReferentialAction.Cascade);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Participations_Dashboards_DashboardId1",
table: "Participations");
migrationBuilder.DropForeignKey(
name: "FK_Participations_Elections_ElectionId1",
table: "Participations");
migrationBuilder.RenameColumn(
name: "ElectionId1",
table: "Participations",
newName: "ElectionId");
migrationBuilder.RenameColumn(
name: "DashboardId1",
table: "Participations",
newName: "DashboardId");
migrationBuilder.RenameIndex(
name: "IX_Participations_ElectionId1",
table: "Participations",
newName: "IX_Participations_ElectionId");
migrationBuilder.RenameIndex(
name: "IX_Participations_DashboardId1",
table: "Participations",
newName: "IX_Participations_DashboardId");
migrationBuilder.AddColumn<string>(
name: "ParticipantId",
table: "Participations",
type: "nvarchar(450)",
nullable: true);
migrationBuilder.AddColumn<bool>(
name: "isCandidate",
table: "Participations",
type: "bit",
nullable: false,
defaultValue: false);
migrationBuilder.CreateIndex(
name: "IX_Participations_ParticipantId",
table: "Participations",
column: "ParticipantId");
migrationBuilder.AddForeignKey(
name: "FK_Participations_AspNetUsers_ParticipantId",
table: "Participations",
column: "ParticipantId",
principalTable: "AspNetUsers",
principalColumn: "Id");
migrationBuilder.AddForeignKey(
name: "FK_Participations_Dashboards_DashboardId",
table: "Participations",
column: "DashboardId",
principalTable: "Dashboards",
principalColumn: "DashboardId",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_Participations_Elections_ElectionId",
table: "Participations",
column: "ElectionId",
principalTable: "Elections",
principalColumn: "ElectionId",
onDelete: ReferentialAction.Cascade);
}
}
}
......@@ -108,25 +108,17 @@ namespace electronicvotingfyp.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ParticipationId"), 1L, 1);
b.Property<int>("DashboardId")
b.Property<int>("DashboardId1")
.HasColumnType("int");
b.Property<int>("ElectionId")
b.Property<int>("ElectionId1")
.HasColumnType("int");
b.Property<string>("ParticipantId")
.HasColumnType("nvarchar(450)");
b.Property<bool>("isCandidate")
.HasColumnType("bit");
b.HasKey("ParticipationId");
b.HasIndex("DashboardId");
b.HasIndex("ElectionId");
b.HasIndex("DashboardId1");
b.HasIndex("ParticipantId");
b.HasIndex("ElectionId1");
b.ToTable("Participations");
});
......@@ -365,26 +357,20 @@ namespace electronicvotingfyp.Migrations
modelBuilder.Entity("electronicvotingfyp.Models.Participation", b =>
{
b.HasOne("electronicvotingfyp.Models.Dashboard", "Dashboard")
.WithMany()
.HasForeignKey("DashboardId")
.WithMany("Participations")
.HasForeignKey("DashboardId1")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("electronicvotingfyp.Models.Election", "Election")
.WithMany("CandidatesVoters")
.HasForeignKey("ElectionId")
.WithMany("Participations")
.HasForeignKey("ElectionId1")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", "Participant")
.WithMany()
.HasForeignKey("ParticipantId");
b.Navigation("Dashboard");
b.Navigation("Election");
b.Navigation("Participant");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
......@@ -438,11 +424,16 @@ namespace electronicvotingfyp.Migrations
.IsRequired();
});
modelBuilder.Entity("electronicvotingfyp.Models.Election", b =>
modelBuilder.Entity("electronicvotingfyp.Models.Dashboard", b =>
{
b.Navigation("CandidatesVoters");
b.Navigation("Participations");
});
modelBuilder.Entity("electronicvotingfyp.Models.Election", b =>
{
b.Navigation("Options");
b.Navigation("Participations");
});
#pragma warning restore 612, 618
}
......
......@@ -9,6 +9,10 @@ namespace electronicvotingfyp.Models
//navigation properties
public IdentityUser User { get; set; }
public ICollection<Election> Elections { get; set; }
public ICollection<Participation> Participations { get; set;}
}
}
......@@ -15,16 +15,16 @@ namespace electronicvotingfyp.Models
[Required]
[Column(TypeName = "varchar(64)")]
public string Title {
get { return Title ?? "your election title"; }
get;
set {}
set;
}
[Required]
[Column(TypeName = "varchar(128)")]
public string Question {
get { return Question ?? "Which option describes your choice best?"; }
get;
set { }
set;
}
[Required]
......@@ -36,10 +36,14 @@ namespace electronicvotingfyp.Models
[Required]
public DateTime EndDate { get; set; }
public ICollection<Participation> CandidatesVoters { get; set; }
public ICollection<ElectionOption> Options { get; set; }
//navigation properties
public ICollection<Participation> Participations{ get; set; }
public ICollection<Dashboard> Dashboards { get; set; }
public ICollection<ElectionOption> Options { get; set; }
public IdentityUser Creator { get; set; }
......
......@@ -34,18 +34,18 @@ namespace electronicvotingfyp.Models
[Display(Name ="Election manifest")]
public string Question { get; set; }
public List<string>? VotingOptions { get; set;}
public List<string> VotingOptions { get; set;}
public List<string>? Voters { get; set; }
public List<string> Voters { get; set; }
[BindProperty]
[Required]
[Display(Name = "Candidate Options")]
[PageRemote]
public IFormFile VotingOptionsFile { get; set; }
[BindProperty]
[Required]
[Display(Name = "Authorised Voters")]
[PageRemote]
public IFormFile VotersFile { get; set; }
[Required]
......@@ -60,7 +60,6 @@ namespace electronicvotingfyp.Models
{
return new SelectList(SystemUsers, "Id", "UserName");
}
}
}
......@@ -16,7 +16,7 @@ namespace electronicvotingfyp.Models
public decimal filesize { get; set; }
public Boolean validFile = true;
public Boolean validFile = false;
public Boolean UploadUserFile(IFormFile file)
{
......
......@@ -7,13 +7,14 @@ namespace electronicvotingfyp.Models
{
public int ParticipationId { get; set; }
public Boolean isCandidate { get; set; }
public int ElectionId;
public int DashboardId;
//navigation properties
public Election Election { get; set; }
public Election Election { get; set; } = null!;
public IdentityUser Participant { get; set; }
// public IdentityUser Participant { get; set; }
public Dashboard Dashboard { get; set; }
public Dashboard Dashboard { get; set; } = null!;
}
}
@model ElectionCreateViewModel
@{
ViewBag.Title = "Create Election";
ViewBag.ResultErrorMessage = @TempData["Message"];
}
<div class="container-fluid">
<div class="row align-item-center">
<div class="col-md-5 offset-md-1">
<h1>Start a new Election</h1>
@using (Html.BeginForm(actionName: "Create", controllerName: "Election", FormMethod.Post))
@using (Html.BeginForm(actionName: "Create", controllerName: "Election", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-row justify-content-center">
<div class="form-group" style="margin-top:15px">
......
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment