Esempio n. 1
0
        public MobileLoginModel PostResult(HttpRequestMessage message)
        {
            try
            {
                var postText = message.Content.ReadAsStringAsync().Result;
                if (string.IsNullOrEmpty(postText))
                {
                    return(new MobileLoginModel {
                        Message = "No data synced."
                    });
                }

                var models = JsonConvert.DeserializeObject <List <MoiblePostResult> >(postText);
                //if (model.Module > 0)
                //{
                //    return new MobileLoginModel { Message = "Something is wrong with the provided data. Please try again later" };
                //}
                List <int> ints = new List <int>();
                foreach (var model in models)
                {
                    string[] codes = model.PersonCode.Split('/');
                    var      path  = "";
                    if (model.ProofImage.Length != 0)
                    {
                        var image = Utils.ConvertToImage(model.ProofImage);
                        path = Path.Combine(HttpContext.Current.Server.MapPath("~/Images/Proof/"), model.PersonCode.Replace('/', '_') + ".jpg");
                        image.Save(path);
                    }

                    var result_id = _resultRepository.AddResult(new Result
                    {
                        Module       = model.Module,
                        RegVotes     = model.RegVotes,
                        AccredVotes  = model.AccredVotes,
                        CastVotes    = model.CastVotes,
                        InvalidVotes = model.InvVotes,
                        ReportedBy   = model.ReportedBy,
                        ProofUrl     = path,
                        State        = int.Parse(codes[0]),
                        Lga          = int.Parse(codes[1]),
                        Ward         = int.Parse(codes[2]),
                        PollingUnit  = int.Parse(codes[3])
                    });
                    if (result_id > 0)
                    {
                        foreach (var m in model.Votes)
                        {
                            _voteRepository.AddVote(new Vote
                            {
                                ResultId = result_id,
                                PartyId  = m.Party,
                                Vote1    = m.Vote
                            });
                        }

                        _voteRepository.AddBytePhoto(new Photo {
                            ResultId = result_id,
                            Image    = model.ProofImage,
                            Reporter = model.ReportedBy
                        });
                    }
                    ints.Add(model.Id);
                }

                if (ints.Count > 0)
                {
                    return(new MobileLoginModel
                    {
                        Message = "Successfully Synced",
                        Result = ints
                    });
                }
                return(new MobileLoginModel {
                    Message = "synchronization failed."
                });
            }
            catch (Exception e)
            {
                return(new MobileLoginModel {
                    Message = "Synchronization failed due to internal server error."
                });
            }
        }