public ActionResult NewUser() { //first we register the user //make sure the user does not exist var user = User.GetApplicationUser(); if (user == null) { //TODO: move this behind a repository or something using (var db = new LiftBroContext()) { //get the users name somehow db.Users.Add( new User() { Username = User.Identity.Name, Name = User.Identity.Name }); db.SaveChanges(); } } return View(); }
public static LiftBro.Model.User GetApplicationUser(this IPrincipal principal) { using (var db = new LiftBroContext()) { return db.Users.FirstOrDefault(user => user.Username == principal.Identity.Name); } }
public IEnumerable<Exercise> GetAll() { using (var db = new LiftBroContext()) { return db.Exercises.ToList(); } }
public void Delete(Guid id) { using (var db = new LiftBroContext()) { var set = db.Sets.Find(id); db.Sets.Remove(set); db.SaveChanges(); } }
public void Delete(Guid id) { using (var db = new LiftBroContext()) { var weight = db.UserWeights.Find(id); db.UserWeights.Remove(weight); db.SaveChanges(); } }
public void Delete(Guid id) { using (var db = new LiftBroContext()) { var exercise = db.WorkoutExercises.Find(id); db.WorkoutExercises.Remove(exercise); db.SaveChanges(); } }
public void Create(CreateSet update) { using (var db = new LiftBroContext()) { db.WorkoutExercises.Attach(update.Exercise); update.Exercise.Sets.Add(update.Set); db.SaveChanges(); } }
public void Update(WorkoutExercise exercise) { using (var db = new LiftBroContext()) { db.WorkoutExercises.Attach(exercise); db.ChangeTracker.Entries<WorkoutExercise>().First(e => e.Entity == exercise) .State = EntityState.Modified; db.SaveChanges(); } }
public void Update(Set set) { using (var db = new LiftBroContext()) { db.Sets.Attach(set); db.ChangeTracker.Entries<Set>().First(e => e.Entity == set) .State = EntityState.Modified; db.SaveChanges(); } }
public IEnumerable<UserWeight> GetAll() { using (var db = new LiftBroContext()) { var currentUser = User.Identity.Name; return db.UserWeights .Include("User") .Where(weight => weight.User.Username == currentUser).ToList(); } }
public void Create(Exercise exercise) { using (var db = new LiftBroContext()) { var user = User.GetApplicationUser(); db.Users.Attach(user); exercise.Creator = user; db.Exercises.Add(exercise); db.SaveChanges(); } }
public void Create(CreateWorkoutExercise update) { using (var db = new LiftBroContext()) { var workoutDay = db.WorkoutDays .Include(day => day.Exercises).FirstOrDefault(day => day.Id == update.WorkoutDay.Id); //db.WorkoutDays.Attach(update.WorkoutDay); db.Exercises.Attach(update.Exercise.Exercise); workoutDay.Exercises.Add(update.Exercise); db.SaveChanges(); } }
public ActionResult Weight() { //TODO: check if the user has any registered programs using (var db = new LiftBroContext()) { if (!db.UserPrograms.Any(program => program.User.Username == User.Identity.Name)) { return Redirect("/Program/NewUser"); } } return View(); }
public void Create(CreateWorkoutDay update) { using (var db = new LiftBroContext()) { var program = db.Programs.Include(pr => pr.WorkoutDays) .FirstOrDefault(program1 => program1.Id == update.Program.Id); if (program == null) throw new Exception("Program not found"); program.WorkoutDays.Add(update.WorkoutDay); db.SaveChanges(); } }
public void CompleteWorkout(WorkoutDay workoutDay) { using (var db = new LiftBroContext()) { //TODO: use authenticated user User currentUser = db.Users.First(); //TOOD:unecessary attach? db.Users.Attach(currentUser); UserProgram currentUserProgram = db.UserPrograms .Include(userProgram => userProgram.Program.WorkoutDays) .Include(userProgram => userProgram.User) .FirstOrDefault(program => program.User.Username == User.Identity.Name && program.CurrentlyUsing); int nextOrder = workoutDay.Order + 1; WorkoutDay nextWorkoutDay = currentUserProgram.Program.WorkoutDays.FirstOrDefault(day => day.Order == nextOrder); //Program currentProgram = db.Programs // .Include(program => program.WorkoutDays) // .FirstOrDefault(program => // program.Id == currentUserProgram.Program.Id); if (nextWorkoutDay == null) nextWorkoutDay = currentUserProgram.Program.WorkoutDays.OrderBy(day => day.Order).First(); var attached = db.WorkoutDays.Find(nextWorkoutDay.Id); //attached.CurrentWorkoutDay = true; currentUserProgram.NextWorkout = attached; //nextWorkoutDay.CurrentWorkoutDay = true; db.CompletedWorkouts.Add(new CompletedWorkoutDay() { User = currentUser, When = DateTime.Now, Workout = db.WorkoutDays.FirstOrDefault(day => day.Id == workoutDay.Id) }); db.SaveChanges(); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { LiftBroContext context = new LiftBroContext(); context.Database.EnsureCreated(); if (!context.Users.Any()) { LiftBroDBInitializer.Seed(context);//<---Do your seeding here } services.AddDbContext <LiftBroContext>(); services.AddScoped <IUserReposetory>(s => new UserReposetory(context)); services.AddScoped <IWorkoutReposetory>(s => new WorkoutReposetory(context)); services.AddScoped <IExerciseReposetory>(s => new ExerciseReposetory(context)); services.AddScoped <IActivityReposetory>(s => new ActivityReposetory(context)); services.AddMvc(); }
public UserWeight Register(double amount) { var weight = new UserWeight { Weight = amount, Id = Guid.NewGuid(), User = User.GetApplicationUser() }; if (weight.When.Equals(default(DateTime))) weight.When = DateTime.Now; using (var db = new LiftBroContext()) { db.Users.Attach(weight.User); db.UserWeights.Add(weight); db.SaveChanges(); } return weight; }
public void Deserialize_seed_data() { //var file = File.Open("seedData.json", FileMode.Open); using (var db = new LiftBroContext()) { SeedData data = JsonConvert.DeserializeObject<SeedData>(File.ReadAllText("seedData.json")); db.UserPrograms.AddOrUpdate(data.UserPrograms.First()); db.SaveChanges(); var exercise = new UserExercise() { Exercise = db.Exercises.First(), Id = new Guid(), OneRepetitionMax = 44, User = db.Users.First() }; db.UserExercises.AddOrUpdate(exercise); db.SaveChanges(); } }
public static void Seed(LiftBroContext context) { //context.Database.EnsureDeleted(); //context.Database.EnsureCreated(); var LiftSeed = new LiftBroSeed(context); //context.Database.EnsureDeleted(); //context.Database.EnsureCreated(); //LiftBroSeed = new LiftBroSeed(); ////context.Database.EnsureDeleted(); ////I'm bombing here ////LiftBroContext context = applicationBuilder.ApplicationServices.GetRequiredService<LiftBroContext>(); ////SetupActivitys(context); ////SetupExercises(context); ////SetupWorkout(context); //if (!context.Users.Any()) //{ // SetupUsers(context); //} }
public Guid Log(WorkoutExerciseLog log) { using (var db = new LiftBroContext()) { log.When = DateTime.Now; db.WorkoutExercises.Attach(log.WorkoutExercise); //update an already saved one if (!log.Id.Equals(Guid.Empty)) { db.WorkoutExerciseLogs.Attach(log); db.Entry(log).State = EntityState.Modified; } else { db.WorkoutExerciseLogs.Add(log); log.Id = Guid.NewGuid(); } db.SaveChanges(); return log.Id; } }
public void UpdateUserExercise(UserExercise userExercise) { if (userExercise.User == null) userExercise.User = User.GetApplicationUser(); if (userExercise.Exercise == null) throw new Exception("Exercise but be set"); using (var db = new LiftBroContext()) { db.Exercises.Attach(userExercise.Exercise); var foundExercise = db.UserExercises //.Include(exc => exc.) .FirstOrDefault(exercise => exercise.User.Username == userExercise.User.Username && exercise.Exercise.Name == userExercise.Exercise.Name); if (foundExercise != null) { foundExercise.OneRepetitionMax = userExercise.OneRepetitionMax; } else { db.UserExercises.Add(new UserExercise() { User = db.Users.Find(userExercise.User.Username), Exercise = userExercise.Exercise, Id = Guid.NewGuid(), OneRepetitionMax = userExercise.OneRepetitionMax }); } db.SaveChanges(); } }
public LiftBroSeed(LiftBroContext context) { //User user = new User() //{ // Name = "Sondre", Password = "******", // ID = 1,Workouts = new List<Workout>() // { // new Workout(){ // Name = "Workout 1", // ID = 1, // Exercises = new List<Exercise>(){ // new Exercise() // { // ID = 1,Repitations = 8,Sets = 3,Weight = 50,Activity = new Activity() { ID = 1,Name = "Bench press"} // }, // new Exercise() // { // ID = 2,Repitations = 8,Sets = 3,Weight = 50,Activity = new Activity() { ID = 2,Name = "Pushups"} // }, // new Exercise() // { // ID = 3,Repitations = 8,Sets = 3,Weight = 50,Activity = new Activity() { ID = 3,Name = "Squat"} // } // } // }, // new Workout(){ // Name = "Workout 2", // ID = 2, // Exercises = new List<Exercise>(){ // new Exercise() // { // ID = 4,Repitations = 8,Sets = 3,Weight = 50,Activity = new Activity() { ID = 4,Name = "Military press"} // }, // new Exercise() // { // ID = 5,Repitations = 8,Sets = 3,Weight = 50,Activity = new Activity() { ID = 5,Name = "Farmers Walk"} // }, // new Exercise() // { // ID = 6,Repitations = 8,Sets = 3,Weight = 50,Activity = new Activity() { ID = 6,Name = "Skiing"} // } // } // } // } //}; //context.Users.Add(user); //context.SaveChanges(); Activities = new List <Activity>() { new Activity() { ID = 1, Name = "Jump Squat" }, new Activity() { ID = 2, Name = "Squat" }, new Activity() { ID = 3, Name = "Pushup" }, new Activity() { ID = 4, Name = "Bench Press" }, new Activity() { ID = 5, Name = "Deadlift" }, new Activity() { ID = 6, Name = "Farmers walk" }, new Activity() { ID = 7, Name = "Arnold Press" }, new Activity() { ID = 8, Name = "Situps" } }; context.Activities.AddRange(Activities); context.SaveChanges(); Exercises = new List <Exercise>() { new Exercise() { ID = 1, Repitations = 8, Sets = 3, Weight = 20 }, new Exercise() { ID = 2, Repitations = 8, Sets = 6, Weight = 20 }, new Exercise() { ID = 3, Repitations = 12, Sets = 3, Weight = 40 }, new Exercise() { ID = 4, Repitations = 8, Sets = 1, Weight = 20 }, new Exercise() { ID = 5, Repitations = 3, Sets = 3, Weight = 60 }, new Exercise() { ID = 6, Repitations = 8, Sets = 2, Weight = 20 }, new Exercise() { ID = 7, Repitations = 2, Sets = 5, Weight = 220 }, new Exercise() { ID = 8, Repitations = 2, Sets = 5, Weight = 220 }, }; context.AddRange(Exercises); context.SaveChanges(); var Ex0 = context.Exercises.FirstOrDefault(x => x.ID == 1); Ex0.Activity = context.Activities.FirstOrDefault((x) => x.ID == 2); var Ex1 = context.Exercises.FirstOrDefault(x => x.ID == 2); Ex1.Activity = context.Activities.FirstOrDefault((x) => x.ID == 3); var Ex2 = context.Exercises.FirstOrDefault(x => x.ID == 2); Ex2.Activity = context.Activities.FirstOrDefault((x) => x.ID == 4); var Ex3 = context.Exercises.FirstOrDefault(x => x.ID == 3); Ex3.Activity = context.Activities.FirstOrDefault((x) => x.ID == 5); var Ex4 = context.Exercises.FirstOrDefault(x => x.ID == 4); Ex4.Activity = context.Activities.FirstOrDefault((x) => x.ID == 6); var Ex5 = context.Exercises.FirstOrDefault(x => x.ID == 5); Ex5.Activity = context.Activities.FirstOrDefault((x) => x.ID == 1); var Ex6 = context.Exercises.FirstOrDefault(x => x.ID == 6); Ex6.Activity = context.Activities.FirstOrDefault((x) => x.ID == 1); var Ex7 = context.Exercises.First(x => x.ID == 7); Ex7.Activity = context.Activities.FirstOrDefault((x) => x.ID == 3); context.Exercises.Update(Ex0); context.Exercises.Update(Ex1); context.Exercises.Update(Ex2); context.Exercises.Update(Ex3); context.Exercises.Update(Ex4); context.Exercises.Update(Ex5); context.Exercises.Update(Ex6); context.Exercises.Update(Ex7); context.SaveChanges(); Workouts = new List <Workout>() { new Workout() { Exercises = context.Exercises.Where((x) => x.ID == 1 || x.ID == 2 || x.ID == 4 || x.ID == 6).ToList(), Name = "Workout1" }, new Workout() { Exercises = context.Exercises.Where((x) => x.ID == 1 || x.ID == 2 || x.ID == 5 || x.ID == 7).ToList(), Name = "Workout2" }, new Workout() { Exercises = context.Exercises.Where((x) => x.ID == 1 || x.ID == 4 || x.ID == 5).ToList(), Name = "Workout4" }, new Workout() { Exercises = context.Exercises.Where((x) => x.ID == 7 || x.ID == 4 || x.ID == 1 || x.ID == 4).ToList(), Name = "Workout5" } }; context.Workouts.AddRange(Workouts); context.SaveChanges(); Users = new List <User> { new User() { Name = "Jostein", Password = "******", Workouts = context.Workouts.Where((x) => x.ID == 1).ToList() }, new User() { Name = "Einar", Password = "******", Workouts = context.Workouts.Where((x) => x.ID == 1 || x.ID == 1).ToList() }, new User() { Name = "Sondre", Password = "******", Workouts = context.Workouts.Where((x) => x.ID == 3 || x.ID == 1 || x.ID == 2).ToList() }, new User() { Name = "Peder", Password = "******", Workouts = new List <Workout>() } }; context.Users.AddRange(Users); context.SaveChanges(); }
public static void SetupActivitys(LiftBroContext context) { context.Activities.AddRange(LiftBroSeed.Activities); context.SaveChanges(); }
private static void SetupExercises(LiftBroContext context) { context.Exercises.AddRange(LiftBroSeed.Exercises); context.SaveChanges(); }
private static void SetupWorkout(LiftBroContext context) { context.Workouts.AddRange(LiftBroSeed.Workouts); context.SaveChanges(); }
private static void SetupUsers(LiftBroContext context) { context.Users.AddRange(LiftBroSeed.Users); context.SaveChanges(); }
public ActivityReposetory(LiftBroContext context) { this.context = context; }
public void Delete(Guid id) { using (var db = new LiftBroContext()) { var day = db.WorkoutDays.Find(id); var programs = db.UserPrograms .Include(program => program.NextWorkout) .Where(program => program.NextWorkout.Id == id).ToList(); programs.ForEach(program => program.NextWorkout = null); db.WorkoutDays.Remove(day); db.SaveChanges(); } }
public ExerciseReposetory(LiftBroContext context) { this.context = context; }
private User GetCurrentUser() { using (var db = new LiftBroContext()) return db.Users.FirstOrDefault(user => user.Username == User.Identity.Name); }
public void Update(WorkoutDay workoutDay) { using (var db = new LiftBroContext()) { db.WorkoutDays.Attach(workoutDay); db.ChangeTracker.Entries<WorkoutDay>().First(e => e.Entity == workoutDay) .State = EntityState.Modified; db.SaveChanges(); } }
public void SetNextWorkoutDay([FromBody]string id) { using (var db = new LiftBroContext()) { Guid guidId = Guid.Parse(id); UserProgram userProgram = db.UserPrograms .Include("User") //.Include("Program.WorkoutDays") .FirstOrDefault(program => program.User.Username == User.Identity.Name && program.CurrentlyUsing); WorkoutDay workoutDay = db.WorkoutDays.Find(guidId); userProgram.NextWorkout = workoutDay; db.SaveChanges(); } }
public IEnumerable<CompletedWorkoutDay> GetCompletedWorkouts(int take, int skip) { using (var db = new LiftBroContext()) { //TODO: use authenticated user User currentUser = db.Users.First(); return db.CompletedWorkouts.Where(day => day.User.Username == User.Identity.Name) .OrderByDescending(day => day.When) .Skip(skip) .Take(take) .Include(day => day.Workout) .ToList(); } }
public UserReposetory(LiftBroContext context) { this.context = context; }
public WorkoutReposetory(LiftBroContext context) { this.context = context; }
public WorkoutDay GetNextWorkoutDay() { using (var db = new LiftBroContext()) { //TODO: use authenticated user User currentUser = GetCurrentUser(); UserProgram currentUserProgram = db.UserPrograms .Include(userProgram => userProgram.NextWorkout.Exercises) .Include(userProgram => userProgram.User) .Include("Program") .Include("Program.WorkoutDays") .FirstOrDefault(program => program.User.Username == User.Identity.Name && program.CurrentlyUsing); WorkoutDay nextWorkoutDay = currentUserProgram.NextWorkout; if (nextWorkoutDay == null) { currentUserProgram.NextWorkout = currentUserProgram.Program.WorkoutDays .OrderBy(day => day.Order).FirstOrDefault(); nextWorkoutDay = currentUserProgram.NextWorkout; db.SaveChanges(); } //NOTE: seems there are no workouts. This means there workouts in the program if (nextWorkoutDay != null) { foreach (var workoutExercise in nextWorkoutDay.Exercises) { workoutExercise.Sets = db.WorkoutExercises .Include(we => we.Exercise) .Include(we => we.Sets) .FirstOrDefault(we => we.Id == workoutExercise.Id).Sets; } } return nextWorkoutDay; } }