Exemplo n.º 1
0
        /// <summary>
        /// manages creating exercises and responding to the user with information about that request
        /// including all the exercises that were created during the request
        /// </summary>
        /// <param name="root"></param>
        /// <param name="db"></param>
        /// <param name="manager"></param>
        static void SetupExerciseInfoManager(mpBase root, Database db, UserManager manager)
        {
            root.addProperty("exercise-info",
                             new mpRestfulTarget(
                                 new Func <System.Net.HttpListenerRequest, mpResponse>(
                                     req => {
                return(mpResponse.empty400());
            }
                                     ),
                                 new Func <System.Net.HttpListenerRequest, mpResponse>(
                                     req => {
                Console.Write("Exercise Data Requested...");

                string requestData  = req.data();
                string requestEmail = "", requestPasswordEmailHash = "";
                int requestNumItems = -1;

                try {
                    mpObject requestJSON = (mpObject)mpJson.parse(requestData);

                    requestEmail             = ((mpValue)requestJSON.getChild("email")).data.asString();
                    requestPasswordEmailHash = ((mpValue)requestJSON.getChild("passwordEmailHash")).data.asString();
                    requestNumItems          = ((mpValue)requestJSON.getChild("numItems")).data.asInt();
                } catch (Exception ex) {
                    Console.WriteLine("Exercise Request Error: " + ex.Message);
                    return(new mpResponse(new binaryData("{\"good\":false, \"message\":\"" + ex.Message + "\"}"), 400));
                }

                try {
                    User user           = manager.getUser(requestEmail, requestPasswordEmailHash);
                    UserDataAccess info = new UserDataAccess(db, user);
                    WorkoutGenerator p  = new WorkoutGenerator(info);

                    mpResponse res      = mpResponse.success();
                    WorkoutSession sesh = p.GenSesh(requestNumItems);
                    res.response        = new binaryData(sesh.toJSON(info).ToString());
                    info.AddDay(sesh);
                    info.Store();
                    info.Dispose();

                    Console.WriteLine(res.response.asString());

                    Console.WriteLine("Responded! (user = "******")");
                    return(res);
                } catch (Exception ex) {
                    Console.WriteLine("Exercise Request Error: " + ex.Message);
                    return(new mpResponse(new binaryData("{\"good\":false, \"message\":\"" + ex.Message + "\"}"), 400));
                }
            }
                                     )
                                 )
                             );
        }
Exemplo n.º 2
0
        /// <summary>
        /// takes in calibration information for a certain exercise and returns the calculated 1rm
        /// for that exercise
        /// </summary>
        /// <param name="root"></param>
        /// <param name="db"></param>
        /// <param name="manager"></param>
        static void SetupExerciseCalibrationManager(mpBase root, Database db, UserManager manager)
        {
            root.addProperty("exercise-calibration",
                             new mpRestfulTarget(
                                 new Func <System.Net.HttpListenerRequest, mpResponse>(
                                     req => {
                return(mpResponse.empty400());
            }
                                     ),
                                 new Func <System.Net.HttpListenerRequest, mpResponse>(
                                     req => {
                Console.Write("Received Exercise Calibration Info...");

                string requestData  = req.data();
                string requestEmail = "", requestPasswordEmailHash = "";
                double oneRepMax    = -1;

                try {
                    mpObject requestJSON = (mpObject)mpJson.parse(requestData);

                    requestEmail             = ((mpValue)requestJSON.getChild("email")).data.asString();
                    requestPasswordEmailHash = ((mpValue)requestJSON.getChild("passwordEmailHash")).data.asString();
                    string exName            = ((mpValue)requestJSON.getChild("exercise")).data.asString();

                    UserDataAccess info = new UserDataAccess(db, manager.getUser(requestEmail, requestPasswordEmailHash));
                    info.IngestCalibrationInfo(
                        exName,
                        ((mpValue)requestJSON.getChild("reps")).data.asInt(),
                        ((mpValue)requestJSON.getChild("weight")).data.asInt()
                        );
                    info.Store();
                    oneRepMax = info.GetMostRecentCalibratedOneRepMax(exName).Value;
                } catch (Exception ex) {
                    Console.WriteLine("Calibration Info Error: " + ex.Message);
                    return(new mpResponse(new binaryData("{\"good\":false, \"message\":\"" + ex.Message + "\"}"), 400));
                }

                return(new mpResponse(new binaryData("{\"good\":true, \"oneRepMax\":" + oneRepMax + "}"), 200));
            }
                                     )
                                 )
                             );
        }
Exemplo n.º 3
0
        /// <summary>
        /// takes in feedback for a workout item and passes it to the user's plan for processing
        /// </summary>
        /// <param name="root"></param>
        /// <param name="db"></param>
        /// <param name="manager"></param>
        static void SetupExerciseFeedbackManager(mpBase root, Database db, UserManager manager)
        {
            root.addProperty("exercise-feedback",
                             new mpRestfulTarget(
                                 new Func <System.Net.HttpListenerRequest, mpResponse>(
                                     req => {
                return(mpResponse.empty400());
            }
                                     ),
                                 new Func <System.Net.HttpListenerRequest, mpResponse>(
                                     req => {
                Console.Write("Exercise Feedback Data Requested...");

                string requestData  = req.data();
                string requestEmail = "", requestPasswordEmailHash = "";

                try {
                    mpObject requestJSON = (mpObject)mpJson.parse(requestData);

                    requestEmail             = ((mpValue)requestJSON.getChild("email")).data.asString();
                    requestPasswordEmailHash = ((mpValue)requestJSON.getChild("passwordEmailHash")).data.asString();

                    mpObject feedback = (mpObject)requestJSON.getChild("feedback");

                    WorkoutItem item = new WorkoutItem(feedback);

                    UserDataAccess access = new UserDataAccess(db, manager.getUser(requestEmail, requestPasswordEmailHash));

                    access.UpdateItem(item);
                    access.Store();
                    access.Dispose();
                } catch (Exception ex) {
                    Console.WriteLine("Exercise Request Error: " + ex.Message);
                    return(new mpResponse(new binaryData("{\"good\":false, \"message\":\"" + ex.Message + "\"}"), 400));
                }
                return(new mpResponse(new binaryData("{\"good\":true}"), 200));
            }
                                     )
                                 )
                             );
        }