예제 #1
0
        public static GroupListDto ToGroupListDto(this Group group)
        {
            var groupListDto = new GroupListDto();

            if (group != null)
            {
                groupListDto.Id      = group.Id;
                groupListDto.Title   = group.Title;
                groupListDto.Created = group.Created;
                groupListDto.Number  = group.Number;
            }
            return(groupListDto);
        }
        public async Task <PagedList <GroupListDto> > GetUserGroups(PageParams pageParams, int userId, GroupFilter groupFilter, bool isAdmin)
        {
            List <GroupListDto> lstGroups = new List <GroupListDto>();


            if (isAdmin)
            {
                foreach (Groups grp in await _context.Groups.Where(x => x.GroupAdminId == userId || isAdmin).ToListAsync())
                {
                    var adminUserName = await _context.Users.FirstOrDefaultAsync(x => x.Id == grp.GroupAdminId);

                    GroupListDto gto = new GroupListDto()
                    {
                        GroupAdminId   = grp.GroupAdminId,
                        GroupName      = grp.GroupName,
                        Id             = grp.Id,
                        UserId         = userId,
                        GroupAdminName = adminUserName.UserName
                    };
                    lstGroups.Add(gto);
                }
            }
            else
            {
                var userGroupsAdmin = await _context.Groups.Where(x => x.GroupAdminId == userId).ToListAsync();

                // grupy użytkownika do których należy
                var userGroups = await _context.UserToGroups.Where(x => x.UserId == userId).ToListAsync();

                // jeżeli uzytkownik jest administratorem danej grupy = założycielem
                if (userGroupsAdmin.Count > 0)
                {
                    foreach (Groups grp in userGroupsAdmin)
                    {
                        var adminUserName = await _context.Users.FirstOrDefaultAsync(x => x.Id == grp.GroupAdminId);

                        GroupListDto gto = new GroupListDto()
                        {
                            GroupAdminId   = grp.GroupAdminId,
                            GroupName      = grp.GroupName,
                            Id             = grp.Id,
                            UserId         = userId,
                            GroupAdminName = adminUserName.UserName
                        };
                        lstGroups.Add(gto);
                    }
                }

                // jeżeli użytkownik nalezy do której z grup
                if (userGroups.Count > 0)
                {
                    foreach (UserToGroups utg in userGroups)
                    {
                        if (!userGroupsAdmin.Any(x => x.GroupAdminId == utg.UserId))
                        {
                            if (await _context.Groups.AnyAsync(x => x.Id == utg.GroupId)) //pobranie grup
                            {
                                var grp = await _context.Groups.FirstOrDefaultAsync(x => x.Id == utg.GroupId);

                                var adminUserName = await _context.Users.FirstOrDefaultAsync(x => x.Id == grp.GroupAdminId);

                                GroupListDto gto = new GroupListDto()
                                {
                                    GroupAdminId   = grp.GroupAdminId,
                                    GroupName      = grp.GroupName,
                                    Id             = grp.Id,
                                    UserId         = userId,
                                    GroupAdminName = adminUserName.UserName
                                };
                                lstGroups.Add(gto);
                            }
                        }
                    }
                }
            }


            if (groupFilter != null)
            {
                if (groupFilter.GroupNameLike != null)
                {
                    lstGroups = lstGroups.Where(x => x.GroupName.ToLower().Contains(groupFilter.GroupNameLike.ToLower())).ToList();
                }

                if (groupFilter.GroupAdminNameLike != null)
                {
                    lstGroups = lstGroups.Where(x => x.GroupAdminName.ToLower().Contains(groupFilter.GroupAdminNameLike.ToLower())).ToList();
                }
            }

            return(await PagedList <GroupListDto> .Create(lstGroups, pageParams.PageNumber, pageParams.PageSize));
        }