Exemplo n.º 1
0
        public async Task <AcademyProCourseDto> CreateAsync(AcademyProCourseDto dto)
        {
            var userId = _userService.CurrentUserId;

            var user = await _unitOfWork?.Users?.Query.FirstOrDefaultAsync(x => x.GlobalId == dto.Author.GlobalId);

            var course = new AcademyProCourse
            {
                StartDate = dto.StartDate,
                IsDeleted = false,
                Tags      = new List <Tag>(),
                FileUnit  = new FileUnit
                {
                    Name         = dto.FileUnit.Name,
                    Description  = dto.FileUnit.Description,
                    CreatedAt    = DateTime.Now,
                    LastModified = DateTime.Now,
                    Owner        = await _unitOfWork.Users.Query.SingleOrDefaultAsync(u => u.GlobalId == userId),
                    FileType     = FileType.AcademyPro,
                    IsDeleted    = false,
                    FolderUnit   = await _unitOfWork.Folders.Query.SingleOrDefaultAsync(f => f.Id == dto.FileUnit.ParentId),
                    Space        = await _unitOfWork.Spaces.Query.SingleOrDefaultAsync(s => s.Id == dto.FileUnit.SpaceId)
                },
                Author = await _unitOfWork.Users.Query.SingleOrDefaultAsync(u => u.GlobalId == dto.Author.GlobalId)
            };

            dto.Tags.ForEach(tag =>
            {
                course.Tags.Add(_unitOfWork.Tags.Query.FirstOrDefault(x => x.Name == tag.Name) ?? new Tag {
                    Name = tag.Name, IsDeleted = false
                });
            });

            _unitOfWork.AcademyProCourses.Create(course);
            await _unitOfWork.SaveChangesAsync();

            dto.Id = course.Id;
            return(dto);
        }
Exemplo n.º 2
0
        public async Task CreateCopyAsync(int id, FileUnitDto dto)
        {
            var file = await _unitOfWork?.Files.Query
                       .Include(f => f.ModifyPermittedUsers)
                       .Include(f => f.ReadPermittedUsers)
                       .Include(f => f.MorifyPermittedRoles)
                       .Include(f => f.ReadPermittedRoles)
                       .SingleOrDefaultAsync(f => f.Id == id);;

            if (file == null)
            {
                return;
            }

            var space = await _unitOfWork.Spaces.GetByIdAsync(dto.SpaceId);

            var user = await _usersService?.GetCurrentUser();

            Regex regEx = new Regex(@"(.+?)(\.[^.]*$|$)");

            string name      = regEx.Match(file.Name)?.Groups[1].Value;
            string extention = regEx.Match(file.Name)?.Groups[2].Value;

            var copies = await _unitOfWork.Files.Query.Where(f => f.Name.StartsWith(name + "-copy") &&
                                                             (f.FolderUnit.Id == dto.ParentId || (dto.ParentId == 0 && f.Space.Id == dto.SpaceId))).ToListAsync();

            if (copies.Count > 0)
            {
                int index    = 0;
                int maxIndex = 1;
                foreach (var copyStr in copies.Select(c => c.Name.Substring(name.Length)))
                {
                    if (Int32.TryParse(copyStr, out index))
                    {
                        if (index > maxIndex)
                        {
                            maxIndex = index;
                        }
                    }
                }
                name = name + (maxIndex + 1).ToString();
            }
            else
            {
                name = name + "-copy";
            }

            var copy = new FileUnit
            {
                Name                 = name + extention,
                Description          = file.Description,
                FileType             = file.FileType,
                IsDeleted            = file.IsDeleted,
                LastModified         = DateTime.Now,
                CreatedAt            = DateTime.Now,
                Link                 = file.Link,
                Space                = space,
                Owner                = await _unitOfWork.Users.Query.FirstOrDefaultAsync(u => u.GlobalId == user.id),
                ModifyPermittedUsers = file.ModifyPermittedUsers,
                ReadPermittedUsers   = file.ReadPermittedUsers,
                MorifyPermittedRoles = file.MorifyPermittedRoles,
                ReadPermittedRoles   = file.ReadPermittedRoles
            };

            if (dto.ParentId != 0)
            {
                var parent = await _unitOfWork.Folders.GetByIdAsync(dto.ParentId);

                copy.FolderUnit = parent;
            }
            if (file.FileType == FileType.AcademyPro)
            {
                var course = await _unitOfWork.AcademyProCourses.Query
                             .Include(a => a.Author)
                             .Include(a => a.Tags)
                             .Include(a => a.Lectures.Select(l => l.CodeSamples))
                             .Include(a => a.Lectures.Select(l => l.ContentList))
                             .Include(a => a.Lectures.Select(l => l.HomeTasks))
                             .Include(a => a.Lectures.Select(l => l.Author))
                             .FirstOrDefaultAsync(a => a.FileUnit.Id == file.Id);

                var coursecopy = new AcademyProCourse
                {
                    StartDate = course.StartDate,
                    IsDeleted = false,
                    Tags      = new List <Tag>(),
                    FileUnit  = copy,
                    Author    = course.Author,
                    Lectures  = new List <Lecture>()
                };

                course.Tags?.ToList().ForEach(tag =>
                {
                    coursecopy.Tags.Add(_unitOfWork.Tags.Query.FirstOrDefault(x => x.Name == tag.Name) ?? new Tag {
                        Name = tag.Name, IsDeleted = false
                    });
                });

                course.Lectures?.ToList().ForEach(lecture =>
                {
                    coursecopy.Lectures.Add(new Lecture
                    {
                        Author      = lecture.Author,
                        Name        = lecture.Name,
                        Description = lecture.Description,
                        IsDeleted   = lecture.IsDeleted,
                        CreatedAt   = DateTime.Now,
                        StartDate   = lecture.StartDate,
                        ModifiedAt  = DateTime.Now,
                        CodeSamples = lecture.CodeSamples.Select(cs => new CodeSample
                        {
                            Code      = cs.Code,
                            IsDeleted = cs.IsDeleted,
                            Name      = cs.Name
                        }).ToList(),
                        ContentList = lecture.ContentList.Select(cl => new ContentLink
                        {
                            Name        = cl.Name,
                            IsDeleted   = cl.IsDeleted,
                            Description = cl.Description,
                            LinkType    = cl.LinkType,
                            Link        = cl.Link
                        }).ToList(),
                        HomeTasks = lecture.HomeTasks.Select(ht => new HomeTask
                        {
                            Description  = ht.Description,
                            IsDeleted    = ht.IsDeleted,
                            DeadlineDate = ht.DeadlineDate
                        }).ToList()
                    });
                });

                _unitOfWork.AcademyProCourses.Create(coursecopy);
            }

            if (file.FileType == FileType.Events)
            {
                var originalEvent = await _unitOfWork.Events.Query
                                    .Include(e => e.ContentList)
                                    .FirstOrDefaultAsync(e => e.FileUnit.Id == file.Id);

                Event eventCopy = new Event
                {
                    IsDeleted   = originalEvent.IsDeleted,
                    EventDate   = originalEvent.EventDate,
                    EventType   = originalEvent.EventType,
                    FileUnit    = copy,
                    ContentList = new List <EventContent>()
                };

                originalEvent.ContentList.ToList().ForEach(c =>
                                                           eventCopy.ContentList.Add(new EventContent
                {
                    Name         = c.Name,
                    Description  = c.Description,
                    IsDeleted    = c.IsDeleted,
                    ContentType  = c.ContentType,
                    Order        = c.Order,
                    CreatedAt    = DateTime.Now,
                    LastModified = DateTime.Now,
                    Content      = c.Content
                })
                                                           );
                _unitOfWork.Events.Create(eventCopy);
            }

            _unitOfWork.Files.Create(copy);

            await _unitOfWork?.SaveChangesAsync();
        }