protected override string VisitMemberAccess(MemberExpression m)
        {
            if (m.Expression != null &&
                m.Expression.NodeType == ExpressionType.Parameter &&
                m.Expression.Type == typeof(T))
            {
                var field = _mapper.Map(m.Member.Name, true);
                if (field.IsNullOrWhiteSpace())
                {
                    throw new InvalidOperationException("The mapper returned an empty field for the member name: " + m.Member.Name);
                }
                return(field);
            }

            if (m.Expression != null && m.Expression.NodeType == ExpressionType.Convert)
            {
                var field = _mapper.Map(m.Member.Name, true);
                if (field.IsNullOrWhiteSpace())
                {
                    throw new InvalidOperationException("The mapper returned an empty field for the member name: " + m.Member.Name);
                }
                return(field);
            }

            var    member = Expression.Convert(m, typeof(object));
            var    lambda = Expression.Lambda <Func <object> >(member);
            var    getter = lambda.Compile();
            object o      = getter();

            SqlParameters.Add(o);
            return(string.Format("@{0}", SqlParameters.Count - 1));

            //return GetQuotedValue(o, o != null ? o.GetType() : null);
        }
Ejemplo n.º 2
0
        protected override string VisitMemberAccess(MemberExpression m)
        {
            if (m.Expression != null &&
                m.Expression.NodeType == ExpressionType.Parameter &&
                m.Expression.Type == typeof(T))
            {
                var field = _mapper.Map(m.Member.Name);
                return(field);
            }

            if (m.Expression != null && m.Expression.NodeType == ExpressionType.Convert)
            {
                var field = _mapper.Map(m.Member.Name);
                return(field);
            }

            var    member = Expression.Convert(m, typeof(object));
            var    lambda = Expression.Lambda <Func <object> >(member);
            var    getter = lambda.Compile();
            object o      = getter();

            SqlParameters.Add(o);
            return(string.Format("@{0}", SqlParameters.Count - 1));

            //return GetQuotedValue(o, o != null ? o.GetType() : null);
        }
Ejemplo n.º 3
0
        public bool AddTour(TourDto tour)
        {
            TourEntity tourEntity = BaseMapper <TourDto, TourEntity> .Map(tour);

            this.unitOfWork.TourRepository.Add(tourEntity);
            this.unitOfWork.Commit();

            return(true);
        }
Ejemplo n.º 4
0
        public bool UpdateTourById(int id, TourDto tour)
        {
            var newTour = BaseMapper <TourDto, TourEntity> .Map(tour);

            newTour.TourId = id;

            this.unitOfWork.TourRepository.Update(newTour);
            this.unitOfWork.Commit();
            return(true);
        }
Ejemplo n.º 5
0
        public async Task <IEnumerable <Tour> > GetAllToursAsync()
        {
            var entitiesList = await this.unitOfWork.TourRepository.GetAllAsync();

            IList <Tour> tours = new List <Tour>();

            entitiesList.ToList().ForEach(i => tours.Add(BaseMapper <TourEntity, Tour> .Map(i)));

            return(tours);
        }
Ejemplo n.º 6
0
        public IList <Tour> GetAllTours()
        {
            var entitiesList = this.unitOfWork.TourRepository.GetAll();

            IList <Tour> tours = new List <Tour>();

            entitiesList.ToList().ForEach(i => tours.Add(BaseMapper <TourEntity, Tour> .Map(i)));

            return(tours);
        }
Ejemplo n.º 7
0
        public async Task <int> AddTourAsync(TourDto tour)
        {
            TourEntity tourEntity = BaseMapper <TourDto, TourEntity> .Map(tour);

            await this.unitOfWork.TourRepository.AddAsync(tourEntity);

            await this.unitOfWork.CommitAsync();

            return(tourEntity.TourId);
        }
Ejemplo n.º 8
0
        public async Task UpdateTourByIdAsync(int id, TourDto tour)
        {
            var newTour = BaseMapper <TourDto, TourEntity> .Map(tour);

            newTour.TourId = id;

            await this.unitOfWork.TourRepository.UpdateAsync(newTour);

            await this.unitOfWork.CommitAsync();
        }
        protected virtual string VisitMemberAccess(MemberExpression m)
        {
            if (m.Expression != null && m.Expression.NodeType == ExpressionType.Parameter && m.Expression.Type == typeof(T))
            {
                var field = _mapper.Map(m.Member.Name);
                return(field);
            }

            if (m.Expression != null && m.Expression.NodeType == ExpressionType.Convert)
            {
                var field = _mapper.Map(m.Member.Name);
                return(field);
            }

            var    member = Expression.Convert(m, typeof(object));
            var    lambda = Expression.Lambda <Func <object> >(member);
            var    getter = lambda.Compile();
            object o      = getter();

            return(GetQuotedValue(o, o != null ? o.GetType() : null));
        }
Ejemplo n.º 10
0
        public IEnumerable <CategoryDTO> Get(bool isPaging, int page)
        {
            IEnumerable <CategoryDTO> categoryDTOs;

            try
            {
                categoryDTOs = BaseMapper.Map(_categoryMapper, _dataBase.Categories.Get(isPaging, page));
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex.Message, "Stopped program because of exception");
                throw;
            }

            return(categoryDTOs);
        }
Ejemplo n.º 11
0
        public IEnumerable <TagDTO> Get(bool isPaging, int page)
        {
            IEnumerable <TagDTO> tagDTOs;

            try
            {
                tagDTOs = BaseMapper.Map(_tagMapper, _dataBase.Tags.Get(isPaging, page));
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex.Message, "Stopped program because of exception");
                throw;
            }

            return(tagDTOs);
        }
Ejemplo n.º 12
0
        public async Task AddReservationAsync(ReservationDto reservation)
        {
            var tour = this.unitOfWork.TourRepository.FindById(reservation.TourId);

            if (tour == null)
            {
                throw new ServiceException($"No tour with id {reservation.TourId}");
            }

            ReservationEntity reservationEntity = BaseMapper <ReservationDto, ReservationEntity> .Map(reservation);

            reservationEntity.Tour             = tour;
            reservationEntity.ReservationPrice = tour.Price * reservationEntity.PeopleCount;

            await this.unitOfWork.ReservationRepository.AddAsync(reservationEntity);

            await this.unitOfWork.CommitAsync();
        }
    protected override string VisitMemberAccess(MemberExpression?m)
    {
        if (m is null)
        {
            return(string.Empty);
        }

        if (m.Expression != null &&
            m.Expression.NodeType == ExpressionType.Parameter &&
            m.Expression.Type == typeof(T))
        {
            // don't execute if compiled
            if (Visited == false)
            {
                var field = _mapper?.Map(m.Member.Name);
                if (field.IsNullOrWhiteSpace())
                {
                    throw new InvalidOperationException(
                              $"The mapper returned an empty field for the member name: {m.Member.Name} for type: {m.Expression.Type}.");
                }

                return(field !);
            }

            // already compiled, return
            return(string.Empty);
        }

        if (m.Expression != null && m.Expression.NodeType == ExpressionType.Convert)
        {
            // don't execute if compiled
            if (Visited == false)
            {
                var field = _mapper?.Map(m.Member.Name);
                if (field.IsNullOrWhiteSpace())
                {
                    throw new InvalidOperationException(
                              $"The mapper returned an empty field for the member name: {m.Member.Name} for type: {m.Expression.Type}.");
                }

                return(field !);
            }

            // already compiled, return
            return(string.Empty);
        }

        if (m.Expression != null &&
            m.Expression.Type != typeof(T) &&
            EndsWithConstant(m) == false &&
            _mappers is not null &&
            _mappers.TryGetMapper(m.Expression.Type, out BaseMapper? subMapper))
        {
            // if this is the case, it means we have a sub expression / nested property access, such as: x.ContentType.Alias == "Test";
            // and since the sub type (x.ContentType) is not the same as x, we need to resolve a mapper for x.ContentType to get it's mapped SQL column

            // don't execute if compiled
            if (Visited == false)
            {
                var field = subMapper.Map(m.Member.Name);
                if (field.IsNullOrWhiteSpace())
                {
                    throw new InvalidOperationException(
                              $"The mapper returned an empty field for the member name: {m.Member.Name} for type: {m.Expression.Type}");
                }

                return(field);
            }

            // already compiled, return
            return(string.Empty);
        }

        // TODO: When m.Expression.NodeType == ExpressionType.Constant and it's an expression like: content => aliases.Contains(content.ContentType.Alias);
        // then an SQL parameter will be added for aliases as an array, however in SqlIn on the subclass it will manually add these SqlParameters anyways,
        // however the query will still execute because the SQL that is written will only contain the correct indexes of SQL parameters, this would be ignored,
        // I'm just unsure right now due to time constraints how to make it correct. It won't matter right now and has been working already with this bug but I've
        // only just discovered what it is actually doing.

        // TODO
        // in most cases we want to convert the value to a plain object,
        // but for in some rare cases, we may want to do it differently,
        // for instance a Models.AuditType (an enum) may in some cases
        // need to be converted to its string value.
        // but - we cannot have specific code here, really - and how would
        // we configure this? is it even possible?

        /*
         * var toString = typeof(object).GetMethod("ToString");
         * var member = Expression.Call(m, toString);
         */
        UnaryExpression member = Expression.Convert(m, typeof(object));
        var             lambda = Expression.Lambda <Func <object> >(member);
        Func <object>   getter = lambda.Compile();
        var             o      = getter();

        SqlParameters.Add(o);

        // don't execute if compiled
        if (Visited == false)
        {
            return($"@{SqlParameters.Count - 1}");
        }

        // already compiled, return
        return(string.Empty);
    }
        protected override string VisitMemberAccess(MemberExpression m)
        {
            if (m.Expression != null &&
                m.Expression.NodeType == ExpressionType.Parameter &&
                m.Expression.Type == typeof(T))
            {
                //don't execute if compiled
                if (Visited == false)
                {
                    var field = _mapper.Map(SqlSyntax, m.Member.Name, true);
                    if (field.IsNullOrWhiteSpace())
                    {
                        throw new InvalidOperationException($"The mapper returned an empty field for the member name: {m.Member.Name} for type: {m.Expression.Type}.");
                    }
                    return(field);
                }

                //already compiled, return
                return(string.Empty);
            }

            if (m.Expression != null && m.Expression.NodeType == ExpressionType.Convert)
            {
                //don't execute if compiled
                if (Visited == false)
                {
                    var field = _mapper.Map(SqlSyntax, m.Member.Name, true);
                    if (field.IsNullOrWhiteSpace())
                    {
                        throw new InvalidOperationException($"The mapper returned an empty field for the member name: {m.Member.Name} for type: {m.Expression.Type}.");
                    }
                    return(field);
                }

                //already compiled, return
                return(string.Empty);
            }

            if (m.Expression != null &&
                m.Expression.Type != typeof(T) &&
                TypeHelper.IsTypeAssignableFrom <IUmbracoEntity>(m.Expression.Type) &&
                EndsWithConstant(m) == false)
            {
                //if this is the case, it means we have a sub expression / nested property access, such as: x.ContentType.Alias == "Test";
                //and since the sub type (x.ContentType) is not the same as x, we need to resolve a mapper for x.ContentType to get it's mapped SQL column

                //don't execute if compiled
                if (Visited == false)
                {
                    var subMapper = _mappers[m.Expression.Type]; // throws if not found
                    var field     = subMapper.Map(SqlSyntax, m.Member.Name, true);
                    if (field.IsNullOrWhiteSpace())
                    {
                        throw new InvalidOperationException($"The mapper returned an empty field for the member name: {m.Member.Name} for type: {m.Expression.Type}");
                    }
                    return(field);
                }
                //already compiled, return
                return(string.Empty);
            }

            //TODO: When m.Expression.NodeType == ExpressionType.Constant and it's an expression like: content => aliases.Contains(content.ContentType.Alias);
            // then an SQL parameter will be added for aliases as an array, however in SqlIn on the subclass it will manually add these SqlParameters anyways,
            // however the query will still execute because the SQL that is written will only contain the correct indexes of SQL parameters, this would be ignored,
            // I'm just unsure right now due to time constraints how to make it correct. It won't matter right now and has been working already with this bug but I've
            // only just discovered what it is actually doing.

            var member = Expression.Convert(m, typeof(object));
            var lambda = Expression.Lambda <Func <object> >(member);
            var getter = lambda.Compile();
            var o      = getter();

            SqlParameters.Add(o);

            //don't execute if compiled
            if (Visited == false)
            {
                return($"@{SqlParameters.Count - 1}");
            }

            //already compiled, return
            return(string.Empty);
        }
Ejemplo n.º 15
0
        public Tour GetTourById(int id)
        {
            var entity = unitOfWork.TourRepository.FindById(id);

            return(BaseMapper <TourEntity, Tour> .Map(entity));
        }