public static LogEntry AddLogEntry(LogEntry entry)
        {
            LogEntry returnLog = new LogEntry();

            string jsonString = JSONhelper.JSONSerializer<LogEntry>(entry);
            Debug.WriteLine(jsonString);
            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create("http://localhost:3369/LevelService.svc/entry/add"); //Addressen på metoden

            webReq.Method = "POST";                                     //Set metodetypen. Default er POST, men vi skriver det ALTID alligevel.
            webReq.ContentType = "application/json; charset=utf-8";     //Sæt contenttypen, i.e Sæt til JSON
            webReq.ContentLength = jsonString.Length;                     //Længden på strengen
            Debug.WriteLine(webReq.ContentLength.ToString() + jsonString.Length.ToString());

            using (StreamWriter sw = new StreamWriter(webReq.GetRequestStream()))   //Opret en streamwriter med vores request som parameter (aner ikke hvad requeststream er, slå det selv op)
            {
                sw.Write(jsonString); //Skyder requesten afsted.
            }

               try
               {
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();           //Opret et objekt der kan modtage svar på vores request
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))    //Opret et objekt der kan læse svaret
                {
                    string text = sr.ReadToEnd();                           //Læs svaret igennem og gem det i en streng. Det er JSON streng svaret kommer i.

                    returnLog = JSONhelper.JsonDeserialize<LogEntry>(text);    //kør json-strengen igennem deserialiseringen, med PersonModel som type objekt
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }

               return returnLog;
        }
        public LogEntry AddEntryToDb(string typeOfExcercise, string distance, int hours, int minutes, int seconds, int userId, string dateCreated, long kcal)
        {
            LogEntry demoLog = new LogEntry();
            int result = -1;
            using (var db = new DataAccessContext())
            {
                LogEntry log = new LogEntry
                {
                    TypeOfExcercise = typeOfExcercise,
                    Distance = distance,
                    Hours = hours,
                    Minutes = minutes,
                    Seconds = seconds,
                    UserId = userId,
                    DateCreated = dateCreated,
                    Kcal = kcal,
                };
                demoLog = log;
                db.LogEntries.Add(log);
                result = db.SaveChanges();
            }

            if (result != -1)
            {
                return demoLog;
            }
            else
            {
                return null;
            }
        }
        public LogEntry GetEntryFromDB(int entryId, int userId)
        {
            LogEntry entry = new LogEntry();
            using (var db = new DataAccessContext()) {

                LogEntry theEntry = (from log in db.LogEntries
                                     where log.LogEntryId == entryId & log.UserId == userId
                                     select log).Single();
                entry = theEntry;
            }

            return entry;
        }
        public void TestCreateLogEntry()
        {
            // Expected object properties
            int userId = 6;
            string typeOfExcerciseExpected = "Running";
            string distanceExpected = "22km";
            int hoursExpected = 1;
            int minutesExpected = 34;
            int secondsExpected = 54;
            int userIdExpected = 6;
            int logEntryIdExpected = 43;
            string dateExpected = "2013-12-11"; // Datetime Issues
            long kcal = 354;
            //DateTime DateExpected = new DateTime(2013,12,12);
            //string dateExpected = "2013-12-11"; // Datetime Issues
            //Debug.WriteLine("Expected : " + dateExpected);

            /* create a Log | check that Log can be found in db | compare */
            // Add the Expected LogEntry to DB
            LogEntryController leCtr = new LogEntryController();
            leCtr.AddEntryToDb(typeOfExcerciseExpected, distanceExpected, hoursExpected, minutesExpected, secondsExpected, userId, dateExpected, kcal);
            //leCtr.AddEntryToDb(typeOfExcerciseExpected, distanceExpected, hoursExpected, minutesExpected, secondsExpected);

            //leCtr.AddEntryToDb(typeOfExcerciseExpected, distanceExpected, hoursExpected, minutesExpected, secondsExpected, DateExpected);

            // Get the actual LogEntry from Entry Id
            LogEntry actualEntry = new LogEntry();
            actualEntry = leCtr.GetEntryFromDB(logEntryIdExpected,userIdExpected); //int entryId, int userId

            // Properties of the Actual LogEntry
            string typeOfExcerciseActual = actualEntry.TypeOfExcercise;
            string distanceActual = actualEntry.Distance;
            int hoursActual = actualEntry.Hours;
            int minutesActual = actualEntry.Minutes;
            int secondsActual = actualEntry.Seconds;
            int userIdActual = actualEntry.UserId;
            //int logEntryIdActual = actualEntry.LogEntryId;
            //string dateActual = actualEntry.DateCreated.ToShortDateString();
            //Debug.WriteLine("Actual   : " + dateActual);

            /* COMPARE */
            Assert.AreEqual(typeOfExcerciseExpected,typeOfExcerciseActual);
            Assert.AreEqual(distanceExpected,distanceActual);
            Assert.AreEqual(hoursExpected,hoursActual);
            Assert.AreEqual(minutesExpected,minutesActual);
            Assert.AreEqual(secondsExpected,secondsActual);
            Assert.AreEqual(userIdExpected,userIdActual);
            //Assert.AreEqual(logEntryIdExpected,logEntryIdActual);
            //Assert.AreEqual(dateExpected,dateActual, dateActual.ToString());
        }
        public void TestGetLogEntry()
        {
            /* EXPECTED */
            string typeOfExcerciseExpected = "Running";
            string distanceExpected = "22km";
            int hoursExpected = 1;
            int minutesExpected = 34;
            int secondsExpected = 54;
            int userIdExpected = 6;
            int logEntryIdExpected = 22;
            DateTime dateExpected = new DateTime(2013, 12, 11);

            /* GET THE USER */
            LogEntryController leCtr = new LogEntryController();
            LogEntry actualLogEntry = new LogEntry();
            actualLogEntry = leCtr.GetEntryFromDB(logEntryIdExpected,userIdExpected);

            /* COMPARE */
            string typeOfExerciseActual = actualLogEntry.TypeOfExcercise;
            string distanceActual = actualLogEntry.Distance;
            int hoursActual = actualLogEntry.Hours;
            int minutesActual = actualLogEntry.Minutes;
            int secondsActual = actualLogEntry.Seconds;
            int userIdActual = actualLogEntry.UserId;
            int logEntryIdActual = actualLogEntry.LogEntryId;
            String dateActual = actualLogEntry.DateCreated;

            Assert.AreEqual(typeOfExcerciseExpected,typeOfExerciseActual);
            Assert.AreEqual(distanceExpected,distanceActual);
            Assert.AreEqual(hoursExpected,hoursActual);
            Assert.AreEqual(minutesExpected,minutesActual);
            Assert.AreEqual(secondsExpected,secondsActual);
            Assert.AreEqual(logEntryIdExpected,logEntryIdActual);
            Assert.AreEqual(userIdExpected,userIdActual);
            Assert.AreEqual(dateExpected,dateActual);
            // DONE
        }
        public void ShowRewardsOutput(User u, LogEntry log)
        {
            string achievementName = "Collector";
            string achFlavourText = "Bedrift for at oprette din første træning";

            RewardOutput.Text += StringValues.REWARDS_TITLE;
            RewardOutput.Text += "\n";
            RewardOutput.Text += " Du har i alt "+ u.Xp + " XP";
            RewardOutput.Text += "\n";
            RewardOutput.Text += " Du har forbrændt " + log.Kcal + " KCAL på denne træning";
            RewardOutput.Text += "\n";
            RewardOutput.Text += StringValues.ACHIEVEMENT_TITLE;
            RewardOutput.Text += "\n";
            foreach( Achievement ach in u.Achievements)
            {
                RewardOutput.Text += "  - " + ach.Name;
                RewardOutput.Text += "\n";
                RewardOutput.Text += "  - " + ach.Description;
                RewardOutput.Text += "\n" + "---------------";
            }
        }
 private void UpdateFields(LogEntry log)
 {
     excerciseTextBox.Text = log.TypeOfExcercise;
     distanceTextBox.Text = log.Distance;
     MinutesTextBox.Text = log.Minutes.ToString();
     HoursTextBox.Text = log.Hours.ToString();
     SecondsTextBox.Text = log.Seconds.ToString();
 }
        protected void CreateLog_Click(object sender, EventArgs e)
        {
            User user = (User)Session["UserItem"];

            if (IsValidActitity(excerciseTextBox.Text) &&
                IsValidDistance(distanceTextBox.Text) &&
                IsvalidTime(HoursTextBox.Text) &&
                IsvalidTime(MinutesTextBox.Text) &&
                IsvalidTime(SecondsTextBox.Text))
            {
                LogEntry log = new LogEntry();
                log.LogEntryId = 0;
                log.TypeOfExcercise = excerciseTextBox.Text;
                log.Distance = distanceTextBox.Text;
                log.Hours = Convert.ToInt32(HoursTextBox.Text);
                log.Minutes = Convert.ToInt32(MinutesTextBox.Text);
                log.Seconds = Convert.ToInt32(SecondsTextBox.Text);
                log.DateCreated = DateTime.Today.ToShortDateString();
                log.UserId = user.UserId;

                int minutes = CalculateMinutes(Convert.ToInt32(HoursTextBox.Text), Convert.ToInt32(MinutesTextBox.Text), Convert.ToInt32(SecondsTextBox.Text));
                log.Kcal = CalculateKcal(excerciseTextBox.Text, minutes, user.Weight);

                log = EntryCalls.AddLogEntry(log);

                UserCalls.UpdateUserXP(user.UserName, log.Kcal);

                Session["UserItem"] = UserCalls.GetUser(user.UserName, user.Password);
                user = (User)Session["UserItem"];

                UpdateFields(log);
                ShowRewardsOutput(user, log);
                BindGrid(); // bind the gridView again after log is added to DB
            }
            else
            {
                // Pop up some dialog + accumulate error message
                string display = "Fejl i Input";
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);
            }
        }