public void PrepareVoteResponse(string content)
        {
            NodeVoteResponse response;

            try
            {
                response = JsonConvert.DeserializeObject <NodeVoteResponse>(content);
                SendMessage(response);
            }
            catch (JsonException e)
            {
                _logger.Error(e);
                throw e;
            }

            using (var db = new SystemDBContext())
            {
                VoteStatus voteStatus = new VoteStatus
                {
                    Idvn      = response.IDVN,
                    VoteState = "Voted"
                };

                Block block = new Block
                {
                    //  BlockId = response.block.BlockId,
                    CreatedOn    = (DateTime)response.block.CreatedOn,
                    PartyId      = (int)response.block.PartyChoosed,
                    Gender       = response.block.Gender,
                    Hash         = response.block.Hash,
                    Idbd         = response.block.Idbd,
                    PreviousHash = response.block.PreviousHash,
                    RegionId     = (int)response.block.RegionChoosed,
                    YearBirth    = 1999// (int)response.block.YearBirth
                };
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Add(voteStatus);
                        db.Add(block);
                        db.SaveChanges();
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        _logger.Error(e);
                        Console.WriteLine(e);
                    }
                }
            }
        }
Beispiel #2
0
        internal async Task <RegistrationResponse> RegistrationAction(RegistrationMessage registration)
        {
            return(await Task.Run(async() =>
            {
                var config = new MapperConfiguration(cfg =>
                                                     cfg.CreateMap <RegistrationMessage, FiscData>());
                var mapper = new Mapper(config);
                var fiscregistration = mapper.Map <FiscData>(registration);
                fiscregistration.BirthDate = registration.Birth_date;
                //Verify if registration registration are valid.
                using (var db = new SystemDBContext())
                {
                    try
                    {
                        var fisc = await db.FiscData.FirstOrDefaultAsync(x =>
                                                                         x.Idnp == fiscregistration.Idnp &&
                                                                         x.Gender == fiscregistration.Gender &&
                                                                         x.Region.Contains(fiscregistration.Region) &&
                                                                         x.Surname == fiscregistration.Surname &&
                                                                         x.Name == fiscregistration.Name &&
                                                                         x.BirthDate == fiscregistration.BirthDate);
                        if (fisc == null)
                        {
                            return new RegistrationResponse {
                                Status = false, Message = "Datele introduse nu sunt correcte."
                            };
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Error("Registration | " + registration.IDNP + " " + e.Message);
                    }
                }

                //Send registration to LoadBalancer
                var content = new StringContent(JsonConvert.SerializeObject(registration), Encoding.UTF8, "application/json");
                var clientCertificate = new X509Certificate2(Path.Combine(@"..\Certs", "administrator.pfx"), "ar4iar4i", X509KeyStorageFlags.Exportable);

                var handler = new HttpClientHandler();
                handler.ClientCertificates.Add(clientCertificate);
                handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
                var regLbResponse = new NodeRegResponse();
                using (var client = new HttpClient(handler))
                {
                    try
                    {
                        client.BaseAddress = new Uri("https://localhost:44322/");
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                        var response = client.PostAsync("api/Regist", content);
                        var registration_resp = await response.Result.Content.ReadAsStringAsync();
                        regLbResponse = JsonConvert.DeserializeObject <NodeRegResponse>(registration_resp);
                    }
                    catch (AggregateException e)
                    {
                        logger.Error("Registration |" + registration.IDNP + " " + e.Message);
                        return new RegistrationResponse {
                            Status = false, Message = "Error! Sistemul nu funcționează la moment reveniți mai târziu"
                        };
                    }
                }

                if (regLbResponse.Status == true)
                {
                    ///-------SEND EMAIL WITH PASSWORD------
                    EmailSender.Send(registration.Email, regLbResponse.VnPassword);

                    using (var db = new SystemDBContext())
                    {
                        var account = new IdvnAccount();
                        account.Idvn = regLbResponse.IDVN;
                        account.VnPassword = LoginHelper.HashGen(regLbResponse.VnPassword);
                        account.StatusNumber = "Non confirmed";
                        account.IpAddress = registration.Ip_address;
                        account.PhoneNumber = registration.Phone_Number;
                        account.Email = registration.Email;
                        account.Gender = registration.Gender;
                        account.Region = db.Regions.FirstOrDefault(m => m.RegionName == registration.Region);
                        db.Add(account);
                        try
                        {
                            db.SaveChanges();
                        }
                        catch (Exception e)
                        {
                        }
                    }

                    var random = new Random();
                    return new RegistrationResponse {
                        Status = true, ConfirmKey = random.Next(12452, 87620).ToString(), Message = "Înregistrare | IDNP: " + registration.IDNP + " a fost înregistrat.", IDVN = regLbResponse.IDVN, Email = registration.Email
                    };
                }
                else
                {
                    return new RegistrationResponse {
                        Status = false, Message = "Înregistrare | IDNP: " + registration.IDNP + " nu a fost posibil de înregistrat"
                    };
                }
            }));
        }
 public void Add(SelectRecord entity)
 {
     _context.Add(entity);
     _context.SaveChanges();
     _context.DetachAll();
 }
Beispiel #4
0
 public void Add(Course entity)
 {
     _context.Add(entity);
     _context.SaveChanges();
     _context.DetachAll();
 }