/// <summary> /// Updates binding source /// </summary> public async void UpdateGridView() { Application.UseWaitCursor = true; currUser = Globals.CurrentUser; var repo = new HttpGroupsRepository(); groupsBindingSource.Clear(); foreach (var groupDetail in currUser.GroupDetails) { var group = await repo.GetOne(groupDetail.group_id); groupsBindingSource.Add(group); } groupsGridView.Update(); Application.UseWaitCursor = false; }
/// <summary> /// Saves and validates provided data. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void SaveChangesClick(object sender, EventArgs e) { IStringValidator validator = new Validator(); if (!validator.IsNotEmpty(tbTitle.Text)) { MessageBox.Show("Title can not be empty!"); return; } HttpGroupsRepository groupRepo = new HttpGroupsRepository(); currGroup.description = tbDesc.Text; if (!validator.IsNotEmpty(tbTitle.Text)) { MessageBox.Show("Group name can't be empty!","Error!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); return; } currGroup.name = tbTitle.Text; if (edit) { await UpdateUsers(); await groupRepo.EditOne(currGroup); await Globals.UpdateCurrentUser(); this.Close(); } else { var detailRepo = new HttpGroupDetailsRepository(); var newGroup = new GroupsViewModel() { created_at = DateTime.Now, GroupDetails = currGroup.GroupDetails, name = currGroup.name, owner_id = Globals.CurrentUser.id, description = currGroup.description }; GroupsViewModel retGroup = await groupRepo.AddOne(newGroup); //we need it to get created id currGroup.id = retGroup.id; //adding users await UpdateUsers(); await Globals.UpdateCurrentUser(); this.Close(); } }
/// <summary> /// Updates added members added to the database. /// </summary> /// <returns></returns> private async Task UpdateUsers() { HttpGroupDetailsRepository repo = new HttpGroupDetailsRepository(); ICollection<GroupDetailsViewModel> listOfDetails = new List<GroupDetailsViewModel>(); var users = usersBindingSource.List.Cast<UsersViewModel>(); var isUser = users.Count(u => u.id == Globals.CurrentUser.id) == 0; //checking if creator is added to the group if (isUser) //if not add him and proper communicate { usersBindingSource.Add(Globals.CurrentUser); MessageBox.Show("You removed yourself from the group, you were added to it automatically!","Warning", MessageBoxButtons.OK,MessageBoxIcon.Information); } //adding new users foreach (UsersViewModel user in usersBindingSource) { var newDetail = new GroupDetailsViewModel() { user_id = user.id, group_id = currGroup.id }; listOfDetails.Add(newDetail); if (!(await repo.DetailExists(newDetail))) //if given connection does not exist then add it { var detailRepo = new HttpGroupDetailsRepository(); await detailRepo.AddOne(newDetail); // currGroup.GroupDetails.Add(newDetail); //synchronize current group with db } } HttpGroupsRepository groupsRepo = new HttpGroupsRepository(); var actualGroup = await groupsRepo.GetOne(currGroup.id); //actual group to synchronize from db foreach (var detail in actualGroup.GroupDetails) { if (listOfDetails.Count(d => d.group_id == detail.group_id && d.user_id == detail.user_id) == 0) //if currGroup does not contain any of detail then delete it from db { await repo.DeleteOne(detail); } } }
/// <summary> /// Synchronizes current group with actual group from database. /// </summary> /// <returns></returns> public async Task SyncGroup() { HttpGroupsRepository repo = new HttpGroupsRepository(); currGroup = await repo.GetOne(currGroup.id); }
/// <summary> /// Deletes current group. /// </summary> /// <returns></returns> private async Task DeleteGroup() { var repo = new HttpGroupsRepository(); await repo.DeleteOne(currGroup); }
/*-----------------------------UPDATING FUNCTIONS-----------------------------*/ /// <summary> /// Updates repositories. /// </summary> private void UpdateRepositories() { years = new HttpYearsRepository(); subjects = new HttpSubjectsRepository(); grades = new HttpSubjectDetailsRepository(); users = new HttpUsersRepository(); groups = new HttpGroupsRepository(); groupDetails = new HttpGroupDetailsRepository(); }