Exemple #1
0
    // READ (GET BY ID)
    public Blog Get(int id)     // pass in datatype and id to get specific Blog; // Dependency Injection: remove static
    {
        Blog p = null;
        List <BlogsTagsDomain> blogList = null;

        DataProvider.ExecuteCmd(GetConnection, "Blogs_SelectById_V2", inputParamMapper : delegate(SqlParameterCollection paramCollection)
        {
            paramCollection.AddWithValue("@Id", id);
        }
                                , map : delegate(IDataReader reader, short set)
        {
            if (set == 0)
            {
                p = new Blog();
                mapBlog(reader, p);
            }
            else if (set == 1)
            {
                BlogsTagsDomain f = mapTag(reader);

                if (blogList == null)
                {
                    blogList = new List <BlogsTagsDomain>();
                }

                blogList.Add(f);
            }
        });
        if (p != null)
        {
            p.Tags = blogList;
        }

        return(p);
    }
Exemple #2
0
    private static BlogsTagsDomain mapTag(IDataReader reader)
    {
        BlogsTagsDomain t             = new BlogsTagsDomain();
        int             startingIndex = 0;

        t.id   = reader.GetSafeInt32(startingIndex++);
        t.text = reader.GetSafeString(startingIndex++);

        t.dateAdded = reader.GetSafeDateTime(startingIndex++);

        t.dateModified = reader.GetSafeDateTime(startingIndex++);


        return(t);
    }
Exemple #3
0
    // SELECT ALL
    public List <BlogsTagsDomain> GetTags()
    {
        List <BlogsTagsDomain> list = null;

        DataProvider.ExecuteCmd(GetConnection, "dbo.BlogTags_SelectAll"
                                , inputParamMapper : null
                                , map : delegate(IDataReader reader, short set)
        {
            BlogsTagsDomain t = mapTag(reader);

            if (list == null)
            {
                list = new List <BlogsTagsDomain>();
            }

            list.Add(t);
        }
                                );

        return(list);
    }
Exemple #4
0
    // GET BLOG BY SLUG - READ
    public Blog GetBlog(string slug)
    {
        Blog p = null;
        List <BlogsTagsDomain> blogList = null;


        DataProvider.ExecuteCmd(GetConnection, "dbo.Blogs_SelectBlogBySlug", inputParamMapper : delegate(SqlParameterCollection paramCollection)
        {
            paramCollection.AddWithValue("@Slug", slug);
        }
                                , map : delegate(IDataReader reader, short set)
        {
            if (set == 0)
            {
                p = new Blog();
                mapBlog(reader, p);
            }
            else if (set == 1)
            {
                BlogsTagsDomain f = mapTag(reader);

                if (blogList == null)
                {
                    blogList = new List <BlogsTagsDomain>();
                }

                blogList.Add(f);
            }
        });
        if (p != null)
        {
            p.Tags = blogList;
        }

        return(p);
    }
Exemple #5
0
    // READ (GET Blogs by Publish status)
    public List <Blog> GetPublished(bool isPublished)    // Dependency Injection: remove static
    {
        List <Blog> blogList = null;

        List <BlogsTagsDomain> list = null;
        //BlogId, is a list of TagId
        Dictionary <int, List <int> > dict = null;

        DataProvider.ExecuteCmd(GetConnection, "dbo.Blogs_Published", inputParamMapper : delegate(SqlParameterCollection paramCollection)
        {
            paramCollection.AddWithValue("@isPublished", isPublished);
        }
                                , map : delegate(IDataReader reader, short set)

        {
            if (set == 0)
            {
                Blog topic = new Blog();

                mapBlog(reader, topic);
                if (blogList == null)
                {
                    blogList = new List <Blog>();
                }
                blogList.Add(topic);
            }

            else if (set == 1)
            {
                BlogsTagsDomain f = mapTag(reader);

                if (list == null)
                {
                    list = new List <BlogsTagsDomain>();
                }
                list.Add(f);
            }

            else if (set == 2)
            {
                int TagId  = reader.GetSafeInt32(0);
                int BlogId = reader.GetSafeInt32(1);

                if (dict == null)
                {
                    dict = new Dictionary <int, List <int> >();
                }
                if (!dict.ContainsKey(BlogId))
                {
                    dict.Add(BlogId, new List <int>());
                }
                List <int> myList = dict[BlogId];

                myList.Add(TagId);
            }
        });

        if (blogList != null && dict != null)
        {
            foreach (Blog item in blogList)
            {
                if (!dict.ContainsKey(item.Id))
                {
                    continue;
                }

                List <int>             TagIds = dict[item.Id];
                List <BlogsTagsDomain> tags   = new List <BlogsTagsDomain>();
                foreach (int TagId in TagIds)
                {
                    BlogsTagsDomain d = list.FirstOrDefault(c => c.id == TagId);


                    tags.Add(d);
                }

                item.Tags = tags;
            }
        }

        return(blogList);
    }