using System;
using System.Configuration;
using System.Collections.Generic;
using Yavsc.Model.Blogs;
using System.Linq;
using Yavsc.Model.Circles;
namespace Yavsc.Model.Blogs
{
///
/// Blog entry collection.
///
public class BlogEntryCollection : List
{
///
/// Initializes a new instance of the class.
///
public BlogEntryCollection ()
{
}
///
/// Initializes a new instance of the class.
///
/// Items.
public BlogEntryCollection (IEnumerable items)
{
if (items != null)
this.AddRange (items);
}
///
/// Returns a that represents the current .
///
/// A that represents the current .
public override string ToString ()
{
string titles = Titles == null ?
"none" : string.Join (", ", Titles);
return string.Format ("[BlogEntryCollection: Titles={0}]", titles);
}
///
/// Get the specified bid.
///
/// Bid.
public BlogEntry Get (long bid)
{
return this.First (x => x.Id == bid);
}
///
/// Filters this collection on
/// the given title.
///
/// The by title.
/// Title.
public BlogEntry [] FilterOnTitle (string title)
{
return this.Where (x => x.Title == title).ToArray ();
}
///
/// Filters the current collection for a given user by its name.
/// Assumes that this user is not an author of any of these posts.
///
/// Username.
public BlogEntryCollection FilterFor (string username)
{
BlogEntryCollection res = new BlogEntryCollection ();
foreach (BlogEntry be in this) {
if (be.Visible &&
(be.AllowedCircles == null ||
(username != null && CircleManager.DefaultProvider.Matches
(be.AllowedCircles, username)))) {
res.Add (be);
}
}
return res;
}
///
/// Groups by title.
///
public IEnumerable> GroupByTitle ()
{
return from be in this
orderby be.Posted descending
group new BasePostInfo(be) by be.Title
into titlegroup select titlegroup;
}
///
/// Groups by user.
///
/// The by user.
public IEnumerable> GroupByUser ()
{
return from be in this
orderby be.Posted descending
group
new BasePostInfo (be)
by be.Author
into usergroup
select usergroup;
}
///
/// Gets the titles.
///
/// The titles.
public string[] Titles {
get {
string[] result = this.Select (x => x.Title).Distinct ().ToArray ();
if (result == null)
return new string[0];
return result;
}
}
}
}