Ejemplo n.º 1
0
        /// <summary>
        /// Constructor for YearInGroup form. Assings repositories, disables buttons for non admin users.
        /// </summary>
        /// <param name="_groupId">Id of a group the year belongs to.</param>
        /// <param name="_yearId">Id of a selected year.</param>
        /// <param name="_isAdmin">Determines wether the form is displayed by an admin.</param>
        public YearInGroupForm(int _groupId, int _yearId, bool _isAdmin)
        {
            InitializeComponent();  
            
            groupId = _groupId;
            yearId = _yearId;
            isAdmin = _isAdmin;

            users = new HttpUsersRepository();
            years = new HttpYearsRepository();
            subjects = new HttpSubjectsRepository();
            groupDetails = new HttpGroupDetailsRepository();

            usersGridView.CellDoubleClick += seeUser;
            subjectsGridView.CellDoubleClick += editSubject;

            UpdateTables();

            btnAddSubject.Click += AddSubject;
            if (!isAdmin)
            {
                btnAddSubject.Visible = false;
                btnDeleteYear.Visible = false;
                btnEditYear.Visible = false;
            }
        }
Ejemplo n.º 2
0
        /// <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();
            }

        }
Ejemplo n.º 3
0
        /// <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);
                }
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Current user leaves the group.
 /// </summary>
 /// <returns></returns>
 private async Task LeaveGroup()
 {
     var currDetail = currGroup.GroupDetails.FirstOrDefault(d => d.user_id == Globals.CurrentUser.id);
     var repo = new HttpGroupDetailsRepository();
     await repo.DeleteOne(currDetail);
 }
Ejemplo n.º 5
0
 /*-----------------------------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();
 }