Exemplo n.º 1
0
 public string SaveClientDataFromAndroid(string clientId, string height, string weight, string waist, string hip, string pal, string date, string userId)
 {
     try {
         NewClientData x = new NewClientData();
         using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + db.GetDataBasePath(userId, dataBase))) {
             connection.Open();
             x = GetClientData(userId, clientId, connection);
         }
         if (x.clientId == null)
         {
             x.pal             = new Calculations.Pal();
             x.goal            = new Goals.NewGoal();
             x.activities      = new List <Activities.ClientActivity>();
             x.diet            = new Diets.NewDiet();
             x.meals           = new List <Meals.NewMeal>();
             x.date            = g.FormatDate(DateTime.UtcNow);
             x.dailyActivities = new DetailEnergyExpenditure.Activities();
             x.myMeals         = new MyMeals.NewMyMeals();
         }
         else
         {
             x.clientId  = clientId;
             x.height    = Convert.ToDouble(height);
             x.weight    = Convert.ToDouble(weight);
             x.waist     = Convert.ToDouble(waist);
             x.hip       = Convert.ToDouble(hip);
             x.pal.value = Convert.ToDouble(pal);
             x.date      = g.FormatDate(Convert.ToDateTime(date));
             x.userId    = userId;
         }
         return(Save(userId, x, 0));
     } catch (Exception e) {
         return(e.Message);
     }
 }
Exemplo n.º 2
0
 public string UpdateClientLog(string userId, NewClientData clientData)
 {
     clientData.response = new Global.Response();
     try {
         db.CreateDataBase(userId, db.clientsData);
         using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + db.GetDataBasePath(userId, dataBase))) {
             connection.Open();
             string sql = string.Format(@"UPDATE clientsdata SET clientId = '{0}', height = '{1}', weight ='{2}', waist = '{3}', hip = '{4}', date = '{5}'
                                        WHERE clientId = '{0}' AND rowid = '{6}'"
                                        , clientData.clientId, clientData.height, clientData.weight, clientData.waist, clientData.hip, clientData.date, clientData.id);
             using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                 using (SQLiteTransaction transaction = connection.BeginTransaction()) {
                     command.ExecuteNonQuery();
                     transaction.Commit();
                 }
             }
         }
         clientData.response.isSuccess = true;
         clientData.response.msg       = "saved";
         return(JsonConvert.SerializeObject(clientData, Formatting.None));
     } catch (Exception e) {
         clientData.response.isSuccess = false;
         clientData.response.msg       = e.Message;
         L.SendErrorLog(e, clientData.id.ToString(), userId, "ClientsData", "UpdateClientLog");
         return(JsonConvert.SerializeObject(e.Message, Formatting.None));
     }
 }
Exemplo n.º 3
0
    public string Init(Clients.NewClient client)
    {
        NewClientData x = new NewClientData();

        x.id              = null;
        x.clientId        = client.clientId;
        x.age             = C.Age(client.birthDate);
        x.agemos          = C.Agemos(client.birthDate);
        x.days            = C.Days(client.birthDate);
        x.gender          = GetGender(client.gender.value);
        x.gender.title    = client.gender.title;
        x.height          = 0;
        x.weight          = 0;
        x.waist           = 0;
        x.hip             = 0;
        x.pal             = new Calculations.Pal();
        x.goal            = new Goals.NewGoal();
        x.activities      = new List <Activities.ClientActivity>();
        x.diet            = new Diets.NewDiet();
        x.meals           = new List <Meals.NewMeal>();
        x.date            = DateTime.UtcNow.ToString();
        x.userId          = null;
        x.dailyActivities = new DetailEnergyExpenditure.Activities();
        x.myMeals         = new MyMeals.NewMyMeals();
        x.clientNote      = null;
        x.bmrEquation     = C.MifflinStJeor;
        x.bodyFat         = new BodyFat.NewBodyFat();
        x.targetedMass    = 0;
        x.percentileSrc   = C.WHO;
        x.bmiPercentile   = new Calculations.BmiPercentile();
        x.myCalculation   = new Calculations.MyCalculation();
        x.response        = new Global.Response();
        return(JsonConvert.SerializeObject(x, Formatting.None));
    }
Exemplo n.º 4
0
 private bool Check(string userId, NewClientData x)
 {
     try {
         int count = 0;
         using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + db.GetDataBasePath(userId, dataBase))) {
             connection.Open();
             string sql = string.Format(@"SELECT COUNT([rowid]) FROM clientsdata 
                 WHERE clientId = '{0}' AND ((strftime('%d', date) = '{1}' AND strftime('%m', date) = '{2}' AND strftime('%Y', date) = '{3}') OR date = '{4}')"
                                        , x.clientId, Convert.ToDateTime(x.date).Day, (Convert.ToDateTime(x.date).Month < 10 ? string.Format("0{0}", Convert.ToDateTime(x.date).Month): Convert.ToDateTime(x.date).Month.ToString()), Convert.ToDateTime(x.date).Year, g.FormatDate(Convert.ToDateTime(x.date)));
             using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                 using (SQLiteDataReader reader = command.ExecuteReader()) {
                     while (reader.Read())
                     {
                         count = reader.GetInt32(0);
                     }
                 }
             }
             connection.Close();
         }
         if (count == 0)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     } catch (Exception e) { return(false); }
 }
Exemplo n.º 5
0
 public string Save(string userId, NewClientData x, int userType)
 {
     try {
         db.CreateDataBase(userId, db.clientsData);
         db.AddColumn(userId, db.GetDataBasePath(userId, dataBase), db.clients, "note");            //new column in clients tbl.
         db.AddColumn(userId, db.GetDataBasePath(userId, dataBase), db.clientsData, "bodyFatPerc"); //new column in clients tbl.
         using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + db.GetDataBasePath(userId, dataBase))) {
             connection.Open();
             string sql = "";
             if (Check(userId, x) != false)
             {
                 sql = string.Format(@"INSERT INTO clientsdata (clientId, height, weight, waist, hip, pal, goal, activities, diet, meals, date, userId, bodyFatPerc)
                         VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', '{12}');
                         UPDATE clients SET note = '{13}' WHERE clientId = '{0}';"
                                     , x.clientId, x.height, x.weight, x.waist, x.hip, x.pal.value, x.goal.code
                                     , JsonConvert.SerializeObject(x.activities, Formatting.None)
                                     , JsonConvert.SerializeObject(x.diet, Formatting.None)
                                     , JsonConvert.SerializeObject(x.meals, Formatting.None)
                                     , x.date
                                     , x.userId
                                     , x.bodyFat.bodyFatPerc
                                     , x.clientNote);
             }
             else
             {
                 sql = string.Format(@"UPDATE clientsdata SET  
                         height = '{0}', weight = '{1}', waist = '{2}', hip = '{3}', pal = '{4}', goal = '{5}', activities = '{6}', diet = '{7}', meals = '{8}', date = '{9}', bodyFatPerc = '{10}' 
                         WHERE clientId = '{11}' AND ((strftime('%d', date) = '{12}' AND strftime('%m', date) = '{13}' AND strftime('%Y', date) = '{14}') OR date = '{9}');
                         UPDATE clients SET note = '{15}' WHERE clientId = '{10}';"
                                     , x.height, x.weight, x.waist, x.hip, x.pal.value, x.goal.code
                                     , JsonConvert.SerializeObject(x.activities, Formatting.None)
                                     , JsonConvert.SerializeObject(x.diet, Formatting.None)
                                     , JsonConvert.SerializeObject(x.meals, Formatting.None)
                                     , x.date
                                     , x.bodyFat.bodyFatPerc
                                     , x.clientId
                                     , Convert.ToDateTime(x.date).Day
                                     , (Convert.ToDateTime(x.date).Month < 10 ? string.Format("0{0}", Convert.ToDateTime(x.date).Month) : Convert.ToDateTime(x.date).Month.ToString())
                                     , Convert.ToDateTime(x.date).Year
                                     , x.clientNote);
             }
             using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                 using (SQLiteTransaction transaction = connection.BeginTransaction()) {
                     command.CommandText = sql;
                     command.ExecuteNonQuery();
                     transaction.Commit();
                 }
             }
             connection.Close();
         }
         if (userType > 1)
         {
             SaveMyMeals(userId, x.clientId, x.myMeals);
         }
         return("saved");
     } catch (Exception e) { return("Error: " + e); }
 }
Exemplo n.º 6
0
 public NewClientData GetClientData(string userId, string clientId, SQLiteConnection connection)
 {
     try {
         List <NewClientData> xx = new List <NewClientData>();
         NewClientData        x  = new NewClientData();
         db.AddColumn(userId, db.GetDataBasePath(userId, dataBase), db.clients, "note");            //new column in clients tbl.
         db.AddColumn(userId, db.GetDataBasePath(userId, dataBase), db.clientsData, "bodyFatPerc"); //new column in clients tbl.
         string sql = string.Format(@"SELECT cd.rowid, cd.clientId, c.birthDate, c.gender, cd.height, cd.weight, cd.waist, cd.hip, cd.pal, cd.goal, cd.activities, cd.diet, cd.meals, cd.date, cd.userId, c.note, cd.bodyFatPerc
                     FROM clientsdata as cd
                     LEFT OUTER JOIN clients as c
                     ON cd.clientId = c.clientId
                     WHERE cd.clientId = '{0}'", clientId);
         using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
             Calculations c = new Calculations();
             Goals        g = new Goals();
             using (SQLiteDataReader reader = command.ExecuteReader()) {
                 while (reader.Read())
                 {
                     x                     = new NewClientData();
                     x.id                  = reader.GetInt32(0);
                     x.clientId            = reader.GetValue(1) == DBNull.Value ? "" : reader.GetString(1);
                     x.age                 = C.Age(reader.GetValue(2) == DBNull.Value ? "" : reader.GetString(2));
                     x.gender.value        = reader.GetValue(3) == DBNull.Value ? 0 : reader.GetInt32(3);
                     x.gender.title        = GetGender(x.gender.value).title;
                     x.height              = reader.GetValue(4) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(4));
                     x.weight              = reader.GetValue(5) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(5));
                     x.waist               = reader.GetValue(6) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(6));
                     x.hip                 = reader.GetValue(7) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(7));
                     x.pal                 = c.GetPal(reader.GetValue(8) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(8)));
                     x.goal.code           = reader.GetValue(9) == DBNull.Value ? "" : reader.GetString(9);
                     x.goal.title          = g.GetGoal(x.goal.code).title;
                     x.activities          = JsonConvert.DeserializeObject <List <Activities.ClientActivity> >(reader.GetString(10));
                     x.diet                = JsonConvert.DeserializeObject <Diets.NewDiet>(reader.GetString(11));
                     x.meals               = JsonConvert.DeserializeObject <List <Meals.NewMeal> >(reader.GetString(12));
                     x.date                = reader.GetValue(13) == DBNull.Value ? DateTime.UtcNow.ToString() : reader.GetString(13);
                     x.userId              = reader.GetValue(14) == DBNull.Value ? "" : reader.GetString(14);
                     x.clientNote          = reader.GetValue(15) == DBNull.Value ? "" : reader.GetString(15);
                     x.bodyFat             = new BodyFat.NewBodyFat();
                     x.bodyFat.bodyFatPerc = reader.GetValue(16) == DBNull.Value ? 0 : Convert.ToDouble(reader.GetString(16));
                     DetailEnergyExpenditure.DailyActivities da = new DetailEnergyExpenditure.DailyActivities();
                     x.dailyActivities = da.getDailyActivities(userId, x.clientId);
                     x.myMeals         = GetMyMeals(userId, x.clientId);
                     x.bmrEquation     = C.MifflinStJeor; // TODO GetBmrEquation() & SaveBMREquation()
                     xx.Add(x);
                 }
             }
         }
         if (xx.Count > 0)
         {
             x = xx.OrderByDescending(a => Convert.ToDateTime(a.date)).FirstOrDefault();
         }
         return(x);
     } catch (Exception e) { return(new NewClientData()); }
 }
Exemplo n.º 7
0
 public string Get(string userId, string clientId)
 {
     try {
         NewClientData x = new NewClientData();
         using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + db.GetDataBasePath(userId, dataBase))) {
             connection.Open();
             x = GetClientData(userId, clientId, connection);
             connection.Close();
         }
         return(JsonConvert.SerializeObject(x, Formatting.None));
     } catch (Exception e) { return("Error: " + e); }
 }
Exemplo n.º 8
0
 public string Get(string userId, string clientId)
 {
     try {
         NewClientData x = new NewClientData();
         using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + db.GetDataBasePath(userId, dataBase))) {
             connection.Open();
             x = GetClientData(userId, clientId, connection);
         }
         return(JsonConvert.SerializeObject(x, Formatting.None));
     } catch (Exception e) {
         L.SendErrorLog(e, null, userId, "ClientsData", "Get");
         return(JsonConvert.SerializeObject(e.Message, Formatting.None));
     }
 }
Exemplo n.º 9
0
 public string Delete(string userId, NewClientData clientData)
 {
     try {
         using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + db.GetDataBasePath(userId, dataBase))) {
             connection.Open();
             string sql = string.Format("delete from clientsdata where rowid='{0}' AND clientId='{1}'", clientData.id, clientData.clientId);
             using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                 command.ExecuteNonQuery();
             }
             connection.Close();
         }
         return("OK");
     } catch (Exception e) { return("Error: " + e); }
 }
Exemplo n.º 10
0
 public string GetClientLog(string userId, string clientId)
 {
     try {
         List <NewClientData> xx = new List <NewClientData>();
         using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + db.GetDataBasePath(userId, dataBase))) {
             connection.Open();
             string sql = string.Format(@"SELECT cd.rowid, cd.clientId, c.birthDate, c.gender, cd.height, cd.weight, cd.waist, cd.hip, cd.pal, cd.goal, cd.activities, cd.diet, cd.meals, cd.date, cd.userId, cd.targetedMass
                     FROM clientsdata as cd
                     LEFT OUTER JOIN clients as c
                     ON cd.clientId = c.clientId
                     WHERE cd.clientId = '{0}'", clientId);
             using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                 using (SQLiteDataReader reader = command.ExecuteReader()) {
                     while (reader.Read())
                     {
                         NewClientData x = new NewClientData();
                         Calculations  c = new Calculations();
                         Goals         g = new Goals();
                         x.id           = reader.GetInt32(0);
                         x.clientId     = reader.GetValue(1) == DBNull.Value ? "" : reader.GetString(1);
                         x.age          = C.Age(reader.GetValue(2) == DBNull.Value ? "" : reader.GetString(2));
                         x.gender.value = reader.GetValue(3) == DBNull.Value ? 0 : reader.GetInt32(3);
                         x.gender.title = GetGender(x.gender.value).title;
                         x.height       = reader.GetValue(4) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(4));
                         x.weight       = reader.GetValue(5) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(5));
                         x.waist        = reader.GetValue(6) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(6));
                         x.hip          = reader.GetValue(7) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(7));
                         x.pal          = c.GetPal(reader.GetValue(8) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(8)));
                         x.goal.code    = reader.GetValue(9) == DBNull.Value ? "" : reader.GetString(9);
                         x.goal.title   = g.GetGoal(x.goal.code).title;
                         x.activities   = JsonConvert.DeserializeObject <List <Activities.ClientActivity> >(reader.GetString(10));
                         x.diet         = JsonConvert.DeserializeObject <Diets.NewDiet>(reader.GetString(11));
                         x.meals        = JsonConvert.DeserializeObject <List <Meals.NewMeal> >(reader.GetString(12));
                         x.date         = reader.GetValue(13) == DBNull.Value ? DateTime.UtcNow.ToString() : reader.GetString(13);
                         x.userId       = reader.GetValue(14) == DBNull.Value ? "" : reader.GetString(14);
                         x.targetedMass = reader.GetValue(15) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(15));
                         xx.Add(x);
                     }
                 }
             }
         }
         xx = xx.OrderBy(a => Convert.ToDateTime(a.date)).ToList();
         return(JsonConvert.SerializeObject(xx, Formatting.None));
     } catch (Exception e) {
         L.SendErrorLog(e, null, userId, "ClientsData", "GetClientLog");
         return(JsonConvert.SerializeObject(e.Message, Formatting.None));
     }
 }
Exemplo n.º 11
0
 public string Delete(string userId, NewClientData clientData)
 {
     try {
         using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + db.GetDataBasePath(userId, dataBase))) {
             connection.Open();
             string sql = string.Format("delete from clientsdata where rowid='{0}' AND clientId='{1}'", clientData.id, clientData.clientId);
             using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                 command.ExecuteNonQuery();
             }
         }
         return(JsonConvert.SerializeObject("ok", Formatting.None));
     } catch (Exception e) {
         L.SendErrorLog(e, clientData.id.ToString(), userId, "ClientsData", "Delete");
         return(JsonConvert.SerializeObject(e.Message, Formatting.None));
     }
 }
Exemplo n.º 12
0
 public string Load(string userId)
 {
     try {
         SQLiteConnection connection = new SQLiteConnection("Data Source=" + db.GetDataBasePath(userId, dataBase));
         connection.Open();
         string               sql     = @"SELECT cd.rowid, cd.clientId, c.birthDate, c.gender, cd.height, cd.weight, cd.waist, cd.hip, cd.pal, cd.goal, cd.activities, cd.diet, cd.meals, cd.date, cd.userId
                     FROM clientsdata as cd
                     LEFT OUTER JOIN clients as c
                     ON cd.clientId = c.clientId
                     ORDER BY cd.rowid DESC";
         SQLiteCommand        command = new SQLiteCommand(sql, connection);
         List <NewClientData> xx      = new List <NewClientData>();
         SQLiteDataReader     reader  = command.ExecuteReader();
         while (reader.Read())
         {
             NewClientData x = new NewClientData();
             Calculations  c = new Calculations();
             Goals         g = new Goals();
             x.id           = reader.GetInt32(0);
             x.clientId     = reader.GetValue(1) == DBNull.Value ? "" : reader.GetString(1);
             x.age          = C.Age(reader.GetValue(2) == DBNull.Value ? "" : reader.GetString(2));
             x.gender.value = reader.GetValue(3) == DBNull.Value ? 0 : reader.GetInt32(3);
             x.gender.title = GetGender(x.gender.value).title;
             x.height       = reader.GetValue(4) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(4));
             x.weight       = reader.GetValue(5) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(5));
             x.waist        = reader.GetValue(6) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(6));
             x.hip          = reader.GetValue(7) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(7));
             x.pal          = c.GetPal(reader.GetValue(8) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(8)));
             x.goal.code    = reader.GetValue(9) == DBNull.Value ? "" : reader.GetString(9);
             x.goal.title   = g.GetGoal(x.goal.code).title;
             x.activities   = JsonConvert.DeserializeObject <List <Activities.ClientActivity> >(reader.GetString(10));
             x.diet         = JsonConvert.DeserializeObject <Diets.NewDiet>(reader.GetString(11));
             x.meals        = JsonConvert.DeserializeObject <List <Meals.NewMeal> >(reader.GetString(12));
             x.date         = reader.GetValue(13) == DBNull.Value ? DateTime.UtcNow.ToString() : reader.GetString(13);
             x.userId       = reader.GetValue(14) == DBNull.Value ? "" : reader.GetString(14);
             DetailEnergyExpenditure.DailyActivities da = new DetailEnergyExpenditure.DailyActivities();
             x.dailyActivities = da.getDailyActivities(userId, x.clientId);
             x.myMeals         = new MyMeals.NewMyMeals();
             xx.Add(x);
         }
         connection.Close();
         return(JsonConvert.SerializeObject(xx, Formatting.None));
     } catch (Exception e) { return("Error: " + e); }
 }
Exemplo n.º 13
0
 public string UpdateClientLog(string userId, NewClientData clientData)
 {
     try {
         db.CreateDataBase(userId, db.clientsData);
         using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + db.GetDataBasePath(userId, dataBase))) {
             connection.Open();
             string sql = string.Format(@"UPDATE clientsdata SET clientId = '{0}', height = '{1}', weight ='{2}', waist = '{3}', hip = '{4}', date = '{5}'
                                        WHERE clientId = '{0}' AND rowid = '{6}'"
                                        , clientData.clientId, clientData.height, clientData.weight, clientData.waist, clientData.hip, clientData.date, clientData.id);
             using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                 using (SQLiteTransaction transaction = connection.BeginTransaction()) {
                     command.ExecuteNonQuery();
                     transaction.Commit();
                 }
             }
             connection.Close();
         }
         return("saved");
     } catch (Exception e) { return("Error: " + e.Message); }
 }
Exemplo n.º 14
0
    public NewClientData GetClientData(string userId, string clientId, SQLiteConnection connection)
    {
        NewClientData x = new NewClientData();

        try {
            List <NewClientData> xx = new List <NewClientData>();
            db.AddColumn(userId, db.GetDataBasePath(userId, dataBase), db.clients, "note");                 //new column in clients tbl.
            db.AddColumn(userId, db.GetDataBasePath(userId, dataBase), db.clients, "cids", "VARCHAR(200)"); //new column in cids tbl.
            db.AddColumn(userId, db.GetDataBasePath(userId, dataBase), db.clients, "dailyActivities", "TEXT");
            db.AddColumn(userId, db.GetDataBasePath(userId, dataBase), db.clients, "myRecommendedEnergyIntake", "VARCHAR(50)");
            db.AddColumn(userId, db.GetDataBasePath(userId, dataBase), db.clients, "myRecommendedEnergyExpenditure", "VARCHAR(50)");
            db.AddColumn(userId, db.GetDataBasePath(userId, dataBase), db.clients, "myMeals", "TEXT");
            db.AddColumn(userId, db.GetDataBasePath(userId, dataBase), db.clients, "allergens", "TEXT");

            string sql = string.Format(@"SELECT cd.rowid, cd.clientId, c.birthDate, c.gender, cd.height, cd.weight, cd.waist, cd.hip, cd.pal, cd.goal, cd.activities, cd.diet, cd.meals, cd.date, cd.userId, c.note, cd.bodyFatPerc, cd.targetedMass, c.dailyActivities, c.myRecommendedEnergyIntake, c.myRecommendedEnergyExpenditure, c.myMeals
                        FROM clientsdata as cd
                        LEFT OUTER JOIN clients as c
                        ON cd.clientId = c.clientId
                        WHERE cd.clientId = '{0}'", clientId);
            using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                Calculations c = new Calculations();
                Goals        g = new Goals();
                DetailEnergyExpenditure.DailyActivities da = new DetailEnergyExpenditure.DailyActivities();
                string birthDate = null;
                using (SQLiteDataReader reader = command.ExecuteReader()) {
                    while (reader.Read())
                    {
                        x              = new NewClientData();
                        x.id           = reader.GetInt32(0);
                        x.clientId     = reader.GetValue(1) == DBNull.Value ? "" : reader.GetString(1);
                        birthDate      = reader.GetValue(2) == DBNull.Value ? "" : reader.GetString(2);
                        x.age          = C.Age(birthDate);
                        x.agemos       = C.Agemos(birthDate);
                        x.days         = C.Days(birthDate);
                        x.gender.value = reader.GetValue(3) == DBNull.Value ? 0 : reader.GetInt32(3);
                        x.gender.title = GetGender(x.gender.value).title;
                        x.height       = reader.GetValue(4) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(4));
                        x.weight       = reader.GetValue(5) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(5));
                        x.waist        = reader.GetValue(6) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(6));
                        x.hip          = reader.GetValue(7) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(7));
                        x.pal          = c.GetPal(reader.GetValue(8) == DBNull.Value ? 0.0 : Convert.ToDouble(reader.GetString(8)));
                        x.goal.code    = reader.GetValue(9) == DBNull.Value ? "" : reader.GetString(9);
                        x.goal.title   = g.GetGoal(x.goal.code).title;
                        x.activities   = JsonConvert.DeserializeObject <List <Activities.ClientActivity> >(reader.GetString(10));
                        x.diet         = JsonConvert.DeserializeObject <Diets.NewDiet>(reader.GetString(11));
                        if (x.diet == null)
                        {
                            x.diet = new Diets.NewDiet();
                        }
                        x.meals               = JsonConvert.DeserializeObject <List <Meals.NewMeal> >(reader.GetString(12));
                        x.date                = reader.GetValue(13) == DBNull.Value ? DateTime.UtcNow.ToString() : reader.GetString(13);
                        x.userId              = reader.GetValue(14) == DBNull.Value ? "" : reader.GetString(14);
                        x.clientNote          = reader.GetValue(15) == DBNull.Value ? "" : reader.GetString(15);
                        x.bodyFat             = new BodyFat.NewBodyFat();
                        x.bodyFat.bodyFatPerc = reader.GetValue(16) == DBNull.Value ? 0 : Convert.ToDouble(reader.GetString(16));
                        x.targetedMass        = reader.GetValue(17) == DBNull.Value ? 0 : Convert.ToDouble(reader.GetString(17));
                        string dailyActivities = reader.GetValue(18) == DBNull.Value ? null : reader.GetString(18);
                        if (!string.IsNullOrWhiteSpace(dailyActivities))
                        {
                            var activities = JsonConvert.DeserializeObject <List <DetailEnergyExpenditure.Activity> >(dailyActivities);
                            x.dailyActivities            = new DetailEnergyExpenditure.Activities();
                            x.dailyActivities.activities = activities;
                            if (activities != null)
                            {
                                x.dailyActivities.energy   = activities.Sum(a => a.energy);
                                x.dailyActivities.duration = activities.Sum(a => a.duration);
                            }
                        }
                        else
                        {
                            x.dailyActivities = da.getDailyActivities(userId, x.clientId); // old sistem: data saved in json file
                        }
                        x.myCalculation = new Calculations.MyCalculation();
                        int?myRecommendedEnergyIntake      = reader.GetValue(19) == DBNull.Value ? 0 : string.IsNullOrWhiteSpace(reader.GetString(19)) ? 0 : Convert.ToInt32(reader.GetString(19));
                        int?myRecommendedEnergyExpenditure = reader.GetValue(20) == DBNull.Value ? 0 : string.IsNullOrWhiteSpace(reader.GetString(20)) ? 0 : Convert.ToInt32(reader.GetString(20));
                        if (myRecommendedEnergyIntake > 0 || myRecommendedEnergyExpenditure > 0)
                        {
                            x.myCalculation.recommendedEnergyIntake      = myRecommendedEnergyIntake;
                            x.myCalculation.recommendedEnergyExpenditure = myRecommendedEnergyExpenditure;
                        }
                        else
                        {
                            x.myCalculation = C.GetJsonFile(userId, x.clientId); // old sistem: data saved in json file
                        }
                        string myMeals = reader.GetValue(21) == DBNull.Value ? null : reader.GetString(21);
                        if (!string.IsNullOrWhiteSpace(myMeals))
                        {
                            x.myMeals = JsonConvert.DeserializeObject <MyMeals.NewMyMeals>(myMeals);  // new sistem: data saved in db
                        }
                        else
                        {
                            x.myMeals = GetMyMeals(userId, x.clientId); // old sistem: data saved in json file
                        }
                        // TODO: allergens
                        //x.allergens = reader.GetValue(22) == DBNull.Value ? null : reader.GetString(22);
                        x.bmrEquation   = C.MifflinStJeor; // TODO GetBmrEquation() & SaveBMREquation()
                        x.percentileSrc = C.WHO;
                        x.bmiPercentile = C.GetBmiPercentile(x.agemos, x.gender.value, x.percentileSrc);
                        xx.Add(x);
                    }
                }
            }
            if (xx.Count > 0)
            {
                x = xx.OrderByDescending(a => Convert.ToDateTime(a.date)).FirstOrDefault();
            }
            return(x);
        } catch (Exception e) {
            L.SendErrorLog(e, clientId, userId, "ClientsData", "GetClientData");
            return(x);
        }
    }
Exemplo n.º 15
0
    public string Save(string userId, NewClientData x, int userType)
    {
        string sql = null;

        x.response = new Global.Response();
        try {
            Global G = new Global();
            x.clientNote = G.RemoveSingleQuotes(x.clientNote);
            using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + db.GetDataBasePath(userId, dataBase))) {
                connection.Open();
                if (Check(userId, x))
                {
                    sql = string.Format(@"INSERT INTO clientsdata (clientId, height, weight, waist, hip, pal, goal, activities, diet, meals, date, userId, bodyFatPerc, targetedMass)
                            VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', '{12}', '{13}');
                            UPDATE clients SET note = '{14}' WHERE clientId = '{0}';"
                                        , x.clientId, x.height, x.weight, x.waist, x.hip, x.pal.value, x.goal.code
                                        , JsonConvert.SerializeObject(x.activities, Formatting.None)
                                        , JsonConvert.SerializeObject(x.diet, Formatting.None)
                                        , JsonConvert.SerializeObject(x.meals, Formatting.None)
                                        , x.date
                                        , x.userId
                                        , x.bodyFat.bodyFatPerc
                                        , x.targetedMass
                                        , x.clientNote);
                }
                else
                {
                    sql = string.Format(@"UPDATE clientsdata SET  
                            height = '{0}', weight = '{1}', waist = '{2}', hip = '{3}', pal = '{4}', goal = '{5}', activities = '{6}', diet = '{7}', meals = '{8}', date = '{9}', bodyFatPerc = '{10}', targetedMass = '{16}' 
                            WHERE clientId = '{11}' AND ((strftime('%d', date) = '{12}' AND strftime('%m', date) = '{13}' AND strftime('%Y', date) = '{14}') OR date = '{9}');
                            UPDATE clients SET note = '{15}' WHERE clientId = '{11}';"
                                        , x.height, x.weight, x.waist, x.hip, x.pal.value, x.goal.code
                                        , JsonConvert.SerializeObject(x.activities, Formatting.None)
                                        , JsonConvert.SerializeObject(x.diet, Formatting.None)
                                        , JsonConvert.SerializeObject(x.meals, Formatting.None)
                                        , x.date
                                        , x.bodyFat.bodyFatPerc
                                        , x.clientId
                                        , Convert.ToDateTime(x.date).Day
                                        , (Convert.ToDateTime(x.date).Month < 10 ? string.Format("0{0}", Convert.ToDateTime(x.date).Month) : Convert.ToDateTime(x.date).Month.ToString())
                                        , Convert.ToDateTime(x.date).Year
                                        , x.clientNote
                                        , x.targetedMass);
                }
                using (SQLiteCommand command = new SQLiteCommand(sql, connection)) {
                    using (SQLiteTransaction transaction = connection.BeginTransaction()) {
                        command.CommandText = sql;
                        command.ExecuteNonQuery();
                        transaction.Commit();
                    }
                }
            }

            C.SaveMyCalculation(userId, x.clientId, x.myCalculation);

            if (userType > 1)
            {
                SaveMyMeals(userId, x.clientId, x.myMeals);
            }
            x.response.isSuccess = true;
            x.response.msg       = "saved";
            return(JsonConvert.SerializeObject(x, Formatting.None));
        } catch (Exception e) {
            x.response.isSuccess = false;
            x.response.msg       = e.Message;
            L.SendErrorLog(e, sql, userId, "ClientsData", "Save");
            return(JsonConvert.SerializeObject(x, Formatting.None));
        }
    }