Ejemplo n.º 1
0
            public async Task <DocumentDetailDto> GetAsync(DocumentById query, CancellationToken token)
            {
                Document          documentAlias   = null;
                ReadTutor         tutorAlias      = null;
                University        universityAlias = null;
                BaseUser          userAlias       = null;
                DocumentDetailDto dtoAlias        = null;

                var similarDocumentQueryOver = QueryOver.Of <Document>()
                                               .Where(w => w.Md5 == documentAlias.Md5 && w.Status.State == ItemState.Ok)
                                               .And(x => x.Md5 != null).OrderBy(o => o.Id).Asc.Select(s => s.Id).Take(1);


                var futureValue = _session.QueryOver(() => documentAlias)
                                  .JoinAlias(x => x.University, () => universityAlias, JoinType.LeftOuterJoin)
                                  .JoinAlias(x => x.User, () => userAlias)
                                  .JoinEntityAlias(() => tutorAlias, () => documentAlias.User.Id == tutorAlias.Id, JoinType.LeftOuterJoin)
                                  .Where(w => w.Id == query.Id && w.Status.State == ItemState.Ok)
                                  .SelectList(l =>
                                              l.Select(() => documentAlias.PageCount).WithAlias(() => dtoAlias.Pages)
                                              .SelectSubQuery(similarDocumentQueryOver).WithAlias(() => dtoAlias.DuplicateId)
                                              .Select(Projections.Property(() => documentAlias.Id).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.Id)}"))
                                              .Select(Projections.Property(() => documentAlias.Name).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.Title)}"))
                                              .Select(Projections.Property(() => documentAlias.TimeStamp.UpdateTime).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.DateTime)}"))
                                              .Select(Projections.Property(() => universityAlias.Name).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.University)}"))
                                              .Select(Projections.Property(() => documentAlias.Views).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.Views)}"))
                                              .Select(Projections.Property(() => documentAlias.Price).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.Price)}"))
                                              .Select(Projections.Property(() => documentAlias.Course.Id).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.Course)}"))
                                              .Select(Projections.Property(() => documentAlias.Downloads).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.Downloads)}"))
                                              .Select(Projections.Property(() => documentAlias.VoteCount).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.Vote)}.{nameof(VoteDto.Votes)}"))
                                              .Select(Projections.SqlFunction("COALESCE", NHibernateUtil.String
                                                                              , Projections.Property <Document>(documentAlias2 => documentAlias2.Description)
                                                                              , Projections.Property <Document>(documentAlias2 => documentAlias2.MetaContent))
                                                      .WithAlias($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.Snippet)}"))
                                              .Select(Projections.Property(() => userAlias.Id).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.User)}.{nameof(DocumentUserDto.Id)}"))
                                              .Select(Projections.Property(() => userAlias.Name).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.User)}.{nameof(DocumentUserDto.Name)}"))
                                              .Select(Projections.Property(() => userAlias.Country).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.User)}.{nameof(DocumentUserDto.Country)}"))
                                              .Select(Projections.Property(() => userAlias.ImageName).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.User)}.{nameof(DocumentUserDto.Image)}"))
                                              .Select(Projections.Property(() => documentAlias.DocumentType).As($"{nameof(DocumentDetailDto.Document)}.{nameof(DocumentFeedDto.DocumentType)}"))
                                              .Select(Projections.Property(() => tutorAlias.Id).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.UserId)}"))
                                              .Select(Projections.Property(() => tutorAlias.SubsidizedPrice).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.DiscountPrice)}"))
                                              .Select(Projections.Property(() => tutorAlias.Name).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.Name)}"))
                                              .Select(Projections.Property(() => tutorAlias.ImageName).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.Image)}"))
                                              .Select(Projections.Property(() => tutorAlias.Courses).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.Courses)}"))
                                              .Select(Projections.Property(() => tutorAlias.Subjects).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.Subjects)}"))
                                              .Select(Projections.Property(() => tutorAlias.Price).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.Price)}"))
                                              .Select(Projections.Property(() => tutorAlias.Rate).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.Rate)}"))
                                              .Select(Projections.Property(() => tutorAlias.RateCount).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.ReviewsCount)}"))
                                              .Select(Projections.Property(() => tutorAlias.Bio).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.Bio)}"))
                                              .Select(Projections.Property(() => tutorAlias.University).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.University)}"))
                                              .Select(Projections.Property(() => tutorAlias.Lessons).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.Lessons)}"))
                                              .Select(Projections.Property(() => tutorAlias.Country).As($"{nameof(DocumentDetailDto.Tutor)}.{nameof(TutorCardDto.Country)}"))


                                              )
                                  .TransformUsing(new DeepTransformer <DocumentDetailDto>())
                                  .FutureValue <DocumentDetailDto>();


                if (!query.UserId.HasValue)
                {
                    return(await futureValue.GetValueAsync(token));
                }
                var purchaseFuture = _session.Query <DocumentTransaction>()
                                     .Where(w => w.User.Id == query.UserId.Value && w.Document.Id == query.Id && w.Type == TransactionType.Spent)
                                     .ToFutureValue();

                var purchaseCountFuture = _session.QueryOver <DocumentTransaction>()
                                          .Where(w => w.Document.Id == query.Id && w.Type == TransactionType.Spent)
                                          .SelectList(s => s.SelectCount(c => c.Id)).FutureValue <int>();

                var voteQuery = _session.Query <Vote>().Where(w => w.User.Id == query.UserId && w.Document.Id == query.Id).Select(s => s.VoteType).Take(1).ToFutureValue();



                var result = await futureValue.GetValueAsync(token);


                if (result == null)
                {
                    return(null);
                }
                result.IsPurchased = true;
                var voteResult = await voteQuery.GetValueAsync(token);

                if (voteResult == VoteType.None)
                {
                    result.Document.Vote.Vote = null;
                }
                else
                {
                    result.Document.Vote.Vote = voteResult;
                }
                if (result.Document.Price.GetValueOrDefault() <= 0)
                {
                    return(result);
                }
                if (purchaseFuture == null)
                {
                    result.IsPurchased = false;
                }
                else
                {
                    if (result.Document.User.Id == query.UserId.Value)
                    {
                        result.IsPurchased = true;
                    }
                    else
                    {
                        var transactionResult = await purchaseFuture.GetValueAsync(token);

                        result.IsPurchased = transactionResult != null;
                    }
                }
                result.Document.Purchased = await purchaseCountFuture.GetValueAsync(token);

                return(result);
            }
Ejemplo n.º 2
0
            public async Task <IList <SiteMapCountDto> > GetAsync(SiteMapQuery query, CancellationToken token)
            {
                University universityAlias = null;
                BaseUser   userAlias       = null;

                var documentCountFutureQuery = _session.QueryOver <Document>()
                                               .Left.JoinAlias(x => x.University, () => universityAlias)
                                               .JoinAlias(x => x.User, () => userAlias)
                                               .Where(w => w.Status.State == ItemState.Ok);

                if (query.IsFrymo)
                {
                    documentCountFutureQuery.Where(() => universityAlias.Country.IfNull(userAlias.Country) == "IN");
                }
                else
                {
                    documentCountFutureQuery.Where(() => universityAlias.Country.IfNull(userAlias.Country) != "IN");
                }
                var documentCountFuture = documentCountFutureQuery.ToRowCountQuery().FutureValue <int>();


                var questionCountFutureQuery = _session.QueryOver <Question>()
                                               .Left.JoinAlias(x => x.University, () => universityAlias)
                                               .JoinAlias(x => x.User, () => userAlias)
                                               .Where(w => w.Status.State == ItemState.Ok);

                if (query.IsFrymo)
                {
                    questionCountFutureQuery.Where(() => universityAlias.Country.IfNull(userAlias.Country) == "IN");
                }
                else
                {
                    questionCountFutureQuery.Where(() => universityAlias.Country.IfNull(userAlias.Country) != "IN");
                }

                var questionCountFuture = questionCountFutureQuery.ToRowCountQuery().FutureValue <int>();

                var tutorCountFutureQuery = _session.QueryOver <ReadTutor>();

                if (query.IsFrymo)
                {
                    tutorCountFutureQuery.Where(w => w.Country == "IN");
                }
                else
                {
                    tutorCountFutureQuery.Where(w => w.Country != "IN");
                }
                var tutorCountFuture = tutorCountFutureQuery.ToRowCountQuery().FutureValue <int>();

                UserCourse userCourseAlias = null;
                ReadTutor  tutorAlias      = null;

                var tutorCoursesFutureQuery = _session.QueryOver(() => tutorAlias)
                                              .JoinEntityAlias(() => userCourseAlias, () => tutorAlias.Id == userCourseAlias.User.Id)
                                              .Where(() => userCourseAlias.IsTeach)
                                              .SelectList(t => t.SelectCountDistinct(() => userCourseAlias.Course.Id));

                if (query.IsFrymo)
                {
                    tutorCoursesFutureQuery.Where(w => w.Country == "IN");
                }
                else
                {
                    tutorCoursesFutureQuery.Where(w => w.Country != "IN");
                }



                var tutorCoursesFuture = tutorCoursesFutureQuery.FutureValue <int>();



                var documentCount = await documentCountFuture.GetValueAsync(token);

                var questionCount = await questionCountFuture.GetValueAsync(token);

                var tutorCount = await tutorCountFuture.GetValueAsync(token);

                var tutorSiteCount = await tutorCoursesFuture.GetValueAsync(token);

                return(new List <SiteMapCountDto>
                {
                    new SiteMapCountDto(SeoType.Document, documentCount),
                    new SiteMapCountDto(SeoType.Question, questionCount),
                    new SiteMapCountDto(SeoType.Tutor, tutorCount),
                    new SiteMapCountDto(SeoType.TutorList, tutorSiteCount)
                });
            }
Ejemplo n.º 3
0
        private async Task DeleteAsync(ReadTutor tutor, CancellationToken token)
        {
            await _repository.DeleteAsync(tutor, token);

            await _unitOfWork.CommitAsync(CancellationToken.None);
        }
Ejemplo n.º 4
0
            public async Task <IEnumerable <TutorCardDto> > GetAsync(TutorListByCourseQuery query, CancellationToken token)
            {
                if (query.Count == 0)
                {
                    throw  new ArgumentException("query count cannot be 0");
                }
                //TODO maybe we can fix this query
                ReadTutor    tutorAlias            = null;
                UserCourse   userCourseAlias       = null;
                Course       courseAlias           = null;
                TutorCardDto tutorCardDtoAlias     = null;
                var          relevantTutorByCourse = QueryOver.Of(() => tutorAlias)
                                                     .JoinEntityAlias(() => userCourseAlias,
                                                                      () => userCourseAlias.User.Id == tutorAlias.Id)
                                                     .Where(() => userCourseAlias.IsTeach)
                                                     .And(() => userCourseAlias.Course.Id == query.CourseId)
                                                     .Select(s => s.Id)
                                                     .Take(query.Count);


                var relevantTutorBySubject = QueryOver.Of(() => tutorAlias)
                                             .JoinEntityAlias(() => userCourseAlias,
                                                              () => userCourseAlias.User.Id == tutorAlias.Id)
                                             .JoinAlias(() => userCourseAlias.Course, () => courseAlias)
                                             .Where(() => userCourseAlias.IsTeach)
                                             .WithSubquery.WhereProperty(() => courseAlias.Subject.Id).Eq(
                    QueryOver.Of <Course>().Where(w => w.Id == query.CourseId)
                    .Select(s => s.Subject.Id))
                                             .Select(s => s.Id)
                                             .Take(query.Count);


                var futureCourse = _session.QueryOver <ReadTutor>()
                                   .WithSubquery.WhereProperty(w => w.Id).In(relevantTutorByCourse)
                                   .Where(w => w.Id != query.UserId)
                                   .And(w => w.Country == query.Country)
                                   .SelectList(s =>
                                               s.Select(x => x.Id).WithAlias(() => tutorCardDtoAlias.UserId)

                                               .Select(x => x.Name).WithAlias(() => tutorCardDtoAlias.Name)
                                               .Select(x => x.ImageName).WithAlias(() => tutorCardDtoAlias.Image)
                                               .Select(x => x.Courses).WithAlias(() => tutorCardDtoAlias.Courses)
                                               .Select(x => x.Subjects).WithAlias(() => tutorCardDtoAlias.Subjects)
                                               .Select(x => x.Price).WithAlias(() => tutorCardDtoAlias.Price)
                                               .Select(x => x.Rate).WithAlias(() => tutorCardDtoAlias.Rate)
                                               .Select(x => x.RateCount).WithAlias(() => tutorCardDtoAlias.ReviewsCount)
                                               .Select(x => x.Bio).WithAlias(() => tutorCardDtoAlias.Bio)
                                               .Select(x => x.University).WithAlias(() => tutorCardDtoAlias.University)
                                               .Select(x => x.Country).WithAlias(() => tutorCardDtoAlias.Country)
                                               .Select(x => x.Lessons).WithAlias(() => tutorCardDtoAlias.Lessons)
                                               .Select(x => x.SubsidizedPrice).WithAlias(() => tutorCardDtoAlias.DiscountPrice))
                                   .OrderBy(o => o.OverAllRating).Desc

                                   .TransformUsing(Transformers.AliasToBean <TutorCardDto>())
                                   .Take(query.Count).Future <TutorCardDto>();

                var futureCourse2 = _session.QueryOver <ReadTutor>()
                                    .WithSubquery.WhereProperty(w => w.Id).In(relevantTutorBySubject)
                                    .Where(w => w.Id != query.UserId)
                                    .And(w => w.Country == query.Country)
                                    .SelectList(s =>
                                                s.Select(x => x.Id).WithAlias(() => tutorCardDtoAlias.UserId)
                                                .Select(x => x.Name).WithAlias(() => tutorCardDtoAlias.Name)
                                                .Select(x => x.ImageName).WithAlias(() => tutorCardDtoAlias.Image)
                                                .Select(x => x.Courses).WithAlias(() => tutorCardDtoAlias.Courses)
                                                .Select(x => x.Subjects).WithAlias(() => tutorCardDtoAlias.Subjects)
                                                .Select(x => x.Price).WithAlias(() => tutorCardDtoAlias.Price)
                                                .Select(x => x.Rate).WithAlias(() => tutorCardDtoAlias.Rate)
                                                .Select(x => x.RateCount).WithAlias(() => tutorCardDtoAlias.ReviewsCount)
                                                .Select(x => x.Bio).WithAlias(() => tutorCardDtoAlias.Bio)
                                                .Select(x => x.Country).WithAlias(() => tutorCardDtoAlias.Country)
                                                .Select(x => x.University).WithAlias(() => tutorCardDtoAlias.University)
                                                .Select(x => x.Lessons).WithAlias(() => tutorCardDtoAlias.Lessons))

                                    .OrderBy(o => o.OverAllRating).Desc

                                    .TransformUsing(Transformers.AliasToBean <TutorCardDto>())
                                    .Take(query.Count).Future <TutorCardDto>();
                var tutors = await futureCourse.GetEnumerableAsync(token);

                var tutors2 = await futureCourse2.GetEnumerableAsync(token);

                return(tutors.Union(tutors2).Skip(query.Page * query.Count).Take(query.Count)
                       .Distinct(TutorCardDto.UserIdComparer)
                       .Select(s =>
                {
                    s.Image = _urlBuilder.BuildUserImageEndpoint(s.UserId, s.Image);
                    return s;
                }));
            }