private void UpdateActivityInDB()
        {
            var duration = parseDuration();

            //If you don't do this, it will create a new ActivityType with every new Activity (Weird WinForms thing)
            var selectedActivityType = activityTypeDropDown.SelectedItem as ActivityType;
            var selectedActivityName = selectedActivityType.ActivityName.ToString();
            var currentActivityType  = _context.ActivityTypes.Single(t => t.ActivityName == selectedActivityName);

            string notes = notesTextBox.Text.Replace(Environment.NewLine, "~");

            double distance = double.TryParse(distanceTextBox.Text, out distance) ?
                              distance = double.Parse(distanceTextBox.Text) : 0;


            //Find and update current activity in the db
            var currentAct = _context.Activities.Find(_activity.Id);

            currentAct.Title            = activityTitleTextBox.Text;
            currentAct.ActivityDuration = duration;
            currentAct.ActivityDistance = distance;
            currentAct.ActivityType     = currentActivityType;
            currentAct.ActivityStart    = dateDateTimeChooser.Value;
            currentAct.ActivityNotes    = notes;

            _context.SaveChanges();
        }
示例#2
0
        private void createProfileButton_Click(object sender, EventArgs e)
        {
            if (!IsValidated())
            {
                return;
            }

            //add new user and set properties
            var newUser = new User
            {
                Username     = newUsernameTextBox.Text,
                FirstName    = firstNameTextBox.Text,
                LastName     = lastNameTextBox.Text,
                EmailAddress = emailTextBox.Text,
                Location     = locationTextBox.Text
            };

            _context.Users.Add(newUser);
            _context.SaveChanges();


            this.Hide();

            Form newActivitiesListForm = new ActivitiesListForm(newUser);

            newActivitiesListForm.Show();
        }
        private void AddNewActivityToDB()
        {
            var duration = ParseDuration();

            var currentUser = _context.Users.Single(u => u.Username == _username);

            //If you don't do this, it will create a new ActivityType with every new Activity (Weird WinForms thing)
            var selectedActivityType = activityTypeDropDown.SelectedItem as ActivityType;
            var selectedActivityName = selectedActivityType.ActivityName.ToString();
            var currentActivityType  = _context.ActivityTypes.Single(t => t.ActivityName == selectedActivityName);

            string notes = notesTextBox.Text.Replace(Environment.NewLine, "~");

            double distance = double.TryParse(distanceTextBox.Text, out distance) ?
                              double.Parse(distanceTextBox.Text) : 0;

            //Create new activity and save to db
            var newActivity = new Activity
            {
                Title            = activityTitleTextBox.Text,
                ActivityType     = currentActivityType,
                ActivityStart    = dateDateTimeChooser.Value,
                ActivityDuration = duration,
                ActivityDistance = distance,
                ActivityNotes    = notes,
                User             = currentUser
            };

            _context.Activities.Add(newActivity);
            _context.SaveChanges();
        }
示例#4
0
        public Result CreateUser(FitnessContext context, HttpRequest request)
        {
            Result result = new Result();
            var    user   = new User
            {
                FirstName  = request.Form["firstname"],
                LastName   = request.Form["lastname"],
                Age        = int.Parse(request.Form["age"]),
                Email      = request.Form["email"],
                Password   = request.Form["password"],
                Activities = new System.Collections.Generic.List <Activity>()
            };

            if (context.Users.Where(o => o.Email == user.Email).Count() > 0)
            {
                result.Success = false;
                result.ErrorMessages.Add("Email address already in use.");
            }
            else
            {
                context.Users.Add(user);
                context.SaveChanges();
                result.Success = true;
            }
            return(result);
        }
        private void AddActivityTypeToDB()
        {
            var newActivity = new ActivityType();

            newActivity.ActivityName = addActivityNameTextBox.Text;

            _context.ActivityTypes.Add(newActivity);
            _context.SaveChanges();
        }