Example #1
0
        /*
         *      public void PutObject(string postUrl, object payload)
         *      {
         *          var request = (HttpWebRequest)WebRequest.Create(postUrl);
         *          request.Method = "PUT";
         *          request.ContentType = "application/json";
         *          if (payload != null)
         *          {
         *            //  request.ContentLength = JsonConvert.SerializeObject(payload).Length;
         *              Stream dataStream = request.GetRequestStream();
         *              Serialize(dataStream, payload);
         *              dataStream.Close();
         *          }
         *
         *          HttpWebResponse response = (HttpWebResponse)request.GetResponse();
         *          string returnString = response.StatusCode.ToString();
         *      }
         *
         *      public void Serialize(Stream output, object input)
         *      {
         *          var ser = new DataContractSerializer(input.GetType());
         *          ser.WriteObject(output, JsonConvert.SerializeObject(input));
         *      }
         */

        public string toServerPost(UserAttendance userAttendance, string id, string method)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://192.168.1.202:4984/db/" + id);

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.AllowWriteStreamBuffering = true;
            httpWebRequest.PreAuthenticate           = true;
            httpWebRequest.Headers.Add("Authorization", "Basic YXR0ZW5kYW5jZTphdHRlbmRhbmNl");
            httpWebRequest.Method    = method;
            httpWebRequest.MediaType = "json";

            httpWebRequest.Credentials = new NetworkCredential("attendance", "attendance");

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                var serializer = new JavaScriptSerializer();
                streamWriter.Write(serializer.Serialize(userAttendance));
                streamWriter.Flush();
                streamWriter.Close();
            }
            try
            {
                System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return(true); };
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse() as HttpWebResponse;
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    return(JsonConvert.SerializeObject(result));
                }
            }
            catch (Exception er)
            {
                Console.WriteLine(er.ToString());
                return("false");
            }
            return("false");
        }
Example #2
0
        public List <string> postAttendance(string email, string user_id, string timeIn, int location)
        {
            var           queryRequest = new QueryRequest();
            DateTime      TimeIn       = DateTime.Now;
            List <string> values       = new List <string>();
            TimeSpan      work;

            try
            {
                if (timeIn.CompareTo("") != 0)
                {
                    TimeIn = DateTime.Parse(HttpUtility.UrlDecode(timeIn));
                }
                string userCond = "";
                userCond = " and user_id=$user_id ";
                if (user_id != null && !user_id.Equals(""))
                {
                    queryRequest.AddNamedParameter("user_id", user_id);
                }
                else if (email != null && !email.Equals(""))
                {
                    queryRequest.AddNamedParameter("email", email);

                    var queryGetUserId = new QueryRequest();
                    queryGetUserId.Statement("SELECT META(Testing).id AS id FROM `Testing`  WHERE email=$email")
                    .AddNamedParameter("email", email);
                    IQueryResult <dynamic> resultId = bucket.Query <dynamic>(queryGetUserId);
                    if (resultId.Rows.Count() > 0)
                    {
                        JObject userAttendanceData = JsonConvert.DeserializeObject <JObject>(resultId.Rows.ElementAt(0) + "");
                        values.Add(userAttendanceData["id"].ToString());
                        queryRequest.AddNamedParameter("user_id", userAttendanceData["id"].ToString());
                        user_id = userAttendanceData["id"].ToString();
                    }
                    else
                    {
                        values.Add("No User Found");
                        return(values);
                    }
                }
                else
                {
                    values.Add("No User Found");
                    return(values);
                }
                string         dat           = TimeIn.ToString("yyyy-MM-dd");
                UserAttendance newAttendance = null;
                queryRequest.Statement("SELECT META(Testing).id AS id,* FROM `Testing` WHERE date=$date2 " + userCond)
                .AddNamedParameter("date2", dat);

                IQueryResult <dynamic> result = bucket.Query <dynamic>(queryRequest);

                if (result.Rows.Count() == 0)
                {
                    DateTime defTime = DateTime.Parse(TimeIn.ToString("yyyy/MM/dd") + "T17:00:00");
                    work          = defTime.Subtract(TimeIn);
                    newAttendance = new UserAttendance
                    {
                        user_id         = user_id,
                        date            = dat,
                        marked_at       = new List <DateTime>(),
                        time_in         = TimeIn.ToString("yyyy-MM-ddTHH:mm:ss"),
                        time_out        = "",
                        default_out     = defTime.ToString(),
                        shifttotalhours = "8",
                        worktotalhours  = work.Hours + ":" + work.Minutes,
                        doctype         = "user_attendance",
                        channels        = new[] { "attendance" },

                        client    = "Esajee",
                        shiftId   = "1",
                        shiftName = "Default",
                        location  = location
                    };
                    newAttendance.marked_at.Add(TimeIn);
                    values.Add(user_id);
                    toServerPost(newAttendance, "", "POST");
                }
                else
                {
                    JObject userAttendanceData = JsonConvert.DeserializeObject <JObject>(result.Rows.ElementAt(0) + "");
                    string  docId = "";
                    string  revId = "";
                    docId         = userAttendanceData["id"].ToString();
                    revId         = userAttendanceData["Testing"]["_sync"]["rev"].ToString();
                    newAttendance = JsonConvert.DeserializeObject <UserAttendance>(JsonConvert.SerializeObject(userAttendanceData["Testing"]));
                    DateTime markTimeIn = DateTime.Parse(newAttendance.time_in);

                    if (newAttendance != null)
                    {
                        newAttendance.time_out = TimeIn.ToString("yyyy-MM-ddTHH:mm:ss");
                        TimeSpan work12 = TimeIn.Subtract(markTimeIn);
                        newAttendance.worktotalhours = work12.Hours + ":" + work12.Minutes;
                        if (newAttendance.marked_at != null)
                        {
                            newAttendance.marked_at.Add(TimeIn);
                        }
                        values.Add(toServerPost(newAttendance, docId + "?rev=" + revId, "PUT"));
                    }
                    else
                    {
                        values.Add("View Not Working");
                    }
                }
            }
            catch (Exception re)
            {
                values.Add("Times convertion failed");
                return(values);
            }
            return(values);
        }
Example #3
0
        public List <string> fromFile(int location)
        {
            List <string> statsInsert = new List <string>();
            StreamReader  stReader    = new StreamReader(@"C:\ATTENDNCE.csv");
            string        headLine    = stReader.ReadLine();

            string[] dateIds = headLine.Split(',');
            string   date    = dateIds[0];
            DateTime asdasd  = DateTime.Parse(date);
            string   month   = asdasd.ToString("yyyy-MM");
            string   line    = "";

            while ((line = stReader.ReadLine()) != null)
            {
                string[] time = line.Split(',');
                for (int i = 1; i < time.Length; i += 3)
                {
                    try
                    {
                        if (time[i].CompareTo("") != 0 && time[i + 1].CompareTo("") != 0)
                        {
                            string   inTime  = "";
                            string   outTime = "";
                            DateTime timeIn;
                            DateTime timeOut;
                            if (int.Parse(time[0]) > 9)
                            {
                                Console.WriteLine("");
                            }
                            if (int.Parse(time[0]) < 10)
                            {
                                date    = month + "-" + "0" + time[0];
                                inTime  = month + "-" + "0" + time[0] + " " + time[i];
                                outTime = month + "-" + "0" + time[0] + " " + time[i + 1];
                            }
                            else
                            {
                                date    = month + "-" + time[0];
                                inTime  = month + "-" + time[0] + " " + time[i];
                                outTime = month + "-" + time[0] + " " + time[i + 1];
                            }
                            timeIn = DateTime.Parse(inTime);

                            timeOut = DateTime.Parse(outTime);
                            TimeSpan       timeSp        = timeOut.Subtract(timeIn);
                            UserAttendance newAttendance = new UserAttendance
                            {
                                user_id         = dateIds[i],
                                date            = date,
                                marked_at       = new List <DateTime>(),
                                time_in         = timeIn.ToString("yyyy-MM-ddTHH:mm:ss"),
                                time_out        = timeOut.ToString("yyyy-MM-ddTHH:mm:ss"),
                                default_out     = month + "-" + time[0] + "T17:00:00",
                                shifttotalhours = "8",
                                worktotalhours  = timeSp.Hours + ":" + timeSp.Minutes,
                                doctype         = "user_attendance",
                                channels        = new[] { "attendance" },

                                client    = "Esajee",
                                shiftId   = "1",
                                shiftName = "Default",
                                location  = location
                            };
                            newAttendance.marked_at.Add(DateTime.Now);
                            //  toServerPost(newAttendance, "","POST");
                        }
                        else
                        {
                            statsInsert.Add("Date not Found");
                        }
                    }
                    catch (Exception re)
                    {
                        statsInsert.Add(time[0] + ":" + time[i]);
                    }
                }
            }

            return(statsInsert);
        }
        /*
                public void PutObject(string postUrl, object payload)
                {
                    var request = (HttpWebRequest)WebRequest.Create(postUrl);
                    request.Method = "PUT";
                    request.ContentType = "application/json";
                    if (payload != null)
                    {
                      //  request.ContentLength = JsonConvert.SerializeObject(payload).Length;
                        Stream dataStream = request.GetRequestStream();
                        Serialize(dataStream, payload);
                        dataStream.Close();
                    }

                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    string returnString = response.StatusCode.ToString();
                }

                public void Serialize(Stream output, object input)
                {
                    var ser = new DataContractSerializer(input.GetType());
                    ser.WriteObject(output, JsonConvert.SerializeObject(input));
                }
         */
        public string toServerPost(UserAttendance userAttendance, string id,string method)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://192.168.1.202:4984/db/" + id);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.AllowWriteStreamBuffering = true;
            httpWebRequest.PreAuthenticate = true;
            httpWebRequest.Headers.Add("Authorization", "Basic YXR0ZW5kYW5jZTphdHRlbmRhbmNl");
            httpWebRequest.Method = method;
            httpWebRequest.MediaType = "json";

            httpWebRequest.Credentials = new NetworkCredential("attendance", "attendance");

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                var serializer = new JavaScriptSerializer();
                streamWriter.Write(serializer.Serialize(userAttendance));
                streamWriter.Flush();
                streamWriter.Close();
            }
            try
            {
                System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse() as HttpWebResponse;
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    return JsonConvert.SerializeObject(result);
                }
            }
            catch (Exception er)
            {
                Console.WriteLine(er.ToString());
                return "false";
            }
            return "false";
        }
        public List<string> postAttendance(string email, string user_id, string timeIn, int location)
        {
            var queryRequest = new QueryRequest();
            DateTime TimeIn = DateTime.Now;
            List<string> values = new List<string>();
            TimeSpan work;
            try
            {
                if (timeIn.CompareTo("") != 0)
                {

                    TimeIn = DateTime.Parse(HttpUtility.UrlDecode(timeIn));
                }
                string userCond = "";
                userCond = " and user_id=$user_id ";
                if (user_id != null && !user_id.Equals(""))
                {

                    queryRequest.AddNamedParameter("user_id", user_id);

                }
                else if (email != null && !email.Equals(""))
                {
                    queryRequest.AddNamedParameter("email", email);

                    var queryGetUserId = new QueryRequest();
                    queryGetUserId.Statement("SELECT META(Testing).id AS id FROM `Testing`  WHERE email=$email")
                        .AddNamedParameter("email", email);
                    IQueryResult<dynamic> resultId = bucket.Query<dynamic>(queryGetUserId);
                    if (resultId.Rows.Count() > 0)
                    {
                        JObject userAttendanceData = JsonConvert.DeserializeObject<JObject>(resultId.Rows.ElementAt(0) + "");
                        values.Add(userAttendanceData["id"].ToString());
                        queryRequest.AddNamedParameter("user_id", userAttendanceData["id"].ToString());
                        user_id = userAttendanceData["id"].ToString();
                    }
                    else
                    {
                        values.Add("No User Found");
                        return values;
                    }
                }
                else
                {
                    values.Add("No User Found");
                    return values;
                }
                string dat=TimeIn.ToString("yyyy-MM-dd");
                UserAttendance newAttendance = null;
                queryRequest.Statement("SELECT META(Testing).id AS id,* FROM `Testing` WHERE date=$date2 " + userCond)
                        .AddNamedParameter("date2",dat );

                IQueryResult<dynamic> result = bucket.Query<dynamic>(queryRequest);

                if (result.Rows.Count() == 0)
                {
                    DateTime defTime = DateTime.Parse(TimeIn.ToString("yyyy/MM/dd") + "T17:00:00");
                    work = defTime.Subtract(TimeIn);
                    newAttendance = new UserAttendance
                   {
                       user_id = user_id,
                       date = dat,
                       marked_at = new List<DateTime>(),
                       time_in = TimeIn.ToString("yyyy-MM-ddTHH:mm:ss"),
                       time_out = "",
                       default_out = defTime.ToString(),
                       shifttotalhours = "8",
                       worktotalhours = work.Hours + ":" + work.Minutes,
                       doctype = "user_attendance",
                       channels = new[] { "attendance" },

                       client = "Esajee",
                       shiftId = "1",
                       shiftName = "Default",
                       location = location
                   };
                    newAttendance.marked_at.Add(TimeIn);
                    values.Add(user_id);
                    toServerPost(newAttendance, "","POST");
                }
                else
                {
                    JObject userAttendanceData = JsonConvert.DeserializeObject<JObject>(result.Rows.ElementAt(0)+ "");
                    string docId = "";
                    string revId = "";
                    docId = userAttendanceData["id"].ToString();
                    revId = userAttendanceData["Testing"]["_sync"]["rev"].ToString();
                    newAttendance = JsonConvert.DeserializeObject<UserAttendance>(JsonConvert.SerializeObject(userAttendanceData["Testing"]));
                    DateTime markTimeIn = DateTime.Parse(newAttendance.time_in);

                    if (newAttendance != null)
                    {

                        newAttendance.time_out = TimeIn.ToString("yyyy-MM-ddTHH:mm:ss");
                        TimeSpan work12 = TimeIn.Subtract(markTimeIn);
                        newAttendance.worktotalhours = work12.Hours + ":" + work12.Minutes;
                        if (newAttendance.marked_at != null)
                        {
                            newAttendance.marked_at.Add(TimeIn);
                        }
                        values.Add(toServerPost(newAttendance,docId + "?rev="+revId,"PUT"));
                    }
                    else
                    {
                        values.Add("View Not Working");
                    }

                }
            }
            catch (Exception re)
            {
                values.Add("Times convertion failed");
                return values;
            }
            return values;
        }
        public List<string> fromFile(int location)
        {
            List<string> statsInsert = new List<string>();
            StreamReader stReader = new StreamReader(@"C:\ATTENDNCE.csv");
            string headLine = stReader.ReadLine();
            string[] dateIds = headLine.Split(',');
            string date = dateIds[0];
            DateTime asdasd = DateTime.Parse(date);
            string month = asdasd.ToString("yyyy-MM");
            string line = "";
            while ((line = stReader.ReadLine()) != null)
            {
                string[] time = line.Split(',');
                for (int i = 1; i < time.Length; i += 3)
                {
                    try
                    {
                        if (time[i].CompareTo("") != 0 && time[i + 1].CompareTo("") != 0)
                        {
                            string inTime = "";
                            string outTime = "";
                            DateTime timeIn;
                            DateTime timeOut;
                            if (int.Parse(time[0]) > 9)
                            {
                                Console.WriteLine("");
                            }
                            if (int.Parse(time[0]) < 10)
                            {
                                date = month + "-" + "0" + time[0];
                                inTime = month + "-" + "0" + time[0] + " " + time[i];
                                outTime = month + "-" + "0" + time[0] + " " + time[i + 1];
                            }
                            else
                            {
                                date = month + "-" + time[0];
                                inTime = month + "-" + time[0] + " " + time[i];
                                outTime = month + "-" + time[0] + " " + time[i + 1];
                            }
                            timeIn = DateTime.Parse(inTime);

                            timeOut = DateTime.Parse(outTime);
                            TimeSpan timeSp = timeOut.Subtract(timeIn);
                            UserAttendance newAttendance = new UserAttendance
                             {
                                 user_id = dateIds[i],
                                 date = date,
                                 marked_at = new List<DateTime>(),
                                 time_in = timeIn.ToString("yyyy-MM-ddTHH:mm:ss"),
                                 time_out = timeOut.ToString("yyyy-MM-ddTHH:mm:ss"),
                                 default_out = month + "-" + time[0] + "T17:00:00",
                                 shifttotalhours = "8",
                                 worktotalhours = timeSp.Hours + ":" + timeSp.Minutes,
                                 doctype = "user_attendance",
                                 channels = new[] { "attendance" },

                                 client = "Esajee",
                                 shiftId = "1",
                                 shiftName = "Default",
                                 location =location

                             };
                            newAttendance.marked_at.Add(DateTime.Now);
                          //  toServerPost(newAttendance, "","POST");
                        }
                        else
                        {
                            statsInsert.Add("Date not Found");
                        }

                    }
                    catch (Exception re)
                    {
                        statsInsert.Add(time[0] + ":" + time[i]);
                    }
                }
            }

            return statsInsert;
        }