示例#1
0
        public void Insert( )
        {
            muscle_log.framework.Models.MuscleLogContext ctx = new muscle_log.framework.Models.MuscleLogContext("metadata=res://*/Models.MuscleLogModel.csdl|res://*/Models.MuscleLogModel.ssdl|res://*/Models.MuscleLogModel.msl;provider=System.Data.SqlClient;provider connection string='data source=MAX-PC;Initial Catalog=muscle-log;User ID=Max-PC;integrated security=SSPI;multipleactiveresultsets=True;application name=EntityFramework'");
            var unitOfWork = new EFContextUnitOfWork(ctx);
            var repo = new Repository<ml_ForumThread>(unitOfWork);
            ForumThreadService service = new ForumThreadService(repo);
            int countBefore = service.ListThreads(muscle_log.framework.Util.Enum.Forums.Meta).Count();

            ForumThreadViewModel input = new ForumThreadViewModel( );
            input.ID = -1;
            input.UserID = 7;
            input.Title = "Testeintrag";
            input.Body = "Das ist ein Testeintrag. ~*</fsd !§$(%)=&??``´´'<<<><>>>@fsdf%%<%=fds";
            input.Forum = muscle_log.framework.Util.Enum.Forums.Meta;
            service.Save( input );

            int countAfter = service.ListThreads(muscle_log.framework.Util.Enum.Forums.Meta).Count();

            // there must be one more thread than  before
            Assert.AreEqual( countAfter, ( countBefore + 1 ) );

            // the id of the new thread must be bigger than 0
            Assert.Greater( input.ID, 0 );

            // the members of the input object and the output object must be equal
            var output = repo.Get(input.ID);
            Assert.AreNotEqual( input, output );
            Assert.AreEqual( input.Title, output.Title );
            Assert.AreEqual( input.Body, output.Body );
            Assert.AreEqual( input.Forum, output.Forum );
            Assert.AreEqual( input.UserID, output.UserID );

            unitOfWork.Dispose();
        }
        /// <summary>
        /// lists all exercises
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var context = new MuscleLogContext();
            using(var unitOfWork = new EFContextUnitOfWork(context)) {
                var userService = new UserService(new Repository<ml_User>(unitOfWork));
                var exerciseService = new ExerciseLibraryService(new ExerciseRepository(unitOfWork));
                ViewData["UserCanEdit"] = userService.ExerciseAccess(CurrentUser, Convert.ToInt32(ConfigurationManager.AppSettings["EditExerciseRequiredLevel"]));
                return View(exerciseService.GetMuscleGroups());

            }
        }
示例#3
0
 /// <summary>
 /// gets a integer value from the table ml_Settings. 
 /// </summary>
 /// <param name="configProperty">name of the settings property</param>
 /// <returns>the integer value of the property</returns>
 public static int GetIntVal( string configProperty, MuscleLogContext ctx )
 {
     ml_Settings settings = ctx.ml_Settings.Where( s => s.PropertyName == configProperty ).FirstOrDefault( );
     if( settings == null ) {
         throw new SettingsNotFoundException( configProperty );
     }
     int settingsValue = 0;
     if( !Int32.TryParse( settings.IntValue.ToString(), out settingsValue ) ) {
         throw new SettingsTypeException( configProperty );
     }
     return settingsValue;
 }
示例#4
0
        public void IncorrectSettingsType()
        {
            MuscleLogContext ctx = new MuscleLogContext("metadata=res://*/Models.MuscleLogModel.csdl|res://*/Models.MuscleLogModel.ssdl|res://*/Models.MuscleLogModel.msl;provider=System.Data.SqlClient;provider connection string='data source=MAX-PC;Initial Catalog=muscle-log;User ID=Max-PC;integrated security=SSPI;multipleactiveresultsets=True;application name=EntityFramework'");

            ml_Settings test = new ml_Settings();
            test.StringValue = "test";
            test.PropertyName = "incorrectTypeSetting";
            ctx.ml_Settings.Add(test);
            ctx.SaveChanges();
            Assert.Throws(typeof(SettingsTypeException), delegate { muscle_log.Config.GetIntVal("incorrectTypeSetting", ctx); });

            test = null;
            test = ctx.ml_Settings.Where(s => s.PropertyName == "incorrectTypeSetting").FirstOrDefault();
            ctx.ml_Settings.Remove(test);
            ctx.SaveChanges();

            ctx.Dispose();
        }
 /// <summary>
 /// displays an activity
 /// </summary>
 /// <param name="helper">HtmlHelper class</param>
 /// <param name="activity">the activity to display</param>
 /// <returns></returns>
 public static string DisplayActivity(this HtmlHelper helper, ActivityViewModel activity)
 {
     string html = "";
     ml_User user = new MuscleLogContext().ml_User.First(u => u.ID == activity.UserID);
     if(activity.Type == Util.Enum.Activities.UserRegistration)
     {
         html = "<p><a href=\"/User/Show/" + activity.UserID + "/\">" + user.userName + "</a> hat sich registriert.</p>";
     }
     else if(activity.Type == Enum.Activities.WorkoutCreated)
     {
         html = "<p><a href=\"/User/Show/" + activity.UserID + "/\">" + user.userName + "</a> hat einen neuen <a href=\"/Workout/Show/" + activity.TargetID + "/\">Trainingsplan</a> erstellt.</p>";
     }
     else if(activity.Type == Enum.Activities.WorkoutLogged)
     {
         html = "<p><a href=\"/User/Show/" + activity.UserID + "/\">" + user.userName + "</a> hat eine <a href=\"/Session/Show/" + activity.TargetID + "/\">Trainingseinheit</a> absolviert.</p>";
     }
     html += "<p><small>" + activity.CRDT.GetRelativeTime() + "</small></p>";
     return html;
 }
示例#6
0
        /// <summary>
        /// constructor that creates a user object identified by the data from the user with the given email-password-combination
        /// </summary>
        /// <param name="email">email address the user uses to login</param>
        /// <param name="password">users password to login</param>
        /// <returns>the user object if password/email match, otherwise null</returns>
        public UserModel(string email, string password)
        {
            var service = new UtilityService();
            string hashedPassword = service.GetMd5Hash(password);
            ml_User user = null;
            using (MuscleLogContext context = new MuscleLogContext())
            {
                user = context.ml_User.Where(u => u.userEmail == email && u.userPassword == hashedPassword).FirstOrDefault();
            }

            if(user != null) {
                // set values if user/password combination is correct
                userEmail = user.userEmail;
                userName = user.userName;
                userStatus = (Util.Enum.UserStatus)(user.userStatus ?? 100);
                _userID = user.ID;
            } else {
                // otherwise set UserID to -1 and indicate a non-existant user
                _userID = -1;
            }
        }
示例#7
0
        public void ExerciseAccessTest()
        {
            MuscleLogContext ctx = new MuscleLogContext("data source=localhost;Initial Catalog=muscle-log;User ID=Max;integrated security=SSPI;multipleactiveresultsets=True;application name=EntityFramework");
            TestUserService service = new TestUserService(3);

            // check admin's rights
            ml_User user = new ml_User();
            user.userStatus = (int)muscle_log.framework.Util.Enum.UserStatus.Admin;

            Assert.AreEqual(muscle_log.framework.Util.Enum.Rights.ReadWrite, service.ExerciseAccess(user, 5));
            Assert.AreEqual(muscle_log.framework.Util.Enum.Rights.ReadWrite, service.ExerciseAccess(user, 1));

            // non-admin
            user.userStatus = (int)muscle_log.framework.Util.Enum.UserStatus.DefaultUser;
            Assert.AreEqual(muscle_log.framework.Util.Enum.Rights.ReadOnly, service.ExerciseAccess(user, 5));
            Assert.AreEqual(muscle_log.framework.Util.Enum.Rights.ReadWrite, service.ExerciseAccess(user, 1));

            // deleted user
            user.userStatus = (int)muscle_log.framework.Util.Enum.UserStatus.Deleted;
            Assert.AreEqual(muscle_log.framework.Util.Enum.Rights.ReadOnly, service.ExerciseAccess(user, 5));
            Assert.AreEqual(muscle_log.framework.Util.Enum.Rights.ReadWrite, service.ExerciseAccess(user, 1));
        }
示例#8
0
        public void Update( )
        {
            muscle_log.framework.Models.MuscleLogContext ctx = new muscle_log.framework.Models.MuscleLogContext("metadata=res://*/Models.MuscleLogModel.csdl|res://*/Models.MuscleLogModel.ssdl|res://*/Models.MuscleLogModel.msl;provider=System.Data.SqlClient;provider connection string='data source=MAX-PC;Initial Catalog=muscle-log;User ID=Max-PC;integrated security=SSPI;multipleactiveresultsets=True;application name=EntityFramework'");
            var unitOfWork = new EFContextUnitOfWork(ctx);
            var repo = new Repository<ml_ForumThread>(unitOfWork);
            ForumThreadService service = new ForumThreadService(repo);

            ml_ForumThread model = service.GetThread(6);

            int beforeTraining = service.ListThreads(muscle_log.framework.Util.Enum.Forums.Training).Count();
            int beforeMeta = service.ListThreads(muscle_log.framework.Util.Enum.Forums.Meta).Count();
            model.Forum = muscle_log.framework.Util.Enum.Forums.Training.ToString();
            service.Save(model);

            ml_ForumThread modelAfterChange = repo.Get(model.ID);
            int afterTraining = service.ListThreads(muscle_log.framework.Util.Enum.Forums.Training).Count();
            int afterMeta = service.ListThreads(muscle_log.framework.Util.Enum.Forums.Meta).Count();

            Assert.AreNotEqual(afterTraining, beforeTraining);
            Assert.AreNotEqual(afterMeta, beforeMeta);
            Assert.AreNotEqual(model.LUDT, modelAfterChange.LUDT);

            unitOfWork.Dispose();
        }
示例#9
0
 public void PropertyNotFound()
 {
     MuscleLogContext ctx = new MuscleLogContext("metadata=res://*/Models.MuscleLogModel.csdl|res://*/Models.MuscleLogModel.ssdl|res://*/Models.MuscleLogModel.msl;provider=System.Data.SqlClient;provider connection string='data source=MAX-PC;Initial Catalog=muscle-log;User ID=Max-PC;integrated security=SSPI;multipleactiveresultsets=True;application name=EntityFramework'");
     Assert.Throws(typeof(SettingsNotFoundException), delegate { muscle_log.Config.GetIntVal("missingProperty", ctx); });
     ctx.Dispose();
 }
示例#10
0
        public void Setup()
        {
            _context = new MuscleLogContext("metadata=res://*/Models.MuscleLogModel.csdl|res://*/Models.MuscleLogModel.ssdl|res://*/Models.MuscleLogModel.msl;provider=System.Data.SqlClient;provider connection string='data source=MAX-PC;Initial Catalog=muscle-log;User ID=Max-PC;integrated security=SSPI;multipleactiveresultsets=True;application name=EntityFramework'");
            _unitOfWork = new EFContextUnitOfWork(_context);
            _repository = new Repository<ml_Bugreport>(_unitOfWork);

            _addedEntities = new List<ml_Bugreport>();
            _deletedEntities = new List<ml_Bugreport>();
            _updatedEntities = new List<ml_Bugreport>();
        }
        public ActionResult Create(FormCollection formData)
        {
            RedirectIfNotLoggedIn();

            bool isValid = true;

            // Anzahl der Trainingseinheiten validieren
            int numWorkouts = -1;
            if (!Int32.TryParse(formData["numWorkouts"], out numWorkouts) || numWorkouts < 1 || numWorkouts > 7)
            {
                ModelState.AddModelError("numWorkouts", "Die Anzahl der Trainingseinheiten ist nicht gültig.");
                isValid = false;
            }

            // Trainingsplan-Name validieren
            string workoutName = formData["workoutName"] ?? "";
            if (String.IsNullOrEmpty(workoutName))
            {
                ModelState.AddModelError("workoutName", "Bitte gib einen gültigen Namen für den Trainingsplan an.");
                isValid = false;
            }

            // einzelne Trainingseinheiten validieren
            Dictionary<int, IEnumerable<ml_WorkoutDay>> daysOfWorkout = null;
            try
            {
                daysOfWorkout = _workoutDayService.GetDaysOfWorkout(formData, numWorkouts);
                if (daysOfWorkout == null || daysOfWorkout.Count == 0)
                {
                    ModelState.AddModelError("noWorkouts", "Es wurden keine gültigen Trainingseinheiten erstellt.");
                    isValid = false;
                }
                else
                {
                    int workoutIndex = 0;
                    foreach (var singleDayOfWorkout in daysOfWorkout)
                    {
                        workoutIndex++;
                        if (singleDayOfWorkout.Value == null)
                        {
                            ModelState.AddModelError("day-" + workoutIndex, "Die Angaben für Trainingseinheit " + workoutIndex + " sind nicht vollständig.");
                            isValid = false;
                        }
                    }
                }
            }
            catch (muscle_log.framework.Exceptions.WorkoutDayException e)
            {
                ModelState.AddModelError("day-" + e.Day, e.Message);
                isValid = false;
            }

            // wenn Validierung keine Fehler gefunden hat, dann koennen jetzt der Plan und die Einheiten gespeichert werden..
            if (isValid)
            {
                MuscleLogContext ctx = new MuscleLogContext();
                // WorkoutModel erstellen
                ml_WorkoutPlan workoutModel = new ml_WorkoutPlan();
                workoutModel.PlanName = workoutName;
                workoutModel.NumWorkouts = (byte)numWorkouts;
                workoutModel.Public = (byte)(((formData["workoutPublic"] ?? "").ToString() == "on") ? 1 : 0);
                workoutModel.CRDT = DateTime.Now;
                workoutModel.LUDT = DateTime.Now;
                workoutModel.UserID = CurrentUser.ID;
                workoutModel.Deleted = 0;
                _workoutService.SaveWorkout(workoutModel);

                // WorkoutDayModels speichern
                _workoutDayService.SaveDaysOfWorkout(daysOfWorkout, workoutModel.ID);
                _unitOfWork.SaveChanges();

                var workouts = _workoutService.List(CurrentUser.ID);

                var pager = new Pager(workouts);
                pager.DisplayedPages = 5;
                pager.PageSize = 10; // TODO: retrieve from DB.
                pager.StartIndex = 0;
                ViewData["WorkoutPageSize"] = pager.PageSize;
                ViewData["WorkoutCount"] = pager.Count;
                ViewData["WorkoutPagesCount"] = pager.PageCount;
                ViewData["ActivePage"] = pager.GetActivePage();
                ViewData["FirstPage"] = pager.StartPage;
                ViewData["LastPage"] = Math.Min(pager.StartPage + pager.DisplayedPages, pager.PageCount);

                ViewData["success"] = "Der Trainingsplan wurde erstellt.";
                return View("List", workouts);
            }

            return View();
        }
示例#12
0
        public ActionResult SaveUserTraining(UserTrainingSettings model)
        {
            // Formular-Daten validieren
            if (ModelState.IsValid)
            {

                // Daten fuer CurrentUser speichern
                MuscleLogContext ctx = new MuscleLogContext();
                CurrentUser.ml_UserTraining.ElementAt(0).WorkoutSince = model.WorkoutSince;
                CurrentUser.ml_UserTraining.ElementAt(0).Achievements = model.Achievements;
                CurrentUser.ml_UserTraining.ElementAt(0).Targets = model.Targets;
                _service.Save(CurrentUser);
                _unitOfWork.SaveChanges();

            }

            return View("Settings");
        }
示例#13
0
        public ActionResult SaveUserPrivacy(UserPrivacySettings model)
        {
            // Formular-Daten validieren
            if (ModelState.IsValid)
            {

                // Daten fuer CurrentUser speichern
                MuscleLogContext ctx = new MuscleLogContext();
                CurrentUser.ml_UserPrivacy.ElementAt(0).NotifyPrivateMessage = model.NotifyPrivateMessage;
                CurrentUser.ml_UserPrivacy.ElementAt(0).NotifyWorkoutComment = model.NotifyWorkoutComment;
                CurrentUser.ml_UserPrivacy.ElementAt(0).NotifyVote = model.NotifyVote;
                _service.Save(CurrentUser);
                _unitOfWork.SaveChanges();
            }

            return View("Settings");
        }
示例#14
0
        /// <summary>
        /// saves the current user with all its values in the database
        /// </summary>
        public void Save()
        {
            var user = new ml_User();
            user.ID = ID;
            user.userStatus = (int)userStatus;
            user.userName = userName;
            user.userEmail = userEmail;

            using (var context = new MuscleLogContext())
            {
                if (user.ID > 0)
                {
                    context.ml_User.Attach(user);
                    context.Entry(user).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    context.ml_User.Add(user);
                }
                context.SaveChanges();
            }
        }