Esempio n. 1
0
        public async Task <IHttpActionResult> CreateAsync(AcademyProCourseDto data)
        {
            var result = await _academyProCourseService.CreateAsync(data);

            if (result == null)
            {
                return(BadRequest());
            }
            return(Ok(result));
        }
Esempio n. 2
0
        public async Task <IHttpActionResult> UpdateAsync(int id, AcademyProCourseDto data)
        {
            var result = await _academyProCourseService.UpdateAsync(id, data);

            if (id != result.Id)
            {
                return(BadRequest());
            }

            return(Ok(result));
        }
Esempio n. 3
0
        public async Task <AcademyProCourseDto> SearchCourse(int fileId)
        {
            var authors = (await _usersService.GetAllAsync()).Select(f => new { Id = f.id, Name = f.name });

            var result = await _unitOfWork.AcademyProCourses.Query
                         .Include(a => a.FileUnit)
                         .Include(a => a.Author)
                         .Include(a => a.Tags)
                         .SingleOrDefaultAsync(c => c.FileUnit.Id == fileId);

            if (result == null)
            {
                return(null);
            }
            var resultCourse = new AcademyProCourseDto
            {
                Id        = result.Id,
                StartDate = result.StartDate,
                IsDeleted = result.IsDeleted,
                FileUnit  = new FileUnitDto
                {
                    Id           = result.FileUnit.Id,
                    Name         = result.FileUnit.Name,
                    FileType     = result.FileUnit.FileType,
                    Description  = result.FileUnit.Description,
                    CreatedAt    = result.FileUnit.CreatedAt,
                    LastModified = result.FileUnit.LastModified
                },
                Tags = result.Tags.Select(tag => new TagDto
                {
                    Id   = tag.Id,
                    Name = tag.Name
                }),
                Author = new AuthorDto {
                    Id = result.Author.Id, GlobalId = result.Author.GlobalId
                }
            };

            resultCourse.Author.Name = authors.SingleOrDefault(a => a.Id == resultCourse.Author.GlobalId)?.Name;

            return(resultCourse);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        public async Task <AcademyProCourseDto> UpdateAsync(int id, AcademyProCourseDto dto)
        {
            var course = await _unitOfWork.AcademyProCourses.Query.Include(x => x.FileUnit).Include(x => x.Tags).SingleOrDefaultAsync(c => c.Id == id);

            course.StartDate             = dto.StartDate;
            course.IsDeleted             = dto.IsDeleted;
            course.FileUnit.Name         = dto.FileUnit.Name;
            course.FileUnit.Description  = dto.FileUnit.Description;
            course.FileUnit.LastModified = DateTime.Now;
            var user = await _unitOfWork.Users.Query.SingleOrDefaultAsync(u => u.GlobalId == dto.Author.GlobalId);

            course.Author = user;

            for (int i = 0; i < course.Tags?.Count; i++)
            {
                var tag = course.Tags[i];
                if (dto.Tags?.FirstOrDefault(t => t.Name == tag.Name) == null)
                {
                    course.Tags?.RemoveAt(i);
                }
            }

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

            await _unitOfWork.SaveChangesAsync();

            return(dto);
        }