Exemple #1
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);
                }
            }
        }
        public async Task<IHttpActionResult> PostGroupDetails(GroupDetailsViewModel groupDetails)
        {
            GroupDetails newDetail = new GroupDetails()
            {
                user_id = groupDetails.user_id,
                group_id = groupDetails.group_id
            };
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.GroupDetails.Add(newDetail);
            await db.SaveChangesAsync();

            return CreatedAtRoute("DefaultApi", new { id = groupDetails.id }, groupDetails);
        }