Esempio n. 1
0
    /// <summary>
    /// Update existing entity using object as a new state.
    /// </summary>
    /// <param name="entity">New state of existing entity.</param>
    /// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
    public async Task UpdateAsync(T entity)
    {
        await using CookingContext context = ContextFactory.Create();
        T existing = await GetFullGraph(context.Set <T>()).FirstAsync(x => x.ID == entity.ID);

        Mapper.Map(entity, existing);
        await context.SaveChangesAsync();
    }
Esempio n. 2
0
    /// <summary>
    /// Get entry, projected from <typeparamref name="T" />.
    /// </summary>
    /// <typeparam name="TProjection">Type of entry to project to.</typeparam>
    /// <param name="id">ID of entity to find and project.</param>
    /// <returns>Found projected entity.</returns>
    public TProjection GetProjected <TProjection>(Guid id)
        where TProjection : Entity
    {
        using CookingContext context = ContextFactory.Create();
        TProjection?entryProjected = Mapper.ProjectTo <TProjection>(context.Set <T>())
                                     .AsNoTracking()
                                     .FirstOrDefault(x => x.ID == id);

        return(Mapper.Map <TProjection>(entryProjected));
    }
Esempio n. 3
0
    /// <summary>
    /// Remove entity from database.
    /// </summary>
    /// <param name="id">Id of entity to delete.</param>
    /// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
    public virtual async Task DeleteAsync(Guid id)
    {
        using CookingContext context = ContextFactory.Create();

        // Remove entity without loading it, using stub object.
        context.Set <T>().Remove(new T {
            ID = id
        });
        await context.SaveChangesAsync();
    }
Esempio n. 4
0
    /// <summary>
    /// Create new entity in database.
    /// </summary>
    /// <param name="entity">Entity to insert into database.</param>
    /// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
    public async Task <Guid> CreateAsync(T entity)
    {
        await using CookingContext context = ContextFactory.Create();
        entity.Culture = GetCurrentCulture();
        await context.Set <T>().AddAsync(entity);

        await context.SaveChangesAsync();

        return(entity.ID);
    }
Esempio n. 5
0
    private T Get(Guid id, CookingContext context, bool isTracking = false)
    {
        IQueryable <T> set     = context.Set <T>();
        IQueryable <T> fullSet = GetFullGraph(set);

        if (!isTracking)
        {
            fullSet = fullSet.AsNoTracking();
        }

        return(fullSet.AsSplitQuery().Single(x => x.ID == id));
    }
Esempio n. 6
0
    /// <summary>
    /// Get a list of required properties for all objects matching filter.
    /// </summary>
    /// <typeparam name="TProperty">Type of property.</typeparam>
    /// <param name="propertySelector">Selector for a required property.</param>
    /// <param name="filter">Filter of objects to retrieve.</param>
    /// <returns>List of required properties for all objects matching filter.</returns>
    public List <TProperty> GetProperty <TProperty>(Expression <Func <T, TProperty> > propertySelector, Expression <Func <T, bool> >?filter = null)
    {
        using CookingContext context = ContextFactory.Create();
        IQueryable <T> cultureSpecificSet = context.Set <T>().AsNoTracking();

        if (filter != null)
        {
            cultureSpecificSet = cultureSpecificSet.Where(filter);
        }

        return(cultureSpecificSet.Select(propertySelector).ToList());
    }
Esempio n. 7
0
    /// <summary>
    /// Get all entities for a type.
    /// </summary>
    /// <param name="predicate">Predicate to filter.</param>
    /// <returns>All entities for type <typeparamref name="T" />.</returns>
    public List <T> GetAll(Expression <Func <T, bool> >?predicate = null)
    {
        using CookingContext context = ContextFactory.Create();
        IQueryable <T>?fullSet = GetFullGraph(context.Set <T>());

        if (predicate != null)
        {
            fullSet = fullSet.Where(predicate);
        }

        return(fullSet.AsNoTracking()
               .AsSplitQuery()
               .ToList());
    }
Esempio n. 8
0
    /// <summary>
    /// Get reipe list filtered by optional parameters.
    /// </summary>
    /// <typeparam name="T">Type of required projection.</typeparam>
    /// <param name="requiredTags">Filter reipies by tags.</param>
    /// <param name="requiredCalorieTypes">Filter reipies by calorie types.</param>
    /// <param name="maxComplexity">Filter reipies by maximal complexity.</param>
    /// <param name="minRating">Filter reipies by minimal rating.</param>
    /// <param name="onlyNew">Filter out reipies which already was cooked.</param>
    /// <returns>List of filtered recipies.</returns>
    public List <T> GetRecipiesByParametersProjected <T>(List <Guid>?requiredTags = null,
                                                         List <CalorieType>?requiredCalorieTypes = null,
                                                         int?maxComplexity = null,
                                                         int?minRating     = null,
                                                         bool?onlyNew      = false)
        where T : Entity
    {
        using CookingContext context = ContextFactory.Create();
        IQueryable <Recipe> query = context.Set <Recipe>()
                                    .Include(x => x.Garnishes)
                                    .Include(x => x.Tags)
                                    .AsNoTracking()
                                    .AsQueryable();

        if (requiredTags?.Count > 0)
        {
            query = query.Where(x => x.Tags !.Any(t => requiredTags.Contains(t.ID)));
        }

        if (requiredCalorieTypes?.Count > 0)
        {
            query = query.Where(x => requiredCalorieTypes.Contains(x.CalorieType));
        }

        if (maxComplexity.HasValue)
        {
            query = query.Where(x => x.Difficulty <= maxComplexity.Value);
        }

        if (minRating.HasValue)
        {
            query = query.Where(x => x.Rating >= minRating.Value);
        }

        var queryResult = Mapper.ProjectTo <T>(query).ToList();

        // Client filtration
        if (onlyNew.HasValue && onlyNew.Value)
        {
            queryResult = queryResult.Where(x => dayService.GetLastCookedDate(x.ID) == null).ToList();
        }

        return(queryResult.OrderByDescending(x => DaysFromLasCook(x.ID)).ToList());
    }
Esempio n. 9
0
    /// <summary>
    /// Get all entities of type <typeparamref name="T" /> mapped to TProjection.
    /// </summary>
    /// <typeparam name="TProjection">Type to map <typeparamref name="T" /> to.</typeparam>
    /// <param name="predicate">Filter of objects to retrieve.</param>
    /// <param name="clientsidePredicate">Predicate to filter after data loading.</param>
    /// <returns>List of all mapped entities.</returns>
    public List <TProjection> GetMapped <TProjection>(Expression <Func <T, bool> >?predicate = null, Func <T, bool>?clientsidePredicate = null)
    {
        using CookingContext context = ContextFactory.Create();
        IQueryable <T> cultureSet = context.Set <T>();
        IQueryable <T>?fullSet    = GetFullGraph(cultureSet);

        IEnumerable <T>?set = fullSet.AsNoTracking();

        if (predicate != null)
        {
            fullSet = fullSet.Where(predicate);
        }

        IEnumerable <T> queryResult = fullSet.AsEnumerable();

        if (clientsidePredicate != null)
        {
            queryResult = queryResult.Where(clientsidePredicate);
        }

        return(Mapper.Map <List <TProjection> >(queryResult));
    }
Esempio n. 10
0
    /// <summary>
    /// Get all entities of type <typeparamref name="T" /> projected to TProjection.
    /// </summary>
    /// <typeparam name="TProjection">Type to project <typeparamref name="T" /> to.</typeparam>
    /// <param name="predicate">Predicate to filter.</param>
    /// <param name="callAfterMap">Do mapping after projection to call AfterMap. Needs mapping from TProjection to TProjection, otherwise will have no effect.</param>
    /// <returns>List of all projected entities.</returns>
    public List <TProjection> GetProjected <TProjection>(Expression <Func <T, bool> >?predicate = null, bool callAfterMap = false)
    {
        using CookingContext context = ContextFactory.Create();
        IQueryable <T> cultureSpecificSet = context.Set <T>().AsNoTracking();

        if (predicate != null)
        {
            cultureSpecificSet = cultureSpecificSet.Where(predicate);
        }

        var allProjected = Mapper.ProjectTo <TProjection>(cultureSpecificSet).ToList();

        if (callAfterMap)
        {
            // Here we mapping projected objects to themselves to enable AfterMap calls
            return(Mapper.Map <List <TProjection> >(allProjected));
        }
        else
        {
            return(allProjected);
        }
    }
Esempio n. 11
0
        public virtual IQueryable <T> GetAll()
        {
            IQueryable <T> query = Context.Set <T>();

            return(query);
        }