Ejemplo n.º 1
0
        public async Task HandleAsync(UserUpdatePrivilegesCommand command)
        {
            var user = await _context
                       .User
                       .Include(t => t.UserCoursePrivilege)
                       .FirstOrDefaultAsync(x => x.Id == command.UserId);

            var currentCoursePrivilegeIds = user.UserCoursePrivilege.Select(x => x.CourseId);
            var res = currentCoursePrivilegeIds.Union(command.Courses).Except(currentCoursePrivilegeIds.Intersect(command.Courses)).ToList();

            res.ForEach(x =>
            {
                if (currentCoursePrivilegeIds.Contains(x))
                {
                    var privileges = _context.UserCoursePrivilege.Where(y => y.CourseId == x && y.UserId == command.UserId);
                    _context.UserCoursePrivilege.RemoveRange(privileges);
                }
                else
                {
                    _context.UserCoursePrivilege.Add(new UserCoursePrivilege()
                    {
                        CourseId    = x,
                        PrivilegeId = (int)PrivilegeEnum.IsInvolvedWithCourse,
                        UserId      = command.UserId
                    });
                }
            });

            await _context.SaveChangesAsync();
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> UpdatePrivileges(UserUpdatePrivilegesCommand command)
        {
            await _commandBus.ExecuteAsync(command);

            return(Ok());
        }