Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

246 code cleanup polling station observer #369

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace VoteMonitor.Api.Observer.Commands
{
public class DeleteObserverCommand : IRequest<bool>
{
public int IdObserver { get; }
public int ObserverId { get; }

public DeleteObserverCommand(int idObserver)
public DeleteObserverCommand(int observerId)
{
IdObserver = idObserver;
ObserverId = observerId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace VoteMonitor.Api.Observer.Commands
{
public class EditObserverCommand : IRequest<int>
{
public int IdObserver { get; set; }
public int ObserverId { get; set; }
public string Phone { get; set; }
public string Name { get; set; }
public string Pin { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace VoteMonitor.Api.Observer.Commands
{
public class ImportObserversRequest : IRequest<int>
{
public int IdOng { get; set; }
public int NgoId { get; set; }
public IFormFile File { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace VoteMonitor.Api.Observer.Commands
{
public class NewObserverCommand : IRequest<int>
{
public int IdNgo { get; set; }
public int NgoId { get; set; }
public string Phone { get; set; }
public string Pin { get; set; }
public string Name { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ namespace VoteMonitor.Api.Observer.Commands
{
public class ObserverCountCommand : IRequest<int>
{
public int IdNgo { get; }
public int NgoId { get; }
public bool IsCallerOrganizer { get; }

public ObserverCountCommand(int idNgo, bool isCallerOrganizer)
public ObserverCountCommand(int ngoId, bool isCallerOrganizer)
{
IdNgo = idNgo;
NgoId = ngoId;
IsCallerOrganizer = isCallerOrganizer;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace VoteMonitor.Api.Observer.Commands
{
public class ObserverGenerateCommand : IRequest<List<GeneratedObserver>>
{
public ObserverGenerateCommand(int nrObs, int id)
public ObserverGenerateCommand(int numberOfObservers, int ngoId)
{
NrObservers = nrObs;
IdNgo = id;
NumberOfObservers = numberOfObservers;
NgoId = ngoId;
}

public int NrObservers { get; set; }
public int IdNgo { get; set; }
public int NumberOfObservers { get; set; }
public int NgoId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace VoteMonitor.Api.Observer.Commands
{
public class ObserverListCommand : IRequest<ApiListResponse<ObserverModel>>
{
public int IdNgo { get; set; }
public int NgoId { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public int Page { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace VoteMonitor.Api.Observer.Commands
{
public class ResetDeviceCommand : IRequest<int>
{
public int IdNgo { get; set; }
public int NgoId { get; set; }
public string PhoneNumber { get; set; }

public bool Organizer { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace VoteMonitor.Api.Observer.Commands
{
public class ResetPasswordCommand : IRequest<bool>
{
public int IdNgo { get; set; }
public int NgoId { get; set; }
public string PhoneNumber { get; set; }
public string Pin { get; set; }
public bool Organizer { get; set; }
public bool IsOrganizer { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task<ApiListResponse<ObserverModel>> GetObservers(ObserverListQuery
{
var command = _mapper.Map<ObserverListCommand>(query);

command.IdNgo = IsOrganizer ? -1 : NgoId;
command.NgoId = IsOrganizer ? -1 : NgoId;

var result = await _mediator.Send(command);
return result;
Expand All @@ -52,7 +52,7 @@ public async Task<List<ObserverModel>> GetActiveObservers(ActiveObserverFilter q
{
var command = _mapper.Map<ActiveObserversQuery>(query);

command.IdNgo = IsOrganizer ? -1 : NgoId;
command.NgoId = IsOrganizer ? -1 : NgoId;

var result = await _mediator.Send(command);
return result;
Expand Down Expand Up @@ -81,7 +81,7 @@ public async Task<int> Import(IFormFile file, [FromForm] int ongId)
var counter = await _mediator.Send(new ImportObserversRequest
{
File = file,
IdOng = ongId
NgoId = ongId
});

return counter;
Expand All @@ -103,7 +103,7 @@ public async Task<IActionResult> NewObserver(NewObserverModel model)
}

var newObsCommand = _mapper.Map<NewObserverCommand>(model);
newObsCommand.IdNgo = NgoId;
newObsCommand.NgoId = NgoId;
var newId = await _mediator.Send(newObsCommand);

return Ok(newId);
Expand Down Expand Up @@ -131,7 +131,7 @@ public async Task<IActionResult> EditObserver([FromBody] EditObserverModel model
return BadRequest("Invalid request");
}

var isActionAllowed = await IsActionAllowed(model.IdObserver);
var isActionAllowed = await IsActionAllowed(model.ObserverId);
if (!isActionAllowed)
{
return Problem("Action not allowed", statusCode: (int)HttpStatusCode.BadRequest);
Expand Down Expand Up @@ -224,7 +224,7 @@ public async Task<IActionResult> Reset([FromBody] ResetModel model)
{
var result = await _mediator.Send(new ResetDeviceCommand
{
IdNgo = NgoId,
NgoId = NgoId,
PhoneNumber = model.PhoneNumber,
Organizer = this.GetOrganizatorOrDefault(false)
});
Expand All @@ -242,10 +242,10 @@ public async Task<IActionResult> Reset([FromBody] ResetModel model)
{
var result = await _mediator.Send(new ResetPasswordCommand
{
IdNgo = NgoId,
NgoId = NgoId,
PhoneNumber = model.PhoneNumber,
Pin = model.Pin,
Organizer = this.GetOrganizatorOrDefault(false)
IsOrganizer = this.GetOrganizatorOrDefault(false)
});
if (result == false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public Task<List<ObserverModel>> Handle(ActiveObserversQuery request, Cancellati
.Where(i => i.PollingStation.Number >= request.FromPollingStationNumber)
.Where(i => i.PollingStation.Number <= request.ToPollingStationNumber);

if (request.IdNgo > 0)
if (request.NgoId > 0)
{
results = results.Where(i => i.Observer.IdNgo == request.IdNgo);
results = results.Where(i => i.Observer.IdNgo == request.NgoId);
}

var observers = results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public async Task<List<GeneratedObserver>> Handle(ObserverGenerateCommand reques
List<GeneratedObserver> generatedObservers = new List<GeneratedObserver>();


for (int i = 0; i < request.NrObservers; ++i)
for (int i = 0; i < request.NumberOfObservers; ++i)
{
dbObservers.Add(RandomObserverBuilder.Instance(_hashService).Build(request.IdNgo));
dbObservers.Add(RandomObserverBuilder.Instance(_hashService).Build(request.NgoId));
}

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public ObserverCountQueryHandler(VoteMonitorContext context, ILogger<ObserverCou
}
public async Task<int> Handle(ObserverCountCommand request, CancellationToken cancellationToken)
{
_logger.LogInformation($"Getting the total Observer count for the ngo with the id {request.IdNgo}");
_logger.LogInformation($"Getting the total Observer count for the ngo with the id {request.NgoId}");

IQueryable<Entities.Observer> observers = _context.Observers;

if (request.IsCallerOrganizer == false && request.IdNgo > 0)
if (request.IsCallerOrganizer == false && request.NgoId > 0)
{
observers = observers.Where(o => o.IdNgo == request.IdNgo);
observers = observers.Where(o => o.IdNgo == request.NgoId);
}

return await observers.CountAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ public ObserverListQueryHandler(VoteMonitorContext context, ILogger<ObserverList
}
public async Task<ApiListResponse<ObserverModel>> Handle(ObserverListCommand request, CancellationToken cancellationToken)
{
_logger.LogInformation($"Searching for Observers with the following filters (IdNgo, Name, Phone): {request.IdNgo}, {request.Name}, {request.Number}");
_logger.LogInformation($"Searching for Observers with the following filters (NgoId, Name, Phone): {request.NgoId}, {request.Name}, {request.Number}");

IQueryable<Entities.Observer> observers = _context.Observers
.Include(o => o.Ngo)
.Include(o => o.Notes)
.Include(o => o.PollingStationInfos);

if (request.IdNgo > 0)
if (request.NgoId > 0)
{
observers = observers.Where(o => o.IdNgo == request.IdNgo);
observers = observers.Where(o => o.IdNgo == request.NgoId);
}

if (!string.IsNullOrEmpty(request.Name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<int> Handle(ImportObserversRequest message, CancellationToken
var observer = new Entities.Observer
{
Id = startId + counter,
IdNgo = message.IdOng,
IdNgo = message.NgoId,
Phone = data[0],
Name = data[2],
Pin = hashed
Expand All @@ -74,7 +74,7 @@ public async Task<int> Handle(NewObserverCommand message, CancellationToken toke
var observer = new Entities.Observer
{
Id = id,
IdNgo = message.IdNgo,
IdNgo = message.NgoId,
Phone = message.Phone,
Name = message.Name,
Pin = _hashService.GetHash(message.Pin)
Expand All @@ -87,7 +87,7 @@ public async Task<int> Handle(NewObserverCommand message, CancellationToken toke
public async Task<int> Handle(EditObserverCommand request, CancellationToken cancellationToken)
{

var observer = await _context.Observers.FirstOrDefaultAsync(o => o.Id == request.IdObserver);
var observer = await _context.Observers.FirstOrDefaultAsync(o => o.Id == request.ObserverId);
if (observer == null)
{
return -1;
Expand All @@ -113,7 +113,7 @@ public async Task<int> Handle(EditObserverCommand request, CancellationToken can

public async Task<bool> Handle(DeleteObserverCommand request, CancellationToken cancellationToken)
{
var observer = await _context.Observers.FirstOrDefaultAsync(o => o.Id == request.IdObserver);
var observer = await _context.Observers.FirstOrDefaultAsync(o => o.Id == request.ObserverId);
if (observer == null)
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Task<int> Handle(ResetDeviceCommand request, CancellationToken cancellati

if (!request.Organizer)
{
observerQuery = observerQuery.Where(o => o.IdNgo == request.IdNgo);
observerQuery = observerQuery.Where(o => o.IdNgo == request.NgoId);
}

var observer = observerQuery.FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public async Task<bool> Handle(ResetPasswordCommand request, CancellationToken c
var observerQuery = _voteMonitorContext.Observers
.Where(o => o.Phone == request.PhoneNumber);

if (!request.Organizer)
if (!request.IsOrganizer)
{
observerQuery = observerQuery.Where(o => o.IdNgo == request.IdNgo);
observerQuery = observerQuery.Where(o => o.IdNgo == request.NgoId);
}

var observer = observerQuery.FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace VoteMonitor.Api.Observer.Models
public class EditObserverModel
{
[Required]
public int? IdObserver { get; set; }
public int? ObserverId { get; set; }
public string Phone { get; set; }
public string Name { get; set; }
public string Pin { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using VoteMonitor.Api.Observer.Models;
using VoteMonitor.Api.Observer.Queries;

namespace VoteMonitor.Api.Observer.Mapping
namespace VoteMonitor.Api.Observer.Profiles
{
public class MappingProfile : Profile
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class ActiveObserversQuery : IRequest<List<ObserverModel>>
public int FromPollingStationNumber { get; set; }
public int ToPollingStationNumber { get; set; }
public bool CurrentlyCheckedIn { get; set; }
public int IdNgo { get; set; }
public int NgoId { get; set; }
}
}
29 changes: 29 additions & 0 deletions src/api/VoteMonitor.Api.Observer/Utils/RandomNumberGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace VoteMonitor.Api.Observer.Utils
{
public class RandomNumberGenerator
{
public static string Generate(int digits)
{
var random = new Random();
var number = "";
for (var i = 1; i < digits + 1; i++)
{
number += random.Next(0, 9).ToString();
}
return number;
}

public static string GenerateWithPadding(int digits, string prefix)
{
var random = new Random();
var number = prefix;
for (var i = 1 + prefix.Length; i < digits + 1; i++)
{
number += random.Next(0, 9).ToString();
}
return number;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,4 @@ public void SetName(string name)
_observer.Name = name;
}
}

public class RandomNumberGenerator
{
public static string Generate(int digits)
{
Random random = new Random();
string number = "";
for (int i = 1; i < digits + 1; i++)
{
number += random.Next(0, 9).ToString();
}
return number;
}

public static string GenerateWithPadding(int digits, string prefix)
{
Random random = new Random();
string number = prefix;
for (int i = 1 + prefix.Length; i < digits + 1; i++)
{
number += random.Next(0, 9).ToString();
}
return number;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CSharpFunctionalExtensions;
using MediatR;
using VoteMonitor.Api.PollingStation.Models;

namespace VoteMonitor.Api.PollingStation.Commands
{
Expand All @@ -10,7 +9,7 @@ public class ClearAllPollingStationsCommand : IRequest<Result>

public ClearAllPollingStationsCommand(bool includeRelatedData)
{
this.IncludeRelatedData = includeRelatedData;
IncludeRelatedData = includeRelatedData;
}
}
}
Loading