public async Task <bool> AddWod(Wod wod) { _context.Wods.Add(wod); var i = await _context.SaveChangesAsync().ConfigureAwait(false); return(i > 0); }
public async Task <IActionResult> Post([FromBody] WodRequest request) { try { Wod wod = new Wod() { Date = request.Date.Value, Name = request.Name, Description = request.Description, Workouts = request.WorkoutRequests.Select(x => new Workout() { Description = x.Description, ResulttypeId = x.ResulttypeId, WorkouttypeId = x.Workouttype.Id }).ToList() }; if (request.Wod != null) { await _wodService.Delete(request.Wod); } wod = await _wodService.Add(wod); request.IsSaved = wod != null; return(Ok(request)); } catch (Exception ex) { _logger.LogError($"Post error: {ex.Message} - Name: {request.Name}"); return(BadRequest()); } }
// --------------------------------------------------------- // Eventhandler for Delete button // --------------------------------------------------------- private void btnDeleteWod_Click(object sender, RoutedEventArgs e) { try { selectedWod = dgCoachGrid.SelectedItem as Wod; ViewModel.RemoveWodFromAthlete(selectedWod.idWod, (int)selectedWod.idAthlete); // Method is called to remove relation between movement and athlete if (selectedWod != null) { selectedWod.idAthlete = 0; // if wod is not selected, set wods idAthlete to 0 tbMessage.Text = $"Movement {selectedWod.movementName} Deleted on date {DateTime.Today.ToString("dd.MM.yyyy")}"; // Update bottom infomessage row cbMovementName.Text = ""; // empty textboxes after delete tbReps.Text = ""; tbRounds.Text = ""; tbComment.Text = ""; } cbMovementName.Text = ""; // empty textboxes after delete tbReps.Text = ""; tbRounds.Text = ""; tbComment.Text = ""; dgCoachGrid.ItemsSource = ViewModel.LoadWodsByAthlete(selected, dateTime); // update datagrid tbMessage.Text = $"Select a movement to delete"; // Update bottom infomessage row } catch (SystemException) { MessageBox.Show("No Movement selected or Movement is in use and can't be deleted", "Oops!", MessageBoxButton.OK, MessageBoxImage.Exclamation); } catch (Exception) { MessageBox.Show("Something isn't right...", "Oops!", MessageBoxButton.OK, MessageBoxImage.Exclamation); } }
// --------------------------------------------------------- // COACHMAIN: Method adds WOD to athlete to certain date // --------------------------------------------------------- public static void AddWodToAthlete(int WodId, int athleteId, DateTime dateTime, string movement, int reps, int rounds, string comment, int level) { using (var ctx = new WODCoachEntities()) { // if value sent in property wodId is 0 it means nothing was selected from datagrid if (WodId == 0) // insert { Wod wod = new Wod(); wod.movementName = movement; wod.date = dateTime; wod.idAthlete = athleteId; wod.repsCount = reps; wod.roundCount = rounds; wod.comment = comment; wod.level = level; wod.done = false; ctx.Wod.Add(wod); } else // if value sent with wodId is not 0 then search for Wod with Id { Wod wod = ctx.Wod.First(i => i.idWod == WodId); wod.movementName = movement; wod.date = dateTime; wod.idAthlete = athleteId; wod.repsCount = reps; wod.roundCount = rounds; wod.comment = comment; wod.level = level; } ctx.SaveChanges(); } }
// --------------------------------------------------------- // Updates values in textboxes when selection is changed // --------------------------------------------------------- private void dgCoachGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { if (dgCoachGrid.SelectedIndex > -1) // if item is selected from datagrid { selectedWod = dgCoachGrid.SelectedItem as Wod; // WOD selection cbMovementName.SelectedItem = selectedWod; cbMovementName.Text = selectedWod.movementName; // update selectedWod properties to textboxea tbComment.Text = selectedWod.comment; tbReps.Text = Convert.ToString(selectedWod.repsCount); tbRounds.Text = Convert.ToString(selectedWod.roundCount); starsLevel.Value = (int)selectedWod.level; selectedWod.Athlete = (Athlete)cbAthleteName.SelectedItem; string message = $"Movement no. {selectedWod.idWod} of athlete {selectedWod.Athlete.fullname} chosen"; tbMessage.Text = message; // Update bottom infomessage row selectedAthlete = selectedWod.Athlete; // update selected Athlete } } catch (Exception) { MessageBox.Show("Something isn't right...", "Oops!", MessageBoxButton.OK, MessageBoxImage.Exclamation); } }
// --------------------------------------------------------- // Athlete Datagrid selection changed // --------------------------------------------------------- private void dgAthleteGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { if (dgAthleteGrid.SelectedIndex > -1) // selected index is in datagrid { selectedWod = dgAthleteGrid.SelectedItem as Wod; // casting selected item from datagrid as Wod string message = $"Movement {selectedWod.idWod} of athlete {selectedWod.Athlete.fullname} chosen"; tbMessage.Text = message; // Update bottom message row tbRatedMovement.Text = selectedWod.movementName; if (selectedWod.Rate.FirstOrDefault() == null) // check if selected wod has Rating { tbRatingComment.Text = ""; // if not set comment and value empty slider.Value = 0; } else { rating = selectedWod.Rate.FirstOrDefault(i => i.athlete_id == selectedWod.idAthlete); // Rating that is made for selected Wod tbRatingComment.Text = rating.comment; // update textbox and slider with selected wod rating values slider.Value = (float)rating.rating; } } } catch (Exception) { MessageBox.Show("Something isn't right...", "Oops!", MessageBoxButton.OK, MessageBoxImage.Exclamation); } }
// --------------------------------------------------------- // SAVE button Eventhandler // --------------------------------------------------------- private void btnSave_Click(object sender, RoutedEventArgs e) { try { if (selectedWod == null) // if wod (movement) is not selected => new wod { selectedWod = new Wod(); // create new movement (wod) selectedWod.movementName = tbMovement.Text; selectedWod.level = starsLevel.Value; ViewModel.SaveWod(0, selectedWod); // call method to save movement to entity model and database } else // if not new then selected wod (movement) is modified { selectedWod.movementName = tbMovement.Text; selectedWod.level = starsLevel.Value; ViewModel.SaveWod(selectedWod.idWod, selectedWod); // call method to save movement to entity model and database } tbMovement.Text = ""; // empty textboxes after create / modify starsLevel.Value = 0; dgMovementGrid.ItemsSource = ViewModel.LoadWods(); // update datagrid tbMessage.Text = $"Movement {selectedWod.movementName} saved on date {DateTime.Today.ToString("dd.MM.yyyy")}"; // update message to bottom inforow } catch (Exception) { MessageBox.Show("Something isn't right...", "Oops!", MessageBoxButton.OK, MessageBoxImage.Exclamation); } }
// --------------------------------------------------------- // ATHLETEMAIN: Saves the state of wod (movement), done (true) or not done (false). // --------------------------------------------------------- public static void SaveDoneWod(Wod selectedWod) { using (var ctx = new WODCoachEntities()) { Wod wod = ctx.Wod.First(i => i.idWod == selectedWod.idWod); wod.done = selectedWod.done; ctx.SaveChanges(); } }
// --------------------------------------------------------- // COACHMAIN: Removes connection from between a wod (movement) and athlete. Nothing is deleted. Used in CoachMain. // --------------------------------------------------------- public static void RemoveWodFromAthlete(int wodId, int athlete_id) { using (var ctx = new WODCoachEntities()) { Wod wod = ctx.Wod.First(i => i.idWod == wodId); wod.idAthlete = null; wod.repsCount = 0; wod.roundCount = 0; ctx.SaveChanges(); } }
// --------------------------------------------------------- // MOVEMENTMAIN: Delete Wod (Movement) identified by wodId // --------------------------------------------------------- public static void DeleteWod(int wodId) { using (var ctx = new WODCoachEntities()) { Wod wod = ctx.Wod.First(i => i.idWod == wodId); if (wod.Rate.Count > 0) { Rate rate = ctx.Rate.First(i => i.wod_id == wodId); ctx.Rate.Remove(rate); } ctx.Wod.Remove(wod); ctx.SaveChanges(); } }
public async Task <IActionResult> Delete(int WodId) { try { Wod wod = await _wodService.Get(WodId); return(Ok(await _wodService.Delete(wod))); } catch (Exception ex) { _logger.LogError($"Delete error: {ex.Message} - WodId: {WodId}"); return(BadRequest()); } }
// --------------------------------------------------------- // CLEAR SELECTION button Eventhandler // --------------------------------------------------------- private void btnClear_Click(object sender, RoutedEventArgs e) { try { dgMovementGrid.UnselectAll(); // unselect datagrid selectedWod = null; // set Athlete selection to null tbMovement.Text = ""; starsLevel.Value = 0; tbMessage.Text = "Selection cleared"; // update message to bottom inforow } catch (Exception) { MessageBox.Show("Something isn't right...", "Oops!", MessageBoxButton.OK, MessageBoxImage.Exclamation); } }
public async Task <Wod> Add(Wod wod) { try { await _context.Wods.AddAsync(wod); await _context.SaveChangesAsync(); return(wod); } catch (Exception ex) { _logger.LogError($"Add() {ex.Message}"); return(null); } }
public async Task <bool> Delete(Wod wod) { try { if (wod == null) { return(false); } _context.Wods.Remove(wod); await _context.SaveChangesAsync(); return(true); } catch (Exception ex) { _logger.LogError($"Delete() {ex.Message}"); return(false); } }
// --------------------------------------------------------- // Datagrid selection changed Eventhandler // --------------------------------------------------------- private void dgAthleteGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { if (dgMovementGrid.SelectedIndex > -1) // if something is seleceted from datagrid { selectedWod = dgMovementGrid.SelectedItem as Wod; // set datagrid selected item to selectedWod -object tbMovement.Text = selectedWod.movementName; // Movement textbox text update tbMessage.Text = $"Movement {selectedWod.idWod} selected"; // update message to bottom inforow starsLevel.Value = Convert.ToInt32(selectedWod.level); // update stars value update } dgMovementGrid.DataContext = ViewModel.LoadWods(); // update datagrid tbMessage.Text = $"Movement {selectedWod.idWod} - {selectedWod.movementName} selected"; // update message to bottom inforow } catch (Exception) { MessageBox.Show("Something isn't right...", "Oops!", MessageBoxButton.OK, MessageBoxImage.Exclamation); } }
// --------------------------------------------------------- // CLEAR SELECTION button Eventhandler // --------------------------------------------------------- private void btnClear_Click(object sender, RoutedEventArgs e) { try { selectedWod = null; dgCoachGrid.UnselectAll(); // unselect datagrid tbComment.Text = ""; tbReps.Text = ""; tbRounds.Text = ""; starsLevel.Value = 0; cbMovementName.Text = ""; cbMovementName.SelectedItem = null; tbMessage.Text = "Selection cleared"; // update message to bottom inforow } catch (Exception) { MessageBox.Show("Something isn't right...", "Oops!", MessageBoxButton.OK, MessageBoxImage.Exclamation); } }
// --------------------------------------------------------- // Updates difficulty star value when Movement selection is changed // --------------------------------------------------------- private void cbMovementName_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { if (cbMovementName.SelectedIndex > -1) { if (dgCoachGrid.SelectedIndex < 0) { selectedWod = (Wod)cbMovementName.SelectedItem; } tbComment.Text = selectedWod.comment; starsLevel.Value = (int)selectedWod.level; tbReps.Text = ""; tbRounds.Text = ""; tbComment.Text = ""; } } catch (Exception) { MessageBox.Show("Something isn't right...", "Oops!", MessageBoxButton.OK, MessageBoxImage.Exclamation); } }
// --------------------------------------------------------- // MOVEMENTMAIN: Add or update Wods (movements) // --------------------------------------------------------- public static void SaveWod(int number, Wod selectedWod) { using (var ctx = new WODCoachEntities()) { if (number == 0) { Wod wod = new Wod(); wod.movementName = selectedWod.movementName; wod.level = selectedWod.level; wod.done = false; ctx.Wod.Add(wod); ctx.SaveChanges(); } else { Wod wod = ctx.Wod.First(i => i.idWod == number); wod.movementName = selectedWod.movementName; wod.level = selectedWod.level; wod.done = selectedWod.done; ctx.SaveChanges(); } } }
public async Task <IActionResult> Add(string date = null) { try { DateTime Date; if (String.IsNullOrWhiteSpace(date) || !DateTime.TryParse(date, out Date) || Date == DateTime.MinValue) { Date = DateTime.Now; } WodRequest request = new WodRequest() { Date = Date, Name = $"WOD-{Date.ToString("yyyyMMdd")}", Workouttypes = await _workouttypesService.GetWorkouttypes(), Resulttypes = await _resulttypeService.GetResulttypes() }; Wod wod = await _wodService.GetWodByDate(Date); if (wod == null) { foreach (var workouttype in request.Workouttypes.Where(x => x.IsAutomatic == true)) { if (request.WorkoutRequests == null) { request.WorkoutRequests = new List <WorkoutRequest>(); } request.WorkoutRequests.Add(new WorkoutRequest { Workouttype = workouttype }); } } else { request.Name = wod.Name; request.Description = wod.Description; request.Wod = wod; foreach (var workout in wod.Workouts) { if (request.WorkoutRequests == null) { request.WorkoutRequests = new List <WorkoutRequest>(); } request.WorkoutRequests.Add(new WorkoutRequest { Workouttype = request.Workouttypes.Where(x => x.Id == workout.WorkouttypeId).FirstOrDefault(), Description = workout.Description, ResulttypeId = workout.ResulttypeId }); } } return(Ok(request)); } catch (Exception ex) { _logger.LogError($"Get error: {ex.Message}"); return(BadRequest()); } }