Skip to content

Commit

Permalink
admin can change user's status -available
Browse files Browse the repository at this point in the history
  • Loading branch information
trungngotdt committed Oct 11, 2018
1 parent 6c4fa58 commit 9189d67
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 29 deletions.
2 changes: 1 addition & 1 deletion AdminVoting/View/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ignore="http://www.galasoft.ch/ignore"
StartupUri="LoginWindow.xaml"
StartupUri="ShellWindow.xaml"
mc:Ignorable="d ignore">
<Application.Resources>
<!--Global View Model Locator-->
Expand Down
21 changes: 11 additions & 10 deletions AdminVoting/View/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,23 @@
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="Aqua"/>
<Grid Grid.Row="1">
<ListView x:Name="LstCandicates"
ItemsSource="{Binding Candidates}">
<ListView x:Name="LstAllUser"
ItemsSource="{Binding AllUser}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name"
DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Number of Vote"
DisplayMemberBinding="{Binding NumVote}"/>
<GridViewColumn Header="aaa"
DisplayMemberBinding="{Binding NumVote,Mode=TwoWay}"/>
<GridViewColumn Header="Voting">
<GridViewColumn Header="Address"
DisplayMemberBinding="{Binding Address}"/>
<GridViewColumn Header="Role"
DisplayMemberBinding="{Binding Role}"/>

<GridViewColumn Header="Available">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ToggleButton Style="{StaticResource MaterialDesignSwitchAccentToggleButton}"
ToolTip="MaterialDesignSwitchAccentToggleButton"
IsChecked="True" />
Command="{Binding ElementName=LstAllUser,Path=DataContext.CommandChecked}"
CommandParameter="{Binding Address}"
IsChecked="{Binding Available}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Expand Down
4 changes: 3 additions & 1 deletion AdminVoting/ViewModel/LoginViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace AdminVoting.ViewModel
{
public class LoginViewModel : ViewModelBase
{
int[] aa = new int[500];
private string account;
private string password;

Expand Down Expand Up @@ -57,10 +58,11 @@ void Init()

async Task SubmitClickAsync()
{
Task<bool> task=HelperUnti.CheckUnlockAccountAsync(Account, Password);
var builder = Builders<User>.Filter;
var filter = builder.Eq("available", true) & builder.Eq("address", Account)&builder.Eq("role","admin");
var user= getMongoCollection.GetData(filter);
var checkAccount= user.Length > 0 && await HelperUnti.CheckUnlockAccountAsync(Account, Password);
var checkAccount = user.Length > 0 && await task;
if(checkAccount)
{
RegisterParamaters.SetParamater("address", account);
Expand Down
82 changes: 78 additions & 4 deletions AdminVoting/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
using CommonLibraryUtilities.HelperMongo;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MongoDB.Driver;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace AdminVoting.ViewModel
Expand All @@ -14,20 +18,32 @@ namespace AdminVoting.ViewModel
/// </summary>
public class MainViewModel : ViewModelBase
{
int[] indexUserBeChanged;
int countUserBeChanged;

private IHelper helper;
private IHelperMongo helperMongo;
private IRegisterParamaters registerParamaters;
private IGetMongoCollection getMongoCollection;

private ObservableCollection<User> allUser;


private ICommand commandChecked;
private ICommand commandLoaded;
private ICommand commandBtnSubmitedClick;


public ICommand CommandLoaded => commandLoaded = new RelayCommand(()=> { GetAllUser(); });



public ICommand CommandLoaded => commandLoaded = new RelayCommand();
public IHelper Helper { get => helper; set => helper = value; }
public IHelperMongo HelperMongo { get => helperMongo; set => helperMongo = value; }
public IRegisterParamaters RegisterParamaters { get => registerParamaters; set => registerParamaters = value; }
public ObservableCollection<User> AllUser { get => allUser; set { allUser = value; RaisePropertyChanged("AllUser"); } }
public IGetMongoCollection GetMongoCollection { get => getMongoCollection; set => getMongoCollection = value; }
public ICommand CommandBtnSubmitedClick => commandBtnSubmitedClick = new RelayCommand(() => { SubmitUser(); });
public ICommand CommandChecked => commandChecked = new RelayCommand<string>((address) => { ToggleCheck(address); });



Expand All @@ -40,10 +56,68 @@ public MainViewModel(IHelper _helper, IHelperMongo _helperMongo, IRegisterParama
{
this.registerParamaters = _registerParamaters;
this.helper = _helper;
this.helperMongo = _helperMongo;
this.helperMongo = _helperMongo;
Init();

}

private void Init()
{
HelperMongo.GetClient("127.0.0.1", 27017, "user1", "pass1");
HelperMongo.GetDatabase("data1");
var database = HelperMongo.GetDatabase("data1");
getMongoCollection = HelperMongo.GetMongoCollection();
getMongoCollection.Init(database, "user", typeof(User));
AllUser = new ObservableCollection<User>();
countUserBeChanged = 0;

}

private void GetAllUser()
{
var builder = Builders<User>.Filter;
var filter = builder.Empty;
AllUser = new ObservableCollection<User>(GetMongoCollection.GetData(filter).Cast <User>());
indexUserBeChanged = Enumerable.Repeat(-1, AllUser.Count).ToArray();
}

private void ToggleCheck(string address)
{

var index = AllUser.IndexOf(AllUser.AsParallel().First(x => x.Address.Equals(address)));
if(!indexUserBeChanged.Contains(index))
{
indexUserBeChanged[countUserBeChanged] = index;
countUserBeChanged++;
}
}

private void SubmitUser()
{
var tempUser =new List<User>();
indexUserBeChanged.AsParallel().ForAll(x =>
{
if (x!=-1)
{
tempUser.Add(AllUser[x]);
}
});
for (int i = 0; i < indexUserBeChanged.Count(); i++)
{


if (indexUserBeChanged[i]!=-1)
{
var previous = Builders<User>.Filter.Eq("address", AllUser[indexUserBeChanged[i]].Address);
var update = Builders<User>.Update.Set("available", AllUser[indexUserBeChanged[i]].Available);
getMongoCollection.FindOneAndUpdateAsync(previous, update);


}
}

}



////public override void Cleanup()
////{
Expand Down
1 change: 0 additions & 1 deletion AdminVoting/ViewModel/ViewModelLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using CommonServiceLocator;
using AdminVoting.Model;
using CommonLibraryUtilities;
using CommonLibraryUtilities.HelperMongo;
using System;
Expand Down
26 changes: 21 additions & 5 deletions CommonLibrary/HelperMongo/GetMongoCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,39 @@ public class GetMongoCollection:IGetMongoCollection
public void Init(IMongoDatabase database,string nameOfCollection,Type type, MongoCollectionSettings settings=null)
{
GetType = type;
mongoCollection = GetMethodRef(typeof(IMongoDatabase), type, "GetCollection", database, new object[] { nameOfCollection, settings });
mongoCollection = GetMethodGeneRef(typeof(IMongoDatabase), type, "GetCollection", database, new object[] { nameOfCollection, settings });
}

public Array GetData(object filter)
{
var method = GetMethodRef(typeof(IMongoCollectionExtensions), GetType, "Find", mongoCollection,new object[] { mongoCollection, filter, null });
var data = GetMethodRef(typeof(IAsyncCursorSourceExtensions), GetType, "ToList", method, new object[] {method, default(CancellationToken) });
var castToArray = GetMethodRef(data.GetType(), GetType, "ToArray", data);
var method = GetMethodGeneRef(typeof(IMongoCollectionExtensions), GetType, "Find", mongoCollection,new object[] { mongoCollection, filter, null });
var data = GetMethodGeneRef(typeof(IAsyncCursorSourceExtensions), GetType, "ToList", method, new object[] {method, default(CancellationToken) });
var castToArray = GetMethodGeneRef(data.GetType(), GetType, "ToArray", data);
return (castToArray as Array);
}

private object GetMethodRef(Type typeOfMethod,Type typeMakeGeMethod,string nameOfMethod,object obj,object[] para=null)


public object FindOneAndUpdateAsync(object filterPrevious,object filterUpdated)
{
var method = GetMethodGeneRef(typeof(IMongoCollectionExtensions),GetType, "FindOneAndUpdate", mongoCollection,
new object[] { mongoCollection, filterPrevious,filterUpdated,null, null });
return method;
}

private object GetMethodGeneRef(Type typeOfMethod,Type typeMakeGeMethod,string nameOfMethod,object obj,object[] para=null)
{
var method = typeOfMethod.GetMethods().Where(x => x.Name.Equals(nameOfMethod)).ElementAt(0);
method =method.IsGenericMethod? method.MakeGenericMethod(typeMakeGeMethod):method;
var methodInvoke = method.Invoke(obj, para);
return methodInvoke;
}

private object GetMethod(Type typeOfMethod, string nameOfMethod, object obj, object[] para = null)
{
var method = typeOfMethod.GetMethods().Where(x => x.Name.Equals(nameOfMethod)).ElementAt(0);
var methodInvoke = method.Invoke(obj, para);
return methodInvoke;
}
}
}
2 changes: 2 additions & 0 deletions CommonLibrary/HelperMongo/IGetMongoCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface IGetMongoCollection
void Init(IMongoDatabase database, string nameOfCollection, Type type, MongoCollectionSettings settings = null);

Array GetData(object filter);

object FindOneAndUpdateAsync(object filterPrevious, object filterUpdated);
}
}
2 changes: 1 addition & 1 deletion CommonLibrary/RegisterParamaters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace CommonLibraryUtilities
{
public class RegisterParamaters:IRegisterParamaters
{
private Dictionary<string, object> paramater;
private Dictionary<string, object> paramater=new Dictionary<string, object>();

public object GetParamater(string nameOfPara)
{
Expand Down
11 changes: 5 additions & 6 deletions EthereumVoting/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ public MainViewModel(IHelper _helper,IHelperMongo _helperMongo, IRegisterParamat
this.registerParamaters = _registerParamaters;
this.helper = _helper;
this.helperMongo = _helperMongo;
Init();
}

private void Init()
{
HelperMongo.GetClient("127.0.0.1", 27017, "user1", "pass1");
HelperMongo.GetDatabase("data1");
address = registerParamaters.GetParamater("address").ToString();
var collec= HelperMongo.GetMongoCollection();
Candidates = new ObservableCollection<Candidate>();
}

Expand Down Expand Up @@ -137,11 +141,6 @@ private async Task GetCandidatesAsync()

}

private async Task<int> GetCountAsync()
{
int count = await GetHelper.CallFunctionAsync<int>(address, "GetVoterCount", null);
return count;
}

public async Task<Candidate> GetCandidateAsync(int i)
{
Expand Down

0 comments on commit 9189d67

Please sign in to comment.